source: cpp/common/util-file.cpp @ 1299

Last change on this file since 1299 was 1153, checked in by Maciej Komosinski, 3 years ago

Added debugging messages and a non-VirtFILE implementation of readUntilEOL()

File size: 4.3 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2021  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#ifdef DEBUGGING_READWRITECOMPLETEFILE
13#include <common/dirs.h>
14#endif
15
16bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
17{
18        bool ok = false;
19#ifdef USE_VIRTFILE
20#ifdef DEBUGGING_READWRITECOMPLETEFILE
21        logPrintf("","readCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);
22#endif 
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);
30                        int przeczytane = (int)f->Vread(&data[0], size, 1);
31                        ok = (przeczytane == 1);
32                        delete f;
33                }
34        }
35//      else
36#endif
37        {
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 
44                MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
45#ifdef DEBUGGING_READWRITECOMPLETEFILE
46        logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen status: %s", f?"ok":"fail");
47#endif
48                if (f)
49                {
50                        int size = getFileSize(f);
51                        data.resize(size);
52                        int przeczytane = (int)mfread(&data[0], size, 1, f);
53                        mfclose(f);
54                        ok = (przeczytane == 1);
55                }
56        }
57        if (warn_on_missing_file && !ok)
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
62        return ok;
63}
64
65bool 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
76bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
77{
78#ifdef USE_VIRTFILE
79#ifdef DEBUGGING_READWRITECOMPLETEFILE
80        logPrintf("","writeCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);
81#endif 
82        VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY);
83        bool ok = f != NULL;
84        if (f)
85        {
86                int zapisane = (int)f->Vwrite(text.c_str(), text.length(), 1);
87                delete f;
88                ok &= (zapisane == 1);
89        }
90#else
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
97        MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY);
98        bool ok = f != NULL;
99#ifdef DEBUGGING_READWRITECOMPLETEFILE
100        logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen status: %s", ok?"ok":"fail");
101#endif
102        if (f)
103        {
104                int zapisane = (int)mfwrite(text.c_str(), text.length(), 1, f);
105                mfclose(f);
106                ok &= (zapisane == 1);
107        }
108#endif
109        if (warn_on_fail && !ok)
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
114        return ok;
115}
116
117bool 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}
122
123// Just like fgets(), but string length is unlimited and does not store trailing \r \n
124#ifdef USE_VIRTFILE
125string readUntilEOL(VirtFILE *f)
126#else
127string readUntilEOL(FILE *f)
128#endif
129{
130        char buf[100];
131        char* line;
132        std::string ret;
133        bool endofline;
134        while ((line =
135#ifdef USE_VIRTFILE
136            f->Vgets(buf, sizeof(buf))
137#else
138            fgets(buf, sizeof(buf), f)
139#endif
140            ))
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}
Note: See TracBrowser for help on using the repository browser.