Hardcoded application name
[aaf.git] / main.c
1 #include <stdio.h>
2 #include <errno.h>
3
4 #include "aaf.h"
5
6
7 int main(int argc, char ** argv)
8 {
9         FILE * in = NULL;
10
11         if (argc <= 1) {
12                 puts("Fallout 1 AAF converter");
13                 puts("Converts INPUT.AAF file into set of PGM images");
14                 printf("\n\tUsage: aaf INPUT\n\n");
15         }
16         else {
17                 struct aaf _aaf;
18                 struct aaf_glyph_data ** glyph_data = NULL;
19                 int i = 0;
20
21                 in = fopen(argv[1], "r");
22                 if (in == NULL) {
23                         perror("Unable to open file for conversion");
24                         return errno;
25                 }
26
27                 if (read_aaf(in, &_aaf) != 0) {
28                         fprintf(stderr, "Problem occured when trying to read AAF file\n");
29                         fclose(in);
30                         return 1;
31                 }
32                 
33                 glyph_data = read_aaf_glyph_data(in, &_aaf);
34                 if (glyph_data == NULL) {
35                         fprintf(stderr, "Problem occured when trying to read AAF glyph data\n");
36                         fclose(in);
37                         return 2;
38                 }
39                 
40                 for (i = 0; i < AAF_GLYPH_MAX; i++) {
41                         const struct aaf_glyph * glyph = &(_aaf.glyphs[i]);
42                         const struct aaf_glyph_data * data = glyph_data[i];
43
44                         FILE * out = NULL;
45                         char filename[13] = {
46                                 'F', 'O', 'N', 'T', '-', '0', '0', '0', '.', 'P', 'G', 'M'
47                         };
48                         
49                         if ((glyph->w * glyph->h) == 0) {
50                                 continue;
51                         }
52                         
53                         if (i / 100) {
54                                 filename[5] = i / 100 + '0';
55                         }
56                         if ((i % 100) / 10) {
57                                 filename[6] = (i % 100) / 10 + '0';
58                         }
59                         if (i % 10) {
60                                 filename[7] = i % 10 + '0';
61                         }
62                         
63                         out = fopen(filename, "w");
64                         if (out == NULL) {
65                                 perror("Unable to open file for writing glyph");
66                                 free_aaf_glyph_data(glyph_data);
67                                 fclose(in);
68                                 return 3;
69                         }
70
71                         if (write_aaf_glyph_as_pgm(out, glyph, data)) {
72                                 fprintf(stderr, "Error occured when trying to write glyph into PGM\n");
73                                 fclose(out);
74                                 free_aaf_glyph_data(glyph_data);
75                                 fclose(in);
76                                 return 4;
77                         }
78                         fclose(out);
79                 }
80
81                 free_aaf_glyph_data(glyph_data);
82                 fclose(in);
83         }
84
85         return 0;
86 }