/* Copyright 2001 Blossom Associates West All rights reserved. */ /* Converts from comma separated values to tab separated values. Leaves quoted commas undisturbed. BUG: Gets out of step with errors in input like quote marks where they shouldn't be. BUG: Preserves spaces after commas. Eric Blossom */ #include int main( int argc, char *argv[] ) { int c; enum states { start, inQuote, escape, error, stop }; static enum states ourState = start; while ( stop != ourState && error != ourState ) { c = getchar(); switch ( ourState ) { case start: switch ( c ) { case '"': ourState = inQuote; break; case EOF: ourState = stop; break; case ',': c = '\t'; default: putchar( c ); } break; case inQuote: switch ( c ) { case '"': ourState = start; break; case EOF: ourState = stop; break; case '\\': ourState = escape; break; case '\n': ourState = start; // Recover from unfinished quote. default: putchar( c ); } break; case escape: switch( c ) { case '\\': case '\"': putchar( c ); break; default: putchar( '\\' ); putchar( c ); } ourState = inQuote; break; default: ourState = error; // Should never happen. } } if ( error == ourState ) { fprintf( stderr, "csv2tsv: Invalid State." ); return 1; } return 0; }