-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.c
More file actions
94 lines (90 loc) · 2.96 KB
/
Copy pathreplace.c
File metadata and controls
94 lines (90 loc) · 2.96 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
/**
* @file replace.c
* @author Meles Meles(mymeles)
* Replace.c is a program which finds and replaces selected words
* in an input text file as directed by the command line arguments.
*/
#include "line.h"
#include "expand.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/** Number of required arguments at the end of the command line. */
#define REQUIRED_ARGS 2
/**
* main is the start of the program. With help from the other
* components, it replaces target strings with their replacements
* in the input file and writes out the resulting text to the output.
* If a command line argument strucure is invalid, if the files specified
* can not be opened, or if a word replacement is illegal, or if there is a
* duplicate target word it will exit out the program with a status of 1.
* @param argc is the an int type indicating the size of argv
* @param argv is an array of char pointers to command line 0arguments.
* @return int the exit status of the program which is 0.
*/
int main(int argc, char *argv[])
{
if (argc % 2 == 0) {
fprintf(stderr, "usage: replace (<target> <replacement>)* <input-file> <output-file>\n");
exit(EXIT_FAILURE);
}
int lenWordList = argc - 3;
char *input = argv[argc - REQUIRED_ARGS];
char *output = argv[argc - 1];
int t_r_list_len = lenWordList / 2;
char *tlist[t_r_list_len];
char *rlist[t_r_list_len];
int tlistcount = 0;
int rlistcount = 0;
int maxRep = 0;
if (lenWordList > 0) {
for (int i = 1; i < argc - 1; i++) {
if (i % 2 == 0) {
int word_len = strlen(argv[i]);
for (int j = 0; j < word_len; j++) {
if (!wordChar(argv[i][j])) {
fprintf(stderr, "Invalid word: %s\n", argv[i]);
exit(EXIT_FAILURE);
}
}
if (maxRep < strlen(argv[i])) {
maxRep = strlen(argv[i]);
}
rlist[rlistcount++] = argv[i];
}
else {
tlist[tlistcount++] = argv[i];
}
}
}
FILE *read = fopen(input, "r"); // opening file to read from
FILE *write = fopen(output, "w"); // opening file to write to
if (read == NULL || write == NULL) {
fprintf(stderr, "Can't open file: %s\n", read == NULL ? input : output);
exit(EXIT_FAILURE);
}
if (lenWordList > 0) {
for (int i = 0; i < t_r_list_len; i++) {
for (int j = i+1; j < t_r_list_len; j++){
if (strcmp(tlist[j], tlist[i]) == 0) {
fprintf(stderr, "Duplicate target: %s\n", tlist[j]);
exit(EXIT_FAILURE);
}
}
}
}
int measuredLined = measureLine(read);
while(measuredLined) {
char str[measuredLined + 1];
readLine(read, str);
int expandline = expansionBound(str, maxRep);
char replacedLine[expandline + 1];
expand(str, replacedLine, tlist, rlist, t_r_list_len);
fprintf(write, "%s", replacedLine);
measuredLined = measureLine(read);
}
fclose(read);
fclose(write);
return EXIT_SUCCESS;
}