forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRageFile.cpp
493 lines (423 loc) · 10.3 KB
/
RageFile.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* This provides an interface to open files in RageFileManager's namespace
* This is just a simple RageFileBasic wrapper on top of another RageFileBasic;
* when a file is open, is acts like the underlying RageFileBasic, except that
* a few extra sanity checks are made to check file modes.
*/
#include "global.h"
#include "RageFileBasic.h"
#include "RageFile.h"
#include "RageUtil.h"
#include "RageUtil.hpp"
#include "RageFileDriver.h"
RageFile::RageFile()
{
m_File = nullptr;
}
RageFile::RageFile( const RageFile &cpy ):
RageFileBasic( cpy )
{
/* This will copy the file driver, including its internal file pointer. */
m_File = cpy.m_File->Copy();
m_Path = cpy.m_Path;
m_Mode = cpy.m_Mode;
}
RageFile *RageFile::Copy() const
{
return new RageFile( *this );
}
std::string RageFile::GetPath() const
{
if ( !IsOpen() )
return std::string();
std::string sRet = m_File->GetDisplayPath();
if( sRet != "" )
return sRet;
return GetRealPath();
}
bool RageFile::Open( const std::string& path, int mode )
{
ASSERT( FILEMAN != nullptr );
Close();
m_Path = path;
FixSlashesInPlace(m_Path);
m_Mode = mode;
if( (m_Mode&READ) && (m_Mode&WRITE) )
{
SetError( "Reading and writing are mutually exclusive" );
return false;
}
if( !(m_Mode&READ) && !(m_Mode&WRITE) )
{
SetError( "Neither reading nor writing specified" );
return false;
}
int error;
m_File = FILEMAN->Open( path, mode, error );
if( m_File == nullptr )
{
SetError( strerror(error) );
return false;
}
return true;
}
void RageFile::Close()
{
if( m_File == nullptr )
return;
delete m_File;
if( m_Mode & WRITE )
FILEMAN->CacheFile( m_File, m_Path );
m_File = nullptr;
}
#define ASSERT_OPEN ASSERT_M( IsOpen(), fmt::sprintf("\"%s\" is not open.", m_Path.c_str()) );
#define ASSERT_READ ASSERT_OPEN; ASSERT_M( !!(m_Mode&READ), fmt::sprintf("\"%s\" is not open for reading", m_Path.c_str()) );
#define ASSERT_WRITE ASSERT_OPEN; ASSERT_M( !!(m_Mode&WRITE), fmt::sprintf("\"%s\" is not open for writing", m_Path.c_str()) );
int RageFile::GetLine( std::string &out )
{
ASSERT_READ;
return m_File->GetLine( out );
}
int RageFile::PutLine( const std::string &str )
{
ASSERT_WRITE;
return m_File->PutLine( str );
}
void RageFile::EnableCRC32( bool on )
{
ASSERT_OPEN;
m_File->EnableCRC32( on );
}
bool RageFile::GetCRC32( uint32_t *iRet )
{
ASSERT_OPEN;
return m_File->GetCRC32( iRet );
}
bool RageFile::AtEOF() const
{
ASSERT_READ;
return m_File->AtEOF();
}
void RageFile::ClearError()
{
if( m_File != nullptr )
m_File->ClearError();
m_sError = "";
}
std::string RageFile::GetError() const
{
if( m_File != nullptr && m_File->GetError() != "" )
return m_File->GetError();
return m_sError;
}
void RageFile::SetError( const std::string &err )
{
if( m_File != nullptr )
m_File->ClearError();
m_sError = err;
}
int RageFile::Read( void *pBuffer, size_t iBytes )
{
ASSERT_READ;
return m_File->Read( pBuffer, iBytes );
}
int RageFile::Seek( int offset )
{
ASSERT_READ;
return m_File->Seek( offset );
}
int RageFile::Tell() const
{
ASSERT_READ;
return m_File->Tell();
}
int RageFile::GetFileSize() const
{
ASSERT_READ;
return m_File->GetFileSize();
}
int RageFile::GetFD()
{
ASSERT_READ;
return m_File->GetFD();
}
int RageFile::Read( std::string &buffer, int bytes )
{
ASSERT_READ;
return m_File->Read( buffer, bytes );
}
int RageFile::Write( const void *buffer, size_t bytes )
{
ASSERT_WRITE;
return m_File->Write( buffer, bytes );
}
int RageFile::Write( const void *buffer, size_t bytes, int nmemb )
{
ASSERT_WRITE;
return m_File->Write( buffer, bytes, nmemb );
}
int RageFile::Flush()
{
if( !m_File )
{
SetError( "Not open" );
return -1;
}
return m_File->Flush();
}
int RageFile::Read( void *buffer, size_t bytes, int nmemb )
{
ASSERT_READ;
return m_File->Read( buffer, bytes, nmemb );
}
int RageFile::Seek( int offset, int whence )
{
ASSERT_READ;
return m_File->Seek( offset, whence );
}
void FileReading::ReadBytes( RageFileBasic &f, void *buf, int size, std::string &sError )
{
if( sError.size() != 0 )
return;
int ret = f.Read( buf, size );
if( ret == -1 )
sError = f.GetError();
else if( ret < size )
sError = "Unexpected end of file";
}
std::string FileReading::ReadString( RageFileBasic &f, int size, std::string &sError )
{
if( sError.size() != 0 )
return std::string();
std::string sBuf;
int ret = f.Read( sBuf, size );
if( ret == -1 )
sError = f.GetError();
else if( ret < size )
sError = "Unexpected end of file";
return sBuf;
}
void FileReading::SkipBytes( RageFileBasic &f, int iBytes, std::string &sError )
{
if( sError.size() != 0 )
return;
iBytes += f.Tell();
FileReading::Seek( f, iBytes, sError );
}
void FileReading::Seek( RageFileBasic &f, int iOffset, std::string &sError )
{
if( sError.size() != 0 )
return;
int iGot = f.Seek( iOffset );
if( iGot == iOffset )
return;
if( iGot == -1 )
sError = f.GetError();
else if( iGot < iOffset )
sError = "Unexpected end of file";
}
uint8_t FileReading::read_8( RageFileBasic &f, std::string &sError )
{
uint8_t val = 0;
ReadBytes( f, &val, sizeof(uint8_t), sError );
// If there is an error, then 0 is returned since val is not set.
return val;
}
uint16_t FileReading::read_u16_le( RageFileBasic &f, std::string &sError )
{
uint16_t val = 0;
ReadBytes( f, &val, sizeof(uint16_t), sError );
return sError.size() == 0 ? Swap16LE( val ) : 0;
}
int16_t FileReading::read_16_le( RageFileBasic &f, std::string &sError )
{
int16_t val = 0;
ReadBytes( f, &val, sizeof(int16_t), sError );
return sError.size() == 0 ? Swap16LE( val ) : 0;
}
uint32_t FileReading::read_u32_le( RageFileBasic &f, std::string &sError )
{
uint32_t val = 0;
ReadBytes( f, &val, sizeof(uint32_t), sError );
return sError.size() == 0 ? Swap32LE( val ) : 0;
}
int32_t FileReading::read_32_le( RageFileBasic &f, std::string &sError )
{
int32_t val = 0;
ReadBytes( f, &val, sizeof(int32_t), sError );
return sError.size() == 0 ? Swap32LE( val ) : 0;
}
// lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the RageFile. */
class LunaRageFile: public Luna<RageFile>
{
public:
static void safely_opened(T* p, lua_State* L)
{
if(!p->IsOpen())
{
luaL_error(L, "File '%s' is not open.", p->GetPath().c_str());
}
}
static void can_safely_read(T* p, lua_State* L)
{
safely_opened(p, L);
if(!(p->GetMode()&RageFile::READ))
{
luaL_error(L, "File '%s' is not open for reading.", p->GetPath().c_str());
}
}
static void can_safely_write(T* p, lua_State* L)
{
safely_opened(p, L);
if(!(p->GetMode()&RageFile::WRITE))
{
luaL_error(L, "File '%s' is not open for writing.", p->GetPath().c_str());
}
}
static int destroy( T* p, lua_State *L )
{
Rage::safe_delete(p);
return 1;
}
static int Open( T* p, lua_State *L )
{
lua_pushboolean( L, p->Open( SArg(1), IArg(2) ) );
return 1;
}
static int Close( T* p, lua_State *L )
{
p->Close();
return 1;
}
static int Write( T* p, lua_State *L )
{
can_safely_write(p, L);
lua_pushinteger( L, p->Write( SArg(1) ) );
return 1;
}
static int Flush(T* p, lua_State* L)
{
p->Flush();
COMMON_RETURN_SELF;
}
static int Read( T* p, lua_State *L )
{
can_safely_read(p, L);
std::string string;
p->Read(string);
lua_pushstring( L, string.c_str() );
return 1;
}
static int ReadBytes( T* p, lua_State *L )
{
can_safely_read(p, L);
std::string string;
p->Read( string, IArg(1) );
lua_pushstring( L, string.c_str() );
return 1;
}
static int Seek( T* p, lua_State *L )
{
can_safely_read(p, L);
lua_pushinteger( L, p->Seek( IArg(1) ) );
return 1;
}
static int Tell( T* p, lua_State *L )
{
can_safely_read(p, L);
lua_pushinteger( L, p->Tell() );
return 1;
}
static int GetLine( T* p, lua_State *L )
{
can_safely_read(p, L);
std::string string;
p->GetLine(string);
lua_pushstring( L, string.c_str() );
return 1;
}
static int PutLine( T* p, lua_State *L )
{
can_safely_write(p, L);
lua_pushinteger( L, p->PutLine( SArg(1) ) );
return 1;
}
static int GetError( T* p, lua_State *L )
{
std::string error;
error = p->GetError();
lua_pushstring( L, error.c_str() );
return 1;
}
static int ClearError( T* p, lua_State *L )
{
p->ClearError();
return 1;
}
static int AtEOF( T* p, lua_State *L )
{
can_safely_read(p, L);
lua_pushboolean( L, p->AtEOF() );
return 1;
}
LunaRageFile()
{
ADD_METHOD( Open );
ADD_METHOD( Close );
ADD_METHOD( Write );
ADD_METHOD(Flush);
ADD_METHOD( Read );
ADD_METHOD( ReadBytes );
ADD_METHOD( Seek );
ADD_METHOD( Tell );
ADD_METHOD( GetLine );
ADD_METHOD( PutLine );
ADD_METHOD( destroy );
ADD_METHOD( GetError );
ADD_METHOD( ClearError );
ADD_METHOD( AtEOF );
}
};
LUA_REGISTER_CLASS( RageFile )
/** @brief Utilities for working with RageFiles. */
namespace RageFileUtil
{
int CreateRageFile( lua_State *L )
{
RageFile *pFile = new RageFile;
pFile->PushSelf( L );
return 1;
}
const luaL_Reg RageFileUtilTable[] =
{
LIST_METHOD( CreateRageFile ),
{ nullptr, nullptr }
};
LUA_REGISTER_NAMESPACE( RageFileUtil );
}
/*
* Copyright (c) 2003-2004 Glenn Maynard, Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/