00001 #ifndef moses_DynSAInclude_utils_h 00002 #define moses_DynSAInclude_utils_h 00003 00004 #include <cstdlib> 00005 #include <vector> 00006 #include <string> 00007 #include <sstream> 00008 #include <cctype> 00009 #include <cmath> 00010 #include <cstring> 00011 00013 class Utils 00014 { 00015 public: 00016 static void trim(std::string& str, const std::string dropChars = " \t\n\r") { 00017 str.erase(str.find_last_not_of(dropChars)+1); 00018 str.erase(0, str.find_first_not_of(dropChars)); 00019 } 00020 static void rtrim(std::string& str, const std::string dropChars = " \t\n\r") { 00021 str.erase(str.find_last_not_of(dropChars)+1); 00022 } 00023 static void ltrim(std::string& str, const std::string dropChars = " \t\n\r") { 00024 str.erase(0, str.find_first_not_of(dropChars)); 00025 } 00026 static std::string IntToStr(int integer) { 00027 std::ostringstream stream; 00028 stream << integer; 00029 return stream.str(); 00030 } 00031 static int splitToStr(const char * str, 00032 std::vector<std::string> & items, 00033 const char * delm = "\t") { 00034 char * buff = const_cast<char *>(str); 00035 items.clear(); 00036 char * pch = strtok(buff, delm); 00037 while( pch != NULL ) { 00038 items.push_back(pch); 00039 pch = strtok(NULL, delm); 00040 } 00041 return items.size(); 00042 } 00043 static int splitToStr(std::string buff, 00044 std::vector<std::string> & items, 00045 std::string delm = "\t") { 00046 std::string cp = buff.substr(); 00047 return splitToStr(cp.c_str(), items, delm.c_str()); 00048 } 00049 static int splitToInt(std::string buff, std::vector<int>& items, 00050 std::string delm = ",") { 00051 items.clear(); 00052 std::vector<std::string> tmpVector(0); 00053 int i = 0; 00054 i = splitToStr(buff.c_str(), tmpVector, delm.c_str()); 00055 if( i > 0 ) 00056 for( int j = 0; j < i; j++ ) 00057 items.push_back(atoi(tmpVector[j].c_str())); 00058 return i; 00059 } 00060 static void strToLowercase(std::string& str) { 00061 for(unsigned i=0; i < str.length(); i++) { 00062 str[i] = tolower(str[i]); 00063 } 00064 } 00065 }; 00066 00067 #endif