int main(int argc, char *argv[]) { astring *dirname = NULL, *tempfilename = NULL, *tempheader = NULL, *tempfooter = NULL; UINT32 bufsize; void *buffer; int listnum; int result; /* first argument is the directory */ if (argc < 4) { fprintf(stderr, "Usage:\nregrep <template> <outputdir> <summary1> [<summary2> [<summary3> ...]]\n"); return 1; } tempfilename = astring_dupc(argv[1]); dirname = astring_dupc(argv[2]); list_count = argc - 3; /* read the template file into an astring */ if (core_fload(astring_c(tempfilename), &buffer, &bufsize) == FILERR_NONE) { tempheader = astring_dupch((const char *)buffer, bufsize); free(buffer); } /* verify the template */ if (tempheader == NULL) { fprintf(stderr, "Unable to read template file\n"); return 1; } result = astring_findc(tempheader, 0, "<!--CONTENT-->"); if (result == -1) { fprintf(stderr, "Template is missing a <!--CONTENT--> marker\n"); return 1; } tempfooter = astring_substr(astring_dup(tempheader), result + 14, -1); tempheader = astring_substr(tempheader, 0, result); /* loop over arguments and read the files */ for (listnum = 0; listnum < list_count; listnum++) { result = read_summary_log(argv[listnum + 3], listnum); if (result != 0) return result; } /* output the summary */ output_report(dirname, tempheader, tempfooter, sort_file_list()); astring_free(dirname); astring_free(tempfilename); astring_free(tempheader); astring_free(tempfooter); return 0; }
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename) { include_path *curpath; /* iterate over include paths and find the file */ for (curpath = incpaths; curpath != NULL; curpath = curpath->next) { astring *srcincpath = astring_dup(curpath->path); core_file *testfile; int lastsepindex = 0; int sepindex; /* a '.' include path is specially treated */ if (astring_cmpc(curpath->path, ".") == 0) astring_cpysubstr(srcincpath, srcfile, 0, astring_rchr(srcfile, 0, PATH_SEPARATOR[0])); /* append the filename piecemeal to account for directories */ while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1) { astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex); /* handle .. by removing a chunk from the incpath */ if (astring_cmpc(pathpart, "..") == 0) { int sepindex_part = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]); if (sepindex_part != -1) astring_substr(srcincpath, 0, sepindex_part); } /* otherwise, append a path separator and the pathpart */ else astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart); /* advance past the previous index */ lastsepindex = sepindex + 1; /* free the path part we extracted */ astring_free(pathpart); } /* now append the filename */ astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1); /* see if we can open it */ if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE) { /* close the file */ core_fclose(testfile); return srcincpath; } /* free our include path */ astring_free(srcincpath); } return NULL; }
astring *astring_trimspace(astring *str) { char *ptr; /* first remove stuff from the end */ for (ptr = str->text + strlen(str->text) - 1; ptr >= str->text && (!(*ptr & 0x80) && isspace((UINT8)*ptr)); ptr--) *ptr = 0; /* then count how much to remove from the beginning */ for (ptr = str->text; *ptr != 0 && (!(*ptr & 0x80) && isspace((UINT8)*ptr)); ptr++) ; if (ptr > str->text) astring_substr(str, ptr - str->text, -1); return str; }
astring *core_filename_extract_base(astring *result, const char *name, int strip_extension) { /* find the start of the name */ const char *start = name + strlen(name); while (start > name && !is_directory_separator(start[-1])) start--; /* copy the rest into an astring */ astring_cpyc(result, start); /* chop the extension if present */ if (strip_extension) astring_substr(result, 0, astring_rchr(result, 0, '.')); return result; }
static astring *find_include_file(int srcrootlen, int dstrootlen, const astring *srcfile, const astring *dstfile, const astring *filename) { include_path *curpath; /* iterate over include paths and find the file */ for (curpath = incpaths; curpath != NULL; curpath = curpath->next) { astring *srcincpath = astring_cat(astring_dupsubstr(srcfile, 0, srcrootlen + 1), curpath->path); core_file *testfile; int lastsepindex = 0; int sepindex; /* a '.' include path is specially treated */ if (astring_cmpc(curpath->path, ".") == 0) astring_cpysubstr(srcincpath, srcfile, 0, astring_rchr(srcfile, 0, PATH_SEPARATOR[0])); /* append the filename piecemeal to account for directories */ while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1) { astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex); /* handle .. by removing a chunk from the incpath */ if (astring_cmpc(pathpart, "..") == 0) { sepindex = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]); if (sepindex != -1) astring_substr(srcincpath, 0, sepindex); } /* otherwise, append a path separator and the pathpart */ else astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart); /* advance past the previous index */ lastsepindex = sepindex + 1; /* free the path part we extracted */ astring_free(pathpart); } /* now append the filename */ astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1); /* see if we can open it */ if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE) { astring *tempfile = astring_alloc(); astring *tempinc = astring_alloc(); /* close the file */ core_fclose(testfile); /* find the longest matching directory substring between the include and source file */ lastsepindex = 0; while ((sepindex = astring_chr(srcincpath, lastsepindex, PATH_SEPARATOR[0])) != -1) { /* get substrings up to the current directory */ astring_cpysubstr(tempfile, srcfile, 0, sepindex); astring_cpysubstr(tempinc, srcincpath, 0, sepindex); /* if we don't match, stop */ if (astring_cmp(tempfile, tempinc) != 0) break; lastsepindex = sepindex + 1; } /* chop off the common parts of the paths */ astring_cpysubstr(tempfile, srcfile, lastsepindex, -1); astring_replacechr(astring_substr(srcincpath, lastsepindex, -1), PATH_SEPARATOR[0], '/'); /* for each directory left in the filename, we need to prepend a "../" */ while ((sepindex = astring_chr(tempfile, 0, PATH_SEPARATOR[0])) != -1) { astring_substr(tempfile, sepindex + 1, -1); astring_insc(srcincpath, 0, "../"); } astring_catc(srcincpath, ".html"); /* free the strings and return the include path */ astring_free(tempfile); astring_free(tempinc); return srcincpath; } /* free our include path */ astring_free(srcincpath); } return NULL; }