Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-12 04:20:44

0001 #ifndef FWCore_Utilities_FileInPath_h
0002 #define FWCore_Utilities_FileInPath_h
0003 
0004 ///
0005 
0006 /// Find a non-event-data file, given a relative path.
0007 
0008 /// FileInPath knows how to take a string, interpreted as a relative
0009 /// path to a file, and to interpret using the "standard CMS
0010 /// non-event-data file searching mechanism".
0011 ///
0012 /// The mechanism using the environment variables:
0013 ///    CMSSW_SEARCH_PATH:       may be set by the end-user
0014 ///    CMSSW_RELEASE_BASE:      should be set by a site administrator
0015 ///    CMSSW_DATA_PATH:         should be set by a site administrator
0016 ///
0017 ///  CMSSW_SEARCH_PATH is a 'search path' limited to 1 to 3
0018 ///  components. The legal values are:
0019 ///
0020 ///
0021 ///       "." or "LOCAL", which means to search for files under
0022 ///            the top level of the "local working area", which is
0023 ///            defined as ${SCRAMRT_LOCALRT}/src
0024 ///
0025 ///       "CMSSW_RELEASE_BASE", which means search the "official place",
0026 ///             defined by the value of the CMSSW_RELEASE_BASE environment
0027 ///             variable, for files.
0028 ///
0029 ///       "CMSSW_DATA_PATH", which means search the "official place",
0030 ///             defined by the value of the CMSSW_DATA_PATH environment
0031 ///             variable, for files.
0032 ///
0033 ///       ".:CMSSW_RELEASE_BASE" or "LOCAL:CMSSW_RELEASE_BASE",
0034 ///              which means look first in the current working
0035 ///              directory, then in the "official place", for files.
0036 ///
0037 ///       ".:CMSSW_DATA_PATH" or "LOCAL:CMSSW_DATA_PATH",
0038 ///              which means look first in the current working
0039 ///              directory, then in the "official place", for files.
0040 ///
0041 ///       ".:CMSSW_RELEASE_BASE:CMSSW_DATA_PATH" or "LOCAL:CMSSW_RELEASE_BASE:CMSSW_DATA_PATH",
0042 ///              which means look first in the current working
0043 ///              directory, then in both "official places", for files.
0044 ///
0045 
0046 // Notes:
0047 //
0048 //  1. We do not deal well with paths that contain spaces; this is because
0049 //     of the way the Utilities system's 'encode' and 'decode' functions
0050 //     are implemented for FileInPath objects. This could be fixed, if it
0051 //     is important to handle filenames or paths with embedded spaces.
0052 //
0053 //  2. All environment variables are read only once, when the FileInPath object is constructed.
0054 //     Therefore, any changes made to these variables externally during the lifetime of
0055 //     a FileInPath object will have no effect.
0056 
0057 #include <iosfwd>
0058 #include <string>
0059 
0060 namespace edm {
0061   class FileInPath {
0062   public:
0063     enum LocationCode { Unknown = 0, Local = 1, Release = 2, Data = 3 };
0064 
0065     /// Default c'tor does no file-existence check; what file would it
0066     /// check for existence?
0067     FileInPath();
0068 
0069     /// We throw an exception is the referenced file is not found.
0070     explicit FileInPath(const std::string& r);
0071     explicit FileInPath(const char* r);
0072 
0073     void swap(FileInPath& other);
0074 
0075     /// Return a string containing the canonical form of the
0076     /// *relative* path. DO NOT USE THIS AS THE FILENAME for any file
0077     /// operations; use fullPath() for that purpose.
0078     const std::string& relativePath() const;
0079 
0080     /// Where was the file found?
0081     LocationCode location() const;
0082 
0083     /// Return a string that can be used to open the referenced
0084     /// file.
0085     ///
0086     /// Note that operations on this file may fail, including
0087     /// testing for existence. This is because the state of a
0088     /// filesystem is global; other threads, processes, etc., may have
0089     /// removed the file since we checked on its existence at the time
0090     /// of construction of the FileInPath object.
0091     const std::string& fullPath() const;
0092 
0093     /// Write contents to the given ostream.
0094     /// Writing errors are reflected in the state of the stream.
0095     void write(std::ostream& os) const;
0096 
0097     /// Read from the given istream, and set contents accordingly.
0098     /// Reading errors are reflected in the state of the stream.
0099     void read(std::istream& is);
0100 
0101     void readFromParameterSetBlob(std::istream& is);
0102 
0103     /// Should only be called while the edmWriteConfigs executable runs
0104     static void disableFileLookup();
0105 
0106     /** Uses the same algorithm to find the file but does not determine
0107         the location. Returns an empty string if unfound.
0108      */
0109     static std::string findFile(std::string const&);
0110 
0111   private:
0112     std::string relativePath_;
0113     std::string canonicalFilename_;
0114     LocationCode location_;
0115     std::string localTop_;
0116     std::string releaseTop_;
0117     std::string dataTop_;
0118     std::string searchPath_;
0119 
0120     // Helper function for construction.
0121     void getEnvironment();
0122     void initialize_();
0123     static std::string const& searchPath();
0124   };
0125 
0126   // Free swap function
0127   inline void swap(FileInPath& a, FileInPath& b) { a.swap(b); }
0128 
0129   inline std::ostream& operator<<(std::ostream& os, const edm::FileInPath& fip) {
0130     fip.write(os);
0131     return os;
0132   }
0133 
0134   inline std::istream& operator>>(std::istream& is, FileInPath& fip) {
0135     fip.read(is);
0136     return is;
0137   }
0138 
0139   inline bool operator==(edm::FileInPath const& a, edm::FileInPath const& b) {
0140     return a.location() == b.location() && a.relativePath() == b.relativePath();
0141   }
0142 
0143 }  // namespace edm
0144 
0145 #endif