-
Notifications
You must be signed in to change notification settings - Fork 0
/
brckt.c
81 lines (74 loc) · 1.31 KB
/
brckt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* bracket field of csv
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELIM '\t'
#define BUFMAX 4096
int ShowUsage();
int main(argc, argv)
int argc;
char *argv[];
{
int ch, delim = DELIM, i;
unsigned char buf[BUFMAX + 1];
if (1 < argc) {
if (strcmp(argv[1], "-h") == 0
|| strcmp(argv[1], "--help") == 0) {
ShowUsage();
exit(0);
}
if (strcmp(argv[1], "-d") == 0) {
delim = argv[2][0];
}
}
for (;;) {
for (i = 0; (ch = getchar()) != EOF && i < BUFMAX; i++) {
if (ch == delim || ch == '\n') {
break;
} else {
switch (ch) {
case '\\':
buf[i] = ch;
i++;
if (i < BUFMAX) {
buf[i] = '\\';
}
break;
case '(':
case ')':
buf[i] = '\\';
i++;
if (i < BUFMAX) {
buf[i] = ch;
}
break;
default:
buf[i] = ch;
break;
}
}
}
buf[i] = '\0';
if (ch == EOF && i == 0) {
break;
}
if (buf[0] & 0x80) {
printf("<%s>%c", buf, ch);
} else {
printf("(%s)%c", buf, ch);
}
}
}
int ShowUsage()
{
fprintf(stderr, "Usage: brckt [OPTION]\n");
fprintf(stderr, "\n");
fprintf(stderr, " -h show usage\n");
fprintf(stderr, " -d delim default is TAB\n");
fprintf(stderr, "\n");
fprintf(stderr,
"bracket field of csv data from stdin to stdout\n");
return 0;
}