diff music2/koedec.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 35ce1a30f3f9
children ddbcbd000206
line wrap: on
line diff
--- a/music2/koedec.cc
+++ b/music2/koedec.cc
@@ -25,15 +25,15 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include<stdio.h>
-#include<stdlib.h>
-#include<string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <vector>
 #include <list>
 #include <algorithm>
-#include"music.h"
-#include"system/file.h"
-#include"system/file_impl.h"
+#include "music.h"
+#include "system/file.h"
+#include "system/file_impl.h"
 
 using namespace std;
 
@@ -64,6 +64,7 @@ struct AvgKoeTable {
 		return koe_num == to;
 	}
 };
+
 struct AvgKoeHead {
 	FILE* stream;
 	int file_number;
@@ -77,19 +78,23 @@ struct AvgKoeHead {
 	bool operator !=(int num) const { return file_number != num; }
 	bool operator ==(int num) const { return file_number == num; }
 };
+
 struct AvgKoeCache {
 	list<AvgKoeHead> cache;
 	AvgKoeInfo Find(int file_number, int index);
 };
+
 static AvgKoeCache koe_cache;
 
 AvgKoeInfo FindKoe(int file_number, int index) {
 	return koe_cache.Find(file_number, index);
-};
+}
 
 AvgKoeInfo AvgKoeCache::Find(int file_number, int index) {
 	AvgKoeInfo info;
-	info.stream = 0; info.length = 0; info.offset = 0;
+	info.stream = NULL;
+	info.length = 0;
+	info.offset = 0;
 
 	list<AvgKoeHead>::iterator it;
 	it = find(cache.begin(), cache.end(), file_number);
@@ -99,18 +104,18 @@ AvgKoeInfo AvgKoeCache::Find(int file_nu
 		KoeType type = koe_unknown;
 		sprintf(fname, "z%03d.koe", file_number);
 		ARCINFO* arcinfo = file_searcher.Find(FILESEARCH::KOE,fname,".koe");
-		if (arcinfo == 0) {
+		if (arcinfo == NULL) {
 			type = koe_nwk;
 			sprintf(fname, "z%04d.nwk", file_number);
 			arcinfo = file_searcher.Find(FILESEARCH::KOE,fname,".nwk");
 		}
-		if (arcinfo == 0) {
+		if (arcinfo == NULL) {
 			type = koe_ovk;
 			sprintf(fname, "z%04d.ovk", file_number);
 			arcinfo = file_searcher.Find(FILESEARCH::KOE,fname,".ovk");
 		}
 #if HAVE_LIBVORBISFILE || HAVE_LIBVORBISIDEC
-		if (arcinfo == 0) {
+		if (arcinfo == NULL) {
 			//FIXME: OMG that's ugly, improve it as soon as you can!
 			DIRFILE* koedir = (DIRFILE*) file_searcher.MakeARCFILE((FILESEARCH::ARCTYPE)0, "koe");
 			sprintf(fname, "%04d", file_number);
@@ -122,7 +127,7 @@ AvgKoeInfo AvgKoeCache::Find(int file_nu
 			arcinfo = koedir->Find(fname, ".ogg");
 			delete koedir;
 
-			if (arcinfo == 0) return info;
+			if (arcinfo == NULL) return info;
 			FILE* stream = arcinfo->OpenFile(&info.length);
 			delete arcinfo;
 			info.type = koe_ogg;
@@ -130,16 +135,17 @@ AvgKoeInfo AvgKoeCache::Find(int file_nu
 			return info;
 		}
 #endif
-		if (arcinfo == 0) return info;
+		if (arcinfo == NULL) return info;
 		FILE* stream = arcinfo->OpenFile();
 		delete arcinfo;
-		if (stream == 0) return info;
+		if (stream == NULL) return info;
 		cache.push_front(AvgKoeHead(stream, file_number, type));
 		if (cache.size() >= koe_cache_size) cache.pop_back();
 		it = cache.begin();
 	}
 	if (it->file_number != file_number) return info; // 番号がおかしい
 	AvgKoeTable* table = it->Find(index);
+	//FIXME: table == NULL ?
 	if (table == 0) return info; // index が見付からない
 	// info を作成する
 	info.length = table->length;
@@ -147,7 +153,7 @@ AvgKoeInfo AvgKoeCache::Find(int file_nu
 	info.rate = it->rate;
 	info.type = it->type;
 	int new_fd = dup(fileno(it->stream));
-	if (new_fd == -1) info.stream = 0;
+	if (new_fd == -1) info.stream = NULL;
 	else info.stream = fdopen(new_fd, "rb");
 	return info;
 }
@@ -155,7 +161,7 @@ AvgKoeInfo AvgKoeCache::Find(int file_nu
 AvgKoeHead::AvgKoeHead(const AvgKoeHead& from) {
 	if (from.stream) {
 		int new_fd = dup(fileno(from.stream));
-		if (new_fd == -1) stream = 0;
+		if (new_fd == -1) stream = NULL;
 		else stream = fdopen(new_fd, "rb");
 	}
 	file_number = from.file_number;
@@ -170,7 +176,7 @@ AvgKoeHead::AvgKoeHead(FILE* _s, int _fi
 	//int offset = ftell(stream);
 	rate = 22050;
 	type = _type;
-	if (stream == 0) return;
+	if (stream == NULL) return;
 	/* header 読み込み */
 	if (type == koe_nwk) { // 新しい形式 : .nwk file
 		rate = 44100;
@@ -201,7 +207,7 @@ AvgKoeHead::AvgKoeHead(FILE* _s, int _fi
 	} else { // .koe file
 		fread(head, 0x20, 1, stream);
 		if (strncmp(head, "KOEPAC", 7) != 0) { // invalid header
-			stream = 0;
+			stream = NULL;
 			return;
 		}
 		int table_len = read_little_endian_int(head+0x10);
@@ -223,13 +229,14 @@ AvgKoeHead::AvgKoeHead(FILE* _s, int _fi
 
 AvgKoeHead::~AvgKoeHead(void) {
 	if (stream) fclose(stream);
-	stream = 0;
+	stream = NULL;
 }
+
 AvgKoeTable* AvgKoeHead::Find(int koe_num) {
-	if (table.empty()) return 0;
+	if (table.empty()) return NULL;
 	vector<AvgKoeTable>::iterator it;
 	it = lower_bound(table.begin(), table.end(), koe_num);
-	if (it == table.end() || it->koe_num != koe_num) return 0;
+	if (it == table.end() || it->koe_num != koe_num) return NULL;
 	return &table[it-table.begin()];
 }
 
@@ -342,29 +349,30 @@ extern int is_koe_ogg(char* head);
 extern char* decode_koe_ogg(AvgKoeInfo info, int* dest_len);
 
 char* decode_koe(AvgKoeInfo info, int* dest_len) {
-	char buf[1024]; char* table;
+	char buf[1024];
+	char* table;
 	unsigned char* src_orig, *src;
 	unsigned short* dest_orig, *dest;
-	int all_len; int i,j;
+	int all_len, i, j;
 	if (info.stream == NULL) {
 		return NULL;
 	}
-	fseek(info.stream, info.offset, 0);
+	fseek(info.stream, info.offset, SEEK_SET);
 	if (info.type == koe_nwk) {
 		return decode_koe_nwa(info, dest_len);
 	}
 	fread(buf, 1, 0x20, info.stream);
 	if (is_koe_ogg(buf)) {
-		fseek(info.stream, -20, 1);
+		fseek(info.stream, -20, SEEK_CUR);
 		return decode_koe_ogg(info, dest_len);
 	}
 	/* avg32 の声データ展開 */
 	table = (char*)malloc(info.length*2);
-	fseek(info.stream, info.offset, 0);
+	fseek(info.stream, info.offset, SEEK_SET);
 	fread(table, 2, info.length, info.stream);
 
 	all_len = 0;
-	for (i=0; i<info.length; i++)
+	for (i=0; i < info.length; i++)
 		all_len += read_little_endian_short(table + i*2);
 	/* データ読み込み */
 	src_orig  = (unsigned char*) malloc(all_len);