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 static int verbose = 0;
29 static void dbg(const char*format, ...)
36 va_start(arglist, format);
37 vsprintf(buf, format, arglist);
40 while(l && buf[l-1]=='\n') {
44 printf("(includefilehandler) ");
50 typedef struct _include_dir {
52 struct _include_dir*next;
57 char* current_filename=0;
58 include_dir_t* current_include_dirs=0;
60 #define MAX_INCLUDE_DEPTH 16
62 void*include_stack[MAX_INCLUDE_DEPTH];
63 int line_stack[MAX_INCLUDE_DEPTH];
64 int column_stack[MAX_INCLUDE_DEPTH];
65 char* filename_stack[MAX_INCLUDE_DEPTH];
66 include_dir_t* includedir_stack[MAX_INCLUDE_DEPTH];
67 int include_stack_ptr = 0;
69 void add_include_dir(char*dir)
71 include_dir_t*d = malloc(sizeof(include_dir_t));
72 memset(d , 0, sizeof(include_dir_t));
74 d->next = current_include_dirs;
75 current_include_dirs = d;
77 char*get_path(char*file)
79 char*path = strdup(file);
80 char*r1 = strrchr(path, '/');
81 char*r2 = strrchr(path, '\\');
92 char* concat_paths(const char*base, const char*add)
94 int l1 = strlen(base);
98 while(l1 && base[l1-1] == '/')
100 while(pos < l2 && add[pos] == '/')
102 n = (char*)malloc(l1 + (l2-pos) + 2);
105 memcpy(&n[l1+1],&add[pos],l2-pos+1);
108 char is_absolute(char*filename)
110 if(!filename || !filename[0])
112 if(filename[0]=='/' || filename[0]=='\\')
114 if(filename[1]==':' && filename[1]=='/')
116 if(filename[1]==':' && filename[1]=='\\')
120 char*find_file(char*filename)
122 include_dir_t*i = current_include_dirs;
124 if(is_absolute(filename)) {
125 FILE*fi = fopen(filename, "rb");
128 return strdup(filename);
133 char*p = concat_paths(i->path, filename);
144 char*enter_file(char*filename, void*state)
146 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
147 syntaxerror("Includes nested too deeply");
150 char*fullfilename = find_file(filename);
152 syntaxerror("Couldn't find file %s", filename);
153 include_dir_t*i = current_include_dirs;
155 fprintf(stderr, "include dir: %s\n", i->path);
160 include_stack[include_stack_ptr] = state;
161 line_stack[include_stack_ptr] = current_line;
162 column_stack[include_stack_ptr] = current_column;
163 filename_stack[include_stack_ptr] = current_filename;
164 includedir_stack[include_stack_ptr] = current_include_dirs;
165 add_include_dir(get_path(fullfilename));
168 dbg("entering file %s", fullfilename);
172 current_filename=fullfilename;
178 dbg("leaving file %s", current_filename);
179 if(--include_stack_ptr<0) {
182 current_column = column_stack[include_stack_ptr];
183 current_line = line_stack[include_stack_ptr];
184 free(current_filename);current_filename=0;
185 current_filename = filename_stack[include_stack_ptr];
186 current_include_dirs = includedir_stack[include_stack_ptr];
187 return include_stack[include_stack_ptr];