Skip to content

Instantly share code, notes, and snippets.

@tacke758
Last active December 16, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tacke758/5379180 to your computer and use it in GitHub Desktop.
Save tacke758/5379180 to your computer and use it in GitHub Desktop.
C program that removes escaped line breaks, which are surrounded by double quotes from CSV file and change it into unix style file format (using line feed (LF) as line break). This program implements an automaton that accepts CSV, so it's short and run fast.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* Size of atomic reads. */
#define BUFFER_SIZE (16 * 1024)
int state = 0;
void exit_with_error(int state, char c) {
fprintf(stderr, "error when %d (%x)", state, c);
exit(-1);
}
/*
Qs={0}, Qf={7}
0: (init)
1: "
2: (any character)
3: "
4: "
5: ,
6: EOF
7: CR
8: LF
0 1
1 2 3
2 2 3
3 4 5 6 7 8
4 2 3
5 1
7 1 6 8
8 1 6
*/
int main() {
char buf[BUFFER_SIZE + 1];
size_t bytes_read;
while (0 < (bytes_read = read(0, buf, BUFFER_SIZE))) {
int i;
for (i = 0; i < bytes_read; i ++) {
char next = buf[i];
switch(state) {
case 0:
if (next == '"') {
state = 1;
putchar(next);
}
else {
exit_with_error(state, next);
}
break;
case 1:
/* Fall through */
case 2:
/* Fall through */
case 4:
if (next == '"') {
state = 3;
putchar(next);
}
else {
state = 2;
// CR/LFは空白を出力
if (next == 0x0D || next == 0x0A) {
putchar(' ');
}
else {
putchar(next);
}
}
break;
case 3:
if (next == '"') {
state = 4;
putchar(next);
}
else if (next == ',') {
state = 5;
putchar(next);
}
else if (next == EOF) {
state = 6;
exit(0);
}
else if (next == 0x0D) {
// CRを出力しない
state = 7;
//putchar(next);
}
else if (next == 0x0A) {
state = 8;
putchar(next);
}
break;
case 5:
if (next == '"') {
state = 1;
putchar(next);
}
else {
exit_with_error(state, next);
}
break;
case 7:
if (next == EOF) {
state = 6;
exit(0);
}
else if (next == '"') {
state = 1;
putchar(next);
}
else if (next == 0x0A) {
state = 8;
putchar(next);
}
else {
exit_with_error(state, next);
}
break;
case 8:
if (next == EOF) {
state = 6;
exit(0);
}
else if (next == '"') {
state = 1;
putchar(next);
}
else {
exit_with_error(state, next);
}
break;
default:
exit_with_error(state, next);
break;
}
}
}
//printf("finish!\n");
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment