00001 /* 00002 * Fimex 00003 * 00004 * (C) Copyright 2008, met.no 00005 * 00006 * Project Info: https://wiki.met.no/fimex/start 00007 * 00008 * This library is free software; you can redistribute it and/or modify it 00009 * under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation; either version 2.1 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00015 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00016 * License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00021 * USA. 00022 */ 00023 00024 #ifndef UTILS_H_ 00025 #define UTILS_H_ 00026 00027 #include <vector> 00028 #include <sstream> 00029 #include "fimex/CDMException.h" 00030 00031 namespace MetNoFimex 00032 { 00036 int round(double num); 00037 00042 std::string trim(const std::string& str); 00043 00051 template<class InputIterator> 00052 std::string join(InputIterator start, InputIterator end, std::string delim = ",") 00053 { 00054 std::ostringstream buffer; 00055 if (start == end) return ""; 00056 while (start+1 != end) { 00057 buffer << *start << delim; 00058 start++; 00059 } 00060 buffer << *start; 00061 return buffer.str(); 00062 } 00063 00073 std::vector<std::string> tokenize(const std::string& str, const std::string& delimiters = " "); 00074 00075 00079 std::string string2lowerCase(const std::string& str); 00080 00084 template<typename T> 00085 std::string type2string(T in) { 00086 std::ostringstream buffer; 00087 buffer << in; 00088 return buffer.str(); 00089 } 00090 00091 template<typename T> 00092 T string2type(std::string s) { 00093 T retVal; 00094 std::stringstream buffer; 00095 buffer << s; 00096 buffer >> retVal; 00097 return retVal; 00098 } 00099 00105 template<typename T> 00106 std::vector<T> tokenizeDotted(const std::string& str, const std::string& delimiter = ",") throw(CDMException) 00107 { 00108 std::vector<std::string> tokens = tokenize(str, delimiter); 00109 std::vector<T> vals; 00110 bool pricks = false; 00111 for (std::vector<std::string>::iterator tok = tokens.begin(); tok != tokens.end(); ++tok) { 00112 std::string current = trim(*tok); 00113 if (current == "...") { 00114 pricks = true; 00115 } else { 00116 T val = string2type<T>(current); 00117 if (pricks == true) { 00118 pricks = false; 00119 size_t end = vals.size(); 00120 if (end < 2) { 00121 throw CDMException("tokenizeDotted: cannot use ... expansion at position " + type2string(end-1) +", need at least two values before"); 00122 } 00123 T last = vals[end-1]; 00124 T dist = last - vals[end-2]; 00125 T curVal = last + dist; 00126 while (curVal < val) { 00127 vals.push_back(curVal); 00128 curVal += dist; 00129 } 00130 } 00131 vals.push_back(val); 00132 } 00133 } 00134 return vals; 00135 } 00136 00137 } 00138 00139 #endif /*UTILS_H_*/