X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fas3%2Ffiles.c;h=53900271ed8ec7a58816dab0f52f50257b87ecf5;hb=015233e6dbf54b574dd7f55d11c0d0d614802fde;hp=fc4ab6d781b8395a80b1ca4b5caa8fca5f1137be;hpb=0ee8042821654bf4c07ad8fe2880a19593d6cec3;p=swftools.git diff --git a/lib/as3/files.c b/lib/as3/files.c index fc4ab6d..5390027 100644 --- a/lib/as3/files.c +++ b/lib/as3/files.c @@ -53,6 +53,7 @@ int current_line=1; int current_column=0; char* current_filename=0; char* current_filename_short=0; +char* current_filename_long=0; include_dir_t* current_include_dirs=0; #define MAX_INCLUDE_DEPTH 16 @@ -62,6 +63,7 @@ int line_stack[MAX_INCLUDE_DEPTH]; int column_stack[MAX_INCLUDE_DEPTH]; char* filename_stack[MAX_INCLUDE_DEPTH]; char* shortfilename_stack[MAX_INCLUDE_DEPTH]; +char* longfilename_stack[MAX_INCLUDE_DEPTH]; include_dir_t* includedir_stack[MAX_INCLUDE_DEPTH]; int include_stack_ptr = 0; @@ -100,6 +102,78 @@ char*get_path(const char*file) return strdup("."); } } +char* normalize_path(const char*path) +{ + char*n = 0, *d = 0; + if(path[0] != '/') { + char buf[512]; + char*c = getcwd(buf,512); + int l = strlen(buf); + d = n = malloc(l+strlen(path)+10); + strcpy(n, buf);d += l; + if(!l || n[l-1]!='/') { + *d='/';d++; + } + } else { + d = n = strdup(path); + } + const char*s=path; + char init = 1; + + while(*s) { + if(init && s[0] == '.' && (s[1]=='/' || s[1]=='\0')) { + if(!s[1]) break; + s+=2; + init=1; + continue; + } + if(init && s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2]=='\0')) { + // step one down + char*last = 0; + if(d<=n) + return 0; + *--d = 0; + if(!(last=strrchr(n, '/'))) { + return 0; + } + d = last+1; + if(!s[2]) break; + s+=3; + init=1; + continue; + } + + *d = *s; + if(*s=='/') init=1; + else init=0; + d++;s++; + } + if(d!=n && d[-1]=='/') + d--; + *d = 0; + return n; +} +static void testnormalize() +{ +#define TEST(x) {printf("%s -> %s\n", (x), normalize_path(x));} + TEST("."); + TEST("../as3"); + TEST("../as3/"); + TEST("../as3/parser.y"); + TEST("../as3/ok/../ok/scope.as"); + TEST("ok/scope.as"); + TEST("ok/./scope.as"); + TEST("./ok/scope.as"); + TEST("./"); + TEST("/tmp/"); + TEST("/./tmp/"); + TEST("../"); + TEST("/"); + TEST("/tmp"); + TEST("/tmp/../usr/"); +} + + char* concat_paths(const char*base, const char*add) { int l1 = strlen(base); @@ -116,7 +190,7 @@ char* concat_paths(const char*base, const char*add) memcpy(&n[l1+1],&add[pos],l2-pos+1); return n; } -char is_absolute(char*filename) +char is_absolute(const char*filename) { if(!filename || !filename[0]) return 0; @@ -129,7 +203,7 @@ char is_absolute(char*filename) return 0; } -char*find_file(const char*filename) +char*find_file(const char*filename, char error) { include_dir_t*i = current_include_dirs; FILE*fi = 0; @@ -140,7 +214,7 @@ char*find_file(const char*filename) return strdup(filename); } } else { - if(!i) { + if(!i && error) { as3_warning("Include directory stack is empty, while looking for file %s", filename); } while(i) { @@ -155,6 +229,9 @@ char*find_file(const char*filename) i = i->next; } } + if(!error) { + return 0; + } as3_error("Couldn't find file %s", filename); i = current_include_dirs; @@ -175,19 +252,23 @@ void enter_file(const char*name, const char*filename, void*state) line_stack[include_stack_ptr] = current_line; column_stack[include_stack_ptr] = current_column; shortfilename_stack[include_stack_ptr] = current_filename_short; + longfilename_stack[include_stack_ptr] = current_filename_long; filename_stack[include_stack_ptr] = current_filename; includedir_stack[include_stack_ptr] = current_include_dirs; - char*dir = get_path(filename); + + /*char*dir = get_path(filename); add_include_dir(dir); - free(dir); + free(dir);*/ + include_stack_ptr++; dbg("entering file %s", filename); current_line=1; current_column=0; - current_filename = strdup(filename); + current_filename = strdup(name); current_filename_short = strdup(name); + current_filename_long = strdup(filename); } FILE*enter_file2(const char*name, const char*filename, void*state) @@ -208,6 +289,7 @@ void* leave_file() } else { free(current_filename);current_filename = filename_stack[include_stack_ptr]; free(current_filename_short);current_filename_short = shortfilename_stack[include_stack_ptr]; + free(current_filename_long);current_filename_long = longfilename_stack[include_stack_ptr]; current_column = column_stack[include_stack_ptr]; current_line = line_stack[include_stack_ptr]; del_include_dirs(includedir_stack[include_stack_ptr], current_include_dirs);