[Scummvm-cvs-logs] SF.net SVN: scummvm:[42646] tools/branches/gsoc2009-gui

Remere at users.sourceforge.net Remere at users.sourceforge.net
Wed Jul 22 02:12:50 CEST 2009


Revision: 42646
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42646&view=rev
Author:   Remere
Date:     2009-07-22 00:12:48 +0000 (Wed, 22 Jul 2009)

Log Message:
-----------
*Removed old IO functions and the implicit conversion from File to FILE*

Modified Paths:
--------------
    tools/branches/gsoc2009-gui/compress.cpp
    tools/branches/gsoc2009-gui/compress_gob.cpp
    tools/branches/gsoc2009-gui/compress_scumm_bun.cpp
    tools/branches/gsoc2009-gui/compress_scumm_san.cpp
    tools/branches/gsoc2009-gui/compress_scumm_sou.cpp
    tools/branches/gsoc2009-gui/compress_sword1.cpp
    tools/branches/gsoc2009-gui/compress_sword2.cpp
    tools/branches/gsoc2009-gui/encode_dxa.cpp
    tools/branches/gsoc2009-gui/extract_gob_stk.cpp
    tools/branches/gsoc2009-gui/kyra_ins.cpp
    tools/branches/gsoc2009-gui/kyra_pak.cpp
    tools/branches/gsoc2009-gui/util.cpp
    tools/branches/gsoc2009-gui/util.h

Modified: tools/branches/gsoc2009-gui/compress.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -243,7 +243,7 @@
 			char *rawData;
 
 			File inputRaw(inname, "rb");
-			length = fileSize(inputRaw);
+			length = inputRaw.size();
 			rawData = (char *)malloc(length);
 			inputRaw.read(rawData, 1, length);
 
@@ -270,11 +270,11 @@
 			sampleRate = inputWav.readUint32LE();
 
 			inputWav.seek(34, SEEK_SET);
-			bitsPerSample = readUint16LE(inputWav);
+			bitsPerSample = inputWav.readUint16LE();
 
 			/* The size of the raw audio is after the RIFF chunk (12 bytes), fmt chunk (8 + fmtHeaderSize bytes), and data chunk id (4 bytes) */
-			fseek(inputWav, 24 + fmtHeaderSize, SEEK_SET);
-			length = readUint32LE(inputWav);
+			inputWav.seek(24 + fmtHeaderSize, SEEK_SET);
+			length = inputWav.readUint32LE();
 
 			wavData = (char *)malloc(length);
 			inputWav.read(wavData, 1, length);
@@ -415,8 +415,8 @@
 				break;
 			}
 
-			fwrite(og.header, 1, og.header_len, outputOgg);
-			fwrite(og.body, 1, og.body_len, outputOgg);
+			outputOgg.write(og.header, 1, og.header_len);
+			outputOgg.write(og.body, 1, og.body_len);
 		}
 
 		while (!eos) {
@@ -470,8 +470,8 @@
 							break;
 						}
 
-						totalBytes += fwrite(og.header, 1, og.header_len, outputOgg);
-						totalBytes += fwrite(og.body, 1, og.body_len, outputOgg);
+						totalBytes += outputOgg.write(og.header, 1, og.header_len);
+						totalBytes += outputOgg.write(og.body, 1, og.body_len);
 
 						if (ogg_page_eos(&og)) {
 							eos = 1;
@@ -625,14 +625,14 @@
 			real_samplerate = getSampleRateFromVOCRate(sample_rate);
 		} else { /* (blocktype == 9) */
 			length -= 12;
-			real_samplerate = sample_rate = readUint32LE(input);
+			real_samplerate = sample_rate = input.readUint32LE();
 			bits = input.readChar();;
 			channels = input.readChar();;
 			if (bits != 8 || channels != 1) {
 				error("Unsupported VOC file format (%d bits per sample, %d channels)", bits, channels);
 			}
-			comp = readUint16LE(input);
-			readUint32LE(input);
+			comp = input.readUint16LE();
+			input.readUint32LE();
 		}
 
 		print(" - length = %d\n", length);
@@ -650,14 +650,14 @@
 
 		/* Copy the raw data to a temporary file */
 		while (length > 0) {
-			size = fread(fbuf, 1, length > sizeof(fbuf) ? sizeof(fbuf) : (uint32)length, input);
+			size = input.readN(fbuf, 1, length > sizeof(fbuf) ? sizeof(fbuf) : (uint32)length);
 
 			if (size <= 0) {
 				break;
 			}
 
 			length -= (int)size;
-			fwrite(fbuf, 1, size, f);
+			f.write(fbuf, 1, size);
 		}
 	}
 

Modified: tools/branches/gsoc2009-gui/compress_gob.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_gob.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress_gob.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -109,27 +109,25 @@
 	Chunk *curChunk = chunks;
 	Chunk *parseChunk;
 	File src1;
-	char buffer[1024];
 
 	chunkCount = 1;
 
 // First read: Output filename
-	fscanf(gobConf, "%s", buffer);
-	stkName.setFullName(buffer);
+	stkName.setFullName(gobConf.readString());
 
 // Second read: signature
-	fscanf(gobConf, "%s", buffer);
-	if (!strcmp(buffer, confSTK21))
+	std::string signature = gobConf.readString();
+	if (signature == confSTK21)
 		error("STK21 not yet handled");
-	else if (strcmp(buffer, confSTK10))
+	else if (signature != confSTK10)
 		error("Unknown format signature");
 
 // All the other reads concern file + compression flag
-	fscanf(gobConf, "%s", buffer);
-	while (!feof(gobConf)) {
-		strcpy(curChunk->name, buffer);
-		fscanf(gobConf, "%s", buffer);
-		if ((strcmp(buffer, "1") == 0) || (_execMode & MODE_FORCE))
+	std::string fname = gobConf.readString();
+	while (!gobConf.reachedEOF()) {
+		strcpy(curChunk->name, fname.c_str());
+		fname = gobConf.readString();
+		if ((fname == "1") || (_execMode & MODE_FORCE))
 			curChunk->packed = true;
 		else
 			curChunk->packed = false;
@@ -157,8 +155,8 @@
 		}
 		src1.close();
 		
-		fscanf(gobConf, "%s", buffer);
-		if (!feof(gobConf)) {
+		std::string tmp = gobConf.readString();
+		if (!gobConf.reachedEOF()) {
 			curChunk->next = new Chunk;
 			curChunk = curChunk->next;
 			chunkCount++;

Modified: tools/branches/gsoc2009-gui/compress_scumm_bun.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_scumm_bun.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress_scumm_bun.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -689,7 +689,7 @@
 }
 
 void CompressScummBun::writeToTempWave(char *fileName, byte *output_data, unsigned int size) {
-	if (!_waveTmpFile) {
+	if (!_waveTmpFile.isOpen()) {
 		_waveTmpFile.open(fileName, "wb");
 		byte wav[44];
 		memset(wav, 0, 44);
@@ -1112,11 +1112,11 @@
 		int z2;
 
 		for (z2 = 0; z2 < 8; z2++)
-			if ((c = readByte(input)) != 0)
+			if ((c = input.readByte()) != 0)
 				filename[z++] = c;
 		filename[z++] = '.';
 		for (z2 = 0; z2 < 4; z2++)
-			if ((c = readByte(input)) != 0)
+			if ((c = input.readByte()) != 0)
 				filename[z++] = c;
 		filename[z] = '\0';
 		strcpy(_bundleTable[i].filename, filename);

Modified: tools/branches/gsoc2009-gui/compress_scumm_san.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_scumm_san.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress_scumm_san.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -89,9 +89,9 @@
 	_waveTmpFile.write(wav, 1, 44);
 }
 void CompressScummSan::writeToTempWaveFile(char *fileName, byte *output_data, unsigned int size) {
-	if (!_waveTmpFile) {
+	if (!_waveTmpFile.isOpen()) {
 		_waveTmpFile.open(fileName, "wb");
-		if (!_waveTmpFile) {
+		if (!_waveTmpFile.isOpen()) {
 			error("error writing temp wave file");
 		}
 		byte wav[44];
@@ -191,7 +191,7 @@
 
 CompressScummSan::AudioTrackInfo *CompressScummSan::findAudioTrack(int trackId) {
 	for (int l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
-		if (_audioTracks[l].trackId == trackId && _audioTracks[l].used && _audioTracks[l].file)
+		if (_audioTracks[l].trackId == trackId && _audioTracks[l].used && _audioTracks[l].file.isOpen())
 			return &_audioTracks[l];
 	}
 	return NULL;
@@ -199,7 +199,7 @@
 
 void CompressScummSan::flushTracks(int frame) {
 	for (int l = 0; l < COMPRESS_SCUMM_SAN_MAX_TRACKS; l++) {
-		if (_audioTracks[l].used && _audioTracks[l].file && (frame - _audioTracks[l].lastFrame) > 1) {
+		if (_audioTracks[l].used && _audioTracks[l].file.isOpen() && (frame - _audioTracks[l].lastFrame) > 1) {
 			_audioTracks[l].file.close();
 		}
 	}
@@ -338,7 +338,7 @@
 
 	print("Creating silent wav file...\n");
 	for (l = 0; l < 44 + (frameAudioSize * frames); l++) {
-		fputc(0, wavFile);
+		wavFile.writeByte(0);
 	}
 
 	print("Mixing tracks into wav file...\n");
@@ -346,7 +346,6 @@
 		if (_audioTracks[l].used) {
 			sprintf(filename, "%s/%s_%04d_%03d.tmp", outputDir, inputFilename, _audioTracks[l].animFrame, _audioTracks[l].trackId);
 			_audioTracks[l].file.open(filename, "rb");
-			assert(_audioTracks[l].file);
 			_audioTracks[l].file.seek(0, SEEK_END);
 			int fileSize = _audioTracks[l].file.pos();
 			_audioTracks[l].file.seek(0, SEEK_SET);
@@ -495,7 +494,7 @@
 		}
 		sprintf(tmpPath, "%s/%s_%04d_%03d.tmp", outputDir, inputFilename, frame, trackId);
 		audioTrack->file.open(tmpPath, "wb");
-		if (!audioTrack->file) {
+		if (!audioTrack->file.isOpen()) {
 			error("error writing temp file");
 		}
 	} else {
@@ -532,10 +531,10 @@
 }
 
 void CompressScummSan::handleDigIACT(File &input, int size, const char *outputDir, const char *inputFilename,int flags, int track_flags, int frame) {
-	int track = readUint16LE(input);
-	int index = readUint16LE(input);
-	int nbframes = readUint16LE(input);
-	/*int data_size = */ readUint32LE(input);
+	int track = input.readUint16LE();
+	int index = input.readUint16LE();
+	int nbframes = input.readUint16LE();
+	/*int data_size = */ input.readUint32LE();
 	int volume = 127;
 	int trackId = track;
 	int pan = 0;
@@ -563,12 +562,12 @@
 }
 
 void CompressScummSan::handlePSAD(File &input, int size, const char *outputDir, const char *inputFilename, int frame) {
-	int trackId = readUint16LE(input);
-	int index = readUint16LE(input);
-	int nbframes = readUint16LE(input);
-	/*int flags = */ readUint16LE(input);
-	int volume = readByte(input);
-	int pan = readByte(input);
+	int trackId = input.readUint16LE();
+	int index = input.readUint16LE();
+	int nbframes = input.readUint16LE();
+	/*int flags = */ input.readUint16LE();
+	int volume = input.readByte();
+	int pan = input.readByte();
 
 	handleAudioTrack(index, trackId, frame, nbframes, input, outputDir, inputFilename, size, volume, pan, false);
 }
@@ -627,12 +626,12 @@
 	output.writeUint32BE(input.readUint32BE()); // AHDR
 	size = input.readUint32BE();
 	output.writeUint32BE(size); // AHDR size
-	writeUint16BE(output, readUint16BE(input)); // version
-	int32 nbframes = readUint16LE(input); // number frames
-	writeUint16LE(output, nbframes);
-	writeUint16BE(output, readUint16BE(input)); // unk
+	output.writeUint16BE(input.readUint16BE()); // version
+	int32 nbframes = input.readUint16LE(); // number frames
+	output.writeUint16LE(nbframes);
+	output.writeUint16BE(input.readUint16BE()); // unk
 	for (l = 0; l < size - 6; l++) {
-		writeByte(output, readByte(input)); // 0x300 palette + some bytes
+		output.writeByte(input.readByte()); // 0x300 palette + some bytes
 	}
 
 	FrameInfo *frameInfo = (FrameInfo *)malloc(sizeof(FrameInfo) * nbframes);
@@ -661,7 +660,7 @@
 		output.writeUint32BE(frameSize);
 		for (;;) {
 			tag = input.readUint32BE(); // chunk tag
-			if (feof(input))
+			if (input.reachedEOF())
 				break;
 			if (tag == 'FRME') {
 				input.seek(-4, SEEK_CUR);
@@ -675,7 +674,7 @@
 				byte *zlibInputBuffer = (byte *)malloc(size);
 				byte *zlibOutputBuffer = (byte *)malloc(outputSize);
 				for (int k = 0; k < size; k++) {
-					*(zlibInputBuffer + k) = readByte(input); // FOBJ datas
+					*(zlibInputBuffer + k) = input.readByte(); // FOBJ datas
 				}
 				int result = compress2(zlibOutputBuffer, &outputSize, zlibInputBuffer, size, 9);
 				if (result != Z_OK) {
@@ -689,17 +688,17 @@
 				output.writeUint32BE(outputSize + 4);
 				output.writeUint32BE(size);
 				for (unsigned int k = 0; k < outputSize; k++) {
-					writeByte(output, *(zlibOutputBuffer + k)); // compressed FOBJ datas
+					output.writeByte(*(zlibOutputBuffer + k)); // compressed FOBJ datas
 				}
 				free(zlibInputBuffer);
 				free(zlibOutputBuffer);
 				continue;
-			} else if ((tag == 'IACT') && (!flu_in)) {
+			} else if ((tag == 'IACT') && (!flu_in.isOpen())) {
 				size = input.readUint32BE(); // chunk size
-				int code = readUint16LE(input);
-				int flags = readUint16LE(input);
-				int unk = readUint16LE(input);
-				int track_flags = readUint16LE(input);
+				int code = input.readUint16LE();
+				int flags = input.readUint16LE();
+				int unk = input.readUint16LE();
+				int track_flags = input.readUint16LE();
 				if ((code == 8) && (track_flags == 0) && (unk == 0) && (flags == 46)) {
 					handleComiIACT(input, size, outpath.getPath().c_str(), inpath.getFullName().c_str());
 				} else if ((code == 8) && (track_flags != 0) && (unk == 0) && (flags == 46)) {
@@ -717,7 +716,7 @@
 				}
 				frameInfo[l].lessIACTSize += size + 8;
 				continue;
-			} else if ((tag == 'PSAD') && (!flu_in)) {
+			} else if ((tag == 'PSAD') && (!flu_in.isOpen())) {
 				size = input.readUint32BE(); // chunk size
 				handlePSAD(input, size, outpath.getPath().c_str(), inpath.getFullName().c_str(), l);
 				if ((size & 1) != 0) {
@@ -735,7 +734,7 @@
 				if ((size & 1) != 0)
 					size++;
 				for (int k = 0; k < size; k++) {
-					writeByte(output, readByte(input)); // chunk datas
+					output.writeByte(input.readByte()); // chunk datas
 				}
 			}
 		}
@@ -747,7 +746,7 @@
 		mixing(outpath.getPath().c_str(), inpath.getFullName().c_str(), nbframes, fps);
 	}
 
-	if (_waveTmpFile) {
+	if (_waveTmpFile.isOpen()) {
 		char tmpPath[1024];
 		writeWaveHeader(_waveDataSize);
 		sprintf(tmpPath, "%s/%s", outpath.getPath().c_str(), inpath.getFullName().c_str());
@@ -790,7 +789,7 @@
 		print("Fixing flu offsets...\n");
 		int fsize = flu_in.size();
 		for (int k = 0; k < fsize; k++) {
-			flu_out.writeByte(readByte(flu_in));
+			flu_out.writeByte(flu_in.readByte());
 		}
 		flu_out.seek(0x324, SEEK_SET);
 		for (l = 0; l < nbframes; l++) {

Modified: tools/branches/gsoc2009-gui/compress_scumm_sou.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_scumm_sou.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress_scumm_sou.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -88,7 +88,7 @@
 	while (memcmp(buf, "VCTL", 4)&&memcmp(buf, "VTTL", 4)) {
 		pos++;
 		append_byte(4, buf);
-		if (feof(_input)) {
+		if (_input.reachedEOF()) {
 			end_of_file(inputPath);
 			return;
 		}

Modified: tools/branches/gsoc2009-gui/compress_sword1.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_sword1.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress_sword1.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -312,7 +312,8 @@
 	int16 length, cnt;
 	uint8 *fBuf = (uint8 *)malloc(cSize);
 	clu.seek(idx, SEEK_SET);
-	assert(fread(fBuf, 1, cSize, clu) == cSize);
+	clu.read(fBuf, 1, cSize);
+
 	while ((READ_BE_UINT32(fBuf + headerPos) != 'data') && (headerPos < 100))
 		headerPos++;
 	if (headerPos < 100) {
@@ -378,7 +379,7 @@
 	cowHeader = (uint32*)malloc(headerSize);
 
 	for (cnt = 0; cnt < (headerSize / 4) - 1; cnt++)
-		cowHeader[cnt] = readUint32LE(clu);
+		cowHeader[cnt] = clu.readUint32LE();
 	assert(!(cowHeader[0] & 3));
 	numRooms = cowHeader[0] / 4;
 	assert(cowHeader[numRooms] == 0);	/* This dword should be unused. */
@@ -413,7 +414,7 @@
 			mp3Data = convertData(smpData, smpSize, &mp3Size);
 			cl3Index[cnt << 1] = cl3.pos();
 			cl3Index[(cnt << 1) | 1] = mp3Size;
-			assert(fwrite(mp3Data, 1, mp3Size, cl3) == mp3Size);
+			cl3.write(mp3Data, 1, mp3Size);
 
 			free(smpData);
 			free(mp3Data);
@@ -461,7 +462,7 @@
 		}
 
 		cl3.open(outName, "wb");
-		if (!cl3) {
+		if (!cl3.isOpen()) {
 			print("Unable to create file \"%s\".\n", outName);
 			print("Please make sure you've got write permission in this directory.\n");
 		} else {

Modified: tools/branches/gsoc2009-gui/compress_sword2.cpp
===================================================================
--- tools/branches/gsoc2009-gui/compress_sword2.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/compress_sword2.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -31,7 +31,7 @@
 	char fbuf[2048];
 
 	File f2(filename, "rb");
-	orig_length = length = fileSize(f2);
+	orig_length = length = f2.size();
 
 	while (length > 0) {
 		size = f2.readN(fbuf, 1, length > sizeof(fbuf) ? sizeof(fbuf) : length);
@@ -143,7 +143,7 @@
 			f.writeUint32BE(0x64617461);	/* "data" */
 			f.writeUint32LE(2 * length);
 
-			fseek(_input, pos, SEEK_SET);
+			_input.seek(pos, SEEK_SET);
 
 			/*
 			 * The first sample is stored uncompressed. Subsequent
@@ -158,7 +158,7 @@
 				byte data;
 				uint16 out;
 
-				data = readByte(_input);
+				data = _input.readByte();
 				if (GetCompressedSign(data))
 					out = prev - (GetCompressedAmplitude(data) << GetCompressedShift(data));
 				else

Modified: tools/branches/gsoc2009-gui/encode_dxa.cpp
===================================================================
--- tools/branches/gsoc2009-gui/encode_dxa.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/encode_dxa.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -664,7 +664,7 @@
 	//if (setjmp(png_jmpbuf(png_ptr)))
 	//	return 1;
 
-	png_init_io(png_ptr, fp);
+	png_init_io(png_ptr, fp.getFileHandle());
 	png_set_sig_bytes(png_ptr, 8);
 
 	png_read_info(png_ptr, info_ptr);

Modified: tools/branches/gsoc2009-gui/extract_gob_stk.cpp
===================================================================
--- tools/branches/gsoc2009-gui/extract_gob_stk.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/extract_gob_stk.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -73,16 +73,16 @@
 
 	gobConf.open(_outputPath.getFullPath(), "w");
 
-	fprintf(gobConf, "%s\n", inpath.getFullName().c_str());
+	gobConf.printf("%s\n", inpath.getFullName().c_str());
 
 	stk.read(signature, 1, 6);
 
 	if (strncmp(signature, "STK2.1", 6) == 0) {
 		print("Signature of new STK format (STK 2.1) detected in file \"%s\"", inpath.getFullPath().c_str());
-		fprintf(gobConf, "%s\n", confSTK21);
+		gobConf.printf("%s\n", confSTK21);
 		readChunkListV2(stk, gobConf);
 	} else {
-		fprintf(gobConf, "%s\n", confSTK10);
+		gobConf.printf("%s\n", confSTK10);
 		stk.rewind();
 		readChunkList(stk, gobConf);
 	}
@@ -117,7 +117,7 @@
 		}
 
 		// Write the chunk info in the gob Conf file
-		fprintf(gobConf, "%s %d\n", curChunk->name, curChunk->packed ? 1 : 0);
+		gobConf.printf("%s %d\n", curChunk->name, curChunk->packed ? 1 : 0);
 
 		if (numDataChunks > 0) {
 			curChunk->next = new Chunk;
@@ -227,8 +227,7 @@
 
 		stk.seek(filenamePos, SEEK_SET);
 
-		if (fgets(curChunk->name, 64, stk) == 0)
-			throw ToolException("Unable to read filename");
+		strcpy(curChunk->name, stk.readString().c_str());
 
 		// Files
 		// =====
@@ -240,7 +239,7 @@
 		curChunk->preGob = false;
 
 		// Write the chunk info in the gob Conf file
-		fprintf(gobConf, "%s %d\n", curChunk->name, curChunk->packed ? 1 : 0);
+		gobConf.printf("%s %d\n", curChunk->name, curChunk->packed ? 1 : 0);
 
 		if (numDataChunks > 0) {
 			curChunk->next = new Chunk;

Modified: tools/branches/gsoc2009-gui/kyra_ins.cpp
===================================================================
--- tools/branches/gsoc2009-gui/kyra_ins.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/kyra_ins.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -467,27 +467,25 @@
 		char filename[64];
 		snprintf(filename, 64, "%s%03d", _baseFilename, currentFile);
 
-		FILE *file = NULL;
-		if ((file = fopen(filename, "rb")) == NULL)
-			break;
+		File file(filename, "rb");
 
-		fseek(file, pos, SEEK_SET);
-		uint8 fileId = readByte(file);
+		file.seek(pos, SEEK_SET);
+		uint8 fileId = file.readByte();
 		pos++;
 
-		uint32 size = fileSize(file) - 1;
+		uint32 size = file.size() - 1;
 		if (startFile) {
 			size -= 4;
 			if (fileId == currentFile) {
 				size -= 6;
 				pos += 6;
-				fseek(file, 6, SEEK_CUR);
+				file.seek(6, SEEK_CUR);
 			} else {
 				size = size + 1 - pos;
 			}
 
 			strcpy(newArchive->filename, _baseFilename);
-			bytesleft = newArchive->totalSize = readUint32LE(file);
+			bytesleft = newArchive->totalSize = file.readUint32LE();
 			pos += 4;
 			newArchive->firstFile = currentFile;
 			newArchive->startOffset = pos;
@@ -497,7 +495,7 @@
 		uint32 cs = MIN(size, bytesleft);
 		bytesleft -= cs;
 
-		fclose(file);
+		file.close();
 
 		pos += cs;
 		if (cs == size) {
@@ -545,26 +543,21 @@
 			char filename[64];
 			snprintf(filename, 64, "%s%03d", _baseFilename, i);
 
-			FILE *file = NULL;
-			if ((file = fopen(filename, "rb")) == NULL) {
-				error("[2] Couldn't open file '%s'", filename);
-				break;
-			}
+			File file(filename, "rb");
 
-			uint32 size = (i == a->lastFile) ? a->endOffset : fileSize(file);
+			uint32 size = (i == a->lastFile) ? a->endOffset : file.size();
 
 			if (startFile) {
 				startFile = false;
 				pos = a->startOffset + kExecSize;
 				if (pos > size) {
 					pos -= size;
-					fclose(file);
 					continue;
 				}
 			} else {
 				if (inPart2) {
-					fseek(file, 1, SEEK_SET);
-					fread(inbuffer + inPart1, 1, inPart2, file);
+					file.seek(1, SEEK_SET);
+					file.read(inbuffer + inPart1, 1, inPart2);
 					inPart2 = 0;
 					exp.process(outbuffer, inbuffer, outsize, insize);
 					delete[] inbuffer;
@@ -589,33 +582,29 @@
 			while (pos < size) {
 				uint8 hdr[43];
 				uint32 m = 0;
-				fseek(file, pos, SEEK_SET);
+				file.seek(pos, SEEK_SET);
 
 				if (pos + 42 > size) {
 					m = size - pos;
 					uint32 b = 42 - m;
 
 					if (m >= 4) {
-						uint32 id = readUint32LE(file);
+						uint32 id = file.readUint32LE();
 						if (id == 0x06054B50) {
 							startFile = true;
 							break;
 						} else {
-							fseek(file, pos, SEEK_SET);
+							file.seek(pos, SEEK_SET);
 						}
 					}
 
 					snprintf(filename, 64, "%s.%03d", _baseFilename, i+1);
 
-					FILE *file2 = fopen(filename, "rb");
-					if (file2 == NULL)
-						error("Couldn't open file '%s'", filename);
-
-					fread(hdr, 1, m, file);
-					fread(hdr + m , 1, b, file2);
-					fclose(file2);
+					File file2(filename, "rb");
+					file.read(hdr, 1, m);
+					file2.read(hdr + m , 1, b);
 				} else {
-					fread(hdr, 1, 42, file);
+					file.read(hdr, 1, 42);
 				}
 
 				uint32 id = READ_LE_UINT32(hdr);
@@ -630,7 +619,7 @@
 					*(hdr + 30 + filestrlen) = 0;
 					strcpy(entryStr, (const char *)(hdr + 30));
 					pos += (kHeaderSize + filestrlen - m);
-					fseek(file, pos, SEEK_SET);
+					file.seek(pos, SEEK_SET);
 
 					outbuffer = new uint8[outsize];
 					assert(outbuffer);
@@ -644,9 +633,9 @@
 						// this is for files that are split between two archive files
 						inPart1 = size - pos;
 						inPart2 = insize - inPart1;
-						fread(inbuffer, 1, inPart1, file);
+						file.read(inbuffer, 1, inPart1);
 					} else {
-						fread(inbuffer, 1, insize, file);
+						file.read(inbuffer, 1, insize);
 						inPart2 = 0;
 						exp.process(outbuffer, inbuffer, outsize, insize);
 						delete[] inbuffer;
@@ -676,8 +665,6 @@
 					pos += (kHeaderSize2 + filestrlen - m);
 				}
 			}
-
-			fclose(file);
 		}
 	}
 }

Modified: tools/branches/gsoc2009-gui/kyra_pak.cpp
===================================================================
--- tools/branches/gsoc2009-gui/kyra_pak.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/kyra_pak.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -23,44 +23,39 @@
 #include "kyra_pak.h"
 
 bool PAKFile::isPakFile(const char *filename) {
-	FILE *f = fopen(filename, "rb");
-	if (!f)
-		error("Couldn't open file '%s'", filename);
+	File f(filename, "rb");
 
-	int32 filesize = fileSize(f);
+	int32 filesize = f.size();
 	int32 offset = 0;
 	bool switchEndian = false;
 	bool firstFile = true;
 
-	offset = readUint32LE(f);
+	offset = f.readUint32LE();
 	if (offset > filesize) {
 		switchEndian = true;
 		offset = SWAP_32(offset);
 	}
 
 	char lastFilenameByte = 0;
-	while (!feof(f)) {
+	while (!f.reachedEOF()) {
 		// The start offset of a file should never be in the filelist
-		if (offset < ftell(f) || offset > filesize) {
-			fclose(f);
+		if (offset < f.pos() || offset > filesize) {
 			return false;
 		}
 
 		byte c = 0;
 
 		lastFilenameByte = 0;
-		while (!feof(f) && (c = readByte(f)) != 0)
+		while (!f.reachedEOF() && (c = f.readByte()) != 0)
 			lastFilenameByte = c;
 
-		if (feof(f)) {
-			fclose(f);
+		if (f.reachedEOF()) {
 			return false;
 		}
 
 		// Quit now if we encounter an empty string
 		if (!lastFilenameByte) {
 			if (firstFile) {
-				fclose(f);
 				return false;
 			} else {
 				break;
@@ -68,13 +63,12 @@
 		}
 
 		firstFile = false;
-		offset = switchEndian ? readUint32BE(f) : readUint32LE(f);
+		offset = switchEndian ? f.readUint32BE() : f.readUint32LE();
 
 		if (!offset || offset == filesize)
 			break;
 	}
 
-	fclose(f);
 	return true;
 }
 
@@ -86,20 +80,16 @@
 	delete _fileList;
 	_fileList = 0;
 
-	FILE *pakfile = fopen(file, "rb");
-	if (!pakfile)
-		return false;
+	File pakfile(file, "rb");
 
-	uint32 filesize = fileSize(pakfile);
+	uint32 filesize = pakfile.size();
 
 	// TODO: get rid of temp. buffer
 	uint8 *buffer = new uint8[filesize];
 	assert(buffer);
 
-	(void)fread(buffer, filesize, 1, pakfile);
+	pakfile.read(buffer, filesize, 1);
 
-	fclose(pakfile);
-
 	const char *currentName = 0;
 
 	uint32 startoffset = _isAmiga ? READ_BE_UINT32(buffer) : READ_LE_UINT32(buffer);
@@ -144,11 +134,7 @@
 		return true;
 	generateLinkEntry();
 
-	FILE *f = fopen(file, "wb");
-	if (!f) {
-		error("couldn't open file '%s' for writing", file);
-		return false;
-	}
+	File f(file, "wb");
 
 	// TODO: implement error handling
 	uint32 startAddr = _fileList->getTableSize()+5+4;
@@ -157,22 +143,21 @@
 	uint32 curAddr = startAddr;
 	for (FileList *cur = _fileList; cur; cur = cur->next) {
 		if (_isAmiga)
-			writeUint32BE(f, curAddr);
+			f.writeUint32BE(curAddr);
 		else
-			writeUint32LE(f, curAddr);
-		fwrite(cur->filename, 1, strlen(cur->filename) + 1, f);
+			f.writeUint32LE(curAddr);
+		f.write(cur->filename, 1, strlen(cur->filename) + 1);
 		curAddr += cur->size;
 	}
 	if (_isAmiga)
-		writeUint32BE(f, curAddr);
+		f.writeUint32BE(curAddr);
 	else
-		writeUint32LE(f, curAddr);
-	fwrite(zeroName, 1, 5, f);
+		f.writeUint32LE(curAddr);
+	f.write(zeroName, 1, 5);
 
 	for (FileList *cur = _fileList; cur; cur = cur->next)
-		fwrite(cur->data, 1, cur->size, f);
+		f.write(cur->data, 1, cur->size);
 
-	fclose(f);
 	return true;
 }
 
@@ -199,20 +184,12 @@
 		return false;
 	}
 
-	FILE *f = fopen(file, "rb");
-	if (!f) {
-		error("couldn't open file '%s'", file);
-		return false;
-	}
+	File f(file, "rb");
 
-	uint32 filesize = fileSize(f);
+	uint32 filesize = f.size();
 	uint8 *data = new uint8[filesize];
 	assert(data);
-	if (fread(data, 1, filesize, f) != filesize) {
-		error("couldn't read from file '%s'", file);
-		return false;
-	}
-	fclose(f);
+	f.read(data, 1, filesize);
 	return addFile(name, data, filesize);
 }
 
@@ -287,9 +264,7 @@
 		return;
 
 	const int countLinks = _links->size();
-	FILE *output = fopen("LINKLIST.TMP", "wb");
-	if (!output)
-		error("Couldn't open file 'LINKLIST.TMP'");
+	File output("LINKLIST.TMP", "wb");
 
 	const char **linkList = new const char *[countLinks];
 	int usedLinks = 0;
@@ -302,8 +277,8 @@
 		linkList[usedLinks++] = entry->linksTo;
 	}
 
-	writeUint32BE(output, MKID_BE('SCVM'));
-	writeUint32BE(output, usedLinks);
+	output.writeUint32BE(MKID_BE('SCVM'));
+	output.writeUint32BE(usedLinks);
 	for (int i = 0; i < usedLinks; ++i) {
 		int count = 0;
 		entry = _links;
@@ -315,23 +290,22 @@
 
 		const char *p = linkList[i];
 		while (*p)
-			writeByte(output, *p++);
-		writeByte(output, 0);
+			output.writeByte(*p++);
+		output.writeByte(0);
 
-		writeUint32BE(output, count);
+		output.writeUint32BE(count);
 		for (entry = _links; entry; entry = entry->next) {
 			if (scumm_stricmp(entry->linksTo, linkList[i]) != 0)
 				continue;
 
 			p = entry->filename;
 			while (*p)
-				writeByte(output, *p++);
-			writeByte(output, 0);
+				output.writeByte(*p++);
+			output.writeByte(0);
 		}
 	}
+	output.close();
 
-	fclose(output);
-
 	addFile("LINKLIST", "LINKLIST.TMP");
 
 	unlink("LINKLIST.TMP");
@@ -487,7 +461,6 @@
 			printf("FAILED\n");
 			return false;
 		}
-		fclose(file);
 		cur = cur->next;
 	}
 	return true;
@@ -514,7 +487,6 @@
 		printf("FAILED\n");
 		return false;
 	}
-	fclose(file);
 	return true;
 }
 

Modified: tools/branches/gsoc2009-gui/util.cpp
===================================================================
--- tools/branches/gsoc2009-gui/util.cpp	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/util.cpp	2009-07-22 00:12:48 UTC (rev 42646)
@@ -20,6 +20,7 @@
  *
  */
 
+#include "stdio.h"
 #include "util.h"
 #include <stdarg.h>
 
@@ -458,6 +459,25 @@
 	return data_read;
 }
 
+std::string File::readString() {
+	if (!_file) 
+		throw FileException("File is not open");
+	if ((_mode & FILEMODE_READ) == 0)
+		throw FileException("Tried to read from file opened in write mode (" + _name.getFullPath() + ")");
+
+	std::string s;
+	try {
+		char c;
+		while (c = readByte()) {
+			s += c;
+		}
+	} catch (FileException &) {
+		// pass, we reached EOF
+	}
+
+	return s;
+}
+
 size_t File::readN(void *data, size_t elementSize, size_t elementCount) {
 	if (!_file) 
 		throw FileException("File is not open");
@@ -520,6 +540,20 @@
 	return data_read;
 }
 
+void File::printf(const char *format, ...) {
+	if (!_file) 
+		throw FileException("File is not open");
+	if ((_mode & FILEMODE_WRITE) == 0)
+		throw FileException("Tried to write to file opened in read mode (" + _name.getFullPath() + ")");
+
+
+	va_list va;
+
+	va_start(va, format);
+	vfprintf(_file, format, va);
+	va_end(va);
+}
+
 void File::seek(long offset, int origin) {
 	if (!_file) 
 		throw FileException("File is not open");
@@ -532,11 +566,15 @@
 	return ::rewind(_file);
 }
 
-int File::pos() {
+int File::pos() const {
 	return ftell(_file);
 }
 
-uint32 File::size() {
+bool File::reachedEOF() const {
+	return feof(_file) != 0;
+}
+
+uint32 File::size() const {
 	uint32 sz;
 	uint32 p = ftell(_file);
 	fseek(_file, 0, SEEK_END);

Modified: tools/branches/gsoc2009-gui/util.h
===================================================================
--- tools/branches/gsoc2009-gui/util.h	2009-07-21 19:39:24 UTC (rev 42645)
+++ tools/branches/gsoc2009-gui/util.h	2009-07-22 00:12:48 UTC (rev 42646)
@@ -207,20 +207,6 @@
 #define NORETURN_POST
 #endif
 
-
-/* File I/O */
-uint8 readByte(FILE *fp);
-uint16 readUint16BE(FILE *fp);
-uint16 readUint16LE(FILE *fp);
-uint32 readUint32BE(FILE *fp);
-uint32 readUint32LE(FILE *fp);
-void writeByte(FILE *fp, uint8 b);
-void writeUint16BE(FILE *fp, uint16 value);
-void writeUint16LE(FILE *fp, uint16 value);
-void writeUint32BE(FILE *fp, uint32 value);
-void writeUint32LE(FILE *fp, uint32 value);
-uint32 fileSize(FILE *fp);
-
 /* Misc stuff */
 void NORETURN_PRE error(const char *s, ...) NORETURN_POST;
 void warning(const char *s, ...);
@@ -377,9 +363,17 @@
 	 */
 	void close();
 
+	/**
+	 * If the file is opened for reading / writing
+	 */
 	bool isOpen() const { return _file != 0; }
 
 	/**
+	 * True if there is nothing more to read from this file
+	 */
+	bool reachedEOF() const;
+
+	/**
 	 * Sets the xor mode of the file, bytes written / read to the file
 	 * will be XORed with this value. This value is *not* reset when
 	 * opening a new file.
@@ -427,6 +421,7 @@
 	 * @param elementCount the number of elements to read
 	 */
 	size_t read(void *data, size_t elementSize, size_t elementCount);
+
 	/**
 	 * Works the same way as fread, does NOT throw if it could not read all elements
 	 * still throws if file is not open.
@@ -437,6 +432,11 @@
 	 */
 	size_t readN(void *data, size_t elementSize, size_t elementCount);
 
+	/**
+	 * Read a single 32-bit word, little endian.
+	 * Throws FileException if file is not open / if read failed.
+	 */
+	std::string readString();
 	
 	/**
 	 * Writes a single character (equivalent of fputc)
@@ -479,6 +479,11 @@
 	size_t write(const void *data, size_t elementSize, size_t elementCount);
 
 	/**
+	 * Works the same as fprintf
+	 */
+	void printf(const char *format, ...);
+
+	/**
 	 * Seek to the specified position in the stream.
 	 *
 	 * @param offset how many bytes to jump
@@ -492,19 +497,13 @@
 	/**
 	 * Returns current position of the file cursor
 	 */
-	int pos();
+	int pos() const;
 
 	/**
 	 * Returns the length of the file, in bytes, does not move the cursor.
 	 */
-	uint32 size();
+	uint32 size() const;
 
-	/**
-	 * We implicitly convert into a FILE, so we can use fread() etc. directly.
-	 * @todo get rid of this ASAP
-	 */
-	operator FILE *() { return _file; }
-
 	// FIXME: Remove this method ASAP
 	FILE *getFileHandle() { return _file; }
 
@@ -519,11 +518,6 @@
 	uint8 _xormode;
 };
 
-// This generates a warning when used, so we don't use fclose accidently on 
-// a File object (this is a VERY EASY error to do when converting, and a warning
-// really helps find those cases)
-void fclose(File& f);
-
 void displayHelp(const char *msg = NULL, const char *exename = NULL);
 void parseHelpArguments(const char * const argv[], int argc, const char *msg = NULL);
 bool parseOutputFileArguments(Filename *outputname, const char * const argv[], int argc, int start_arg);


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list