1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/
|
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski.
|
---|
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
|
---|
12 |
|
---|
13 | bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
|
---|
14 | {
|
---|
15 | bool ok = false;
|
---|
16 | #ifdef USE_VIRTFILE
|
---|
17 | // if (!isAbsolutePath(filename))
|
---|
18 | {
|
---|
19 | VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY);
|
---|
20 | if (f)
|
---|
21 | {
|
---|
22 | int size=f->getSize();
|
---|
23 | data.resize(size);
|
---|
24 | int przeczytane = (int)f->Vread(&data[0], size, 1);
|
---|
25 | ok = (przeczytane == 1);
|
---|
26 | delete f;
|
---|
27 | }
|
---|
28 | }
|
---|
29 | // else
|
---|
30 | #endif
|
---|
31 | {
|
---|
32 | MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
|
---|
33 | if (f)
|
---|
34 | {
|
---|
35 | int size = getFileSize(f);
|
---|
36 | data.resize(size);
|
---|
37 | int przeczytane = (int)mfread(&data[0], size, 1, f);
|
---|
38 | mfclose(f);
|
---|
39 | ok = (przeczytane == 1);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | if (warn_on_missing_file && !ok)
|
---|
43 | logPrintf("stl-util", "readCompleteFile", LOG_WARN, "Couldn't open file '%s'", filename);
|
---|
44 | return ok;
|
---|
45 | }
|
---|
46 |
|
---|
47 | bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file)
|
---|
48 | {
|
---|
49 | vector<char> data;
|
---|
50 | if (readCompleteFile(filename, data, warn_on_missing_file))
|
---|
51 | {
|
---|
52 | out = string(&data[0], data.size());
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 |
|
---|
58 | bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
|
---|
59 | {
|
---|
60 | #ifdef USE_VIRTFILE
|
---|
61 | VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY);
|
---|
62 | bool ok = f != NULL;
|
---|
63 | if (f)
|
---|
64 | {
|
---|
65 | int zapisane = (int)f->Vwrite(text.c_str(), text.length(), 1);
|
---|
66 | delete f;
|
---|
67 | ok &= (zapisane == 1);
|
---|
68 | }
|
---|
69 | #else
|
---|
70 | MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY);
|
---|
71 | bool ok = f != NULL;
|
---|
72 | if (f)
|
---|
73 | {
|
---|
74 | int zapisane = (int)mfwrite(text.c_str(), text.length(), 1, f);
|
---|
75 | mfclose(f);
|
---|
76 | ok &= (zapisane == 1);
|
---|
77 | }
|
---|
78 | #endif
|
---|
79 | if (warn_on_fail && !ok)
|
---|
80 | logPrintf("stl-util", "writeCompleteFile", LOG_WARN, "Couldn't write file '%s'", filename);
|
---|
81 | return ok;
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)
|
---|
85 | {
|
---|
86 | string s(&data[0], data.size());
|
---|
87 | return writeCompleteFile(filename, s, warn_on_fail);
|
---|
88 | }
|
---|