-
Notifications
You must be signed in to change notification settings - Fork 32
/
ObsFiles.cpp
256 lines (227 loc) · 5.84 KB
/
ObsFiles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2021 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Oberon+ parser/compiler library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "ObsFiles.h"
#include <QDir>
#include <QDateTime>
#include <QBuffer>
#include <stdint.h>
#include <QtDebug>
#ifdef _WIN32
#define DllExport __declspec(dllexport)
#else
#define DllExport
#endif
static QFileInfoList s_files;
static QString s_root;
static QFile s_disk;
void Obs::Files::setFileSystemRoot(const QString& dirPath) // cheat so that ObsFiles need no header just for this
{
s_root = dirPath;
}
typedef uint8_t CharArray[];
static inline QString getPath()
{
QString str = s_root;
if( str.isEmpty() )
str = QDir::currentPath();
return str;
}
static QList<QBuffer*> s_buffers;
static int getFreeBufferSlot(QBuffer* b)
{
for( int i = 0; i < s_buffers.size(); i++ )
{
if( s_buffers[i] == 0 )
{
s_buffers[i] = b;
return i;
}
}
s_buffers.append( b );
return s_buffers.size() - 1;
}
static inline QBuffer* getBuffer(int i)
{
if( i >= 0 && i < s_buffers.size() )
return s_buffers[i];
else
return 0;
}
extern "C"
{
DllExport int ObsFiles_setRootPath( const char* path )
{
s_root = QString::fromLatin1(path);
return 0;
}
DllExport int ObsFiles_listFiles()
{
QString str = getPath();
QDir dir( str );
s_files = dir.entryInfoList( QDir::Files | QDir::Readable | QDir::Writable );
return s_files.size();
}
DllExport const char* ObsFiles_fileName( int i )
{
static QByteArray name;
name = s_files[i].fileName().left(31).toUtf8();
return name.constData();
}
DllExport uint32_t ObsFiles_fileSize( int i )
{
return s_files[i].size();
}
DllExport uint32_t ObsFiles_fileTime( int i )
{
return s_files[i].created().toTime_t();
}
DllExport int ObsFiles_openFile( CharArray filename )
{
QDir dir( getPath() );
const QString path = dir.absoluteFilePath( QString::fromLatin1((char*)filename) );
if( !QFileInfo(path).isFile() )
return -1;
QFile f( path );
if( f.exists() )
{
if( !f.open(QIODevice::ReadOnly) )
{
qWarning() << "*** could not open for reading" << f.fileName();
return false;
}
QBuffer* b = new QBuffer();
b->setData(f.readAll());
b->open( QIODevice::ReadWrite );
return getFreeBufferSlot(b);
}else
return -1;
}
DllExport int ObsFiles_newFile()
{
QBuffer* b = new QBuffer();
b->open( QIODevice::ReadWrite );
return getFreeBufferSlot(b);
}
DllExport void ObsFiles_freeFile(int fb)
{
if( fb >= 0 && fb < s_buffers.size() )
{
if( s_buffers[fb] )
delete s_buffers[fb];
s_buffers[fb] = 0;
}
}
DllExport int ObsFiles_saveFile( CharArray filename, int fb )
{
QDir dir( getPath() );
QFile f( dir.absoluteFilePath( QString::fromLatin1((char*)filename)) );
QBuffer* buf = getBuffer(fb);
if( buf )
{
if( !f.open(QIODevice::WriteOnly) )
qWarning() << "*** could not open for writing" << f.fileName();
buf->close();
f.write(buf->data());
buf->open( QIODevice::ReadWrite );
return true;
}else
return false;
}
DllExport int ObsFiles_removeFile( CharArray filename )
{
QDir dir( getPath() );
return dir.remove(QString::fromLatin1((char*)filename));
}
DllExport int ObsFiles_renameFile( CharArray oldName, CharArray newName )
{
QDir dir( getPath() );
const QString old = QString::fromLatin1((const char*)oldName);
const QString _new = QString::fromLatin1((const char*)newName);
if( !QFileInfo(dir.absoluteFilePath(old)).exists() )
return false; // not found
QFileInfo info(dir.absoluteFilePath(_new));
if( info.exists() )
{
dir.remove(_new);
return dir.rename(old,_new);
}else
return dir.rename(old,_new);
}
DllExport uint32_t ObsFiles_length( int fb )
{
QBuffer* buf = getBuffer(fb);
if( buf )
return buf->size();
else
return 0;
}
DllExport int ObsFiles_setPos( int fb, int pos )
{
QBuffer* buf = getBuffer(fb);
if( buf )
{
if( pos < 0 ) // it happens a few times that -1 instead of 0 is passed; according to Oberon book should always be >= 0
pos = 0;
if( !buf->seek(pos) )
{
qWarning() << "*** could not seek to" << pos << buf->pos() << buf->size();
return false;
}
return true;
}else
return false;
}
DllExport int ObsFiles_getPos( int fb )
{
QBuffer* buf = getBuffer(fb);
if( buf )
return buf->pos();
else
return 0;
}
DllExport int ObsFiles_atEnd( int fb )
{
QBuffer* buf = getBuffer(fb);
if( buf )
return buf->atEnd();
else
return false;
}
DllExport int ObsFiles_writeByte( int fb, uint32_t byte )
{
QBuffer* buf = getBuffer(fb);
if( buf )
return buf->putChar( (char) (byte & 0xff) );
else
return false;
}
DllExport uint32_t ObsFiles_readByte( int fb )
{
QBuffer* buf = getBuffer(fb);
if( buf )
{
char ch;
if( buf->getChar( &ch ) )
return (quint8)ch;
else
return 0;
}else
return 0;
}
}