Subversion Repositories spk

Rev

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

Rev 33 Rev 42
Line 194... Line 194...
194
	char *data = new char[offset];
194
	char *data = new char[offset];
195
	fread ( data, sizeof(unsigned char), offset, id );
195
	fread ( data, sizeof(unsigned char), offset, id );
196
	fwrite ( data, sizeof(unsigned char), offset, writeId );
196
	fwrite ( data, sizeof(unsigned char), offset, writeId );
197
	delete data;
197
	delete data;
198
 
198
 
199
	/*
-
 
200
	// first write the data before the file
-
 
201
	size_t tosize = offset;
-
 
202
	while ( tosize )
-
 
203
	{
-
 
204
		int read = 500000;
-
 
205
		if ( read > tosize )
-
 
206
			read = tosize;
-
 
207
 
-
 
208
		fread ( data, sizeof(unsigned char), read, id );
-
 
209
		fwrite ( data, sizeof(unsigned char), read, writeId );
-
 
210
 
-
 
211
		tosize -= read;
-
 
212
	}
-
 
213
*/
-
 
214
	// next fseek after and write
199
	// next fseek after and write
215
	fseek ( id, datasize, SEEK_CUR );
200
	fseek ( id, datasize, SEEK_CUR );
216
	size = fullsize - offset - datasize;
201
	size = fullsize - offset - datasize;
-
 
202
	if ( size > 0 ) {
217
	data = new char[size];
203
		data = new char[size];
218
	fread ( data, sizeof(unsigned char), size, id );
204
		fread ( data, sizeof(unsigned char), size, id );
219
	fwrite ( data, sizeof(unsigned char), size, writeId );
205
		fwrite ( data, sizeof(unsigned char), size, writeId );
220
 
-
 
221
	delete data;
206
		delete data;
222
	/*
-
 
223
	while ( size )
-
 
224
	{
207
	}
225
		int read = 500000;
-
 
226
		if ( read > size )
-
 
227
			read = size;
-
 
228
 
-
 
229
		fread ( data, sizeof(unsigned char), read, id );
-
 
230
		fwrite ( data, sizeof(unsigned char), read, writeId );
-
 
231
 
-
 
232
		size -= read;
-
 
233
	}*/
-
 
234
 
208
 
235
	fclose ( writeId );
209
	fclose ( writeId );
236
	fclose ( id );
210
	fclose ( id );
237
 
211
 
238
	// now copy to original file
212
	// now copy to original file
Line 257... Line 231...
257
		// read data
231
		// read data
258
		fseek ( id, readpos, SEEK_SET );
232
		fseek ( id, readpos, SEEK_SET );
259
		fread ( data, sizeof(unsigned char), read, id );
233
		fread ( data, sizeof(unsigned char), read, id );
260
		size -= read;
234
		size -= read;
261
		readpos += read;
235
		readpos += read;
262
 
236
 
263
		// now seek back and write
237
		// now seek back and write
264
		fseek ( id, writepos, SEEK_SET );
238
		fseek ( id, writepos, SEEK_SET );
265
		fwrite ( data, sizeof(unsigned char), read, id );
239
		fwrite ( data, sizeof(unsigned char), read, id );
266
		writepos += read;
240
		writepos += read;
267
 
241
 
268
	}
242
	}
269
 
243
 
270
	truncate ( m_sFilename.c_str(), fullsize - datasize );
244
	truncate ( m_sFilename.c_str(), fullsize - datasize );
271
	fclose ( id );
245
	fclose ( id );
272
#endif
246
#endif
273
 
247
 
274
	return FILEERR_NONE;
248
	return FILEERR_NONE;
275
}
249
}
276
 
250
 
277
char *CFileIO::ReadToData ( size_t *size )
251
char *CFileIO::ReadToData ( size_t *size )
278
{
252
{
279
	*size = 0;
253
	*size = 0;
280
 
254
 
281
	if ( NoFile() )
255
	if ( NoFile() )
282
		return NULL;
256
		return NULL;
283
 
257
 
284
	if ( !m_lSize )
258
	if ( !m_lSize )
285
		ReadFileSize();
259
		ReadFileSize();
286
 
260
 
287
	if ( !m_lSize )
261
	if ( !m_lSize )
288
		return NULL;
262
		return NULL;
289
 
263
 
290
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb" : "r" );
264
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "rb" : "r" );
291
	if ( !id )
265
	if ( !id )
292
		return NULL;
266
		return NULL;
293
 
267
 
294
	char *data = new char[m_lSize];
268
	char *data = new char[m_lSize];
295
 
269
 
296
	fread ( data, sizeof(char), m_lSize, id );
270
	fread ( data, sizeof(char), m_lSize, id );
297
	if ( ferror (id) )
271
	if ( ferror (id) )
298
	{
272
	{
Line 310... Line 284...
310
{
284
{
311
	if ( NoFile() )
285
	if ( NoFile() )
312
		return false;
286
		return false;
313
 
287
 
314
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "wb" : "w" );
288
	FILE *id = fopen ( m_sFilename.c_str(), (m_bBinary) ? "wb" : "w" );
315
	if ( !id )
289
	if ( !id )
316
		return false;
290
		return false;
317
 
291
 
318
	fwrite ( data, size, sizeof(char), id );
292
	fwrite ( data, size, sizeof(char), id );
319
	fclose ( id );
293
	fclose ( id );
320
 
294
 
321
	ReadFileSize();
295
	ReadFileSize();
322
 
296
 
323
	return true;
297
	return true;
324
}
298
}
325
 
299
 
326
bool CFileIO::WriteString ( CyString data )
300
bool CFileIO::WriteString ( CyString data )
327
{
301
{
328
	return WriteData ( data.c_str(), data.Length() );
302
	return WriteData ( data.c_str(), data.Length() );
Line 334... Line 308...
334
	if ( !id )
308
	if ( !id )
335
		return false;
309
		return false;
336
 
310
 
337
	fclose ( id );
311
	fclose ( id );
338
	return true;
312
	return true;
339
}
313
}
340
 
314
 
341
bool CFileIO::StartReadOld()
315
bool CFileIO::StartReadOld()
342
{
316
{
343
	if ( !m_sFilename.Empty() )
317
	if ( !m_sFilename.Empty() )
344
	{
318
	{
Line 355... Line 329...
355
}
329
}
356
 
330
 
357
bool CFileIO::StartRead()
331
bool CFileIO::StartRead()
358
{
332
{
359
	if ( !m_sFilename.Empty() )
333
	if ( !m_sFilename.Empty() )
360
	{
334
	{
361
		if ( m_bOpened )
335
		if ( m_bOpened )
362
			StopRead();
336
			StopRead();
363
 
337
 
364
		m_fId.open(m_sFilename.c_str(), std::ios_base::in);
338
		m_fId.open(m_sFilename.c_str(), std::ios_base::in);
365
		if ( m_fId.is_open() )
339
		if ( m_fId.is_open() )
366
		{
340
		{
367
			m_bOpened = true;
341
			m_bOpened = true;
368
			return true;
342
			return true;
369
		}
343
		}
370
	}
344
	}
371
	return false;
345
	return false;
372
}
346
}
373
 
347
 
374
std::vector<CyString> *CFileIO::ReadLines()
348
std::vector<CyString> *CFileIO::ReadLines()
375
{
349
{
376
	if ( m_sFilename.Empty() )
350
	if ( m_sFilename.Empty() )
377
		return 0;
351
		return 0;
378
 
352
 
379
	std::vector<CyString> *file = new std::vector<CyString>;
353
	std::vector<CyString> *file = new std::vector<CyString>;
380
	std::string line;
354
	std::string line;
381
	file->clear();
355
	file->clear();
Line 399... Line 373...
399
 
373
 
400
	CyStringList *file = new CyStringList;
374
	CyStringList *file = new CyStringList;
401
	std::string line;
375
	std::string line;
402
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
376
	std::ifstream infile (m_sFilename.c_str(), std::ios_base::in);
403
	while (getline(infile, line, '\n'))
377
	while (getline(infile, line, '\n'))
404
	{
378
	{
405
		CyString l = line;
379
		CyString l = line;
406
		l.RemoveChar((char)0);
380
		l.RemoveChar((char)0);
407
		file->PushBack(l);
381
		file->PushBack(l);
408
	}
382
	}
409
 
383
 
Line 486... Line 460...
486
	}
460
	}
487
	else
461
	else
488
	{
462
	{
489
		if ( m_fId.eof() )
463
		if ( m_fId.eof() )
490
			return true;
464
			return true;
491
	}
465
	}
492
 
466
 
493
	return false;
467
	return false;
494
}
468
}
495
 
469
 
496
bool CFileIO::AppendFile ( CyString filename )
470
bool CFileIO::AppendFile ( CyString filename )
497
{
471
{
498
	FILE *id = fopen ( filename.c_str(), (m_bBinary) ? "rb" : "r" );
472
	FILE *id = fopen ( filename.c_str(), (m_bBinary) ? "rb" : "r" );
499
	if ( !id )
473
	if ( !id )
500
		return false;
474
		return false;
501
 
475
 
502
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
476
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
503
	if ( !aid )
477
	if ( !aid )
504
	{
478
	{
505
		return false;
479
		return false;
506
		fclose ( id );
480
		fclose ( id );
507
	}
481
	}
508
 
482
 
509
	// move to the end of the file
483
	// move to the end of the file
510
	fseek ( aid, 0, SEEK_END );
484
	fseek ( aid, 0, SEEK_END );
511
 
485
 
512
	// get size of file
486
	// get size of file
513
	fseek ( id, 0, SEEK_END );
487
	fseek ( id, 0, SEEK_END );
514
	size_t size = ftell ( id );
488
	size_t size = ftell ( id );
515
	fseek ( id, 0, SEEK_SET );
489
	fseek ( id, 0, SEEK_SET );
516
 
490
 
517
	char data[500000];
491
	char data[500000];
518
	while ( size > 0 )
492
	while ( size > 0 )
519
	{
493
	{
520
		size_t read = 500000;
494
		size_t read = 500000;
521
		if ( read > size )
495
		if ( read > size )
Line 527... Line 501...
527
		fwrite ( data, sizeof(char), read, aid );
501
		fwrite ( data, sizeof(char), read, aid );
528
	}
502
	}
529
 
503
 
530
	fclose ( aid );
504
	fclose ( aid );
531
	fclose ( id );
505
	fclose ( id );
532
	return true;
506
	return true;
533
}
507
}
534
 
508
 
535
 
509
 
536
bool CFileIO::AppendData ( const char *d, size_t size )
510
bool CFileIO::AppendData ( const char *d, size_t size )
537
{
511
{
538
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
512
	FILE *aid = fopen ( m_sFilename.c_str(), (m_bBinary) ? "ab" : "a" );
539
	if ( !aid )
513
	if ( !aid )
540
		return false;
514
		return false;
541
 
515
 
542
	// move to the end of the file
516
	// move to the end of the file
543
	fseek ( aid, 0, SEEK_END );
517
	fseek ( aid, 0, SEEK_END );
544
 
518
 
545
	char *pos = (char *)d;
519
	char *pos = (char *)d;
546
	while ( size > 0 )
520
	while ( size > 0 )
547
	{
521
	{
548
		size_t read = 500000;
522
		size_t read = 500000;
549
		if ( read > size )
523
		if ( read > size )
550
			read = size;
524
			read = size;
551
 
525
 
552
		size -= read;
526
		size -= read;
553
 
527
 
554
		fwrite ( pos, sizeof(char), read, aid );
528
		fwrite ( pos, sizeof(char), read, aid );
555
		pos += read;
529
		pos += read;
556
	}
530
	}
557
 
531
 
558
	fclose ( aid );
532
	fclose ( aid );
559
	return true;
533
	return true;
560
}
534
}
561
 
535
 
562
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
536
bool CFileIO::AppendDataToPos ( const char *d, size_t size, size_t start )
Line 572... Line 546...
572
	{
546
	{
573
		fclose ( aid);
547
		fclose ( aid);
574
		return false;
548
		return false;
575
	}
549
	}
576
 
550
 
-
 
551
	if ( start == end ) {
-
 
552
		fseek(aid, 0, SEEK_END);
-
 
553
	}
-
 
554
	else {
577
	fseek ( aid, start, SEEK_SET );
555
		fseek ( aid, start, SEEK_SET );
-
 
556
	}
578
 
557
 
579
	char *pos = (char *)d;
558
	char *pos = (char *)d;
580
	while ( size > 0 )
559
	while ( size > 0 )
581
	{
560
	{
582
		size_t read = 500000;
561
		size_t read = 500000;