improve agssemble performance 3x
[rofl0r-agsutils.git] / agspack.c
blob4dc4fb1009d0b672068c81239d2045b53b556082
1 #define _GNU_SOURCE
2 #include "Clib32.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <ctype.h>
7 #include "version.h"
8 #define ADS ":::AGSpack " VERSION "by rofl0r:::"
10 __attribute__((noreturn))
11 void usage(char *argv0) {
12 dprintf(2, ADS "\nusage:\n%s directory target-pack\n\n", argv0);
13 exit(1);
16 int main(int argc, char** argv) {
17 if(argc < 3) usage(argv[0]);
18 char *dir = argv[1];
19 char *pack = argv[2];
20 char fnbuf[512];
21 char line[1024];
22 FILE* fp;
23 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dir, "agspack.info");
24 if(!(fp = fopen(fnbuf, "r"))) {
25 dprintf(2, "couldnt open %s\n", fnbuf);
26 return 1;
28 size_t index = 0;
29 struct AgsFile ags_b, *ags = &ags_b;
30 AgsFile_init(ags, pack);
31 AgsFile_setSourceDir(ags, dir);
32 AgsFile_setDataFileCount(ags, 1); //TODO
33 if(!AgsFile_setDataFile(ags, 0, "AGSPACKv" VERSION)) {
34 dprintf(2, "error: packname exceeds 20 chars");
35 return 1;
37 while(fgets(line, sizeof(line), fp)) {
38 size_t l = strlen(line);
39 if(l) line[l - 1] = 0;
40 char *p = strchr(line, '=');
41 if(!p) return 1;
42 *p = 0; p++;
43 if(0) ;
44 else if(strcmp(line, "agsversion") == 0)
45 AgsFile_setVersion(ags, atoi(p));
46 else if(strcmp(line, "filecount") == 0)
47 AgsFile_setFileCount(ags, atoi(p));
48 else if(isdigit(*line))
49 if(!AgsFile_setFile(ags, index++, p)) {
50 perror(p);
51 return 1;
54 fclose(fp);
55 size_t l = AgsFile_getFileCount(ags);
56 for(index = 0; index < l; index++) {
57 // TODO read from input file, but it seems to be all 0 for some games.
58 AgsFile_setFileNumber(ags, index, 0);
60 int ret = AgsFile_write(ags);
61 if(!ret) perror("write");
62 AgsFile_close(ags);
63 return !ret;