Advertisement
demmeln

CAST parse option helpers

Mar 26th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1.   /** Parse a string option.
  2.    *
  3.    * The default value is returned if the option was not passed.
  4.    */
  5.   std::string parseOption(
  6.       const std::string name,
  7.       const std::string defaultValue,
  8.       const std::map<std::string,std::string> &config);
  9.  
  10.   /** Parse a flag (i.e. boolean) option.
  11.    *
  12.    * Default value is 'false'. It is returned if the option was not passed. If
  13.    * the option was passed, but the value passed is maleformed (i.e. not "true"
  14.    * or "false"), print error and return default value.
  15.    */
  16.   bool parseFlagOption(
  17.       const std::string name,
  18.       const std::map<std::string,std::string> &config);
  19.  
  20.   /** Parse an option and perform a lexical cast.
  21.    *
  22.    * The default value is returned if the option was not passed. If the option
  23.    * was passed, but the lexical cast throws an exception, print error and
  24.    * return default value.
  25.    */
  26.   template<class T>
  27.   T parseOptionLexicalCast(
  28.       const std::string name,
  29.       const T defaultValue,
  30.       const std::map<std::string,std::string> &config);
  31.  
  32.   /** Parse a path option and resolve the path.
  33.    *
  34.    * If the option was not passed, the default value is resolved and
  35.    * returned. If in that case the default value can not be resolved, print an
  36.    * error and return an empty string. If the option was passed, but the value
  37.    * could not be resolved as a path, print error and return default value, else
  38.    * return the resolved path.
  39.    */
  40.   std::string parsePathOption(
  41.       const std::string name,
  42.       const std::string defaultValue,
  43.       const std::map<std::string,std::string> &config);
  44.  
  45.  
  46.  
  47.  
  48. // ------------------------------------------------------
  49. string PlacePropertySaver::parseOption(
  50.     const string name,
  51.     const string defaultValue,
  52.     const map<string,string> &config)
  53. {
  54.   map<string, string>::const_iterator it = config.find(name);
  55.  
  56.   if (it != config.end())
  57.     return it->second;
  58.  
  59.   return defaultValue; // default if not given
  60. }
  61.  
  62.  
  63. // ------------------------------------------------------
  64. bool PlacePropertySaver::parseFlagOption(
  65.     const string name,
  66.     const map<string,string> &config)
  67. {
  68.   map<string, string>::const_iterator it = config.find(name);
  69.  
  70.   if (it != config.end())
  71.   {
  72.     if (it->second == "true")
  73.     {
  74.       return true;
  75.     }
  76.     else if (it->second == "false")
  77.     {
  78.       return false;
  79.     }
  80.     else
  81.     {
  82.       error("Invalid value '%s' for flag option '%s'. "
  83.             "Should be one of {'true', 'false'}",
  84.             it->second.c_str(), name.c_str());
  85.  
  86.       return false; // default if malformed
  87.     }
  88.   }
  89.  
  90.   return false; // default if not given
  91. }
  92.  
  93.  
  94. // ------------------------------------------------------
  95. template<class T>
  96. T PlacePropertySaver::parseOptionLexicalCast(
  97.     const std::string name,
  98.     const T defaultValue,
  99.     const std::map<std::string,std::string> &config)
  100. {
  101.   map<string, string>::const_iterator it = config.find(name);
  102.  
  103.   if (it != config.end())
  104.   {
  105.     try
  106.     {
  107.       return boost::lexical_cast<T>(it->second);
  108.     }
  109.     catch(boost::bad_lexical_cast &)
  110.     {
  111.       error("Value '%s' of option '%s' is not of type '%s'",
  112.             it->second.c_str(), name.c_str(), typeid(T).name());
  113.  
  114.       return defaultValue; // default if malformed
  115.     }
  116.   }
  117.  
  118.   return defaultValue; // default if not given
  119. }
  120.  
  121.  
  122. // ------------------------------------------------------
  123. string PlacePropertySaver::parsePathOption(
  124.     const string name,
  125.     const string defaultValue,
  126.     const map<string,string> &config)
  127. {
  128.   string tmp = parseOption("--save-file-name", defaultValue, config);
  129.   shared_array<char> path(realpath(tmp.c_str(), 0));
  130.   if (path)
  131.   {
  132.     return path.get();
  133.   }
  134.   else
  135.   {
  136.     error("Value '%s' of option '%s' could not be resolved as a valid path",
  137.           tmp.c_str(), name.c_str());
  138.     return "";
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement