comparison system/visarc.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 3a6aaeab7b4e
children 4416cfac86ae
comparison
equal deleted inserted replaced
51:cbb301016a4e 52:15a18fbe6f21
62 } 62 }
63 63
64 void List(char* path) { 64 void List(char* path) {
65 ARCFILE* file; 65 ARCFILE* file;
66 FILE* f = fopen(path, "rb"); 66 FILE* f = fopen(path, "rb");
67 if (f == 0) return; 67 if (f == NULL) return;
68 char header[32]; 68 char header[32];
69 fread(header, 32, 1, f); 69 fread(header, 32, 1, f);
70 fclose(f); 70 fclose(f);
71 char magic_raf[8] = {'C','A','P','F',1,0,0,0}; 71 char magic_raf[8] = {'C','A','P','F',1,0,0,0};
72 if (strncmp(header, "PACL", 4) == 0) file = new ARCFILE(path); 72 if (strncmp(header, "PACL", 4) == 0) file = new ARCFILE(path);
73 else file = new SCN2kFILE(path); 73 else file = new SCN2kFILE(path);
74 file->Init(); 74 file->Init();
75 file->ListFiles(stdout); 75 file->ListFiles(stdout);
76 delete file; 76 delete file;
77 return;
78 } 77 }
79 78
80 void ExtractOne(ARCFILE* arc, char* file) { 79 void ExtractOne(ARCFILE* arc, char* file) {
81 ARCINFO* info = arc->Find(file,""); 80 ARCINFO* info = arc->Find(file,"");
82 if (info == 0) { 81 if (info == NULL) {
83 fprintf(stderr, "Cannot find file %s in archive\n",file); 82 fprintf(stderr, "Cannot find file %s in archive\n",file);
84 return; 83 return;
85 } 84 }
86 FILE* out = fopen(file, "w"); 85 FILE* out = fopen(file, "w");
87 if (out == 0) { 86 if (out == NULL) {
88 delete info; 87 delete info;
89 fprintf(stderr, "Cannot open output file %s\n",file); 88 fprintf(stderr, "Cannot open output file %s\n",file);
90 return; 89 return;
91 } 90 }
92 91
95 const char* data = info->Read(); 94 const char* data = info->Read();
96 fwrite(data, size, 1, out); 95 fwrite(data, size, 1, out);
97 fclose(out); 96 fclose(out);
98 fprintf(stdout, "done\n"); 97 fprintf(stdout, "done\n");
99 delete info; 98 delete info;
100 return;
101 } 99 }
102 100
103 void Extract(char* path, char** files, int fnum) { 101 void Extract(char* path, char** files, int fnum) {
104 ARCFILE* file; 102 ARCFILE* file;
105 FILE* f = fopen(path, "rb"); 103 FILE* f = fopen(path, "rb");
106 if (f == 0) return; 104 if (f == NULL) return;
107 char header[32]; 105 char header[32];
108 fread(header, 32, 1, f); 106 fread(header, 32, 1, f);
109 fclose(f); 107 fclose(f);
110 char magic_raf[8] = {'C','A','P','F',1,0,0,0}; 108 char magic_raf[8] = {'C','A','P','F',1,0,0,0};
111 if (strncmp(header, "PACL", 4) == 0) file = new ARCFILE(path); 109 if (strncmp(header, "PACL", 4) == 0) file = new ARCFILE(path);
112 else file = new SCN2kFILE(path); 110 else file = new SCN2kFILE(path);
113 file->Init(); 111 file->Init();
114 if (files != 0 && fnum != 0) { 112 if (files != NULL && fnum != 0) {
115 int i; for (i=0; i<fnum; i++) { 113 int i; for (i=0; i<fnum; i++) {
116 ExtractOne(file, files[i]); 114 ExtractOne(file, files[i]);
117 } 115 }
118 } else { 116 } else {
119 file->InitList(); 117 file->InitList();
120 char* path; while( (path=file->ListItem()) != 0) { 118 char* path;
119 while( (path=file->ListItem()) != 0) {
121 ExtractOne(file, path); 120 ExtractOne(file, path);
122 } 121 }
123 } 122 }
124 delete file; 123 delete file;
125 return;
126 } 124 }
127 125
128 void ChangeExt(char* path, char* new_ext, char* buf) { 126 void ChangeExt(char* path, char* new_ext, char* buf) {
129 char* name = strrchr(path, DIR_SPLIT); 127 char* name = strrchr(path, DIR_SPLIT);
130 if (name == 0) name = path; 128 if (name == NULL) name = path;
131 else name++; 129 else name++;
132 int path_len = name - path; 130 int path_len = name - path;
133 131
134 char* ext = strrchr(name, '.'); 132 char* ext = strrchr(name, '.');
135 int ext_len; 133 int ext_len;
140 strcpy(buf+path_len+ext_len, new_ext); 138 strcpy(buf+path_len+ext_len, new_ext);
141 } 139 }
142 140
143 char* ReadFile(char* fname, int* len) { 141 char* ReadFile(char* fname, int* len) {
144 FILE* in = fopen(fname, "rb"); 142 FILE* in = fopen(fname, "rb");
145 if (in == 0) return 0; 143 if (in == NULL) return 0;
146 fseek(in,0,2); size_t s = ftell(in); fseek(in,0,0); 144 fseek(in, 0, SEEK_END);
145 size_t s = ftell(in);
146 fseek(in, 0, SEEK_SET);
147 char* buf = new char[s]; 147 char* buf = new char[s];
148 fread(buf,s,1,in); 148 fread(buf,s,1,in);
149 fclose(in); 149 fclose(in);
150 if (len) *len = s; 150 if (len) *len = s;
151 return buf; 151 return buf;
213 } 213 }
214 png_write_rows(png_ptr, (png_byte**)&row, 1); 214 png_write_rows(png_ptr, (png_byte**)&row, 1);
215 } 215 }
216 png_write_end(png_ptr, info_ptr); 216 png_write_end(png_ptr, info_ptr);
217 png_destroy_write_struct(&png_ptr, &info_ptr); 217 png_destroy_write_struct(&png_ptr, &info_ptr);
218 return;
219 } 218 }
220 219
221 void ExtractPngRgbaGraphic(char* path,char* outpath = 0) { 220 void ExtractPngRgbaGraphic(char* path,char* outpath = 0) {
222 char buf[1024]; char* fname = buf; 221 char buf[1024]; char* fname = buf;
223 int len; 222 int len;
224 char* dat = ReadFile(path, &len); 223 char* dat = ReadFile(path, &len);
225 if (dat == 0) { 224 if (dat == NULL) {
226 fprintf(stderr, "Cannot open PDT file : %s\n",path); 225 fprintf(stderr, "Cannot open PDT file : %s\n",path);
227 return; 226 return;
228 } 227 }
229 GRPCONV* conv = GRPCONV::AssignConverter(dat, len, path); 228 GRPCONV* conv = GRPCONV::AssignConverter(dat, len, path);
230 if (conv == 0) { 229 if (conv == NULL) {
231 fprintf(stderr, "Invalid format\n"); 230 fprintf(stderr, "Invalid format\n");
232 return; 231 return;
233 } 232 }
234 bool masked = conv->IsMask(); 233 bool masked = conv->IsMask();
235 char* data = new char[conv->Width() * conv->Height() * 4 + 1024]; 234 char* data = new char[conv->Width() * conv->Height() * 4 + 1024];
241 if (! masked) { 240 if (! masked) {
242 for (int i = 0; i < conv->Width() * conv->Height(); i++) { 241 for (int i = 0; i < conv->Width() * conv->Height(); i++) {
243 data[4*i+3] = 0xff; // 不透明度を最大にする 242 data[4*i+3] = 0xff; // 不透明度を最大にする
244 } 243 }
245 } 244 }
246 if (outpath == 0) ChangeExt(path,".png", buf); // path をつくる 245 if (outpath == NULL) ChangeExt(path,".png", buf); // path をつくる
247 else fname = outpath; 246 else fname = outpath;
248 FILE* out = fopen(fname, "wb"); // ファイルを開く 247 FILE* out = fopen(fname, "wb"); // ファイルを開く
249 if (out == 0) { 248 if (out == NULL) {
250 fprintf(stderr, "Cannot open raw file : %s\n",buf); 249 fprintf(stderr, "Cannot open raw file : %s\n",buf);
251 delete conv; 250 delete conv;
252 return; 251 return;
253 } 252 }
254 create_png(out, path, "", conv->Width(), conv->Height(), data); 253 create_png(out, path, "", conv->Width(), conv->Height(), data);