1 #include <cassert> 2 #include <string> 3 4 #include "Util.h" 5 #include "IncExternAI.h" 6 7 namespace AIUtil { GetAbsFileName(IAICallback * icb,const std::string & relFileName)8 std::string GetAbsFileName(IAICallback* icb, const std::string& relFileName) { 9 char dst[2048] = {0}; 10 const char* src = relFileName.c_str(); 11 const int len = relFileName.size(); 12 13 // last char ('\0') in dst 14 // should not be overwritten 15 assert(len < (2048 - 1)); 16 17 memcpy(dst, src, len); 18 19 // get the absolute path to the file 20 // (and create folders along the way) 21 icb->GetValue(AIVAL_LOCATE_FILE_W, dst); 22 23 return (std::string(dst)); 24 } 25 IsFSGoodChar(const char c)26 bool IsFSGoodChar(const char c) { 27 if ((c >= '0') && (c <= '9')) { 28 return true; 29 } else if ((c >= 'a') && (c <= 'z')) { 30 return true; 31 } else if ((c >= 'A') && (c <= 'Z')) { 32 return true; 33 } else if ((c == '.') || (c == '_') || (c == '-')) { 34 return true; 35 } 36 37 return false; 38 } MakeFileSystemCompatible(const std::string & str)39 std::string MakeFileSystemCompatible(const std::string& str) { 40 std::string cleaned = str; 41 42 for (std::string::size_type i=0; i < cleaned.size(); i++) { 43 if (!IsFSGoodChar(cleaned[i])) { 44 cleaned[i] = '_'; 45 } 46 } 47 48 return cleaned; 49 } 50 StringToLowerInPlace(std::string & s)51 void StringToLowerInPlace(std::string& s) { 52 std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))tolower); 53 } StringToLower(std::string s)54 std::string StringToLower(std::string s) { 55 StringToLowerInPlace(s); 56 return s; 57 } 58 } 59