/* Copyright 2001 Blossom Associates West All rights reserved. */ /* Converts from comma separated values to tab separated values. Leaves quoted commas undisturbed. BUG: Does not honor escape sequences \" or \\. Quotation marks can be included in a quote by doubling them up. e.g. "a quotation mark "" is included". However, this is not "standard". BUG: quotes can contain a newline character. Eric Blossom */ #include int main( int argc, char *argv[] ) { int c; enum states { start, inQuote, atQuote, 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 = atQuote; break; case EOF: ourState = error; break; /* case '\n': ourState = start; // Recover from unfinished quote. */ default: putchar( c ); } break; case atQuote: switch ( c ) { case '"': putchar( c ); ourState = inQuote; break; case EOF: ourState = error; break; case ',': c = '\t'; default: putchar( c ); ourState = start; } break; default: ourState = error; // Should never happen. } } if ( error == ourState ) { fprintf( stderr, "csv2tsv: Invalid State." ); return 1; } return 0; }