Logo Search packages:      
Sourcecode: djvulibre version File versions

GUTF8String GURL::UTF8Filename ( void   )  const

Returns a filename for a URL. Argument url# must be a legal file URL. This function applies heuristic rules to convert the URL into a valid file name. It is guaranteed that this function can properly parse all URLs generated by filename_to_url#. The heuristics also work better when the file actually exists. The empty string is returned when this function cannot parse the URL or when the URL is not a file URL. URL formats are as described in RFC 1738 plus the following alternative formats for files on the local host:

file://<letter>:/<path> file://<letter>|/<path> file:/<path>

which are accepted because various browsers recognize them.

Definition at line 1276 of file GURL.cpp.

References GOS::basename(), decode_reserved(), expand_name(), GUTF8String::format(), and is_empty().

Referenced by get_string(), is_dir(), is_file(), is_local_path(), NativeFilename(), and pathname().

{
  GUTF8String retval;
  if(! is_empty())
  {
    const char *url_ptr=url;
  
    // WARNING: Whenever you modify this conversion code,
    // make sure, that the following functions are in sync:
    //   encode_reserved()
    //   decode_reserved()
    //   url_to_filename()
    //   filename_to_url()

    GUTF8String urlcopy=decode_reserved(url);
    url_ptr = urlcopy;

    // All file urls are expected to start with filespec which is "file:"
    if (GStringRep::cmp(filespec, url_ptr, sizeof(filespec)-1))  //if not
      return GOS::basename(url_ptr);
    url_ptr += sizeof(filespec)-1;
  
#ifdef macintosh
    //remove all leading slashes
    for(;*url_ptr==slash;url_ptr++)
      EMPTY_LOOP;
    // Remove possible localhost spec
    if ( !GStringRep::cmp(localhost, url_ptr, sizeof(localhost)-1) )
      url_ptr += sizeof(localhost)-1;
    //remove all leading slashes
    while(*url_ptr==slash)
      url_ptr++;
#else
    // Remove possible localhost spec
    if ( !GStringRep::cmp(localhostspec1, url_ptr, sizeof(localhostspec1)-1) )
      // RFC 1738 local host form
      url_ptr += sizeof(localhostspec1)-1;
    else if ( !GStringRep::cmp(localhostspec2, url_ptr, sizeof(localhostspec2)-1 ) )
      // RFC 1738 local host form
      url_ptr += sizeof(localhostspec2)-1;
    else if ( (strlen(url_ptr) > 4)   // "file://<letter>:/<path>"
        && (url_ptr[0] == slash)      // "file://<letter>|/<path>"
        && (url_ptr[1] == slash)
        && isalpha(url_ptr[2])
        && ( url_ptr[3] == colon || url_ptr[3] == '|' )
        && (url_ptr[4] == slash) )
      url_ptr += 2;
    else if ( (strlen(url_ptr)) > 2 // "file:/<path>"
        && (url_ptr[0] == slash)
        && (url_ptr[1] != slash) )
      url_ptr++;
#endif

    // Check if we are finished
#ifdef macintosh
    {
      char *l_url;
      GPBuffer<char> gl_url(l_url,strlen(url_ptr)+1);
      const char *s;
      char *r;
      for ( s=url_ptr,r=l_url; *s; s++,r++)
      {
        *r=(*s == slash)?colon:*s;
      }
      *r=0;
      retval = expand_name(l_url,root);
    }
#else  
    retval = expand_name(url_ptr,root);
#endif
    
#if defined(WIN32) || defined(OS2)
    if (url_ptr[0] && url_ptr[1]=='|' && url_ptr[2]== slash)
    {
      if ((url_ptr[0]>='a' && url_ptr[0]<='z') 
          || (url_ptr[0]>='A' && url_ptr[0]<='Z'))
      {
      GUTF8String drive;
      drive.format("%c%c%c", url_ptr[0],colon,backslash);
      retval = expand_name(url_ptr+3, drive);
      }
    }
#endif
  }
  // Return what we have
  return retval;
}


Generated by  Doxygen 1.6.0   Back to index