apc initial implementation
[henge/webcc.git] / src / apc / fileparser.y
1 /* Asset Package Compiler */
2 %{
3 #include <stdio.h>
4 #include <string.h>
5 #include <dirent.h>
6 #include "sprite.h"
7 #include "symbol.h"
8 #include <png.h>
9
10
11 extern void yyerror();
12 extern int linit();
13 extern int yylex();
14
15
16 int handle_fname(char *);
17 int eval_png_file(char*);
18 int read_png_file(char*);
19
20 %}
21 %define parse.error verbose
22 %union{
23 char* str;
24 }
25
26 /* %token <str> EXT */
27 /* %token <str> FW */
28 /* %token <str> SPR */
29 /* %token CC */
30
31 /* /\* Rules *\/ */
32 /* %% */
33 /* SS: SPR '/' MD '.' EXT */
34 /* MD: CC '_' FW */
35 /* | FW */
36 /* | CC */
37
38
39 %token <str> FDAT
40
41 /* Rules */
42 %%
43 FLIST : FLIST FNAME {printf("reducing to flist\n");} /* Now output to asspack.o */ | FNAME
44 FNAME : FDAT {printf("handle_fname(%s)\n", $1);handle_fname(yylval.str);printf("handled fname\n");}
45
46 %%
47 int
48 main(int argc, char** argv)
49 {
50 /* Parse cmd line arguments */
51
52 linit();
53 yyparse(); /* runs handle_fname for each filename */
54
55
56 return 0;
57 }
58 void
59 yyerror (char const *s)
60 {
61 fprintf(stderr, "%s\n", s);
62 }
63
64
65
66 /* Analyze file_name for information on sprite sheet,
67 store sprite sheet and label into sprite then
68 push sprite onto sprites[]*/
69 int
70 handle_fname(char* file_name)
71 {
72 /* Parse the file_name for data */
73
74
75 /* Get the specs of the PNG file */
76 if(eval_png_file(file_name))
77 {
78 printf("Failed in eval_png_file\n");
79 return 1;
80 }
81
82 /* Insert sprite sheet from PNG into sprite_sheets */
83 push_sprite();
84
85
86
87
88 /* Store dir/filename in label as well as any other data that fits */
89 }
90 int eval_png_file(char* file_name)
91 {
92 /* Evaluate file_name for metadata */
93
94 /* Extracts header info from png_file */
95 read_png_file(file_name);
96
97 /* Create the symbol and put it in symbol_table[]*/
98
99 return 0;
100 }
101
102
103
104 /* variables for libPNG */
105 int png_width,png_height;
106 png_byte bit_depth;
107 png_byte color_type;
108 png_structp png_ptr;
109 png_infop info_ptr;
110
111 int
112 read_png_file( char* file_name)
113 {
114 /* Setup PNG file */
115 char header[8];
116 int is_png = 0;
117 char* dir = "pngfiles";
118
119 /* TODO: Change this fopen to a real var! */
120 FILE *fp = fopen("pngfiles/env_street_road03_0.png","rb");
121
122 if(!fp)
123 { printf("Could not read file %s in handle_fname()", file_name);
124 return 1;
125 }
126
127 fread(header, 1, 8, fp);
128
129 is_png = !png_sig_cmp(header, 0, 8);
130 if(!is_png)
131 return 1;
132
133 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
134
135 if (!png_ptr)
136 { printf("Failed to allocate png_ptr\n");
137 return 1;
138 }
139 info_ptr = png_create_info_struct(png_ptr);
140
141 if (!info_ptr)
142 { printf("Failed to create info_ptr\n");
143 png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
144 return 1;
145 }
146
147 png_infop end_info = png_create_info_struct(png_ptr);
148 if (!end_info)
149 { printf("Failed to create end_info \n");
150 png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
151 return 1;
152 }
153
154 if(setjmp(png_jmpbuf(png_ptr)))
155 { printf("Failed in init_io\n");
156 png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
157 fclose(fp);
158 return 1;
159 }
160 png_init_io(png_ptr, fp);
161 png_set_sig_bytes(png_ptr, 8);
162
163 png_read_info(png_ptr, info_ptr);
164
165 png_width = png_get_image_width(png_ptr, info_ptr);
166 png_height = png_get_image_height(png_ptr, info_ptr);
167 color_type = png_get_color_type(png_ptr, info_ptr);
168 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
169
170 png_read_update_info(png_ptr, info_ptr);
171
172 printf("successfully read png file %s\n", file_name);
173
174 return 0;
175
176 }