HTML_Generator/RB_HTML_RelativeAddress [ Functions ]

FUNCTION

Link to 'that' from 'this' computing the relative path. Here 'this' and 'that' are both paths. This function is used to create links from one document to another document that might be in a completely different directory.

SYNOPSIS

char               *RB_HTML_RelativeAddress(
    char *thisname,
    char *thatname )

EXAMPLE

The following two

     this /sub1/sub2/sub3/f.html
     that /sub1/sub2/g.html

result in

     ../g.html

     this /sub1/f.html
     that /sub1/sub2/g.html
     ==
     ./sub2/g.html

     this /sub1/f.html
     that /sub1/g.html
     ==
     ./g.html

     this /sub1/doc3/doc1/tt.html
     that /sub1/doc5/doc2/qq.html
     ==
     ../../doc5/doc2/qq.html

NOTES

Notice the execelent docmentation.

SOURCE

#define MAX_RELATIVE_SIZE 1024
{
    static char         relative[MAX_RELATIVE_SIZE + 1];
    char               *i_this;
    char               *i_that;
    char               *i_this_slash = NULL;
    char               *i_that_slash = NULL;

    relative[0] = '\0';

    assert( thisname );
    assert( thatname );

    for ( i_this = thisname, i_that = thatname;
          ( *i_this && *i_that ) && ( *i_this == *i_that );
          ++i_this, ++i_that )
    {
        if ( *i_this == '/' )
        {
            i_this_slash = i_this;
        }
        if ( *i_that == '/' )
        {
            i_that_slash = i_that;
        }
    }

    if ( i_this_slash && i_that_slash )
    {
        int                 this_slashes_left = 0;
        int                 that_slashes_left = 0;
        char               *i_c;

        for ( i_c = i_this_slash + 1; *i_c; ++i_c )
        {
            if ( *i_c == '/' )
            {
                ++this_slashes_left;
            }
        }

        for ( i_c = i_that_slash + 1; *i_c; ++i_c )
        {
            if ( *i_c == '/' )
            {
                ++that_slashes_left;
            }
        }

        if ( this_slashes_left )
        {
            int                 i;

            for ( i = 0; i < this_slashes_left; ++i )
            {
                strcat( relative, "../" );
            }
            strcat( relative, i_that_slash + 1 );
        }
        else if ( that_slashes_left )
        {
            /* !this_slashes_left && that_slashes_left */
            strcat( relative, "./" );
            strcat( relative, i_that_slash + 1 );
        }
        else
        {
            /* !this_slashes_left && !that_slashes_left */
            strcat( relative, "./" );
            strcat( relative, i_that_slash + 1 );
        }
    }
    return relative;
}