/* Copyright (C) 2003, 2004 Dave Bayer. Subject to the terms and conditions of the MIT License. */ #include "root.h" #include "file.h" #include "filter.h" /*, strState */ typedef enum { strStart, strPrint, strNStates } strState; /*, strTable */ static filterRules *strTable[strNStates][NCHARS]; /*, strRules */ static filterRules strRules[] = { { "", "\t\"", strStart, strPrint, 0 }, { "\"", "\\\"", strPrint, strPrint, 0 }, { "\\", "\\\\", strPrint, strPrint, 0 }, { "\t", "\\t", strPrint, strPrint, 0 }, { "\n", "\",\n", strPrint, strStart, 0 }, { 0 } }; /*, fileToCStrings */ void fileToCStrings( const char *inputFile, const char *outputFile ) { FILE *fin, *fout; line *l; lineFilter filter; filterState state; /* construct finite state machine */ initFilterTable( strTable, strRules, strNStates, YES ); /* initialize filter and state */ filter.filt = stateFilter; filter.next = 0; filter.data = &state; state.table = strTable; state.state = strStart; state.filt = 0; /* open input and open files */ fin = fileOpen( inputFile, "r", YES ); fout = fileOpen( outputFile, "w", YES ); /* read and filter fin, write to fout */ l = fileRead( fin, &filter, 0 ); fprintf( fout, "{\n" ); fileWrite( fout, l, 0 ); fprintf( fout, "\t0\n};\n" ); /* close input and open files */ fileClose( fin, YES ); fileClose( fout, YES ); /* free allocated memory */ lineListFree( l, markFree ); /* check that all memory has been freed */ mallocCount(); } /*, main */ int main( int argc, const char *argv[] ) { if ( argc != 3 ) printf( "Usage: cstring input output\n" ); else fileToCStrings( argv[1], argv[2] ); return 0; }