Changeset 1331
- Timestamp:
- 01/02/25 02:04:21 (3 days ago)
- Location:
- cpp/common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/util-file.cpp
r1153 r1331 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-202 1Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2024 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 19 19 #ifdef USE_VIRTFILE 20 20 #ifdef DEBUGGING_READWRITECOMPLETEFILE 21 logPrintf("", "readCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);21 logPrintf("", "readCompleteFile", LOG_DEBUG, "virtfile: '%s'", filename); 22 22 #endif 23 // if (!isAbsolutePath(filename))23 // if (!isAbsolutePath(filename)) 24 24 { 25 VirtFILE *f =Vfopen(filename,FOPEN_READ_BINARY);25 VirtFILE *f = Vfopen(filename, FOPEN_READ_BINARY); 26 26 if (f) 27 27 { 28 int size =f->getSize();28 int size = f->getSize(); 29 29 data.resize(size); 30 30 int przeczytane = (int)f->Vread(&data[0], size, 1); … … 33 33 } 34 34 } 35 // else35 // else 36 36 #endif 37 37 { 38 38 #ifdef DEBUGGING_READWRITECOMPLETEFILE 39 if (isAbsolutePath(filename))40 logPrintf("","readCompleteFile",LOG_DEBUG,"mfopen absolute path: '%s'",filename);41 else42 logPrintf("","readCompleteFile",LOG_DEBUG,"mfopen: '%s' in current dir: '%s'",filename,getCurrentDirectory().c_str());39 if (isAbsolutePath(filename)) 40 logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen absolute path: '%s'", filename); 41 else 42 logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen: '%s' in current dir: '%s'", filename, getCurrentDirectory().c_str()); 43 43 #endif 44 44 MFILE *f = mfopen(filename, FOPEN_READ_BINARY); 45 45 #ifdef DEBUGGING_READWRITECOMPLETEFILE 46 logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen status: %s", f?"ok":"fail");46 logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen status: %s", f ? "ok" : "fail"); 47 47 #endif 48 48 if (f) … … 58 58 logPrintf("", "readCompleteFile", LOG_WARN, "Couldn't open file '%s'", filename); 59 59 #ifdef DEBUGGING_READWRITECOMPLETEFILE 60 logPrintf("", "readCompleteFile", LOG_DEBUG, "bytes:%d status: %s", data.size(), ok ?"ok":"fail");60 logPrintf("", "readCompleteFile", LOG_DEBUG, "bytes:%d status: %s", data.size(), ok ? "ok" : "fail"); 61 61 #endif 62 62 return ok; … … 74 74 } 75 75 76 bool writeCompleteFile(const char* filename, const s tring& text, bool warn_on_fail)76 bool writeCompleteFile(const char* filename, const span<char>& data, bool warn_on_fail) 77 77 { 78 78 #ifdef USE_VIRTFILE 79 79 #ifdef DEBUGGING_READWRITECOMPLETEFILE 80 logPrintf("", "writeCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);80 logPrintf("", "writeCompleteFile", LOG_DEBUG, "virtfile: '%s'", filename); 81 81 #endif 82 82 VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY); … … 84 84 if (f) 85 85 { 86 int zapisane = (int)f->Vwrite(text.c_str(), text.length(), 1);86 int zapisane = data.size() > 0 ? (int)f->Vwrite(data.begin(), data.size(), 1) : 1; //size()==0 is a special case (creating an empty file) 87 87 delete f; 88 88 ok &= (zapisane == 1); … … 91 91 #ifdef DEBUGGING_READWRITECOMPLETEFILE 92 92 if (isAbsolutePath(filename)) 93 logPrintf("", "writeCompleteFile",LOG_DEBUG,"mfopen absolute path: '%s'",filename);93 logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen absolute path: '%s'", filename); 94 94 else 95 logPrintf("", "writeCompleteFile",LOG_DEBUG,"mfopen: '%s' in current dir: '%s'",filename,getCurrentDirectory().c_str());95 logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen: '%s' in current dir: '%s'", filename, getCurrentDirectory().c_str()); 96 96 #endif 97 97 MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY); 98 98 bool ok = f != NULL; 99 99 #ifdef DEBUGGING_READWRITECOMPLETEFILE 100 logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen status: %s", ok ?"ok":"fail");100 logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen status: %s", ok ? "ok" : "fail"); 101 101 #endif 102 102 if (f) 103 103 { 104 int zapisane = (int)mfwrite(text.c_str(), text.length(), 1, f);104 int zapisane = data.size() > 0 ? (int)mfwrite(data.begin(), data.size(), 1, f) : 1; //size()==0 is a special case (creating an empty file) 105 105 mfclose(f); 106 106 ok &= (zapisane == 1); … … 110 110 logPrintf("", "writeCompleteFile", LOG_WARN, "Couldn't write file '%s'", filename); 111 111 #ifdef DEBUGGING_READWRITECOMPLETEFILE 112 logPrintf("", "writeCompleteFile", LOG_DEBUG, "status: %s", ok ?"ok":"fail");112 logPrintf("", "writeCompleteFile", LOG_DEBUG, "status: %s", ok ? "ok" : "fail"); 113 113 #endif 114 114 return ok; 115 115 } 116 116 117 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)117 bool writeCompleteFile(const char* filename, const span<uint8_t>& data, bool warn_on_fail) 118 118 { 119 string s(&data[0], data.size()); 120 return writeCompleteFile(filename, s, warn_on_fail); 119 return writeCompleteFile(filename, span<char>((char*)&data[0], data.size()), warn_on_fail); 121 120 } 121 122 bool writeCompleteFile(const char* filename, const vector<char>& data, bool warn_on_fail) 123 { 124 return writeCompleteFile(filename, span<char>((char*)&data[0], data.size()), warn_on_fail); 125 } 126 127 bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail) 128 { 129 return writeCompleteFile(filename, span<char>((char*)text.c_str(), text.size()), warn_on_fail); 130 } 131 122 132 123 133 // Just like fgets(), but string length is unlimited and does not store trailing \r \n … … 134 144 while ((line = 135 145 #ifdef USE_VIRTFILE 136 146 f->Vgets(buf, sizeof(buf)) 137 147 #else 138 148 fgets(buf, sizeof(buf), f) 139 149 #endif 140 150 )) 141 151 { 142 152 char* end = line + strlen(line); -
cpp/common/util-file.h
r1153 r1331 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-202 1Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2024 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 12 12 #include <stdio.h> 13 13 #endif 14 #include <common/nonstd_span.h> 14 15 15 16 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true); 16 17 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file = true); 17 18 bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true); 18 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true); 19 bool writeCompleteFile(const char* filename, const span<char>& data, bool warn_on_fail = true); 20 bool writeCompleteFile(const char* filename, const span<uint8_t>& data, bool warn_on_fail = true); 21 bool writeCompleteFile(const char* filename, const vector<char>& data, bool warn_on_fail); 19 22 #ifdef USE_VIRTFILE 20 23 string readUntilEOL(VirtFILE *f);
Note: See TracChangeset
for help on using the changeset viewer.