[840] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/
|
---|
[1124] | 2 | // Copyright (C) 1999-2021 Maciej Komosinski and Szymon Ulatowski.
|
---|
[840] | 3 | // See LICENSE.txt for details.
|
---|
| 4 |
|
---|
| 5 | #include "util-file.h"
|
---|
| 6 | #include "nonstd_stdio.h"
|
---|
| 7 | #include "nonstd.h"
|
---|
| 8 | #include "log.h"
|
---|
| 9 | #ifdef USE_VIRTFILE
|
---|
| 10 | #include <common/virtfile/virtfile.h>
|
---|
| 11 | #endif
|
---|
[1153] | 12 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 13 | #include <common/dirs.h>
|
---|
| 14 | #endif
|
---|
[840] | 15 |
|
---|
| 16 | bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
|
---|
| 17 | {
|
---|
| 18 | bool ok = false;
|
---|
| 19 | #ifdef USE_VIRTFILE
|
---|
[1153] | 20 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 21 | logPrintf("","readCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);
|
---|
| 22 | #endif
|
---|
[840] | 23 | // if (!isAbsolutePath(filename))
|
---|
| 24 | {
|
---|
| 25 | VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY);
|
---|
| 26 | if (f)
|
---|
| 27 | {
|
---|
| 28 | int size=f->getSize();
|
---|
| 29 | data.resize(size);
|
---|
[888] | 30 | int przeczytane = (int)f->Vread(&data[0], size, 1);
|
---|
| 31 | ok = (przeczytane == 1);
|
---|
[840] | 32 | delete f;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | // else
|
---|
| 36 | #endif
|
---|
| 37 | {
|
---|
[1153] | 38 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 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 | #endif
|
---|
[840] | 44 | MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
|
---|
[1153] | 45 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 46 | logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen status: %s", f?"ok":"fail");
|
---|
| 47 | #endif
|
---|
[840] | 48 | if (f)
|
---|
| 49 | {
|
---|
| 50 | int size = getFileSize(f);
|
---|
| 51 | data.resize(size);
|
---|
[888] | 52 | int przeczytane = (int)mfread(&data[0], size, 1, f);
|
---|
[840] | 53 | mfclose(f);
|
---|
[888] | 54 | ok = (przeczytane == 1);
|
---|
[840] | 55 | }
|
---|
| 56 | }
|
---|
| 57 | if (warn_on_missing_file && !ok)
|
---|
[1153] | 58 | logPrintf("", "readCompleteFile", LOG_WARN, "Couldn't open file '%s'", filename);
|
---|
| 59 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 60 | logPrintf("", "readCompleteFile", LOG_DEBUG, "bytes:%d status: %s", data.size(), ok?"ok":"fail");
|
---|
| 61 | #endif
|
---|
[840] | 62 | return ok;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file)
|
---|
| 66 | {
|
---|
| 67 | vector<char> data;
|
---|
| 68 | if (readCompleteFile(filename, data, warn_on_missing_file))
|
---|
| 69 | {
|
---|
| 70 | out = string(&data[0], data.size());
|
---|
| 71 | return true;
|
---|
| 72 | }
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
|
---|
| 77 | {
|
---|
| 78 | #ifdef USE_VIRTFILE
|
---|
[1153] | 79 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 80 | logPrintf("","writeCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);
|
---|
| 81 | #endif
|
---|
[840] | 82 | VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY);
|
---|
| 83 | bool ok = f != NULL;
|
---|
| 84 | if (f)
|
---|
| 85 | {
|
---|
[888] | 86 | int zapisane = (int)f->Vwrite(text.c_str(), text.length(), 1);
|
---|
[840] | 87 | delete f;
|
---|
[888] | 88 | ok &= (zapisane == 1);
|
---|
[840] | 89 | }
|
---|
| 90 | #else
|
---|
[1153] | 91 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 92 | if (isAbsolutePath(filename))
|
---|
| 93 | logPrintf("","writeCompleteFile",LOG_DEBUG,"mfopen absolute path: '%s'",filename);
|
---|
| 94 | else
|
---|
| 95 | logPrintf("","writeCompleteFile",LOG_DEBUG,"mfopen: '%s' in current dir: '%s'",filename,getCurrentDirectory().c_str());
|
---|
| 96 | #endif
|
---|
[840] | 97 | MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY);
|
---|
| 98 | bool ok = f != NULL;
|
---|
[1153] | 99 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 100 | logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen status: %s", ok?"ok":"fail");
|
---|
| 101 | #endif
|
---|
[840] | 102 | if (f)
|
---|
| 103 | {
|
---|
[888] | 104 | int zapisane = (int)mfwrite(text.c_str(), text.length(), 1, f);
|
---|
[840] | 105 | mfclose(f);
|
---|
[888] | 106 | ok &= (zapisane == 1);
|
---|
[840] | 107 | }
|
---|
| 108 | #endif
|
---|
| 109 | if (warn_on_fail && !ok)
|
---|
[1153] | 110 | logPrintf("", "writeCompleteFile", LOG_WARN, "Couldn't write file '%s'", filename);
|
---|
| 111 | #ifdef DEBUGGING_READWRITECOMPLETEFILE
|
---|
| 112 | logPrintf("", "writeCompleteFile", LOG_DEBUG, "status: %s", ok?"ok":"fail");
|
---|
| 113 | #endif
|
---|
[840] | 114 | return ok;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)
|
---|
| 118 | {
|
---|
| 119 | string s(&data[0], data.size());
|
---|
| 120 | return writeCompleteFile(filename, s, warn_on_fail);
|
---|
| 121 | }
|
---|
[1124] | 122 |
|
---|
| 123 | // Just like fgets(), but string length is unlimited and does not store trailing \r \n
|
---|
[1153] | 124 | #ifdef USE_VIRTFILE
|
---|
[1124] | 125 | string readUntilEOL(VirtFILE *f)
|
---|
[1153] | 126 | #else
|
---|
| 127 | string readUntilEOL(FILE *f)
|
---|
| 128 | #endif
|
---|
[1124] | 129 | {
|
---|
| 130 | char buf[100];
|
---|
| 131 | char* line;
|
---|
| 132 | std::string ret;
|
---|
| 133 | bool endofline;
|
---|
[1153] | 134 | while ((line =
|
---|
| 135 | #ifdef USE_VIRTFILE
|
---|
| 136 | f->Vgets(buf, sizeof(buf))
|
---|
| 137 | #else
|
---|
| 138 | fgets(buf, sizeof(buf), f)
|
---|
| 139 | #endif
|
---|
| 140 | ))
|
---|
[1124] | 141 | {
|
---|
| 142 | char* end = line + strlen(line);
|
---|
| 143 | endofline = false;
|
---|
| 144 | while (end > line)
|
---|
| 145 | if ((end[-1] == '\n') || (end[-1] == '\r'))
|
---|
| 146 | {
|
---|
| 147 | endofline = true;
|
---|
| 148 | end--;
|
---|
| 149 | }
|
---|
| 150 | else
|
---|
| 151 | break;
|
---|
| 152 | ret += std::string(line, end - line);
|
---|
| 153 | if (endofline) break;
|
---|
| 154 | }
|
---|
| 155 | return ret;
|
---|
| 156 | }
|
---|