3 Extension module for the rfxswf library.
4 Part of the swftools package.
6 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
28 #include "tokenizer.h"
30 static int verbose = 0;
31 static void dbg(const char*format, ...)
38 va_start(arglist, format);
39 vsprintf(buf, format, arglist);
42 while(l && buf[l-1]=='\n') {
46 printf("(includefilehandler) ");
54 char* current_filename=0;
55 char* current_filename_short=0;
56 include_dir_t* current_include_dirs=0;
58 #define MAX_INCLUDE_DEPTH 16
60 void*include_stack[MAX_INCLUDE_DEPTH];
61 int line_stack[MAX_INCLUDE_DEPTH];
62 int column_stack[MAX_INCLUDE_DEPTH];
63 char* filename_stack[MAX_INCLUDE_DEPTH];
64 char* shortfilename_stack[MAX_INCLUDE_DEPTH];
65 include_dir_t* includedir_stack[MAX_INCLUDE_DEPTH];
66 int include_stack_ptr = 0;
68 void add_include_dir(char*dir)
70 include_dir_t*d = malloc(sizeof(include_dir_t));
71 memset(d , 0, sizeof(include_dir_t));
72 d->path = strdup(dir);
73 d->next = current_include_dirs;
74 current_include_dirs = d;
77 void del_include_dirs(include_dir_t*d, include_dir_t*d2)
80 include_dir_t*next = d->next;
81 free(d->path);d->path=0;
88 char*get_path(const char*file)
90 char*path = strdup(file);
91 char*r1 = strrchr(path, '/');
92 char*r2 = strrchr(path, '\\');
103 char* concat_paths(const char*base, const char*add)
105 int l1 = strlen(base);
106 int l2 = strlen(add);
109 while(l1 && base[l1-1] == '/')
111 while(pos < l2 && add[pos] == '/')
113 n = (char*)malloc(l1 + (l2-pos) + 2);
116 memcpy(&n[l1+1],&add[pos],l2-pos+1);
119 char is_absolute(char*filename)
121 if(!filename || !filename[0])
123 if(filename[0]=='/' || filename[0]=='\\')
125 if(filename[1]==':' && filename[1]=='/')
127 if(filename[1]==':' && filename[1]=='\\')
132 char*find_file(const char*filename)
134 include_dir_t*i = current_include_dirs;
136 if(is_absolute(filename)) {
137 FILE*fi = fopen(filename, "rb");
140 return strdup(filename);
144 as3_warning("Include directory stack is empty, while looking for file %s", filename);
147 char*p = concat_paths(i->path, filename);
159 as3_error("Couldn't find file %s", filename);
160 i = current_include_dirs;
162 fprintf(stderr, "include dir: %s\n", i->path);
168 void enter_file(const char*name, const char*filename, void*state)
170 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
171 as3_error("Includes nested too deeply");
174 include_stack[include_stack_ptr] = state;
175 line_stack[include_stack_ptr] = current_line;
176 column_stack[include_stack_ptr] = current_column;
177 shortfilename_stack[include_stack_ptr] = current_filename_short;
178 filename_stack[include_stack_ptr] = current_filename;
179 includedir_stack[include_stack_ptr] = current_include_dirs;
180 char*dir = get_path(filename);
181 add_include_dir(dir);
185 dbg("entering file %s", filename);
189 current_filename = strdup(filename);
190 current_filename_short = strdup(name);
193 FILE*enter_file2(const char*name, const char*filename, void*state)
195 enter_file(name, filename, state);
196 FILE*fi = fopen(filename, "rb");
198 as3_error("Couldn't find file %s: %s", filename, strerror(errno));
205 dbg("leaving file %s", current_filename);
206 if(--include_stack_ptr<=0) {
209 free(current_filename);current_filename = filename_stack[include_stack_ptr];
210 free(current_filename_short);current_filename_short = shortfilename_stack[include_stack_ptr];
211 current_column = column_stack[include_stack_ptr];
212 current_line = line_stack[include_stack_ptr];
213 del_include_dirs(includedir_stack[include_stack_ptr], current_include_dirs);
214 current_include_dirs = includedir_stack[include_stack_ptr];
215 return include_stack[include_stack_ptr];