-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim.c
155 lines (147 loc) · 5.64 KB
/
trim.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "args.h"
#include "trim.h"
bool trim_file(FILE* in_file, FILE* out_file, enum NewlineType newline_type,
bool trailing_newline, bool strip) {
bool changes_made = false;
off_t consecutive_whitespace = 0;
off_t consecutive_newline = 0;
size_t num_lf = 0;
size_t num_crlf = 0;
size_t num_cr = 0;
uint8_t cur_byte;
size_t cur_bytes_len = fread(&cur_byte, 1, 1, in_file);
while(cur_bytes_len) {
if((char)cur_byte == '\r' || (char)cur_byte == '\n') {
// Determine current newline type
uint8_t next_byte;
size_t next_bytes_len = fread(&next_byte, 1, 1, in_file);
enum NewlineType cur_newline;
if(next_bytes_len && (char)cur_byte == '\r' &&
(char)next_byte == '\n') {
cur_newline = CRLF;
num_crlf += 1;
} else if((char)cur_byte == '\n') {
cur_newline = LF;
num_lf += 1;
} else {
cur_newline = CR;
num_cr += 1;
}
// Go back one character if we didn't read a CRLF
if(next_bytes_len && cur_newline != CRLF) {
fseeko(in_file, -1, SEEK_CUR);
}
// Handle trailing whitespace
if(strip && consecutive_whitespace > 0) {
changes_made = true;
fseeko(out_file, -consecutive_whitespace, SEEK_CUR);
consecutive_whitespace = 0;
}
// Write newline
enum NewlineType newline_to_write = cur_newline;
if(newline_type != KEEP && newline_type != cur_newline) {
changes_made = true;
newline_to_write = newline_type;
}
if(newline_to_write == LF) {
fwrite((uint8_t[]){10}, 1, 1, out_file);
if(trailing_newline) {
consecutive_newline += 1;
}
} else if(newline_to_write == CRLF) {
fwrite((uint8_t[]){13, 10}, 1, 2, out_file);
if(trailing_newline) {
consecutive_newline += 2;
}
} else {
fwrite((uint8_t[]){13}, 1, 1, out_file);
if(trailing_newline) {
consecutive_newline += 1;
}
}
} else if((char)cur_byte == ' ' || (char)cur_byte == '\t') {
// Write and count consecutive whitespace
if(!strip) {
consecutive_newline = 0;
} else {
consecutive_whitespace += 1;
}
fwrite(&cur_byte, 1, 1, out_file);
} else {
// Write normal character
consecutive_whitespace = 0;
consecutive_newline = 0;
fwrite(&cur_byte, 1, 1, out_file);
}
cur_bytes_len = fread(&cur_byte, 1, 1, in_file);
}
// Handle trailing whitespace at end of file
if(strip && consecutive_whitespace > 0) {
changes_made = true;
fseeko(out_file, -consecutive_whitespace, SEEK_CUR);
consecutive_whitespace = 0;
}
// Handle trailing newlines
if(trailing_newline) {
if(consecutive_newline > 0) {
// Trim excess trailing newlines
fseeko(out_file, -consecutive_newline, SEEK_CUR);
cur_bytes_len = fread(&cur_byte, 1, 1, out_file);
if(cur_bytes_len && (char)cur_byte == '\n' &&
consecutive_newline > 1) {
// Need to truncate the file after the first LF
changes_made = true;
} else if(cur_bytes_len && (char)cur_byte == '\r') {
// Consume the LF followed by CR if it exists
cur_bytes_len = fread(&cur_byte, 1, 1, out_file);
if(cur_bytes_len && (char)cur_byte == '\n') {
if(consecutive_newline > 2) {
// Need to truncate the file after the first CRLF
changes_made = true;
}
} else {
if(consecutive_newline > 1) {
// Need to truncate the file after the first CR
changes_made = true;
}
if(cur_bytes_len) {
// Seek back if we didn't read a CRLF
fseeko(out_file, -1, SEEK_CUR);
}
}
}
} else {
// Add trailing newline when none exist
enum NewlineType trailing_newline_type = newline_type;
if(trailing_newline_type == KEEP) {
// Choose best newline format, preferring LF, followed by CRLF
if(num_lf >= num_crlf && num_lf >= num_cr) {
trailing_newline_type = LF;
} else if(num_crlf >= num_lf && num_crlf >= num_cr) {
trailing_newline_type = CRLF;
} else {
trailing_newline_type = CR;
}
}
if(trailing_newline_type == LF) {
fwrite(((uint8_t[]){10}), 1, 1, out_file);
} else if(trailing_newline_type == CRLF) {
fwrite((uint8_t[]){13, 10}, 1, 2, out_file);
} else {
fwrite(((uint8_t[]){13}), 1, 1, out_file);
}
changes_made = true;
}
}
off_t out_file_len = ftello(out_file);
fflush(out_file);
if(changes_made) {
ftruncate(fileno(out_file), out_file_len);
}
return changes_made;
}