-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnlfix.c
111 lines (85 loc) · 2.13 KB
/
nlfix.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <string.h>
/// Read from stdin and convert mac and windows newlines into unix.
///
/// compile with: gcc nlfix.c -o nlfix
/// example: echo -e "hello\rfoo" | nlfix
/// example: cat myfile.txt | nlfix | tee out.txt
typedef struct {
int win;
int unix;
int mac;
} Stats;
void analyse(FILE* file, int doOut, Stats* stats);
void print_stats(Stats* stats);
int main(int argc, char** argv) {
int doStats = 0;
for(int i = 1; i < argc; ++i) {
if(strcmp(argv[i], "-s") == 0) {
doStats = i;
break;
}
}
Stats stats = {0, 0, 0};
if(argc > 2 || (!doStats && argc > 1)) {
for(int i = 1; i < argc; ++i) {
if(i != doStats) {
FILE* file = fopen(argv[i], "r");
if(file) {
analyse(file, !doStats, &stats);
fclose(file);
} else {
fprintf(stderr, "Error: Could not open '%s' for reading.\n", argv[i]);
}
}
}
// files.
} else {
analyse(stdin, !doStats, &stats);
}
if(doStats) {
print_stats(&stats);
}
return 0;
}
void analyse(FILE* file, int doOut, Stats* stats) {
char buffer[128];
char last = 0;
do {
int retrieved = fread(buffer, 1, sizeof buffer, file);
for(int i = 0; i < retrieved; ++i) {
char current = buffer[i];
if(current == '\n' && last == '\r') {
++stats->win;
if(doOut)
fputs("\n", stdout);
} else if(current == '\r') {
// withhold tentative Mac newline
} else if(last == '\r') {
++stats->mac;
if(doOut)
fputc('\n', stdout);
} else if(current == '\n') {
++stats->unix;
if(doOut)
fputc(current, stdout);
} else {
if(doOut)
fputc(current, stdout);
}
last = current;
}
} while( ! feof(file));
if(last == '\r') {
++stats->mac;
if(doOut)
fputc('\n', stdout);
}
}
void print_stats(Stats* stats) {
printf(" os | escape | name | hex | count \n");
printf("-------------------------------\n");
printf(" win | \\r\\n | CRLF | 0x0d 0x0a | %d\n", stats->win);
printf(" unix | \\n | LF | 0x0a (^J) | %d\n", stats->unix);
printf(" mac | \\r | CR | 0x0d (^M) | %d\n", stats->mac);
}