Subversion Repositories spk

Rev

Rev 51 | Rev 53 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 51 Rev 52
Line 8... Line 8...
8
 
8
 
9
#include "DirIO.h"
9
#include "DirIO.h"
10
#include "File.h"
10
#include "File.h"
11
#include "Logging/Log.h"
11
#include "Logging/Log.h"
12
 
12
 
13
CFileIO::CFileIO () : m_bOpened(false), m_lSize(0), m_bBinary(true)
13
CFileIO::CFileIO () : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
14
{
14
{
15
}
15
}
16
 
16
 
17
CFileIO::CFileIO(CyString filename) : m_bOpened(false), m_lSize(0), m_bBinary(true)
17
/*CFileIO::CFileIO(const Utils::String &sFilename, bool bBinary = true) : m_lSize(0), m_bBinary(bBinary), m_bAutoDelete(false)
18
{
18
{
19
	Open ( filename, true );
19
	Open(sFilename, bBinary);
20
}
20
}
21
 
21
*/
22
CFileIO::CFileIO(C_File *file) : m_bOpened(false), m_lSize(0), m_bBinary(true)
22
CFileIO::CFileIO(CyString filename) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
23
{
23
{
24
	Open ( file->GetFilePointer(), true );
24
	Open(filename, true);
25
}
25
}
26
 
26
 
27
CFileIO::~CFileIO()
27
CFileIO::CFileIO(C_File *file) : m_lSize(0), m_bBinary(true), m_bAutoDelete(false), m_bSeekP(false)
28
{
28
{
-
 
29
	Open(file->GetFilePointer(), true);
-
 
30
}
-
 
31
 
-
 
32
CFileIO::~CFileIO()
-
 
33
{
29
	if ( m_bOpened ) m_fId.close();
34
	if ( this->isOpened() ) m_fId.close();
-
 
35
	if ( m_bAutoDelete ) this->remove();
-
 
36
}
-
 
37
 
-
 
38
void CFileIO::setAutoDelete(bool bDelete)
-
 
39
{
-
 
40
	m_bAutoDelete = true;
30
}
41
}
31
 
42
 
32
bool CFileIO::Open ( CyString filename, bool binary )
43
bool CFileIO::Open ( CyString filename, bool binary )
33
{
44
{
34
	m_bBinary = binary;
45
	m_bBinary = binary;
35
	m_sFilename = filename.ToString();
46
	m_sFilename = filename.ToString();
36
	m_sFilename = m_sFilename.findReplace("\\", "/");
47
	m_sFilename = m_sFilename.findReplace("\\", "/");
37
	m_sFilename = m_sFilename.findReplace("//", "/");
48
	m_sFilename = m_sFilename.findReplace("//", "/");
38
 
49
 
39
	if ( m_sFilename.isin('/') ) {
50
	if ( m_sFilename.isin('/') ) {
40
		m_sDirIO.SetDir(m_sFilename.tokens("/", 1, -2));
51
		m_sDirIO.SetDir(m_sFilename.tokens("/", 1, -2));
41
		m_sFile = m_sFilename.token("/", -1);
52
		m_sFile = m_sFilename.token("/", -1);
42
	}
53
	}
43
	else
54
	else
44
		m_sFile = filename.ToString();
55
		m_sFile = filename.ToString();
45
 
56
 
46
	this->_readFileSize();
57
	this->_readFileSize();
47
	return true;
58
	return true;
48
}
59
}
-
 
60
 
-
 
61
unsigned char *CFileIO::read(size_t iAmount)
-
 
62
{
-
 
63
	if ( !this->isOpened() ) startRead();
-
 
64
	if ( !this->isOpened() ) return NULL;
-
 
65
 
-
 
66
	if ( iAmount > m_lSize ) iAmount = m_lSize;
-
 
67
 
-
 
68
	unsigned char *data;
-
 
69
	try {
-
 
70
		data = new unsigned char[iAmount + 1];
-
 
71
	}
-
 
72
	catch (std::exception &e) {
-
 
73
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to malloc data, %d (%s)", iAmount + 1, e.what());
-
 
74
		return NULL;
-
 
75
	}
-
 
76
 
-
 
77
	if ( !read(data, iAmount, true) ) {
-
 
78
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::read(size_t) unable to read data from file, %s/%d", m_sFilename.c_str(), iAmount);
-
 
79
		delete data;
-
 
80
		return NULL;
-
 
81
	}
-
 
82
 
-
 
83
	return data;
-
 
84
}
-
 
85
 
-
 
86
unsigned char *CFileIO::readAll(size_t *pSize)
-
 
87
{
-
 
88
	if ( pSize ) (*pSize) = 0;
-
 
89
	if ( !this->isOpened() ) startRead();
-
 
90
	if ( !this->isOpened() ) return NULL;
-
 
91
 
-
 
92
	unsigned char *data;
-
 
93
	try {
-
 
94
		data = new unsigned char[m_lSize + 1];
-
 
95
	}
-
 
96
	catch (std::exception &e) {
-
 
97
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to malloc data, %d (%s)", m_lSize + 1, e.what());
-
 
98
		return NULL;
-
 
99
	}
-
 
100
 
-
 
101
	if ( !read(data, m_lSize, true) ) {
-
 
102
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::readAll() unable to read data from file, %s/%d", m_sFilename.c_str(), m_lSize);
-
 
103
		delete data;
-
 
104
		return NULL;
-
 
105
	}
-
 
106
 
-
 
107
	if ( pSize ) (*pSize) = m_lSize;
-
 
108
	return data;
-
 
109
}
-
 
110
 
-
 
111
bool CFileIO::read(unsigned char *buf, size_t iSize, bool bEndChar)
-
 
112
{
-
 
113
	if ( !this->isOpened() ) startRead();
-
 
114
	if ( !this->isOpened() ) return false;
-
 
115
 
-
 
116
	try {
-
 
117
		m_fId.read((char *)buf, iSize);
-
 
118
	}
-
 
119
	catch (std::exception &e) {
-
 
120
		CLog::logf(CLog::Log_IO, 3, "ERROR: CFileIO::read() unable to read from file: %s (%s)", m_sFilename.c_str(), e.what());
-
 
121
		return false;
-
 
122
	}
-
 
123
 
-
 
124
	if ( bEndChar ) buf[iSize] = '\0';
-
 
125
 
-
 
126
	return !m_fId.bad();
-
 
127
}
-
 
128
 
49
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
129
bool CFileIO::WritePartFile ( size_t *offsets, size_t numOffset )
50
{
130
{
51
	if ( NoFile() )	return false;
131
	if ( NoFile() )	return false;
52
 
132
 
53
	std::fstream File(m_sFilename, _in());
133
	std::fstream File(m_sFilename, _in());
Line 145... Line 225...
145
	bool bDone = file.is_open();
225
	bool bDone = file.is_open();
146
	file.close();
226
	file.close();
147
 
227
 
148
	return bDone;
228
	return bDone;
149
}
229
}
-
 
230
 
-
 
231
 
150
int CFileIO::TruncateFile ( size_t offset, size_t datasize )
232
int CFileIO::TruncateFile ( size_t offset, size_t datasize )
151
{
233
{
152
	if ( NoFile() )
-
 
153
		return FILEERR_NOFILE;
234
	if ( NoFile() ) return FILEERR_NOFILE;
154
 
235
 
155
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb+" : "r+" );
-
 
156
	if ( !id )
-
 
157
		return FILEERR_NOOPEN;
236
	if ( !this->startRead() ) return FILEERR_NOOPEN;
158
 
-
 
159
	// first find file size
-
 
160
	fseek ( id, 0, SEEK_END );
-
 
161
	size_t fullsize = ftell ( id ), size = fullsize;
237
	if ( (offset + datasize) > m_lSize ) return FILEERR_TOSMALL;
162
	fseek ( id, 0, SEEK_SET );
-
 
163
 
238
 
164
	if ( (offset + datasize) > fullsize )
239
	CFileIO File("temp.tmp";);
165
		return FILEERR_TOSMALL;
240
	if ( !File.startWrite() ) return FILEERR_NOWRITE;
166
//	char data[500000];
241
	File.setAutoDelete(true);
167
 
242
 
168
#ifdef _WIN32
-
 
169
	FILE *writeId = fopen ( "temp.tmp", (m_bBinary) ? "wb" : "w" );
-
 
170
	if ( !writeId )
-
 
171
	{
-
 
172
		fclose ( id );
-
 
173
		return FILEERR_NOWRITE;
-
 
174
	}
-
 
175
	
-
 
176
	char *data = new char[offset];
-
 
177
	fread ( data, sizeof(unsigned char), offset, id );
-
 
178
	fwrite ( data, sizeof(unsigned char), offset, writeId );
243
	if ( !File.write(*this, offset) ) return false;
179
	delete data;
-
 
180
 
244
 
181
	// next fseek after and write
245
	// next fseek after and write
182
	fseek ( id, datasize, SEEK_CUR );
246
	this->seek(datasize);
183
	size = fullsize - offset - datasize;
247
	size_t size = m_lSize - offset - datasize;
184
	if ( size > 0 ) {
248
	if ( size > 0 ) {
185
		data = new char[size];
249
		File.write(*this, size);
186
		fread ( data, sizeof(unsigned char), size, id );
-
 
187
		fwrite ( data, sizeof(unsigned char), size, writeId );
-
 
188
		delete data;
-
 
189
	}
250
	}
190
 
251
 
191
	fclose ( writeId );
252
	File.close();
192
	fclose ( id );
253
	this->close();
-
 
254
	File.setAutoDelete(false);
193
 
255
 
194
	// now copy to original file
256
	// now copy to original file
195
	std::remove(m_sFilename.c_str());
257
	std::remove(m_sFilename.c_str());
196
	std::rename("temp.tmp", m_sFilename.c_str());
258
	std::rename("temp.tmp", m_sFilename.c_str());
197
 
259
 
198
#else
-
 
199
	// move to beginning of file data to remove
-
 
200
	fseek ( id, offset, SEEK_SET );
-
 
201
 
-
 
202
	size_t writepos = offset;
-
 
203
	size_t readpos = offset + datasize;
-
 
204
 
-
 
205
	size -= readpos;
-
 
206
 
-
 
207
	while ( size > 0 )
-
 
208
	{
-
 
209
		int read = 500000;
-
 
210
		if ( read > size )
-
 
211
			read = size;
-
 
212
 
-
 
213
		// read data
-
 
214
		fseek ( id, readpos, SEEK_SET );
-
 
215
		fread ( data, sizeof(unsigned char), read, id );
-
 
216
		size -= read;
-
 
217
		readpos += read;
-
 
218
 
-
 
219
		// now seek back and write
-
 
220
		fseek ( id, writepos, SEEK_SET );
-
 
221
		fwrite ( data, sizeof(unsigned char), read, id );
-
 
222
		writepos += read;
-
 
223
 
-
 
224
	}
-
 
225
 
-
 
226
	truncate ( m_sFilename.c_str(), fullsize - datasize );
-
 
227
	fclose ( id );
-
 
228
#endif
-
 
229
 
-
 
230
	return FILEERR_NONE;
260
	return FILEERR_NONE;
231
}
261
}
232
 
262
 
233
int CFileIO::_in()
263
int CFileIO::_in() const
234
{
264
{
235
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
265
	if ( m_bBinary ) return std::ios::in | std::ios::binary;
236
	return std::ios::in;
266
	return std::ios::in;
237
}
267
}
238
 
268
 
239
int CFileIO::_out()
269
int CFileIO::_out() const
240
{
270
{
241
	if ( m_bBinary ) return std::ios::out | std::ios::binary;
271
	if ( m_bBinary ) return std::ios::out | std::ios::binary;
242
	return std::ios::out;
272
	return std::ios::out;
-
 
273
}
-
 
274
int CFileIO::_inout() const
-
 
275
{
-
 
276
	if ( m_bBinary ) return std::ios::in | std::ios::out | std::ios::binary;
-
 
277
	return std::ios::in | std::ios::out;
243
}
278
}
244
 
279
 
245
int CFileIO::_append()
280
int CFileIO::_append() const
246
{
281
{
247
	if ( m_bBinary ) return std::ios::out | std::ios::binary | std::ios::app;
282
	if ( m_bBinary ) return std::ios::out | std::ios::binary | std::ios::app;
248
	return std::ios::out | std::ios::app;
283
	return std::ios::out | std::ios::app;
249
}
284
}
250
 
285
 
Line 263... Line 298...
263
	try {
298
	try {
264
		data = new char[m_lSize + 1];
299
		data = new char[m_lSize + 1];
265
	}
300
	}
266
	catch (std::exception &e) {
301
	catch (std::exception &e) {
267
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
302
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to malloc storage, %d (%s)", m_lSize + 1, e.what());
268
		file.close();
303
		file.close();
269
		return NULL;
304
		return NULL;
270
	}
305
	}
271
 
306
 
272
	try {
307
	try {
273
		file.read((char *)data, m_lSize);
308
		file.read((char *)data, m_lSize);
274
	}
309
	}
275
	catch (std::exception &e) {
310
	catch (std::exception &e) {
276
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
311
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file (%s)", e.what());
277
		file.close();
312
		file.close();
Line 299... Line 334...
299
	catch (std::exception &e) {
334
	catch (std::exception &e) {
300
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
335
		CLog::logf(CLog::Log_IO, 2, "CFileIO::ReadToData() unable to read data from file: %s (%s)", m_sFilename.c_str(), e.what());
301
		ret = false;
336
		ret = false;
302
	}
337
	}
303
 
338
 
304
	File.close();
339
	File.close();
305
 
340
 
306
	if ( ret ) this->_readFileSize();
341
	if ( ret ) this->_readFileSize();
307
	return ret;
342
	return ret;
308
}
343
}
309
 
344
 
310
bool CFileIO::WriteString ( CyString data )
345
bool CFileIO::WriteString ( CyString data )
311
{
346
{
312
	return WriteData ( data.c_str(), data.Length() );
347
	return WriteData ( data.c_str(), data.Length() );
313
}
348
}
-
 
349
 
-
 
350
bool CFileIO::Exists(const Utils::String &filename)
-
 
351
{
-
 
352
	std::fstream File(filename, std::ios::in | std::ios::binary);
-
 
353
	bool bRet = File.good();
-
 
354
	File.close();
-
 
355
	return bRet;
-
 
356
}
314
 
357
 
-
 
358
bool CFileIO::ExistsOld () const
-
 
359
{
-
 
360
	return this->exists();
-
 
361
}
315
bool CFileIO::Exists ()
362
bool CFileIO::exists () const
316
{
363
{
-
 
364
	if ( this->isOpened() ) return true;
317
	std::fstream File(m_sFilename, _in());
365
	std::fstream File(m_sFilename, _in());
318
	bool bRet = File.good();
366
	bool bRet = File.good();
319
	File.close();
367
	File.close();
320
	return bRet;
368
	return bRet;
321
}
369
}
-
 
370
 
-
 
371
bool CFileIO::StartRead() { return startRead(); }
322
 
372
 
323
bool CFileIO::StartRead()
373
bool CFileIO::startRead()
324
{
374
{
-
 
375
	return _start(_in(), false);
-
 
376
}
-
 
377
 
325
	if ( !m_sFilename.empty() ) {
378
bool CFileIO::startWrite()
-
 
379
{
326
		if ( m_bOpened ) StopRead();
380
	return _start(_out(), true);
-
 
381
}
327
 
382
 
328
		m_fId.open(m_sFilename, _in());
383
bool CFileIO::startModify()
-
 
384
{
329
		if ( m_fId.is_open() ) {
385
	return _start(_inout(), true);
-
 
386
}
-
 
387
 
330
			m_bOpened = true;
388
bool CFileIO::startAppend()
-
 
389
{
331
			return true;
390
	return _start(_append(), false);
332
		}
391
}
-
 
392
 
-
 
393
bool CFileIO::_start(int iFlags, bool bSeekP)
-
 
394
{
-
 
395
	if ( !m_sFilename.empty() ) {
-
 
396
		if ( this->isOpened() ) this->close();
-
 
397
 
-
 
398
		m_fId.open(m_sFilename, iFlags);
333
	}
399
	}
-
 
400
	m_bSeekP = bSeekP;
334
	return false;
401
	return m_fId.is_open();
335
}
402
}
336
 
403
 
337
std::vector<CyString> *CFileIO::ReadLines()
404
std::vector<CyString> *CFileIO::ReadLines()
338
{
405
{
339
	if ( m_sFilename.empty() ) return 0;
406
	if ( m_sFilename.empty() ) return 0;
340
 
407
 
341
	std::vector<CyString> *file = new std::vector<CyString>;
408
	std::vector<CyString> *file = new std::vector<CyString>;
342
	std::string line;
409
	std::string line;
343
	file->clear();
410
	file->clear();
344
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
411
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
345
	while (getline(infile, line, '\n'))
412
	while (getline(infile, line, '\n'))
346
	{
413
	{
347
		CyString l = line;
414
		CyString l = line;
348
		l.RemoveChar((char)0);
415
		l.RemoveChar((char)0);
349
		file->push_back(l);
416
		file->push_back(l);
350
	}
417
	}
351
 
418
 
352
	infile.close();
419
	infile.close();
353
 
420
 
Line 369... Line 436...
369
	}
436
	}
370
 
437
 
371
	infile.close();
438
	infile.close();
372
 
439
 
373
	return file;
440
	return file;
-
 
441
}
-
 
442
 
-
 
443
bool CFileIO::put(const unsigned char c)
-
 
444
{
-
 
445
	if ( !this->isOpened() ) return false;
-
 
446
 
-
 
447
	m_fId.put(c);
-
 
448
	return !m_fId.bad();
-
 
449
}
-
 
450
 
-
 
451
bool CFileIO::writeSize(unsigned int iSize)
-
 
452
{
-
 
453
	if ( !this->isOpened() ) return false;
-
 
454
	if ( !this->put(static_cast<unsigned char>(iSize >> 24)) ) return false;
-
 
455
	if ( !this->put(static_cast<unsigned char>(iSize >> 16)) ) return false;
-
 
456
	if ( !this->put(static_cast<unsigned char>(iSize >> 8)) ) return false;
-
 
457
	if ( !this->put(static_cast<unsigned char>(iSize)) ) return false;
-
 
458
	m_lSize += 4;
-
 
459
	return true;
-
 
460
}
-
 
461
 
-
 
462
bool CFileIO::write(CFileIO &file, size_t iSize)
-
 
463
{
-
 
464
	if ( !this->isOpened() ) startWrite();
-
 
465
	if ( !this->isOpened() ) return false;
-
 
466
 
-
 
467
	int iMaxSize = 1000000;
-
 
468
 
-
 
469
	unsigned char *data;
-
 
470
	try {
-
 
471
		data = new unsigned char[iMaxSize + 1];
-
 
472
	}
-
 
473
	catch (std::exception &e) {
-
 
474
		CLog::logf(CLog::Log_IO, 2, "ERROR: CFileIO::write(CFileIO, size_t) unable to malloc data, %d (%s)", iMaxSize + 1, e.what());
-
 
475
		return false;
-
 
476
	}
-
 
477
 
-
 
478
	int iSizeLeft = iSize;
-
 
479
	bool bSuccess = true;
-
 
480
	while ( iSizeLeft > 0 ) {
-
 
481
		int iDoSize = iMaxSize;
-
 
482
		if ( iDoSize > iSizeLeft ) iDoSize = iSizeLeft;
-
 
483
 
-
 
484
		if ( !file.read(data, iDoSize) ) bSuccess = false;
-
 
485
		if ( bSuccess && !this->write(data, iDoSize) ) bSuccess = false;
-
 
486
		if ( !bSuccess ) break;
-
 
487
		m_lSize += iDoSize;		
-
 
488
		iSizeLeft -= iDoSize;
-
 
489
	}
-
 
490
 
-
 
491
	delete data;
-
 
492
 
-
 
493
 
-
 
494
	return bSuccess;
-
 
495
}
-
 
496
 
-
 
497
bool CFileIO::write(const char *buf, unsigned int iSize)
-
 
498
{
-
 
499
	if ( !this->isOpened() ) startWrite();
-
 
500
	if ( !this->isOpened() ) return false;
-
 
501
	try {
-
 
502
		m_fId.write((char *)buf, iSize);
-
 
503
	}
-
 
504
	catch (std::exception &e) {
-
 
505
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", m_sFilename.c_str(), e.what());
-
 
506
		return false;
-
 
507
	}
-
 
508
	m_lSize += iSize;
-
 
509
	return !m_fId.bad();
-
 
510
}
-
 
511
bool CFileIO::write(const unsigned char *buf, unsigned int iSize)
-
 
512
{
-
 
513
	return this->write((char *)buf, iSize);
-
 
514
}
-
 
515
 
-
 
516
bool CFileIO::write(const unsigned char *buf, ...)
-
 
517
{
-
 
518
	va_list args;
-
 
519
	va_start(args, buf);
-
 
520
	bool ret = this->_write((char *)buf, args);
-
 
521
	va_end(args);
-
 
522
	return ret;
-
 
523
}
-
 
524
bool CFileIO::_write(const char *buf, va_list args) 
-
 
525
{
-
 
526
	if ( !this->isOpened() ) return false;
-
 
527
 
-
 
528
	char buffer[10000];
-
 
529
 
-
 
530
	vsprintf (buffer, buf, args);
-
 
531
	try {
-
 
532
		int iLen = strlen(buffer);
-
 
533
		m_fId.write((char *)buffer, iLen);
-
 
534
		m_lSize += iLen;
-
 
535
	}
-
 
536
	catch (std::exception &e) {
-
 
537
		CLog::logf(CLog::Log_IO, 2, "CFileIO::write() unable to write to file: %s (%s)", m_sFilename.c_str(), e.what());
-
 
538
		return false;
-
 
539
	}
-
 
540
	return !m_fId.bad();
-
 
541
}
-
 
542
 
-
 
543
bool CFileIO::write(const char *buf, ...)
-
 
544
{
-
 
545
	va_list args;
-
 
546
	va_start(args, buf);
-
 
547
	bool ret = this->_write((char *)buf, args);
-
 
548
	va_end(args);
-
 
549
	return ret;
-
 
550
}
-
 
551
 
-
 
552
void CFileIO::_seek(int iPos, int iFrom)
-
 
553
{
-
 
554
	if ( !this->isOpened() ) {
-
 
555
		if ( !this->startRead() ) return;
-
 
556
	}
-
 
557
	if ( m_bSeekP ) m_fId.seekp(iPos, iFrom);
-
 
558
	else m_fId.seekg(iPos, iFrom);
-
 
559
}
-
 
560
 
-
 
561
void CFileIO::seek(unsigned int iPos)
-
 
562
{
-
 
563
	_seek(iPos, std::ios::cur);
-
 
564
}
-
 
565
 
-
 
566
void CFileIO::seekEnd()
-
 
567
{
-
 
568
	_seek(0, std::ios::end);
-
 
569
}
-
 
570
 
-
 
571
void CFileIO::seekStart(unsigned int iPos)
-
 
572
{
-
 
573
	_seek(iPos, std::ios::beg);
-
 
574
}
-
 
575
 
-
 
576
std::fstream &CFileIO::stream()
-
 
577
{
-
 
578
	return m_fId;
374
}
579
}
375
 
580
 
376
void CFileIO::StopRead()
581
void CFileIO::StopRead()
377
{
582
{
378
	if ( m_fId.is_open() ) m_fId.close();
-
 
379
	m_bOpened = false;
583
	this->close();
380
}
584
}
381
 
585
 
382
bool CFileIO::IsOpened()
586
bool CFileIO::isOpened() const
383
{
587
{
384
	if ( m_fId.is_open() )
588
	if ( m_fId.is_open() )
385
		return true;
589
		return true;
386
	return false;
590
	return false;
387
}
591
}
388
 
592
 
389
CyString CFileIO::ReadToEndLine(bool autoclose)
593
void CFileIO::close()
390
{
594
{
391
	if ( !this->IsOpened() )
595
	if ( this->isOpened() ) m_fId.close();
392
	{
-
 
393
		if ( !StartRead() )
-
 
394
			return "";
-
 
395
	}
-
 
396
 
-
 
397
	int pos = 0;
-
 
398
	char sLine[10000];
-
 
399
	char cur = 0;
-
 
400
 
-
 
401
	std::string line;
-
 
402
	if ( getline(m_fId, line, '\n') )
-
 
403
		return CyString(line);
-
 
404
 
-
 
405
	sLine[pos] = 0;
-
 
406
	if ( pos ) return CyString(sLine);
-
 
407
 
-
 
408
	if ( autoclose )
-
 
409
		StopRead();
-
 
410
	return "";
-
 
411
}
596
}
412
 
597
 
413
bool CFileIO::AtEnd()
598
bool CFileIO::AtEnd()
414
{
599
{
415
	if ( !this->IsOpened() ) return true;
600
	if ( !this->isOpened() ) return true;
416
	if ( m_fId.eof() )		 return true;
601
	if ( m_fId.eof() )		 return true;
417
	return false;
602
	return false;
418
}
603
}
419
 
604
 
420
bool CFileIO::AppendFile ( CyString filename )
605
bool CFileIO::AppendFile ( CyString filename )
Line 485... Line 670...
485
	return true;
670
	return true;
486
}
671
}
487
 
672
 
488
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
673
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
489
{
674
{
490
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
-
 
491
	if ( !aid )
-
 
492
		return false;
-
 
493
 
-
 
494
	// move to the end of the file
675
	if ( start > m_lSize ) return false;
495
	fseek ( aid, 0, SEEK_END );
-
 
496
	size_t end = ftell(aid);
-
 
497
	if ( start &gt; end )
676
	if ( m_lSize &lt;= 0 ) {
498
	{
-
 
499
		fclose ( aid);
-
 
500
		return false;
677
		if ( !this->startWrite() ) return false;
501
	}
678
	}
502
 
-
 
503
	if ( start == end ) {
679
	else if ( start == m_lSize ) {
504
		fseek(aid, 0, SEEK_END);
680
		if ( !this->startAppend() ) return false;
505
	}
681
	}
506
	else {
682
	else {
-
 
683
		if ( !this->startModify() ) return false;
507
		fseek ( aid, start, SEEK_SET );
684
		this->seekStart(start);
508
	}
685
	}
509
 
686
 
510
	char *pos = (char *)d;
687
	return this->write(d, size);
511
	while ( size > 0 )
-
 
512
	{
688
}
513
		size_t read = 500000;
-
 
514
		if ( read > size )
-
 
515
			read = size;
-
 
516
 
689
 
517
		size -= read;
690
Utils::String CFileIO::readEndOfLine()
518
 
691
{
519
		fwrite ( pos, sizeof(char), read, aid );
692
	if ( !this->isOpened() ) {
520
		pos += read;
693
		if ( !StartRead() )	return "";
521
	}
694
	}
522
 
695
 
523
	fclose ( aid );
696
	Utils::String str;
-
 
697
	std::getline(m_fId, str, '\n');
524
	return true;
698
	return str;
525
}
699
}
526
 
700
 
527
CyString CFileIO::GetBaseName()
701
CyString CFileIO::GetBaseName()
528
{
702
{
529
	return m_sFile.token(".", -2);
703
	return m_sFile.token(".", -2);
530
}
704
}
Line 540... Line 714...
540
		return NullString;
714
		return NullString;
541
	
715
	
542
	return m_sFilename.tokens(".", 1, -2) + "." + ext.ToString();
716
	return m_sFilename.tokens(".", 1, -2) + "." + ext.ToString();
543
}
717
}
544
 
718
 
545
bool CFileIO::Remove()
719
bool CFileIO::Remove(const Utils::String &rem)
546
{
720
{
547
	if ( !Exists() )
721
	//if ( !Exists() ) return false;
548
		return false;
722
	return (std::remove(rem) == 0) ? true : false;
549
 
723
}
550
	if ( std::remove(m_sFilename.c_str()) == 0 )
-
 
551
		return true;
-
 
552
 
724
 
-
 
725
bool CFileIO::remove()
-
 
726
{
-
 
727
	if ( this->isOpened() ) this->close();
-
 
728
	if ( !this->exists() ) return false;
-
 
729
	if ( std::remove(m_sFilename.c_str()) == 0 ) return true;
553
	return false;
730
	return false;
554
}
731
}
555
 
732
 
556
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
733
bool CFileIO::WriteFileUTF(std::vector<CyString> *lines)
557
{
734
{
Line 676... Line 853...
676
 
853
 
677
	return true;
854
	return true;
678
}
855
}
679
 
856
 
680
bool CFileIO::Rename(CyString toFile)
857
bool CFileIO::Rename(CyString toFile)
681
{
858
{
682
	if ( rename(m_sFilename.c_str(), toFile.c_str()) == 0 )
859
	if ( rename(m_sFilename.c_str(), toFile.c_str()) == 0 )
683
		return true;
860
		return true;
684
	return false;
861
	return false;
685
}
862
}
686
 
863
 
Line 694... Line 871...
694
void CFileIO::SetCreationTime(time_t time)
871
void CFileIO::SetCreationTime(time_t time)
695
{
872
{
696
#ifdef _WIN32
873
#ifdef _WIN32
697
	// Note that LONGLONG is a 64-bit value
874
	// Note that LONGLONG is a 64-bit value
698
    LONGLONG ll;
875
    LONGLONG ll;
699
 
876
 
700
	FILETIME ft;
877
	FILETIME ft;
701
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
878
    ll = Int32x32To64(time, 10000000) + 116444736000000000;
702
    ft.dwLowDateTime = (DWORD)ll;
879
    ft.dwLowDateTime = (DWORD)ll;
703
    ft.dwHighDateTime = ll >> 32;
880
    ft.dwHighDateTime = ll >> 32;
704
 
881
 
-
 
882
	WCHAR    str[MAX_PATH];
-
 
883
	MultiByteToWideChar(CP_ACP, NULL, GetWindowsFilename().c_str(), -1, str, GetWindowsFilename().Length() + 1);
705
	HANDLE filename = CreateFile((LPCWSTR)GetWindowsFilename().c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
884
	HANDLE filename = CreateFile(str, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
706
	// Set the file time on the file
885
	// Set the file time on the file
707
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
886
	SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&ft);
708
	// Close our handle.
887
	// Close our handle.
709
	CloseHandle(filename);
888
	CloseHandle(filename);
-
 
889
 
710
#endif
890
#endif
711
}
891
}
712
 
892
 
713
time_t CFileIO::GetCreationTime()
893
time_t CFileIO::GetCreationTime()
-
 
894
{
-
 
895
	return modifiedTime();
-
 
896
}
-
 
897
 
-
 
898
time_t CFileIO::modifiedTime()
714
{
899
{
715
#ifdef _WIN32
900
#ifdef _WIN32
-
 
901
	WCHAR    str[MAX_PATH];
-
 
902
	MultiByteToWideChar(CP_ACP, NULL, GetWindowsFilename().c_str(), -1, str, GetWindowsFilename().Length() + 1);
-
 
903
 
716
	WIN32_FILE_ATTRIBUTE_DATA wfad;
904
	WIN32_FILE_ATTRIBUTE_DATA wfad;
717
	GetFileAttributesEx((LPCWSTR)GetWindowsFilename().c_str(), GetFileExInfoStandard, &wfad);
905
	GetFileAttributesEx(str, GetFileExInfoStandard, &wfad);
718
 
906
 
719
	LARGE_INTEGER date, adjust;
907
	LARGE_INTEGER date, adjust;
720
	date.HighPart = wfad.ftCreationTime.dwHighDateTime;
908
	date.HighPart = wfad.ftLastWriteTime.dwHighDateTime;
721
	date.LowPart = wfad.ftCreationTime.dwLowDateTime;
909
	date.LowPart = wfad.ftLastWriteTime.dwLowDateTime;
722
 
910
 
723
	// 100-nanoseconds = milliseconds * 10000
911
	// 100-nanoseconds = milliseconds * 10000
724
	adjust.QuadPart = 11644473600000 * 10000;
912
	adjust.QuadPart = 11644473600000 * 10000;
725
 
913
 
726
	// removes the diff between 1970 and 1601
914
	// removes the diff between 1970 and 1601
727
	date.QuadPart -= adjust.QuadPart;
915
	date.QuadPart -= adjust.QuadPart;
728
 
916
 
729
	// converts back from 100-nanoseconds to seconds
917
	// converts back from 100-nanoseconds to seconds
730
	return (time_t)(date.QuadPart / 10000000);
918
	return (time_t)(date.QuadPart / 10000000);
Line 738... Line 926...
738
/**
926
/**
739
 * Copys the contents of a file to another
927
 * Copys the contents of a file to another
740
 *
928
 *
741
 * Reads and writes the files in block
929
 * Reads and writes the files in block
742
 */
930
 */
743
bool CFileIO::Copy(CyString toFile, bool keepTime)
931
bool CFileIO::copy(const Utils::String &toFile, bool keepTime)
744
{
932
{
745
	//open both files
-
 
746
	FILE *id = fopen(GetWindowsFilename().c_str(), "rb");
-
 
747
	if ( !id )
-
 
748
		return false;
-
 
749
	FILE *toID = fopen(toFile.c_str(), "wb");
933
	time_t time = GetCreationTime();
750
	if ( !toID )
-
 
751
	{
-
 
752
		fclose(id);
-
 
753
		return false;
-
 
754
	}
-
 
755
 
934
 
756
	time_t time = GetCreationTime();
-
 
757
 
-
 
758
	// get length of file
935
	CFileIO File(toFile);
759
	fseek(id, 0, SEEK_END);
936
	if ( File.write(*this, m_lSize) ) {
760
	size_t remainingLen = ftell(id);
937
		if ( keepTime )	File.SetCreationTime(time);
761
	fseek(id, 0, SEEK_SET);
-
 
762
 
-
 
763
	if ( remainingLen <= 0 )
-
 
764
	{
-
 
765
		fclose(id);
-
 
766
		fclose(toID);
-
 
767
		return false;
938
		return true;
768
	}
-
 
769
 
-
 
770
 
-
 
771
	// read then write a block
-
 
772
	char block[100000];
-
 
773
	while ( remainingLen )
-
 
774
	{
-
 
775
		size_t amt = 100000;
-
 
776
		if ( amt > remainingLen )
-
 
777
			amt = remainingLen;
-
 
778
 
-
 
779
		fread(block, amt, sizeof(char), id);
-
 
780
		fwrite(block, amt, sizeof(char), toID);
-
 
781
 
-
 
782
		remainingLen -= amt;
-
 
783
	}
939
	}
-
 
940
/*
-
 
941
	std::fstream f(GetWindowsFilename().c_str(), std::fstream::in | std::fstream::binary);
-
 
942
	f << std::noskipws;
-
 
943
	std::istream_iterator<unsigned char> begin(f);
-
 
944
	std::istream_iterator<unsigned char> end;
-
 
945
 
-
 
946
	std::fstream f2(toFile.c_str(), std::fstream::out | std::fstream::trunc | std::fstream::binary);
-
 
947
	std::ostream_iterator<char> begin2(f2);
-
 
948
 
-
 
949
	std::copy(begin, end, begin2);
-
 
950
	return f2.good();
-
 
951
*/
-
 
952
	return false;
-
 
953
}
784
 
954
 
785
	fclose(id);
-
 
786
	fclose(toID);
-
 
787
 
-
 
788
	if ( keepTime )
-
 
789
		CFileIO(toFile.c_str()).SetCreationTime(time);
955
size_t CFileIO::fileSize() const
790
 
-
 
791
 
956
{
792
	return true;
957
	return m_lSize;
793
}
958
}