PLearn 0.1
PPath.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PPath.h
00004 //
00005 // Copyright (C) 2005 Pascal Vincent 
00006 // Copyright (C) 2005 Olivier Delalleau
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037  * $Id: PPath.h 9436 2008-09-04 18:48:55Z nouiz $ 
00038  ******************************************************* */
00039 
00040 // Authors: Pascal Vincent, Christian Dorion, Nicolas Chapados
00041 
00045 #ifndef PPath_INC
00046 #define PPath_INC
00047 
00048 // Put includes here
00049 #include <map>
00050 #include <string>
00051 #include <plearn/base/TypeTraits.h>   
00052 
00053 #define  FILE_PROTOCOL    "file"
00054 #define  HTTP_PROTOCOL    "http"
00055 #define   FTP_PROTOCOL    "ftp"
00056 
00057 namespace PLearn {
00058 using namespace std;
00059 
00060 // Forward declaration
00061 class PStream;  
00062   
00176 class PPath: public string
00177 {
00178 public:
00179 
00180     static PPath home  ();
00181     static PPath getcwd();
00182     static PPath getenv(const string& var, const PPath& default_="")  ;
00183     
00192     static bool addMetaprotocolBinding(const string& metaprotocol,
00193                                        const PPath& metapath,
00194                                        bool  force = true);
00195 
00203     static void setCanonicalInErrors(bool canonical);
00204 
00205 protected:
00206 
00207     /***********************
00208      *  protected methods  *
00209      **********************/
00210 
00223     static string forbidden_chars();
00224 
00229     static  const map<string, PPath>&  metaprotocolToMetapath();
00230 
00237     static string expandEnvVariables(const string& path);
00238 
00243     void resolveSlashChars   ( );
00244 
00246     void resolveDots         ( );
00247 
00250     void resolveSingleDots   ( );
00251 
00255     void resolveDoubleDots   ( );
00256 
00261     void expandMetaprotocols ( );
00262 
00267     void parseProtocol       ( );
00268 
00269     /***********************
00270      *  protected members  *
00271      **********************/
00272 
00279     string _protocol;
00280 
00281 public:
00282 
00284     PPath(const string &path_="");
00285 
00286     // Shorthand.
00287     PPath(const char* path);
00288 
00295     PPath absolute(bool add_protocol = false) const;
00296 
00300     string canonical() const;
00301 
00307     string errorDisplay() const;
00308 
00317     string protocol      ()  const { return _protocol.empty() ? FILE_PROTOCOL : _protocol;   }
00320     PPath  addProtocol   ()  const ; 
00322     PPath  removeProtocol()  const ; 
00325     void removeTrailingSlash();
00326 
00334     void parseUrlParameters(PPath& base_path, map<string, string>& parameters) const;
00335 
00337     bool   isAbsPath     ()  const { return isabs();   } 
00338     bool   isFilePath    ()  const { return  protocol() == FILE_PROTOCOL; }
00339     bool   isHttpPath    ()  const { return _protocol   == HTTP_PROTOCOL; }
00340     bool   isFtpPath     ()  const { return _protocol   ==  FTP_PROTOCOL; }  
00341 
00344     bool   isEmpty       ()  const { return removeProtocol().empty(); }
00346     bool   isRoot        ()  const;
00347 
00348     static const string& _slash();       
00349     static       char   _slash_char();  
00350 
00354     PPath  operator/  (const char*    other) const { return operator/ (PPath(other)); }
00355     PPath& operator/= (const char*    other)       { return operator/=(PPath(other)); }
00356     PPath  operator/  (const PPath&   other) const;
00357     PPath& operator/= (const PPath&   other);
00358 
00365     bool   operator== (const char*   other) const  { return operator==(string(other)); }
00366     bool   operator== (const string& other) const;
00367     bool   operator== (const PPath&  other) const;
00368 
00370     inline bool operator!= (const char*    other) const { return !(operator==(string(other))); }
00371     inline bool operator!= (const string&  other) const { return !(operator==(other));         }
00372     inline bool operator!= (const PPath&   other) const { return !(operator==(other));         }
00373 
00393     PPath up() const;
00394 
00417     PPath dirname() const;
00418 
00438     PPath basename  () const;
00439 
00450     string hostname() const;
00451 
00457     string extension (bool with_dot = false) const;
00458 
00463     PPath no_extension () const;
00464 
00465 
00466 //   /*! Migrated from fileutils.{h,cc}
00467 // 
00468 //   Returns a ppath shorter than 256 character and exempt of any of the
00469 //   following chars: "*?'\"${}[]@ ,()"  --- replaced by underscores. 
00470 //    */
00471 //   PPath     makeFileNameValid(const PPath& path) const;
00472 
00473   
00478 
00479 
00480     /*************************************************************************
00481      * Dos/Posix dependent methods
00482      ************************************************************************/
00483 
00484 
00489     PPath drive() const;
00490 
00491 protected:
00492 
00503     bool isabs() const;  
00504 
00505 private:
00506 
00510     static bool canonical_in_errors;
00511 
00512 };
00513 
00514 DECLARE_TYPE_TRAITS(PPath);
00515   
00517 PStream& operator<<(PStream& out, const PPath& path);
00518 PStream& operator>>(PStream& in, PPath& path);
00519   
00520 } // end of namespace PLearn
00521 
00522 #endif
00523 
00524 
00525 /*
00526   Local Variables:
00527   mode:c++
00528   c-basic-offset:4
00529   c-file-style:"stroustrup"
00530   c-file-offsets:((innamespace . 0)(inline-open . 0))
00531   indent-tabs-mode:nil
00532   fill-column:79
00533   End:
00534 */
00535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines