Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:12

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     FileInPath(FileInPath const& other);
0074     FileInPath& operator=(FileInPath const& other);
0075     ~FileInPath();
0076     void swap(FileInPath& other);
0077 
0078     /// Return a string containing the canonical form of the
0079     /// *relative* path. DO NOT USE THIS AS THE FILENAME for any file
0080     /// operations; use fullPath() for that purpose.
0081     std::string relativePath() const;
0082 
0083     /// Where was the file found?
0084     LocationCode location() const;
0085 
0086     /// Return a string that can be used to open the referenced
0087     /// file.
0088     ///
0089     /// Note that operations on this file may fail, including
0090     /// testing for existence. This is because the state of a
0091     /// filesystem is global; other threads, processes, etc., may have
0092     /// removed the file since we checked on its existence at the time
0093     /// of construction of the FileInPath object.
0094     std::string fullPath() const;
0095 
0096     /// Write contents to the given ostream.
0097     /// Writing errors are reflected in the state of the stream.
0098     void write(std::ostream& os) const;
0099 
0100     /// Read from the given istream, and set contents accordingly.
0101     /// Reading errors are reflected in the state of the stream.
0102     void read(std::istream& is);
0103 
0104     void readFromParameterSetBlob(std::istream& is);
0105 
0106     /// Should only be called while the edmWriteConfigs executable runs
0107     static void disableFileLookup();
0108 
0109     /** Uses the same algorithm to find the file but does not determine
0110         the location. Returns an empty string if unfound.
0111      */
0112     static std::string findFile(std::string const&);
0113 
0114   private:
0115     std::string relativePath_;
0116     std::string canonicalFilename_;
0117     LocationCode location_;
0118     std::string localTop_;
0119     std::string releaseTop_;
0120     std::string dataTop_;
0121     std::string searchPath_;
0122 
0123     // Helper function for construction.
0124     void getEnvironment();
0125     void initialize_();
0126     static std::string const& searchPath();
0127   };
0128 
0129   // Free swap function
0130   inline void swap(FileInPath& a, FileInPath& b) { a.swap(b); }
0131 
0132   inline std::ostream& operator<<(std::ostream& os, const edm::FileInPath& fip) {
0133     fip.write(os);
0134     return os;
0135   }
0136 
0137   inline std::istream& operator>>(std::istream& is, FileInPath& fip) {
0138     fip.read(is);
0139     return is;
0140   }
0141 
0142   inline bool operator==(edm::FileInPath const& a, edm::FileInPath const& b) {
0143     return a.location() == b.location() && a.relativePath() == b.relativePath();
0144   }
0145 
0146 }  // namespace edm
0147 
0148 #endif