Blame libdjvu/GURL.h

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifndef _GURL_H_
Packit df99a1
#define _GURL_H_
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
#include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma interface
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
#include "GString.h"
Packit df99a1
#include "Arrays.h"
Packit df99a1
#include "GThreads.h"
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
/** @name GURL.h
Packit df99a1
    Files #"GURL.h"# and #"GURL.cpp"# contain the implementation of the
Packit df99a1
    \Ref{GURL} class used to store URLs in a system independent format.
Packit df99a1
    @memo System independent URL representation.
Packit df99a1
    @author Andrei Erofeev <eaf@geocities.com>
Packit df99a1
Packit df99a1
// From: Leon Bottou, 1/31/2002
Packit df99a1
// This has been heavily changed by Lizardtech.
Packit df99a1
// They decided to use URLs for everyting, including
Packit df99a1
// the most basic file access.  The URL class now is a unholy 
Packit df99a1
// mixture of code for syntactically parsing the urls (which is was)
Packit df99a1
// and file status code (only for local file: urls).
Packit df99a1
Packit df99a1
*/
Packit df99a1
Packit df99a1
//@{
Packit df99a1
Packit df99a1
/** System independent URL representation.
Packit df99a1
Packit df99a1
    This class is used in the library to store URLs in a system independent
Packit df99a1
    format. The idea to use a general class to hold URL arose after we
Packit df99a1
    realized, that DjVu had to be able to access files both from the WEB
Packit df99a1
    and from the local disk. While it is strange to talk about system
Packit df99a1
    independence of HTTP URLs, file names formats obviously differ from
Packit df99a1
    platform to platform. They may contain forward slashes, backward slashes,
Packit df99a1
    colons as separators, etc. There maybe more than one URL corresponding
Packit df99a1
    to the same file name. Compare #file:/dir/file.djvu# and
Packit df99a1
    #file://localhost/dir/file.djvu#.
Packit df99a1
Packit df99a1
    To simplify a developer's life we have created this class, which contains
Packit df99a1
    inside a canonical representation of URLs.
Packit df99a1
Packit df99a1
    File URLs are converted to internal format with the help of \Ref{GOS} class.
Packit df99a1
Packit df99a1
    All other URLs are modified to contain only forward slashes.
Packit df99a1
*/
Packit df99a1
Packit df99a1
class DJVUAPI GURL
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  class Filename;
Packit df99a1
  class UTF8;
Packit df99a1
  class Native;
Packit df99a1
protected:
Packit df99a1
      /** @name Constructors
Packit df99a1
	  Accept the string URL, check that it starts from #file:/#
Packit df99a1
	  or #http:/# and convert to internal system independent
Packit df99a1
	  representation.
Packit df99a1
      */
Packit df99a1
      //@{
Packit df99a1
      ///
Packit df99a1
   GURL(const char * url_string);
Packit df99a1
      //@}
Packit df99a1
Packit df99a1
public:
Packit df99a1
   GURL(void);
Packit df99a1
Packit df99a1
   GURL(const GUTF8String & url_string);
Packit df99a1
Packit df99a1
   GURL(const GNativeString & url_string);
Packit df99a1
Packit df99a1
   GURL(const GUTF8String &xurl, const GURL &codebase);
Packit df99a1
Packit df99a1
   GURL(const GNativeString &xurl, const GURL &codebase);
Packit df99a1
Packit df99a1
      /// Copy constructor
Packit df99a1
   GURL(const GURL & gurl);
Packit df99a1
Packit df99a1
      /// The destructor
Packit df99a1
   virtual ~GURL(void) {}
Packit df99a1
Packit df99a1
private:
Packit df99a1
      // The 'class_lock' should be locked whenever you're accessing
Packit df99a1
      // url, or cgi_name_arr, or cgi_value_arr.
Packit df99a1
   GCriticalSection	class_lock;
Packit df99a1
protected:
Packit df99a1
   GUTF8String	url;
Packit df99a1
   DArray<GUTF8String>	cgi_name_arr, cgi_value_arr;
Packit df99a1
   bool validurl;
Packit df99a1
Packit df99a1
   void		init(const bool nothrow=false);
Packit df99a1
   void		convert_slashes(void);
Packit df99a1
   void		beautify_path(void);
Packit df99a1
   static GUTF8String	beautify_path(GUTF8String url);
Packit df99a1
Packit df99a1
   static GUTF8String	protocol(const GUTF8String& url);
Packit df99a1
   void		parse_cgi_args(void);
Packit df99a1
   void		store_cgi_args(void);
Packit df99a1
public:
Packit df99a1
   /// Test if the URL is valid. If invalid, reinitialize.
Packit df99a1
   bool is_valid(void) const;     // const lies to the compiler because of dependency problems
Packit df99a1
Packit df99a1
      /// Extracts the {\em protocol} part from the URL and returns it
Packit df99a1
   GUTF8String	protocol(void) const;
Packit df99a1
Packit df99a1
      /** Returns string after the first '\#' with decoded
Packit df99a1
	  escape sequences. */
Packit df99a1
   GUTF8String	hash_argument(void) const;
Packit df99a1
Packit df99a1
      /** Inserts the #arg# after a separating hash into the URL.
Packit df99a1
	  The function encodes any illegal character in #arg# using
Packit df99a1
	  \Ref{GOS::encode_reserved}(). */
Packit df99a1
   void		set_hash_argument(const GUTF8String &arg;;
Packit df99a1
Packit df99a1
      /** Returns the total number of CGI arguments in the URL.
Packit df99a1
	  CGI arguments follow '#?#' sign and are separated by '#&#' signs */
Packit df99a1
   int		cgi_arguments(void) const;
Packit df99a1
Packit df99a1
      /** Returns the total number of DjVu-related CGI arguments (arguments
Packit df99a1
	  following #DJVUOPTS# in the URL). */
Packit df99a1
   int		djvu_cgi_arguments(void) const;
Packit df99a1
Packit df99a1
      /** Returns that part of CGI argument number #num#, which is
Packit df99a1
	  before the equal sign. */
Packit df99a1
   GUTF8String	cgi_name(int num) const;
Packit df99a1
Packit df99a1
      /** Returns that part of DjVu-related CGI argument number #num#,
Packit df99a1
	  which is before the equal sign. */
Packit df99a1
   GUTF8String	djvu_cgi_name(int num) const;
Packit df99a1
Packit df99a1
      /** Returns that part of CGI argument number #num#, which is
Packit df99a1
	  after the equal sign. */
Packit df99a1
   GUTF8String	cgi_value(int num) const;
Packit df99a1
   
Packit df99a1
      /** Returns that part of DjVu-related CGI argument number #num#,
Packit df99a1
	  which is after the equal sign. */
Packit df99a1
   GUTF8String	djvu_cgi_value(int num) const;
Packit df99a1
   
Packit df99a1
      /** Returns array of all known CGI names (part of CGI argument before
Packit df99a1
	  the equal sign) */
Packit df99a1
   DArray<GUTF8String>cgi_names(void) const;
Packit df99a1
Packit df99a1
      /** Returns array of names of DjVu-related CGI arguments (arguments
Packit df99a1
	  following #DJVUOPTS# option. */
Packit df99a1
   DArray<GUTF8String>djvu_cgi_names(void) const;
Packit df99a1
   
Packit df99a1
      /** Returns array of all known CGI names (part of CGI argument before
Packit df99a1
	  the equal sign) */
Packit df99a1
   DArray<GUTF8String>cgi_values(void) const;
Packit df99a1
Packit df99a1
      /** Returns array of values of DjVu-related CGI arguments (arguments
Packit df99a1
	  following #DJVUOPTS# option. */
Packit df99a1
   DArray<GUTF8String>djvu_cgi_values(void) const;
Packit df99a1
Packit df99a1
      /// Erases everything after the first '\#' or '?'
Packit df99a1
   void		clear_all_arguments(void);
Packit df99a1
Packit df99a1
      /// Erases everything after the first '\#'
Packit df99a1
   void		clear_hash_argument(void);
Packit df99a1
Packit df99a1
      /// Erases DjVu CGI arguments (following "#DJVUOPTS#")
Packit df99a1
   void		clear_djvu_cgi_arguments(void);
Packit df99a1
Packit df99a1
      /// Erases all CGI arguments (following the first '?')
Packit df99a1
   void		clear_cgi_arguments(void);
Packit df99a1
Packit df99a1
      /** Appends the specified CGI argument. Will insert "#DJVUOPTS#" if
Packit df99a1
	  necessary */
Packit df99a1
   void		add_djvu_cgi_argument(const GUTF8String &name, const char * value=0);
Packit df99a1
   
Packit df99a1
      /** Returns the URL corresponding to the directory containing
Packit df99a1
	  the document with this URL. The function basically takes the
Packit df99a1
	  URL and clears everything after the last slash. */
Packit df99a1
   GURL		base(void) const;
Packit df99a1
Packit df99a1
      /// Returns the aboslute URL without the host part.
Packit df99a1
   GUTF8String pathname(void) const;
Packit df99a1
Packit df99a1
      /** Returns the name part of this URL.
Packit df99a1
	  For example, if the URL is #http://www.lizardtech.com/file%201.djvu# then
Packit df99a1
          this function will return #file%201.djvu#. \Ref{fname}() will
Packit df99a1
          return #file 1.djvu# at the same time. */
Packit df99a1
   GUTF8String	name(void) const;
Packit df99a1
Packit df99a1
      /** Returns the name part of this URL with escape sequences expanded.
Packit df99a1
	  For example, if the URL is #http://www.lizardtech.com/file%201.djvu# then
Packit df99a1
          this function will return #file 1.djvu#. \Ref{name}() will
Packit df99a1
          return #file%201.djvu# at the same time. */
Packit df99a1
   GUTF8String	fname(void) const;
Packit df99a1
Packit df99a1
      /// Returns the extention part of name of document in this URL.
Packit df99a1
   GUTF8String	extension(void) const;
Packit df99a1
Packit df99a1
      /// Checks if this is an empty URL
Packit df99a1
   bool		is_empty(void) const;
Packit df99a1
Packit df99a1
      /// Checks if the URL is local (starts from #file:/#) or not
Packit df99a1
   bool		is_local_file_url(void) const;
Packit df99a1
Packit df99a1
      /** @name Concatenation operators
Packit df99a1
	  Concatenate the GURL with the passed {\em name}. If the {\em name}
Packit df99a1
	  is absolute (has non empty protocol prefix), we just return
Packit df99a1
	  #GURL(name)#. Otherwise the #name# is appended to the GURL after a
Packit df99a1
	  separating slash.
Packit df99a1
      */
Packit df99a1
      //@{
Packit df99a1
      ///
Packit df99a1
//   GURL		operator+(const GUTF8String &name) const;
Packit df99a1
      //@}
Packit df99a1
Packit df99a1
      /// Returns TRUE if #gurl1# and #gurl2# are the same
Packit df99a1
   bool	operator==(const GURL & gurl2) const;
Packit df99a1
Packit df99a1
      /// Returns TRUE if #gurl1# and #gurl2# are different
Packit df99a1
   bool	operator!=(const GURL & gurl2) const;
Packit df99a1
Packit df99a1
      /// Assignment operator
Packit df99a1
   GURL &	operator=(const GURL & url);
Packit df99a1
Packit df99a1
      /// Returns Internal URL representation
Packit df99a1
   operator	const char*(void) const { return url; };
Packit df99a1
Packit df99a1
  /** Returns a string representing the URL.  This function normally
Packit df99a1
      returns a standard file URL as described in RFC 1738.  
Packit df99a1
      Some versions of MSIE do not support this standard syntax.
Packit df99a1
      A brain damaged MSIE compatible syntax is generated
Packit df99a1
      when the optional argument #useragent# contains string #"MSIE"# or
Packit df99a1
      #"Microsoft"#. */
Packit df99a1
   GUTF8String get_string(const GUTF8String &useragent) const;
Packit df99a1
Packit df99a1
   GUTF8String get_string(const bool nothrow=false) const;
Packit df99a1
Packit df99a1
      /// Escape special characters
Packit df99a1
   static GUTF8String encode_reserved(const GUTF8String &gs);
Packit df99a1
Packit df99a1
   /** Decodes reserved characters from the URL.
Packit df99a1
      See also: \Ref{encode_reserved}(). */
Packit df99a1
   static GUTF8String decode_reserved(const GUTF8String &url;;
Packit df99a1
Packit df99a1
  /// Test if this url is an existing file, directory, or device.
Packit df99a1
  bool is_local_path(void) const;
Packit df99a1
Packit df99a1
  /// Test if this url is an existing file.
Packit df99a1
  bool is_file(void) const;
Packit df99a1
Packit df99a1
  /// Test if this url is an existing directory.
Packit df99a1
  bool is_dir(void) const;
Packit df99a1
Packit df99a1
  /// Follows symbolic links.
Packit df99a1
  GURL follow_symlinks(void) const;
Packit df99a1
Packit df99a1
  /// Creates the specified directory.
Packit df99a1
  int mkdir(void) const;
Packit df99a1
Packit df99a1
  /** Deletes file or directory.
Packit df99a1
      Directories are not deleted unless the directory is empty.
Packit df99a1
      Returns a negative number if an error occurs. */
Packit df99a1
  int deletefile(void) const;
Packit df99a1
Packit df99a1
  /** Recursively erases contents of directory. The directory
Packit df99a1
      itself will not be removed. */
Packit df99a1
  int cleardir(const int timeout=0) const;
Packit df99a1
Packit df99a1
  /// Rename a file or directory.
Packit df99a1
  int renameto(const GURL &newurl) const;
Packit df99a1
Packit df99a1
  /// List the contents of a directory. 
Packit df99a1
  GList<GURL> listdir(void) const;
Packit df99a1
Packit df99a1
  /** Returns a filename for a URL. Argument #url# must be a legal file URL.
Packit df99a1
      This function applies heuristic rules to convert the URL into a valid
Packit df99a1
      file name. It is guaranteed that this function can properly parse all
Packit df99a1
      URLs generated by #filename_to_url#. The heuristics also work better when
Packit df99a1
      the file actually exists.  The empty string is returned when this
Packit df99a1
      function cannot parse the URL or when the URL is not a file URL.
Packit df99a1
        URL formats are as described in RFC 1738 plus the following alternative
Packit df99a1
      formats for files on the local host:
Packit df99a1
Packit df99a1
                file://<letter>:/<path>
Packit df99a1
                file://<letter>|/<path>
Packit df99a1
                file:/<path>
Packit df99a1
Packit df99a1
      which are accepted because various browsers recognize them.*/
Packit df99a1
   GUTF8String UTF8Filename(void) const;
Packit df99a1
   /// Same but returns a native string.
Packit df99a1
   GNativeString NativeFilename(void) const;
Packit df99a1
Packit df99a1
      /** Hashing function.
Packit df99a1
	  @return hash suitable for usage in \Ref{GMap} */
Packit df99a1
   friend unsigned int	hash(const GURL & gurl);
Packit df99a1
Packit df99a1
  /** Returns fully qualified file names.  This functions constructs the fully
Packit df99a1
      qualified name of file or directory #filename#. When provided, the
Packit df99a1
      optional argument #fromdirname# is used as the current directory when
Packit df99a1
      interpreting relative specifications in #filename#.  Function
Packit df99a1
      #expand_name# is very useful for logically concatenating file names.  It
Packit df99a1
      knows which separators should be used for each operating system and it
Packit df99a1
      knows which syntactical rules apply. */
Packit df99a1
  static GUTF8String expand_name(const GUTF8String &filename, const char *fromdirname=0);
Packit df99a1
};
Packit df99a1
Packit df99a1
class DJVUAPI GURL::UTF8 : public GURL
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  UTF8(const GUTF8String &xurl);
Packit df99a1
  UTF8(const GUTF8String &xurl, const GURL &codebase);
Packit df99a1
};
Packit df99a1
Packit df99a1
class DJVUAPI GURL::Native : public GURL
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  Native(const GNativeString &xurl);
Packit df99a1
  Native(const GNativeString &xurl, const GURL &codebase);
Packit df99a1
};
Packit df99a1
Packit df99a1
class DJVUAPI GURL::Filename : public GURL
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  Filename(const GUTF8String &filename);
Packit df99a1
  Filename(const GNativeString &filename);
Packit df99a1
  class UTF8;
Packit df99a1
  class Native;
Packit df99a1
};
Packit df99a1
Packit df99a1
class DJVUAPI GURL::Filename::UTF8 : public GURL::Filename
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  UTF8(const GUTF8String &filename);
Packit df99a1
};
Packit df99a1
Packit df99a1
class DJVUAPI GURL::Filename::Native : public GURL::Filename
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  Native(const GNativeString &filename);
Packit df99a1
};
Packit df99a1
Packit df99a1
Packit df99a1
inline bool
Packit df99a1
GURL::operator!=(const GURL & gurl2) const
Packit df99a1
{
Packit df99a1
  return !(*this == gurl2);
Packit df99a1
}
Packit df99a1
Packit df99a1
inline GUTF8String
Packit df99a1
GURL::protocol(void) const
Packit df99a1
{
Packit df99a1
   return protocol(get_string());
Packit df99a1
}
Packit df99a1
Packit df99a1
inline bool
Packit df99a1
GURL::is_empty(void) const
Packit df99a1
{
Packit df99a1
   return !url.length()||!get_string().length();
Packit df99a1
}
Packit df99a1
Packit df99a1
// Test if the URL is valid.
Packit df99a1
// If invalid, reinitialize and return the result.
Packit df99a1
inline bool
Packit df99a1
GURL::is_valid(void) const
Packit df99a1
{
Packit df99a1
  if(!validurl)
Packit df99a1
    const_cast<GURL *>(this)->init(true);
Packit df99a1
  return validurl;
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
//@}
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
#endif