-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabspace.cpp
More file actions
265 lines (236 loc) · 8.94 KB
/
tabspace.cpp
File metadata and controls
265 lines (236 loc) · 8.94 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Windows utility to batch convert program files to tab-space rule compliant
//
// Copyright (c)2023 Yiping Cheng, mailto:ypcheng@bjtu.edu.cn
// Beijing Jiaotong University. All rights reserved.
// https://www.researchgate.net/profile/Yiping-Cheng/research
#include <assert.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
LPCTSTR RuleName(BOOL useAAS) noexcept
{
return useAAS ? _T("aligned all-space") : _T("tab-space");
}
// State values
enum State {
stInitial = (int)0x00000,
stRightSeg = (int)0x00001,
stExpectLF = (int)0x00002
};
#define TAB '\t'
#define SPACE ' '
#define CR '\r'
#define LF '\n'
#define PUT_BYTE_MAIN(b) \
do { \
if (fputc(b, tscfile) == EOF) { \
WRITE_FAIL: \
_tprintf(_T("%s : conversion failed " \
"when writing TSC file\n"), \
filename); \
goto FAIL; \
} \
} while (0)
#define PUT_BYTE(b) \
do { \
if (fputc(b, tscfile) == EOF) { \
goto WRITE_FAIL; \
} \
} while (0)
#define PUT_BYTES(b, r) \
do { \
for (UINT i = 0; i < r; i++) { \
if (fputc(b, tscfile) == EOF) { \
goto WRITE_FAIL; \
} \
} \
} while (0)
inline BOOL IsValidPath(LPCTSTR filePath) noexcept
{
DWORD fa = GetFileAttributes(filePath);
// Check if the path exists and is a file (not a directory)
return (fa != INVALID_FILE_ATTRIBUTES);
}
BOOL IsSuffix(const TCHAR* strWhole, const TCHAR* strSub) noexcept
{
assert(strWhole && strSub);
size_t len1 = _tcslen(strWhole);
size_t len2 = _tcslen(strSub);
// If strSub is longer than strWhole, it cannot be a suffix
if (len2 > len1) {
return FALSE;
}
// Compare the last len2 characters of strWhole with strSub
return _tcscmp(strWhole + len1 - len2, strSub) == 0;
}
void Convert(LPCTSTR filename, BOOL useAAS) noexcept
{
static const TCHAR suffix_bak[5] = _T(".bak");
static const TCHAR suffix_tsc[5] = _T(".tsc"); // tsc = tab-space converted
TCHAR bakfilename[MAX_PATH], tscfilename[MAX_PATH];
if (IsSuffix(filename, suffix_bak)) {
_tprintf(_T("%s : not processed by Tabspace as its extension is %s\n"),
filename, suffix_bak);
return;
}
// the backup file name is the orginal file name plus ".bak"
if (_stprintf_s(bakfilename, MAX_PATH, _T("%s%s"), filename, suffix_bak) < 0) {
_tprintf(_T("%s : failed to make backup file name\n"), filename);
return;
}
// test whether the bak file name clashes with existing files
if (IsValidPath(bakfilename)) {
_tprintf(_T("%s : conversion failed because BAK file name clashes with an existing file\n"), filename);
return;
}
// the tsc file name is the orginal file name plus ".tsc"
if (_stprintf_s(tscfilename, MAX_PATH, _T("%s%s"), filename, suffix_tsc) < 0) {
_tprintf(_T("%s : failed to make tsc file name\n"), filename);
return;
}
// now create the tsc file, it must not name-clash with existing files
// we do not think of a different name to avoid name clash
// because that is too rare
if (IsValidPath(tscfilename)) {
_tprintf(_T("%s : conversion failed because TSC file name clashes with an existing file\n"), filename);
return;
}
FILE* tscfile = _tfopen(tscfilename, _T("wb"));
if (tscfile == nullptr) {
_tprintf(_T("%s : conversion failed when creating TSC file\n"), filename);
return;
}
// open the original file
FILE* orgfile = _tfopen(filename, _T("rb"));
if (orgfile == nullptr) {
_tprintf(_T("%s : conversion failed when opening original file\n"), filename);
ABORT_TSC:
fclose(tscfile);
DeleteFile(tscfilename);
return;
}
// now the actual conversion begins
BOOL changed = FALSE;
enum State state = stInitial;
UINT ccws = 0; // count of consecutive white spaces
int ch;
BYTE byte;
for (;;) {
if ((ch = fgetc(orgfile)) == EOF) {
if (ferror(orgfile)) {
_tprintf(_T("%s : conversion failed when reading original file\n"), filename);
FAIL:
fclose(orgfile);
goto ABORT_TSC;
}
// end of file encountered
if (ccws) {
changed = TRUE; // because the trailing spaces are discarded
}
if (state == stExpectLF) {
PUT_BYTE_MAIN(LF); // no need to set changed to TRUE because it is
// already set when processing the CR
}
break;
}
byte = (BYTE)ch;
if (byte == CR) {
state = stExpectLF;
changed = TRUE; // since we adopt the UNIX standard for line breaking
// a single LF is used. So CR will be discarded.
// later, we will not need to set changed to TRUE because
// it is already set here
} else if (byte == LF) {
PUT_BYTE(LF);
state = stInitial;
if (ccws) {
changed = TRUE;
ccws = 0;
}
} else if (byte == TAB) {
if (state == stInitial) {
ccws += 4; // 1 tab = 4 spaces, but not put them until suitable time
if (useAAS) {
changed = TRUE; // because it will be changed to spaces
}
} else if (state == stRightSeg) {
// stRightSeg means after the first non-space non-tab character
ccws += 4;
changed = TRUE;
} else {
PUT_BYTE(LF);
state = stInitial;
ccws = 4; // these spaces belong to the new line
}
} else if (isspace(byte)) {
if (byte != SPACE) {
changed = TRUE; // because it will be changed to SPACE or TAB
}
if (state == stInitial) {
ccws++;
if (!useAAS) {
changed = TRUE; // because it will be changed to tab
}
} else if (state == stRightSeg) {
ccws++;
} else {
PUT_BYTE(LF);
state = stInitial;
ccws = 1; // this space belongs to the new line
}
} else {
if (state == stInitial) {
if (useAAS) {
if (ccws % 4) {
// ccws not a multiple of 4 will be changed
changed = TRUE;
// so that indent of 2 spaces will become 4 spaces
ccws = ((ccws + 2) / 4) * 4;
}
PUT_BYTES(SPACE, ccws);
} else {
// No need to set changed to TRUE here, because if ccws is
// not a multiple of 4, the original file must contain a
// white space in a left seg, and changed is already set to
// TRUE when processing that white space.
PUT_BYTES(TAB, (ccws + 2) / 4);
// so that indent of 2 spaces will get a tab
}
state = stRightSeg;
} else if (state == stRightSeg) {
PUT_BYTES(SPACE, ccws);
} else {
PUT_BYTE(LF);
state = stInitial;
}
PUT_BYTE(byte);
ccws = 0;
}
}
fclose(orgfile);
if (!changed) {
// the tsc file is the same as the original file, so delete it
_tprintf(_T("%s : already %s compliant so left unchanged\n"),
filename, RuleName(useAAS));
goto ABORT_TSC;
}
if (fclose(tscfile)) { // write the remaining bytes in the tscfile buffer
_tprintf(_T("%s : conversion failed when closing TSC file\n"), filename);
DeleteFile(tscfilename);
return;
}
// if conversion successful, rename the original file to bakfilename
// and rename the tsc file to original filename
if (!MoveFile(filename, bakfilename)) {
_tprintf(_T("%s : still the original file due to renaming failure,"
" but its TSC file is %s\n"),
filename, tscfilename);
return;
}
if (!MoveFile(tscfilename, filename)) {
_tprintf(_T("%s : now renamed as %s, whose TSC file is %s\n"),
filename, bakfilename, tscfilename);
return;
}
_tprintf(_T("%s : %s conversion successful\n"), filename, RuleName(useAAS));
}