Changeset 246 for cpp/common
- Timestamp:
- 06/01/14 00:52:01 (10 years ago)
- Location:
- cpp/common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/stl-util.cpp
r243 r246 14 14 { 15 15 string s; //clang crashed when this declaration was in s=buf 16 long size =256;16 long size = 256; 17 17 char* buf; 18 18 19 19 //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying 20 20 #ifdef USE_VSCPRINTF 21 size =_vscprintf(format, ap)+1; //+1 for terminating null character21 size = _vscprintf(format, ap) + 1; //+1 for terminating null character 22 22 #endif 23 23 24 while(1) 24 while (1) 25 { 26 buf = (char*)malloc(size); 27 assert(buf != NULL); 28 int n = vsnprintf(buf, size, format, ap); 29 if (n > -1 && n < size) 25 30 { 26 buf=(char*)malloc(size); 27 assert(buf!=NULL); 28 int n=vsnprintf(buf,size,format,ap); 29 if (n > -1 && n < size) 30 { 31 s=buf; 31 s = buf; 32 32 free(buf); 33 33 return s; 34 34 } 35 35 #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE 36 36 if (n > -1) /* glibc 2.1 */ … … 40 40 size *= 2; /* twice the old size */ 41 41 free(buf); 42 42 } 43 43 } 44 44 … … 47 47 va_list ap; 48 48 va_start(ap, format); 49 string ret =ssprintf_va(format,ap); //is it too wasteful? copying the string again... unless the compiler can handle it better49 string ret = ssprintf_va(format, ap); //is it too wasteful? copying the string again... unless the compiler can handle it better 50 50 va_end(ap); 51 51 return ret; … … 54 54 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file) 55 55 { 56 MFILE *f =mfopen(filename,FOPEN_READ_BINARY);57 bool ok =f!=NULL;56 MFILE *f = mfopen(filename, FOPEN_READ_BINARY); 57 bool ok = f != NULL; 58 58 if (f) 59 59 { 60 mfseek(f, 0,SEEK_END);61 long size =mftell(f);62 mfseek(f, 0,SEEK_SET);60 mfseek(f, 0, SEEK_END); 61 long size = mftell(f); 62 mfseek(f, 0, SEEK_SET); 63 63 data.resize(size); 64 int przeczytane =mfread(&data[0],size,1,f);64 int przeczytane = mfread(&data[0], size, 1, f); 65 65 mfclose(f); 66 ok &=przeczytane==1;66 ok &= przeczytane == 1; 67 67 } 68 68 if (warn_on_missing_file && !ok) 69 FMprintf("stl-util", "readCompleteFile",FMLV_WARN,"Couldn't open file '%s'",filename);69 FMprintf("stl-util", "readCompleteFile", FMLV_WARN, "Couldn't open file '%s'", filename); 70 70 return ok; 71 71 } … … 73 73 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file) 74 74 { 75 std::vector<char> data;76 if (readCompleteFile(filename, data,warn_on_missing_file))75 vector<char> data; 76 if (readCompleteFile(filename, data, warn_on_missing_file)) 77 77 { 78 out =string(&data[0],data.size());78 out = string(&data[0], data.size()); 79 79 return true; 80 80 } … … 84 84 bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail) 85 85 { 86 MFILE *f =mfopen(filename,FOPEN_WRITE_BINARY);87 bool ok =f!=NULL;86 MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY); 87 bool ok = f != NULL; 88 88 if (f) 89 89 { 90 int zapisane =mfwrite(text.c_str(),text.length(),1,f);90 int zapisane = mfwrite(text.c_str(), text.length(), 1, f); 91 91 mfclose(f); 92 ok &=zapisane==1;92 ok &= zapisane == 1; 93 93 } 94 94 if (warn_on_fail && !ok) 95 FMprintf("stl-util", "writeCompleteFile",FMLV_WARN,"couldn't write file '%s'",filename);95 FMprintf("stl-util", "writeCompleteFile", FMLV_WARN, "couldn't write file '%s'", filename); 96 96 return ok; 97 97 } … … 99 99 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail) 100 100 { 101 string s(&data[0], data.size());101 string s(&data[0], data.size()); 102 102 return writeCompleteFile(filename, s, warn_on_fail); 103 103 } … … 105 105 106 106 107 st d::string stripExt(const std::string& filename)107 string stripExt(const string& filename) 108 108 { 109 int dot =filename.rfind('.');110 if (dot ==std::string::npos) return filename;111 int sep =filename.rfind(PATH_SEPARATOR_CHAR);112 if ((sep ==std::string::npos)||(sep<dot))113 return filename.substr(0, dot);109 int dot = filename.rfind('.'); 110 if (dot == string::npos) return filename; 111 int sep = filename.rfind(PATH_SEPARATOR_CHAR); 112 if ((sep == string::npos) || (sep < dot)) 113 return filename.substr(0, dot); 114 114 return filename; 115 115 } 116 116 117 st d::string getFileExt(const std::string& filename)117 string getFileExt(const string& filename) 118 118 { 119 int dot =filename.rfind('.');120 if (dot ==std::string::npos) return string("");121 int sep =filename.rfind(PATH_SEPARATOR_CHAR);122 if ((sep ==std::string::npos)||(sep<dot))119 int dot = filename.rfind('.'); 120 if (dot == string::npos) return string(""); 121 int sep = filename.rfind(PATH_SEPARATOR_CHAR); 122 if ((sep == string::npos) || (sep < dot)) 123 123 return filename.substr(dot); 124 124 return string(""); 125 125 } 126 127 string getFileDir(const string& filename) 128 { 129 int slash = filename.rfind(PATH_SEPARATOR_CHAR); 130 if (slash == string::npos) return string(""); 131 return filename.substr(0, slash); 132 } -
cpp/common/stl-util.h
r197 r246 42 42 string stripExt(const string& filename); // strip extension from filename 43 43 string getFileExt(const string& filename); // get extension (starting with ".") from filename 44 string getFileDir(const string& filename); // get path component excluding filename ("" if no dir in file) 45 44 46 45 47 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file=true);
Note: See TracChangeset
for help on using the changeset viewer.