diff music2/nwatowav.cc @ 52:15a18fbe6f21

* Known bugs added to the README * Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author thib
date Sat, 18 Apr 2009 18:35:39 +0000
parents 01aa5ddf7dc8
children 4416cfac86ae
line wrap: on
line diff
--- a/music2/nwatowav.cc
+++ b/music2/nwatowav.cc
@@ -163,11 +163,11 @@
 
 // #define NDEBUG /* なぜか assertが入った方が速い、、、 */
 
-#include<stdio.h>
-#include<stdlib.h>
-#include<unistd.h>	// for isatty() function
-#include<sys/stat.h>
-#include<string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>	// for isatty() function
+#include <sys/stat.h>
+#include <string.h>
 
 
 #ifdef WORDS_BIGENDIAN
@@ -229,25 +229,26 @@ const char* make_wavheader(int size, int
 
 /* NWA の bitstream展開に必要となる情報 */
 class NWAInfo {
-	int channels;
-	int bps;
-	int complevel;
-	bool use_runlength;
-public:
-	NWAInfo(int c,int b,int cl) {
-		channels=c;
-		bps=b;
-		complevel=cl;
-		use_runlength = false;
-		if (cl == 5) {
-			use_runlength = true; // Tomoyo After (.nwk koe file)
-			if (channels == 2) use_runlength = false; // BGM*.nwa in Little Busters!
+	private:
+		int channels;
+		int bps;
+		int complevel;
+		bool use_runlength;
+	public:
+		NWAInfo(int c,int b,int cl) {
+			channels=c;
+			bps=b;
+			complevel=cl;
+			use_runlength = false;
+			if (cl == 5) {
+				use_runlength = true; // Tomoyo After (.nwk koe file)
+				if (channels == 2) use_runlength = false; // BGM*.nwa in Little Busters!
+			}
 		}
-	}
-	int Channels(void) const{return channels;}
-	int Bps(void) const { return bps;}
-	int CompLevel(void) const { return complevel;}
-	int UseRunLength(void) const { return use_runlength; }
+		int Channels(void) const{return channels;}
+		int Bps(void) const { return bps;}
+		int CompLevel(void) const { return complevel;}
+		int UseRunLength(void) const { return use_runlength; }
 };
 
 template<class NWAI> void NWADecode(const NWAI& info,const char* data, char* outdata, int datasize, int outdatasize) {
@@ -334,54 +335,54 @@ template<class NWAI> void NWADecode(cons
 		if (info.Channels() == 2) flip_flag ^= 1; /* channel 切り替え */
 	}
 	return;
-};
+}
 
 class NWAData {
-public:
-	int channels;
-	int bps; /* bits per sample */
-	int freq; /* samples per second */
-private:
-	int complevel; /* compression level */
-	int dummy; /* ? : 0x00 */
-public:
-	int blocks; /* block count */
-	int datasize; /* all data size */
-private:
-	int compdatasize; /* compressed data size */
-	int samplecount; /* all samples */
-	int blocksize; /* samples per block */
-	int restsize; /* samples of the last block */
-	int dummy2; /* ? : 0x89 */
-	int curblock;
-	int* offsets;
-	int offset_start;
-	int filesize;
-	char* tmpdata;
-public:
-	void ReadHeader(FILE* in, int file_size=-1);
-	int CheckHeader(void); /* false: invalid true: valid */
-	NWAData(void) {
-		offsets = 0;
-		tmpdata = 0;
-	}
-	~NWAData(void) {
-		if (offsets) delete[] offsets;
-		if (tmpdata) delete[] tmpdata;
-	}
-	int BlockLength(void) {
-		if (complevel != -1) {
-			if (offsets == 0) return false;
-			if (tmpdata == 0) return false;
+	public:
+		int channels;
+		int bps; /* bits per sample */
+		int freq; /* samples per second */
+	private:
+		int complevel; /* compression level */
+		int dummy; /* ? : 0x00 */
+	public:
+		int blocks; /* block count */
+		int datasize; /* all data size */
+	private:
+		int compdatasize; /* compressed data size */
+		int samplecount; /* all samples */
+		int blocksize; /* samples per block */
+		int restsize; /* samples of the last block */
+		int dummy2; /* ? : 0x89 */
+		int curblock;
+		int* offsets;
+		int offset_start;
+		int filesize;
+		char* tmpdata;
+	public:
+		void ReadHeader(FILE* in, int file_size=-1);
+		int CheckHeader(void); /* false: invalid true: valid */
+		NWAData(void) {
+			offsets = NULL;
+			tmpdata = NULL;
 		}
-		return blocksize * (bps/8);
-	}
-	/* data は BlockLength 以上の長さを持つこと
-	** 返り値は作成したデータの長さ。終了時は 0。
-	** エラー時は -1
-	*/
-	int Decode(FILE* in, char* data, int& skip_count);
-	void Rewind(FILE* in);
+		~NWAData(void) {
+			if (offsets) delete[] offsets;
+			if (tmpdata) delete[] tmpdata;
+		}
+		int BlockLength(void) {
+			if (complevel != -1) {
+				if (offsets == NULL) return false;
+				if (tmpdata == NULL) return false;
+			}
+			return blocksize * (bps/8);
+		}
+		/* data は BlockLength 以上の長さを持つこと
+		** 返り値は作成したデータの長さ。終了時は 0。
+		** エラー時は -1
+		*/
+		int Decode(FILE* in, char* data, int& skip_count);
+		void Rewind(FILE* in);
 };
 
 void NWAData::ReadHeader(FILE* in, int _file_size) {
@@ -390,15 +391,15 @@ void NWAData::ReadHeader(FILE* in, int _
 	int i;
 	if (offsets) delete[] offsets;
 	if (tmpdata) delete[] tmpdata;
-	offsets = 0;
-	tmpdata = 0;
+	offsets = NULL;
+	tmpdata = NULL;
 	filesize = 0;
 	offset_start = ftell(in);
 	if (offset_start == -1) offset_start = 0;
 	if (_file_size != -1) filesize = _file_size;
 	curblock = -1;
 	/* header 読み込み */
-	if (in == 0 || feof(in) || ferror(in)) {
+	if (in == NULL || feof(in) || ferror(in)) {
 		fprintf(stderr,"invalid stream\n");
 		return;
 	}
@@ -433,9 +434,9 @@ void NWAData::ReadHeader(FILE* in, int _
 	/* regular file なら filesize 読み込み */
 	if (filesize == 0 && fstat(fileno(in), &sb)==0 && (sb.st_mode&S_IFMT) == S_IFREG) {
 		int pos = ftell(in);
-		fseek(in, 0, 2);
+		fseek(in, 0, SEEK_END);
 		filesize = ftell(in);
-		fseek(in, pos, 0);
+		fseek(in, pos, SEEK_SET);
 		if (pos+blocks*4 >= filesize) {
 			fprintf(stderr,"offset block is not exist\n");
 			return;
@@ -451,18 +452,19 @@ void NWAData::ReadHeader(FILE* in, int _
 	if (feof(in) || ferror(in)) {
 		fprintf(stderr,"invalid stream\n");
 		delete[] offsets;
-		offsets = 0;
+		offsets = NULL;
 		return;
 	}
-	return;
 }
+
 void NWAData::Rewind(FILE* in) {
 	curblock = -1;
-	fseek(in, 0x2c, 0);
-	if (offsets) fseek(in, blocks*4, 1);
+	fseek(in, 0x2c, SEEK_SET);
+	if (offsets) fseek(in, blocks*4, SEEK_CUR);
 }
+
 int NWAData::CheckHeader(void) {
-	if (complevel != -1 && offsets == 0) return false;
+	if (complevel != -1 && offsets == NULL) return false;
 	/* データそのもののチェック */
 	if (channels != 1 && channels != 2) {
 		fprintf(stderr,"This program only supports mono / stereo data : data have %d channels.\n",channels);
@@ -513,12 +515,13 @@ int NWAData::CheckHeader(void) {
 }
 
 class NWAInfo_sw2 {
-public:
-	int Channels(void) const{return 2;}
-	int Bps(void) const { return 16;}
-	int CompLevel(void) const { return 2;}
-	int UseRunLength(void) const { return false; }
+	public:
+		int Channels(void) const{return 2;}
+		int Bps(void) const { return 16;}
+		int CompLevel(void) const { return 2;}
+		int UseRunLength(void) const { return false; }
 };
+
 int NWAData::Decode(FILE* in, char* data, int& skip_count) {
 	if (complevel == -1) {		/* 無圧縮時の処理 */
 		if (feof(in) || ferror(in)) return -1;
@@ -548,7 +551,7 @@ int NWAData::Decode(FILE* in, char* data
 		}
 		return -1;
 	}
-	if (offsets == 0 || tmpdata == 0) return -1;
+	if (offsets == NULL || tmpdata == NULL) return -1;
 	if (blocks == curblock) return 0;
 	if (feof(in) || ferror(in)) return -1;
 	if (curblock == -1) {
@@ -610,6 +613,7 @@ void conv(FILE* in, FILE* out, int skip_
 	}
 	return;
 }
+
 int main(int argc, char** argv) {
 	int skip_count = 0;
 
@@ -623,20 +627,20 @@ int main(int argc, char** argv) {
 		fprintf(stderr,"usage : nwatowav [inputfile [outputfile]]\n");
 		return -1;
 	}
-	if (strstr(argv[1], ".nwk") != 0 || strstr(argv[1], ".ovk") != 0) {
+	if (strstr(argv[1], ".nwk") != NULL || strstr(argv[1], ".ovk") != NULL) {
 		bool is_ovk;
 		int headblk_sz;
-		char* out_ext;
+		const char* out_ext;
 
 		char* outpath = new char[strlen(argv[1])+10];
 		char buf[1024];
 		memset(buf, 0, 1024);
 		FILE* in = fopen(argv[1], "rb");
-		if (in == 0) {
+		if (in == NULL) {
 			fprintf(stderr,"Cannot open file : %s\n",argv[1]);
 			return -1;
 		}
-		if (strstr(argv[1], ".ovk") != 0) {
+		if (strstr(argv[1], ".ovk") != NULL) {
 			is_ovk = true;
 			headblk_sz = 16; 
 			out_ext = "ogg";
@@ -666,7 +670,7 @@ int main(int argc, char** argv) {
 			tbl_cnt[i] = read_little_endian_int(buf+8);
 			tbl_origsiz[i] = read_little_endian_int(buf+12);
 		}
-		fseek(in, 0, 2);
+		fseek(in, 0, SEEK_END);
 		int fsize = ftell(in);
 		for (i=0; i<index; i++) {
 			if (tbl_off[i] <= 0 || tbl_siz[i] <= 0 || tbl_off[i]+tbl_siz[i] > fsize) {
@@ -678,12 +682,12 @@ int main(int argc, char** argv) {
 			else
 				sprintf(outpath, "%s-%d.%s", argv[2], tbl_cnt[i],out_ext);
 			FILE* out = fopen(outpath, "wb");
-			if (out == 0) {
+			if (out == NULL) {
 				fprintf(stderr,"Cannot open output file %s\n",outpath);
 				continue;
 			}
 			fprintf(stderr,"Writing file %s...\n",outpath);
-			fseek(in, tbl_off[i], 0);
+			fseek(in, tbl_off[i], SEEK_SET);
 			if (is_ovk) { // copy file
 				int sz = tbl_siz[i];
 				char buf[32*1024];
@@ -705,7 +709,7 @@ int main(int argc, char** argv) {
 		return 0;
 	}
 	FILE* in = fopen(argv[1],"rb");
-	if (in == 0) {
+	if (in == NULL) {
 		fprintf(stderr,"Cannot open file : %s\n",argv[1]);
 		return -1;
 	}
@@ -717,7 +721,7 @@ int main(int argc, char** argv) {
 		sprintf(outpath, "%s.wav",argv[1]);
 		if (argc == 3) outpath = argv[2];
 		out = fopen(outpath, "wb");
-		if (out == 0) {
+		if (out == NULL) {
 			fprintf(stderr,"Cannot open file : %s\n",outpath);
 			return -1;
 		}
@@ -732,7 +736,7 @@ int main(int argc, char** argv) {
 #include"wavfile.h"
 
 void NWAFILE::Seek(int count) {
-	if (data == 0) data = new char[block_size];
+	if (data == NULL) data = new char[block_size];
 	nwa->Rewind(stream);
 	int dmy = 0;
 	nwa->Decode(stream, data, dmy); // skip wav header
@@ -741,7 +745,7 @@ void NWAFILE::Seek(int count) {
 }
 NWAFILE::NWAFILE(FILE* _stream) {
 	skip_count = 0;
-	data = 0;
+	data = NULL;
 	stream = _stream;
 	nwa = new NWAData;
 	nwa->ReadHeader(stream);
@@ -766,8 +770,9 @@ NWAFILE::~NWAFILE() {
 	if (data) delete[] data;
 	if (nwa) delete nwa;
 }
+
 int NWAFILE::Read(char* buf, int blksize, int blklen) {
-	if (data == 0) return -1; // end of file
+	if (data == NULL) return -1; // end of file
 
 	if (data_len > blksize * blklen) {
 		int len = blksize * blklen;
@@ -780,12 +785,13 @@ int NWAFILE::Read(char* buf, int blksize
 	int copied_length = data_len;
 	data_len = 0;
 
-	if (stream == 0) {
+	if (stream == NULL) {
 		delete[] data;
-		data = 0;
+		data = NULL;
 		return copied_length / blksize;
 	}
 
+	//TODO: Rewrite this joke
 	// read
 	do {
 		int err;
@@ -793,7 +799,7 @@ retry:
 		err = nwa->Decode(stream, data, skip_count);
 		if (err == 0 || err == -1) { // eof or error
 			delete[] data;
-			data = 0;
+			data = NULL;
 			return copied_length / blksize;
 		}
 		if (err == -2) goto retry; // EAGAIN
@@ -820,7 +826,7 @@ retry:
 
 char* NWAFILE::ReadAll(FILE* in, int& total_size) {
 	NWAData h;
-	if (in == 0) return 0;
+	if (in == NULL) return NULL;
 	h.ReadHeader(in);
 	h.CheckHeader();
 	int bs = h.BlockLength();
@@ -837,14 +843,14 @@ char* NWAFILE::ReadAll(FILE* in, int& to
 	return d;
 }
 
-#include"music.h"
+#include "music.h"
 
 char* decode_koe_nwa(AvgKoeInfo info, int* data_len) {
 	NWAData h;
-	if (info.stream == 0) return 0;
-	fseek(info.stream, info.offset, 0);
+	if (info.stream == NULL) return NULL;
+	fseek(info.stream, info.offset, SEEK_SET);
 	h.ReadHeader(info.stream, info.length);
-	if (h.CheckHeader() == false) return 0;
+	if (h.CheckHeader() == false) return NULL;
 	int bs = h.BlockLength();
 	int total = h.datasize + 0x2c;
 	char* d = new char[total + bs*2];