// HsTraceDump.c // - extract readable text in HsTraceDatabase.PDB // by HAL 2007/1/13 #include #include #define BUFSZ 8 typedef enum {NL_DOS, NL_UNIX, NL_MAC } NewLine_t; void newline(NewLine_t newlineMode, FILE* ofp){ switch(newlineMode){ case NL_UNIX: fputc('\n', ofp); return; case NL_MAC : fputc('\r', ofp); return; default: case NL_DOS : fprintf(ofp, "\r\n"); return; } } void usage(char* argv0){ fprintf(stderr, "USAGE:\n"); fprintf(stderr, "\t%s [-a][-A][-u][-m][-o output.txt] HsTraceDatabase.PDB\n", argv0); fprintf(stderr, "option:\n"); fprintf(stderr, "\t-a : print all readable chars with newline\n"); fprintf(stderr, "\t-A : print all readable chars without newline\n"); fprintf(stderr, "\t-u : (Unix) LF as newline (default: (Dos) CR-LF as newline)\n"); fprintf(stderr, "\t-m : (Mac) CR as newline\n"); fprintf(stderr, "\t-o : specify output file name (default: STDOUT)\n"); } int main(int argc, char** argv){ int argi = 1; FILE *ifp; FILE *ofp = stdout; char buf[BUFSZ]; enum {WORK_SEARCH, WORK_PRINT} workMode = WORK_SEARCH; enum {PRINT_NORMAL, PRINT_ALL_WITH_NEWLINE, PRINT_ALL} printMode = PRINT_NORMAL; NewLine_t nlMode = NL_DOS; // check arg if(argc < 2){ usage(argv[0]); exit(1); } // check option for(argi=1; argi' && printMode == PRINT_ALL_WITH_NEWLINE) newline(nlMode, ofp); } break; default: case PRINT_NORMAL: while(!feof(ifp)){ buf[0] = fgetc(ifp); if(!isprint(buf[0])) continue; if(workMode == WORK_SEARCH){ // search pattern: "" if(buf[0] == '<'){ if(fgets(buf, 6, ifp) != NULL && strncmp(buf, "Text>", 5) == 0) workMode = WORK_PRINT; else fseek(ifp, -5, SEEK_CUR); } continue; } else if(workMode == WORK_PRINT){ // search pattern: "" if(buf[0] == '<'){ if(fgets(buf, 7, ifp) != NULL && strncmp(buf, "/Text>", 6) == 0){ workMode = WORK_SEARCH; newline(nlMode, ofp); continue; } else{ fseek(ifp, -6, SEEK_CUR); buf[0] = '<'; } } fputc(buf[0], ofp); } } break; } fclose(ifp); if(ofp != stdout) fclose(ofp); exit(0); }