-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2.c
395 lines (367 loc) · 11.7 KB
/
ex2.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
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
//312349103 Eldar Shalev
#include <stdio.h>
#include <memory.h>
//define
#define SIZE 2 // for utf-16
#define BULK 1
// Enum Definition
enum Encoding {
Unix, Mac, Win, Illegal
};
/**
* getEndOfLineFormat function.
* @param os - operation system type.
* @return - the end line char according to the format.
*/
char getEndOfLineFormat(enum Encoding os) {
char endLine;
if (os == Unix) {
endLine = '\n';
} else if (os == Mac) {
endLine = '\r';
}
return endLine;
}
/**
* isBigEndian function.
* @param buffer - the reader char.
* @return 1 if big endiann, 0 othewise.
*/
int isBigEndian(char *buffer) {
int isBigEndian;
unsigned short int r = (unsigned short int) buffer[0];
if ((r == (unsigned short int) 0xfffe)) {
isBigEndian = 1;
} else if ((r = (unsigned short int) buffer[1]) == (unsigned short int) 0xfffe) {
isBigEndian = 0;
}
return isBigEndian;
}
/**
* validationOfInput function.
* @param nameOfDstFile - Destination of file name.
* @return 1 if there is a point in file name, 0 otherwise.
*/
int validationOfInput(char *nameOfDstFile) {
int len = strlen(nameOfDstFile);
int i = 0;
for (; i < len; i++) {
if (nameOfDstFile[i] == '.') {
return 1;
}
}
return 0;
}
/**
* makeDstFile function - the function makes a destination file from copy of source file.
* @param src - source file.
* @param dst - destination file.
* @param isSwap - if need to change endiannes
*/
void makeDstFile(char *src, char *dst, int isSwap) {
FILE *source = fopen(src, "rb");
//check if we can read from files.
if (source == NULL) {
return;
}
FILE *target = fopen(dst, "wb");
if (target == NULL) {
return;
}
// A buffer to start reading from
char buffer[SIZE];
//read each char by buffer
while ((fread(buffer, sizeof(buffer), BULK, source) != 0)) {
if (!isSwap) {
fwrite(buffer, sizeof(buffer), BULK, target);
} else {
//swap in case there was a flag.
char temp = buffer[0];
buffer[0] = buffer[1];
buffer[1] = temp;
fwrite(buffer, sizeof(buffer), BULK, target);
}
}
//close the files
fclose(target);
fclose(source);
}
/**
* getEncodingFormat function.
* @param strType - os type.
* @return enum encoding type.
*/
enum Encoding getEncodingFormat(char *strType) {
enum Encoding type;
//make compare between inpute to our enum
if (strcmp(strType, "-win") == 0) {
type = Win;
} else if (strcmp(strType, "-mac") == 0) {
type = Mac;
} else if (strcmp(strType, "-unix") == 0) {
type = Unix;
} else {
//illegal param
type = Illegal;
}
return type;
}
/**
* encodingFromUnixToMac function.
* @param srcFile - source file.
* @param dstFile - destination file.
* @param srcEncoding - source operation system.
* @param dstEncoding - destination operation system.
* @param isSwap - if need to change file's endiannes.
*/
void
encodingFromUnixToMac(char *srcFile, char *dstFile, enum Encoding srcEncoding, enum Encoding dstEncoding, int isSwap) {
FILE *source = fopen(srcFile, "rb");
//check if there is a source and destination files.
if (source == NULL) {
return;
}
FILE *destination = fopen(dstFile, "wb");
if (destination == NULL) {
return;
}
// Getting format
char endLineSymbolSrc = getEndOfLineFormat(srcEncoding);
char endLineSymboltrg = getEndOfLineFormat(dstEncoding);
char buffer[SIZE];
int count = 0;
int isBig = 0;
// as long as we can read bytes- read it to buffer
while ((fread(buffer, sizeof(buffer), BULK, source) != 0)) {
if (count == 0) {
isBig = isBigEndian(buffer);
count++;
}
if (!isSwap) {
if (buffer[isBig] == endLineSymbolSrc && buffer[!isBig] == '\0') {
buffer[isBig] = endLineSymboltrg;
buffer[!isBig] = '\0';
}
} else {
if (buffer[isBig] == endLineSymbolSrc && buffer[!isBig] == '\0') {
buffer[!isBig] = endLineSymboltrg;
buffer[isBig] = '\0';
} else {
//swap other chars of file
char temp = buffer[0];
buffer[0] = buffer[1];
buffer[1] = temp;
}
}
//write the buffer to destination file
fwrite(buffer, sizeof(buffer), BULK, destination);
}
// Close files.
fclose(destination);
fclose(source);
}
/**
* encodingFromWin function.
* @param srcFile - source file.
* @param dstFile - target file.
* @param srcEncoding - source os.
* @param dstEncoding - target os.
*/
void encodingFromWin(char *srcFile, char *dstFile, enum Encoding srcEncoding, enum Encoding dstEncoding) {
FILE *source = fopen(srcFile, "rb");
//check if files are fine
if (source == NULL) {
return;
}
FILE *destination = fopen(dstFile, "wb");
if (destination == NULL) {
return;
}
char endLineSymbolSrc = getEndOfLineFormat(srcEncoding);
char buffer[SIZE];
int count = 0;
int isBig = 0;
// Read as long as it's not 0
while ((fread(buffer, sizeof(buffer), BULK, source) != 0)) {
if (count == 0) {
isBig = isBigEndian(buffer);
count++;
}
//unix or mac to win
if (srcEncoding != Win && dstEncoding == Win) {
//mac to win
if (buffer[isBig] == endLineSymbolSrc) {
buffer[isBig] = '\n';
buffer[!isBig] = '\0';
char tmpBuffer[SIZE];
tmpBuffer[isBig] = '\r';
tmpBuffer[!isBig] = '\0';
fwrite(tmpBuffer, sizeof(tmpBuffer), BULK, destination);
}
fwrite(buffer, sizeof(buffer), BULK, destination);
} else if (srcEncoding == Win && dstEncoding == Unix) {
//win to unix
if (buffer[isBig] == '\n' || buffer[isBig] != '\r') {
fwrite(buffer, sizeof(buffer), BULK, destination);
}
} else if (srcEncoding == Win && dstEncoding == Mac) {
//win to mac
if (buffer[isBig] != '\n' || buffer[isBig] == '\r') {
fwrite(buffer, sizeof(buffer), BULK, destination);
}
}
}
//close the files
fclose(destination);
fclose(source);
}
/**
* isSwapBytes function.
* @param flag - input flag keep\swap
* @return 1 for swap, 0 to keep.
*/
int isSwapBytes(char *flag) {
if (strcmp(flag, "-swap") == 0) {
return 1;
} else if (strcmp(flag, "-keep") == 0) {
return 0;
} else {
return -1;
}
}
/**
* encodingWinSwap function.
* @param srcFile - source file.
* @param dstFile - target file.
* @param srcEncoding - source os.
* @param dstEncoding - target os
*/
void encodingWinSwap(char *srcFile, char *dstFile, enum Encoding srcEncoding, enum Encoding dstEncoding) {
FILE *source = fopen(srcFile, "rb");
//check if files are fine
if (source == NULL) {
return;
}
FILE *destination = fopen(dstFile, "wb");
if (destination == NULL) {
return;
}
char endLineSymbolSrc = getEndOfLineFormat(srcEncoding);
char buffer[SIZE];
int count = 0;
int isBig = 0;
while ((fread(buffer, sizeof(buffer), BULK, source) != 0)) {
if (count == 0) {
isBig = isBigEndian(buffer);
count++;
}
//unix\mac to win
if (srcEncoding != Win && dstEncoding == Win) {
//mac/unix to win
if (buffer[isBig] == endLineSymbolSrc) {
buffer[!isBig] = '\n';
buffer[isBig] = '\0';
char tmpBuffer[SIZE];
tmpBuffer[!isBig] = '\r';
tmpBuffer[isBig] = '\0';
fwrite(tmpBuffer, sizeof(tmpBuffer), BULK, destination);
} else {
//swap other chars
char temp = buffer[0];
buffer[0] = buffer[1];
buffer[1] = temp;
}
fwrite(buffer, sizeof(buffer), BULK, destination);
} else if (srcEncoding == Win && dstEncoding == Unix) {
//win to unix
if (buffer[isBig] == '\n' || buffer[isBig] != '\r') {
char temp = buffer[0];
buffer[0] = buffer[1];
buffer[1] = temp;
fwrite(buffer, sizeof(buffer), BULK, destination);
}
} else if (srcEncoding == Win && dstEncoding == Mac) {
//win to mac
if (buffer[isBig] != '\n' || buffer[isBig] == '\r') {
char temp = buffer[0];
buffer[0] = buffer[1];
buffer[1] = temp;
fwrite(buffer, sizeof(buffer), BULK, destination);
}
}
}
//close the files
fclose(destination);
fclose(source);
}
/**
*
* @param argc the number of arguments .
* @param argv the given arguments, src, dst and optional flags.
* @return there is no return value but it creates a txt file according to given arguments.
*/
int main(int argc, char *argv[]) {
switch (argc) {
// Case without flags at all.
case 3:
// Checking validation of input
if (validationOfInput(argv[1]) == 1 && validationOfInput(argv[2]) == 1) {
makeDstFile(argv[1], argv[2], 0);
}
break;
// Case we have to convert from one os to another os
case 5: {
// Get the encoding os.
enum Encoding srcEncoding = getEncodingFormat(argv[3]);
enum Encoding dstEncoding = getEncodingFormat(argv[4]);
// Check for validation
if (srcEncoding == Illegal || dstEncoding == Illegal) {
return 0;
}
// If both encoding are the same use regular function
if (srcEncoding == dstEncoding) {
makeDstFile(argv[1], argv[2], 0);
return 0;
}
// If we deal with unix and mac combination
if (srcEncoding != Win && dstEncoding != Win) {
encodingFromUnixToMac(argv[1], argv[2], srcEncoding, dstEncoding, 0);
// If we deal with win and (unix/mac)
} else if (srcEncoding == Win || dstEncoding == Win) {
encodingFromWin(argv[1], argv[2], srcEncoding, dstEncoding);
}
break;
}
// Same as case 5 but with flag swap/keep
case 6: {
enum Encoding srcEncoding = getEncodingFormat(argv[3]);
enum Encoding dstEncoding = getEncodingFormat(argv[4]);
if (srcEncoding == Illegal || dstEncoding == Illegal) {
return 0;
}
// To check if we got swap or keep
int isSwap = isSwapBytes(argv[5]);
if (isSwap == -1) {
return 0;
}
// Send the flag also.
if (srcEncoding == dstEncoding) {
makeDstFile(argv[1], argv[2], isSwap);
return 0;
}
// Dealing with mac/unix
if (srcEncoding != Win && dstEncoding != Win) {
encodingFromUnixToMac(argv[1], argv[2], srcEncoding, dstEncoding, isSwap);
// If we deal with win and (unix/mac)
} else if (srcEncoding == Win || dstEncoding == Win) {
if (isSwap) {
encodingWinSwap(argv[1], argv[2], srcEncoding, dstEncoding);
} else {
encodingFromWin(argv[1], argv[2], srcEncoding, dstEncoding);
}
}
break;
}
}
return 0;
}