00001 #ifndef NCBIENV__HPP 00002 #define NCBIENV__HPP 00003 00004 /* $Id: ncbienv.hpp 155700 2009-03-25 14:14:40Z ivanov $ 00005 * =========================================================================== 00006 * 00007 * PUBLIC DOMAIN NOTICE 00008 * National Center for Biotechnology Information 00009 * 00010 * This software/database is a "United States Government Work" under the 00011 * terms of the United States Copyright Act. It was written as part of 00012 * the author's official duties as a United States Government employee and 00013 * thus cannot be copyrighted. This software/database is freely available 00014 * to the public for use. The National Library of Medicine and the U.S. 00015 * Government have not placed any restriction on its use or reproduction. 00016 * 00017 * Although all reasonable efforts have been taken to ensure the accuracy 00018 * and reliability of the software and data, the NLM and the U.S. 00019 * Government do not and cannot warrant the performance or results that 00020 * may be obtained by using this software or data. The NLM and the U.S. 00021 * Government disclaim all warranties, express or implied, including 00022 * warranties of performance, merchantability or fitness for any particular 00023 * purpose. 00024 * 00025 * Please cite the author in any work or product based on this material. 00026 * 00027 * =========================================================================== 00028 * 00029 * Authors: Denis Vakatov, Eugene Vasilchenko 00030 * 00031 * 00032 */ 00033 00034 /// @file ncbienv.hpp 00035 /// Defines unified interface to application: 00036 /// - Environment -- CNcbiEnvironment 00037 /// - Command-line args -- CNcbiArguments 00038 00039 00040 #include <corelib/ncbimtx.hpp> 00041 #include <map> 00042 #include <deque> 00043 00044 /// Avoid name clash with the NCBI C Toolkit. 00045 #if !defined(NCBI_OS_UNIX) || defined(HAVE_NCBI_C) 00046 # if defined(GetProgramName) 00047 # undef GetProgramName 00048 # endif 00049 # define GetProgramName GetProgramName 00050 # if defined(SetProgramName) 00051 # undef SetProgramName 00052 # endif 00053 # define SetProgramName SetProgramName 00054 #endif 00055 00056 00057 /** @addtogroup Environment 00058 * 00059 * @{ 00060 */ 00061 00062 00063 BEGIN_NCBI_SCOPE 00064 00065 00066 ///////////////////////////////////////////////////////////////////////////// 00067 /// 00068 /// CArgumentsException -- 00069 /// 00070 /// Define exceptions generated by CArgumentsApplication. 00071 /// 00072 /// CArgumentsException inherits its basic functionality from CCoreException 00073 /// and defines additional error codes for applications. 00074 00075 class CArgumentsException : public CCoreException 00076 { 00077 public: 00078 /// Error types that arguments processing can generate. 00079 enum EErrCode { 00080 eNegativeArgc, ///< Negative argc value 00081 eNoArgs ///< No arguments 00082 }; 00083 00084 /// Translate from the error code value to its string representation. 00085 virtual const char* GetErrCodeString(void) const; 00086 00087 // Standard exception boilerplate code. 00088 NCBI_EXCEPTION_DEFAULT(CArgumentsException, CCoreException); 00089 }; 00090 00091 00092 00093 ///////////////////////////////////////////////////////////////////////////// 00094 /// 00095 /// CNcbiEnvironment -- 00096 /// 00097 /// Define the application environment. 00098 /// 00099 /// CNcbiEnvironment provides a data structure for storing, accessing and 00100 /// modifying the environment variables accessed by the C library routine 00101 /// getenv(). 00102 00103 class CNcbiEnvironment 00104 { 00105 public: 00106 /// Constructor. 00107 CNcbiEnvironment(void); 00108 00109 /// Constructor with the envp parameter. 00110 CNcbiEnvironment(const char* const* envp); 00111 00112 /// Destructor. 00113 virtual ~CNcbiEnvironment(void); 00114 00115 /// Reset environment. 00116 /// 00117 /// Delete all cached entries, load new ones from "envp" (if not NULL). 00118 void Reset(const char* const* envp = 0); 00119 00120 /// Get environment value by name. 00121 /// 00122 /// If environmnent value is not cached then call "Load(name)" to load 00123 /// the environmnent value. The loaded name/value pair will then be 00124 /// cached, too, after the call to "Get()". 00125 const string& Get(const string& name) const; 00126 00127 /// Find all variable names starting with an optional prefix. 00128 void Enumerate(list<string>& names, const string& prefix = kEmptyStr) 00129 const; 00130 00131 /// Set an environment variable by name 00132 /// 00133 /// This will throw an exception if setting the variable fails 00134 void Set(const string& name, const string& value); 00135 00136 /// Delete an environment variable by name 00137 /// @param name environment variable name [in] 00138 void Unset(const string& name); 00139 00140 protected: 00141 /// Load value of specified environment variable. 00142 virtual string Load(const string& name) const; 00143 00144 private: 00145 /// Cached environment <name,value> pair. 00146 struct SEnvValue { 00147 SEnvValue(void) : ptr(NULL) {} 00148 SEnvValue(const string& v, char* p) : value(v), ptr(p) {} 00149 00150 string value; // cached value 00151 char* ptr; // string created by strdup() or NULL 00152 }; 00153 typedef map<string, SEnvValue> TCache; 00154 mutable TCache m_Cache; 00155 mutable CFastMutex m_CacheMutex; 00156 }; 00157 00158 00159 00160 ///////////////////////////////////////////////////////////////////////////// 00161 /// 00162 /// CAutoEnvironmentVariable -- 00163 /// 00164 /// Establish an environment setting for a limited time. 00165 /// 00166 /// CAutoEnvironmentVariable establishes an environment variable setting 00167 /// for the lifetime of the instance (which may be associated with a unit 00168 /// test case), restoring the previous value (if any) when destroyed. 00169 class CAutoEnvironmentVariable 00170 { 00171 public: 00172 /// Initializes the environment variable passed as an argument to the 00173 /// corresponding value ("1" by default) 00174 /// @param var_name environment variable name [in] 00175 /// @param value value to set the environment variable to [in] 00176 CAutoEnvironmentVariable(const CTempString& var_name, 00177 const CTempString& value = "1", 00178 CNcbiEnvironment* env = NULL); 00179 00180 /// Destructor which restores the modifications made in the environment by 00181 /// this class 00182 ~CAutoEnvironmentVariable(); 00183 00184 private: 00185 /// Affected CNcbiEnvironment instance 00186 AutoPtr<CNcbiEnvironment> m_Env; 00187 /// Name of the environment variable manipulated 00188 string m_VariableName; 00189 /// Previous value of the environment variable manipulated 00190 string m_PrevValue; 00191 }; 00192 00193 00194 00195 ///////////////////////////////////////////////////////////////////////////// 00196 /// 00197 /// CNcbiArguments -- 00198 /// 00199 /// Store application command-line arguments & application name. 00200 /// 00201 /// CNcbiArgument provides a data structure for storing and accessing the 00202 /// command line arguments and application name. 00203 00204 class CNcbiArguments 00205 { 00206 public: 00207 /// Constructor. 00208 CNcbiArguments(int argc, ///< Standard argument count 00209 const char* const* argv, ///< Standard argument vector 00210 const string& program_name = kEmptyStr, ///< Program name 00211 const string& real_name = kEmptyStr ///< Resolved name 00212 ); 00213 00214 /// Destructor. 00215 virtual ~CNcbiArguments(void); 00216 00217 /// Copy constructor. 00218 CNcbiArguments(const CNcbiArguments& args); 00219 00220 /// Assignment operator. 00221 CNcbiArguments& operator= (const CNcbiArguments& args); 00222 00223 /// Reset arguments. 00224 /// 00225 /// Delete all cached args and program name. Load new ones from "argc", 00226 /// "argv", "program_name", and "real_name". 00227 void Reset(int argc, ///< Standard argument count 00228 const char* const* argv, ///< Standard argument vector 00229 const string& program_name = kEmptyStr, ///< Program name 00230 const string& real_name = kEmptyStr ///< Resolved name 00231 ); 00232 00233 /// Get size (number) of arguments. 00234 SIZE_TYPE Size(void) const { return m_Args.size(); } 00235 00236 /// Get the argument specified by "pos". 00237 const string& operator[] (SIZE_TYPE pos) const { return m_Args[pos]; } 00238 00239 /// Add a new argument. 00240 void Add(const string& arg); 00241 00242 /// Get program name. 00243 const string& GetProgramName(EFollowLinks follow_links = eIgnoreLinks) 00244 const; 00245 00246 /// Get program base name. 00247 string GetProgramBasename(EFollowLinks follow_links = eIgnoreLinks) const; 00248 00249 /// Get program directory name. 00250 /// 00251 /// Program name includes the last '/'. 00252 string GetProgramDirname (EFollowLinks follow_links = eIgnoreLinks) const; 00253 00254 /// Set program name. If real_name is supplied, it should be the 00255 /// fully resolved path to the executable (whereas program_name 00256 /// may legitimately involve symlinks). 00257 void SetProgramName(const string& program_name, 00258 const string& real_name = kEmptyStr); 00259 00260 private: 00261 string m_ProgramName; ///< Program name if different from the 00262 ///< default m_Args[0] 00263 deque<string> m_Args; ///< Queue of arguments 00264 00265 mutable string m_ResolvedName; 00266 mutable CFastMutex m_ResolvedNameMutex; 00267 }; 00268 00269 00270 END_NCBI_SCOPE 00271 00272 00273 /* @} */ 00274 00275 #endif /* NCBIENV__HPP */ 00276 00277
1.4.6
Modified on Mon Dec 07 16:20:35 2009 by modify_doxy.py rev. 173732