-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.cpp
398 lines (377 loc) · 9.14 KB
/
format.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
int xyz = 0;
void processcpp(char* filename);
bool fexists(char *filename);
void clear(FILE* in);
void formatfiles(int argc, char *argv[]);
void revertfiles(int argc, char *argv[]);
void stripwhite(string* line);
int main(int argc, char *argv[])
{
char c;
if (strcmp(argv[1],"-f") == 0)
{
formatfiles(argc,argv);
}
else if (strcmp(argv[1],"-r") == 0)
{
fprintf(stderr,"Revert files? This cannot be undone. (y/n) ");
c = getchar();
clear(stdin);
if (c == 'y' or c == 'Y')
{
revertfiles(argc,argv);
}
}
else
{
fprintf(stderr,"Usage: ./format [-r] [-f] files...\n\n -r : use to revert your files to the backed up version\n -f : use to format your files\n\n");
}
return 0;
}
///////////////////////////////////////////
///////////////////////////////////////////
void revertfiles(int argc, char *argv[])
{
char backupfilename[100];
char oldfilename[100];
for (int i = 2; i < argc; i++)
{
strcpy(backupfilename,argv[i]);
strcat(backupfilename,".backup");
strcpy(oldfilename,argv[i]);
strcat(oldfilename,".old");
fprintf(stderr,"Reverting file: %s\n",argv[i]);
if (fexists(argv[i]))
{
fprintf(stderr," Processing file\n");
if( rename(argv[i],oldfilename) != 0 )
perror( "Error reverting file - original could not be removed" );
else
{
if ( rename(backupfilename,argv[i]) == 0)
{
fprintf(stderr, " Successfully reverted file\n" );
remove(oldfilename);
}
else
{
perror( "Error reverting file - backup could not be reverted" );
rename(oldfilename,argv[i]);
}
}
}
else
{
fprintf(stderr," Backup does not exist. Skipping file.\n");
}
}
}
///////////////////////////////////////////
///////////////////////////////////////////
void formatfiles(int argc, char *argv[])
{
int j;
char* extension;
char c;
bool overwrite = true;
for (int i = 2; i < argc; i ++)
{
j = strlen(argv[i]);
while (j > 0 and argv[i][j] != '.')
{
j = j - 1;
}
extension = &argv[i][j];
fprintf(stderr,"Formatting file: %s, as a %s filetype\n",argv[i],extension);
if (fexists(argv[i]))
{
fprintf(stderr," Back up detected. Overwrite the current backup? (y/n) ");
c = getchar();
clear(stdin);
if (c == 'y' or c == 'Y')
{
overwrite = true;
}
else if (c == 'n' or c == 'N')
{
overwrite = false;
}
else
{
fprintf(stderr,"\n Invalid response. Not overwriting. c = %c\n",c);
overwrite = false;
}
}
if (overwrite)
{
fprintf(stderr," Processing file\n");
//if (strcmp(extension,".cpp") == 0)
//{
processcpp(argv[i]);
//}
//else
//{
//fprintf(stderr,"\n Error: File is an unknown type.\n");
//}
}
}
}
///////////////////////////////////////////
///////////////////////////////////////////
bool fexists(char* filename)
{
char file[strlen(filename)];
strcpy(file,filename);
struct stat buf;
if (stat(strcat(file,".backup"), &buf) != -1)
{
return true;
}
return false;
}
///////////////////////////////////////////
///////////////////////////////////////////
void clear(FILE* in)
{
char ch = getc(in);
while (ch != '\n') ch = getc(in);
}
///////////////////////////////////////////
///////////////////////////////////////////
void stripwhite(string* line, int i)
{
try
{
//cout << "stripping white from: " << *line << endl;
while(line->at(i) == 9 || line->at(i) == 32)
{
//fprintf(stderr,"stripped\n");
line->erase(i,1);
}
}
catch (exception& e)
{
//cerr << "exception caught: " << e.what() << endl;
}
}
///////////////////////////////////////////
///////////////////////////////////////////
void processcpp(char* filename)
{
ifstream myfile;
char newfilename[100];
char backupfilename[100];
int result;
ofstream newfile;
int numtab = 0;
string line = "";
bool quote = false;
bool tick = false;
bool lastnewline = false;
bool lastempty = false;
bool linecom = false;
bool bloccom = false;
bool paren = false;
bool codeline = false;
char newchar[2];
newchar[0] = ' ';
newchar[1] = 0;
strcpy(newfilename,filename);
strcat(newfilename,"new");
strcpy(backupfilename,filename);
strcat(backupfilename,".backup");
string newline = "";
myfile.open(filename);
newfile.open(newfilename);
while (myfile.good())
{
newline = "";
lastnewline = false;
linecom = false;
codeline = false;
getline(myfile,line);
stripwhite(&line,0);
cerr << "current line is : '" << line << endl;
if (line[0] != '}') newline.append(numtab,9);
if (not (line.length() == 0 and lastempty))
{
for (int i = 0; i < line.length();
i++)
{
if ((line[i] == '{') and !quote and !tick and !linecom and !bloccom)
{
if (i != 0 and not lastnewline)
{
newline.append("\n");
lastnewline = true;
//fprintf(stderr,"at %i, before char '%c', adding newline\n",i,line[i]);
newline.append(numtab,9);
}
newline.append("{");
stripwhite(&line,i+1);
numtab++;
//fprintf(stderr,"opening: changing tabs to %i\n",numtab);
if (i != line.length() - 1)
{
newline.append("\n");
lastnewline = true;
//fprintf(stderr,"at %i, after char '%c', adding newline\n",i,line[i]);
newline.append(numtab,9);
}
}
else if(line[i] == '}' and !quote and !tick and !linecom and !bloccom)
{
if (numtab > 0)
{
numtab--;
//fprintf(stderr,"closing: changing tabs to %i\n",numtab);
}
if (i == 0)
{
newline.append(numtab,9);
//fprintf(stderr,"at %i, before char '%c', appending '%i' tabs\n",i,line[i],numtab);
}
if (i != 0 and not lastnewline)
{
newline.append("\n");
lastnewline = true;
//fprintf(stderr,"at %i, before char '%c', adding newline and appending '%i' tabs\n",i,line[i],numtab);
newline.append(numtab,9);
}
else if (newline[newline.length()-1] == ' ' and lastnewline) newline.erase(newline.length()-1);
newline.append("}");
stripwhite(&line,i+1);
if ((i != line.length()-1))
{
newline.append("\n");
lastnewline = true;
//fprintf(stderr,"at %i, after char '%c', adding newline and appending '%i' tabs\n",i,line[i],numtab);
newline.append(numtab,9);
}
}
else if( line[i] == ';' and !quote and !tick and !linecom and !bloccom and !paren)
{
newline.append(";");
stripwhite(&line,i+1);
if ((i != line.length()-1))
{
newline.append("\n");
lastnewline = true;
//fprintf(stderr,"at %i, after char '%c', adding newline and appending '%i' tabs\n",i,line[i],numtab);
newline.append(numtab,9);
}
}
else if(line[i] == '(' and !tick and !quote and !bloccom and !linecom)
{
paren = true;
newchar[0] = line[i];
newline.append(newchar);
lastnewline = false;
}
else if(line[i] == ')' and !tick and !quote and !bloccom and !linecom)
{
paren = false;
newchar[0] = line[i];
newline.append(newchar);
lastnewline = false;
}
else if(i > 0 and line[i] == '/' and line[i-1] == '/' and !tick and !quote and !bloccom)
{
linecom = true;
//fprintf(stderr,"found line comment\n");
newchar[0] = line[i];
newline.append(newchar);
lastnewline = false;
}
else if (i > 0 and line[i] == '*' and line[i-1] == '/' and !tick and !quote and !bloccom and !linecom)
{
bloccom = true;
//fprintf(stderr,"found start of block comment\n");
newchar[0] = line[i];
newline.append(newchar);
lastnewline = false;
}
else if (i > 0 and line[i] == '/' and line[i-1] == '*' and !tick and !quote and bloccom and !linecom)
{
bloccom = false;
//fprintf(stderr,"found end of block comment\n");
newchar[0] = line[i];
newline.append(newchar);
lastnewline = false;
}
else if((line[i] == '"') and !tick and !linecom and !bloccom)
{
if (i > 0)
{
if (line[i-1] != 92)
{
quote = !quote;
}
}
else
{
quote = !quote;
}
//fprintf(stderr,"found quote\n");
newline.append("\"");
lastnewline = false;
}
else if((line[i] == 39/*tick*/) and !quote and !linecom and !bloccom)
{
if (i > 0)
{
if (line[i-1] != 92)
{
tick = !tick;
}
}
else
{
tick = !tick;
}
newline.append("'");
lastnewline = false;
}
else
{
newchar[0] = line[i];
newline.append(newchar);
lastnewline = false;
}
}
if(myfile.good())
{
newline.append("\n");
//fprintf(stderr,"adding newline after line\n");
}
newfile << newline;
}
if (line.length() == 0)
{
lastempty = true;
}
else
{
lastempty = false;
}
}
myfile.close();
newfile.close();
result = rename(filename,backupfilename);
if ( result == 0 ) puts ( " Backup successfully created" );
else perror( "Error making backup file" );
result = rename(newfilename,filename);
if ( result == 0 ) puts ( " Original successfully replaced" );
else perror( "Error replacing original file" );
}