conversion.cpp
Go to the documentation of this file.
00001 #include "yaml-cpp-pm/conversion.h"
00002 #include <algorithm>
00003 
00005 // Specializations for converting a string to specific types
00006 
00007 namespace
00008 {
00009         // we're not gonna mess with the mess that is all the isupper/etc. functions
00010         bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; }
00011         bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; }
00012         char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; }
00013 
00014         std::string tolower(const std::string& str)
00015         {
00016                 std::string s(str);
00017                 std::transform(s.begin(), s.end(), s.begin(), ToLower);
00018                 return s;
00019         }
00020 
00021         template <typename T>
00022         bool IsEntirely(const std::string& str, T func)
00023         {
00024                 for(std::size_t i=0;i<str.size();i++)
00025                         if(!func(str[i]))
00026                                 return false;
00027 
00028                 return true;
00029         }
00030 
00031         // IsFlexibleCase
00032         // . Returns true if 'str' is:
00033         //   . UPPERCASE
00034         //   . lowercase
00035         //   . Capitalized
00036         bool IsFlexibleCase(const std::string& str)
00037         {
00038                 if(str.empty())
00039                         return true;
00040 
00041                 if(IsEntirely(str, IsLower))
00042                         return true;
00043 
00044                 bool firstcaps = IsUpper(str[0]);
00045                 std::string rest = str.substr(1);
00046                 return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
00047         }
00048 }
00049 
00050 namespace YAML_PM
00051 {
00052         bool Convert(const std::string& input, bool& b)
00053         {
00054                 // we can't use iostream bool extraction operators as they don't
00055                 // recognize all possible values in the table below (taken from
00056                 // http://yaml.org/type/bool.html)
00057                 static const struct {
00058                         std::string truename, falsename;
00059                 } names[] = {
00060                         { "y", "n" },
00061                         { "yes", "no" },
00062                         { "true", "false" },
00063                         { "on", "off" },
00064                 };
00065 
00066                 if(!IsFlexibleCase(input))
00067                         return false;
00068 
00069                 for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
00070                         if(names[i].truename == tolower(input)) {
00071                                 b = true;
00072                                 return true;
00073                         }
00074 
00075                         if(names[i].falsename == tolower(input)) {
00076                                 b = false;
00077                                 return true;
00078                         }
00079                 }
00080 
00081                 return false;
00082         }
00083         
00084         bool Convert(const std::string& input, _Null& /*output*/)
00085         {
00086                 return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";
00087         }
00088 }
00089 


libpointmatcher
Author(s):
autogenerated on Mon Sep 14 2015 02:59:04