Changeset 840
- Timestamp:
- 12/08/18 05:31:09 (6 years ago)
- Location:
- cpp/common
- Files:
-
- 3 added
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/util-string.cpp
r839 r840 3 3 // See LICENSE.txt for details. 4 4 5 #include " stl-util.h"5 #include "util-file.h" 6 6 #include <stdarg.h> 7 #include <stdlib.h>8 7 #include "nonstd_stdio.h" 9 #include "Convert.h"10 8 #include "nonstd.h" 11 #include "log.h"12 9 #include <assert.h> 13 10 #ifdef USE_VIRTFILE 14 11 #include <common/virtfile/virtfile.h> 15 12 #endif 16 17 13 18 14 string ssprintf_va(const char* format, va_list ap) … … 71 67 } 72 68 73 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)74 {75 bool ok = false;76 #ifdef USE_VIRTFILE77 // if (!isAbsolutePath(filename))78 {79 VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY);80 if (f)81 {82 int size=f->getSize();83 data.resize(size);84 int przeczytane = f->Vread(&data[0], size, 1);85 ok = przeczytane == 1;86 delete f;87 }88 }89 // else90 #endif91 {92 MFILE *f = mfopen(filename, FOPEN_READ_BINARY);93 if (f)94 {95 int size = getFileSize(f);96 data.resize(size);97 int przeczytane = mfread(&data[0], size, 1, f);98 mfclose(f);99 ok = przeczytane == 1;100 }101 }102 if (warn_on_missing_file && !ok)103 logPrintf("stl-util", "readCompleteFile", LOG_WARN, "Couldn't open file '%s'", filename);104 return ok;105 }106 107 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file)108 {109 vector<char> data;110 if (readCompleteFile(filename, data, warn_on_missing_file))111 {112 out = string(&data[0], data.size());113 return true;114 }115 return false;116 }117 118 bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)119 {120 #ifdef USE_VIRTFILE121 VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY);122 bool ok = f != NULL;123 if (f)124 {125 int zapisane = f->Vwrite(text.c_str(), text.length(), 1);126 delete f;127 ok &= zapisane == 1;128 }129 #else130 MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY);131 bool ok = f != NULL;132 if (f)133 {134 int zapisane = mfwrite(text.c_str(), text.length(), 1, f);135 mfclose(f);136 ok &= zapisane == 1;137 }138 #endif139 if (warn_on_fail && !ok)140 logPrintf("stl-util", "writeCompleteFile", LOG_WARN, "Couldn't write file '%s'", filename);141 return ok;142 }143 144 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)145 {146 string s(&data[0], data.size());147 return writeCompleteFile(filename, s, warn_on_fail);148 }149 150 151 152 69 string stripExt(const string& filename) 153 70 { … … 183 100 return filename.substr(0, slash); 184 101 } 185 -
cpp/common/util-string.h
r839 r840 3 3 // See LICENSE.txt for details. 4 4 5 #ifndef _ STL_UTIL_H_6 #define _ STL_UTIL_H_5 #ifndef _UTIL_STRINGS_H_ 6 #define _UTIL_STRINGS_H_ 7 7 8 8 #include "nonstd_stl.h" 9 9 #include <stdarg.h> 10 11 template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N])12 {13 for (unsigned int i = 0; i < N; i++)14 v.push_back(d[i]);15 }16 17 template<typename T> void erase(vector<T>& v, const T& e)18 {19 typename vector<T>::iterator it = std::find(v.begin(), v.end(), e);20 if (it != v.end())21 v.erase(it);22 }23 24 template<typename T> void deleteVectorElements(vector<T*>& v)25 {26 for (typename vector<T*>::iterator it = v.begin(); it != v.end(); it++)27 delete *it;28 v.clear();29 }30 31 template<typename T> int findIndex(vector<T>& v, const T& e)32 {33 typename vector<T>::iterator it = find(v.begin(), v.end(), e);34 if (it != v.end())35 return &*it - &v.front();36 return -1;37 }38 39 10 40 11 char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap") … … 48 19 string stripFileDir(const string& filename); // strip path component from filename 49 20 50 51 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true);52 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file = true);53 bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true);54 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true);55 56 template<class T> class DeletingVector // deletes the elements (pointers) in destructor57 {58 public:59 std::vector<T*> vector;60 ~DeletingVector()61 {62 for (int i = vector.size() - 1; i >= 0; i--)63 delete vector[i];64 }65 T* operator[](int i) { return vector[i]; }66 int size() { return vector.size(); }67 void push_back(T* x) { vector.push_back(x); }68 };69 70 21 #endif
Note: See TracChangeset
for help on using the changeset viewer.