Config.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @file Config.h
  3. * @brief configuration mgt
  4. * @author Erik Rodner
  5. * @date 2006-03-12
  6. */
  7. #ifndef CONFIGINCLUDE
  8. #define CONFIGINCLUDE
  9. #include <iostream>
  10. #include <map>
  11. #include <math.h>
  12. #include <string>
  13. #include <vector>
  14. #include "StructuredMap.h"
  15. #include "core/basics/Persistent.h"
  16. namespace NICE {
  17. /** @brief This class provides general configuration management. */
  18. class Config : public NICE::Persistent
  19. {
  20. public:
  21. enum {
  22. CONFIG_OVERWRITE_VALUES = 0,
  23. CONFIG_DO_NOT_OVERWRITE_VALUES
  24. };
  25. protected:
  26. /** storing all config double values */
  27. StructuredMap<double> confD;
  28. /** storing all config integer values */
  29. StructuredMap<int> confI;
  30. /** storing all config boolean valzes */
  31. StructuredMap<bool> confB;
  32. /** storing all config strings */
  33. StructuredMap<std::string> confS;
  34. void addArgBoolean ( const std::string & section, const std::string & key );
  35. /** add a setting */
  36. void addKeyValuePair ( const std::string & section,
  37. const std::string & key,
  38. const std::string & value );
  39. /** storing help text for a section/key pair */
  40. std::map<std::string, std::string> helpTexts;
  41. /** storing all aux. options (for use with argv, argc) */
  42. std::vector<std::string> moreOptions;
  43. /** read until end of file fore restore (default: yes)*/
  44. bool ioUntilEndOfFile;
  45. /** stores filename the config was created from*/
  46. std::string m_sConfigFilename;
  47. public:
  48. /** simplest constructor, create an empty config */
  49. Config ();
  50. /** simple constructor
  51. @param configfn config file name */
  52. Config( const std::string & configfn );
  53. /** read config data from arguments
  54. @param argc number of arguments
  55. @param argv arguments */
  56. Config ( int argc,
  57. char **argv );
  58. /** copy constructor */
  59. Config ( const Config & conf );
  60. /** simple destructor */
  61. virtual ~Config();
  62. /** get string value or abort the whole
  63. program when no value is given
  64. @param key name of the variable
  65. */
  66. std::string gS(const std::string & key) const;
  67. /** get string value or abort the whole program
  68. when no value is given
  69. @param block name of the block
  70. @param key name of the variable
  71. */
  72. std::string gS(const std::string & block, const std::string & key) const;
  73. /** get string value, if non-existant its standard
  74. value is used
  75. @param block name of the block
  76. @param key name of the variable
  77. @param defv default value of the variable
  78. */
  79. virtual std::string gS(const std::string & block, const std::string & key, const std::string & defv) const;
  80. /** */
  81. virtual double gD(const std::string & key) const;
  82. virtual double gD(const std::string & block, const std::string & key) const;
  83. virtual double gD(const std::string & block, const std::string & key, const double defv) const;
  84. virtual int gI(const std::string & key) const;
  85. virtual int gI(const std::string & block, const std::string & key) const;
  86. virtual int gI(const std::string & block, const std::string & key, const int defv) const;
  87. virtual bool gB(const std::string & key) const;
  88. virtual bool gB(const std::string & block, const std::string & key) const;
  89. virtual bool gB(const std::string & block, const std::string & key, const bool defv) const;
  90. /** } */
  91. void getAllS ( const std::string & block, std::map< std::string, std::string > & list ) const;
  92. void getAllD ( const std::string & block, std::map< std::string, double> & list ) const;
  93. void getAllI ( const std::string & block, std::map< std::string, int > & list ) const;
  94. void getAllB ( const std::string & block, std::map< std::string, bool > & list ) const;
  95. void getAllBlocks ( std::set< std::string > & list ) const;
  96. /** set values */
  97. void sD(const std::string & block, const std::string & key, const double defv);
  98. void sI(const std::string & block, const std::string & key, const int defv);
  99. void sB(const std::string & block, const std::string & key, const bool defv);
  100. void sS(const std::string & block, const std::string & key, const std::string & defv);
  101. /** read values from arguments
  102. @param argc number of arguments
  103. @param argv list c-strings as provided by main-functions
  104. */
  105. void readFromArguments ( int argc, char **argv );
  106. /** check whether a key exists
  107. @param block block of the variable
  108. @param key name of the variable
  109. */
  110. bool keyExists ( const std::string & block, const std::string & key ) const;
  111. /** returns the filename the config was created by*/
  112. std::string getFilename() const{ return m_sConfigFilename;}
  113. /**
  114. * @brief Returns the given filename as an absolute path relative to this config file' location
  115. *
  116. * If p_Filename is not a relative path, then the filepath is alread absolute and just return that!
  117. *
  118. * @param p_Filename filename being relative or absolute
  119. * @return absolute filename
  120. */
  121. std::string getAbsoluteFilenameRelativeToThisConfig(const std::string &p_Filename) const;
  122. /** add a help text */
  123. void addHelp ( const std::string & block, const std::string & key,
  124. const std::string & helpText );
  125. /** get help text */
  126. std::string help ( const std::string & block, const std::string & key ) const;
  127. /** write all values of the config */
  128. void store (std::ostream & os, int format = 0) const;
  129. /** read config values from a stream
  130. @param is input stream
  131. @param format can be CONFIG_DO_NOT_OVERWRITE_VALUES or CONFIG_OVERWRITE_VALUES
  132. */
  133. void restore (std::istream & is, int format = 0);
  134. /** clear all values */
  135. void clear ();
  136. /** get more options (command line arguments not covered by config values */
  137. const std::vector<std::string> & getMoreOptions() { return moreOptions; };
  138. /** set the flag wether we restore a config and read until end of file or until a precise statement*/
  139. void setIoUntilEndOfFile(const bool & _ioUntilEndOfFile) { ioUntilEndOfFile = _ioUntilEndOfFile;};
  140. };
  141. } // namespace
  142. #endif