-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.c
162 lines (136 loc) · 4.42 KB
/
replace.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
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "resourcestack.h"
#define MAX_KEY_SIZE 100
struct bufferedStream{
FILE* file;
int bufferSize;
char *buffer;
int bytesAvailable;
};
struct keyPair{
char key[MAX_KEY_SIZE];
char replaceString[MAX_KEY_SIZE];
};
void closeFile(void* f){//JUST to make function pointer compatible with resourse stack
fclose((FILE *)f);
}
void fillBuffer(struct bufferedStream*);
int bufferContains(struct bufferedStream*, char*);
char getNextByte(struct bufferedStream*);
int main(){
INTIALIZE_RESOURCE_MANAGER;
//Getting keys
FILE* keyFile = fopen("keys.txt", "r");
PUSH_RESOURCE_MESSAGE(keyFile, closeFile, "failed to open keys file");
int keyCount;
fscanf(keyFile, "%d\n", &keyCount);
struct keyPair *keyPairArray = malloc(sizeof(struct keyPair) * keyCount);
PUSH_RESOURCE_MESSAGE(keyPairArray, free, "failed to initialize keyPairArray");
for (int i = 0; i< keyCount; i++){
struct keyPair *currentKeyPair = keyPairArray + i;
int skipReplace = 0;
char c;
c = getc(keyFile);
while(c=='\n' || c==' ' || c=='\r' || c=='\t'){
c = getc(keyFile);
}
for (int j = 0;; c = getc(keyFile)){
if (j == MAX_RESOURCE_COUNT){
R_EXIT_FAILURE_MESSAGE("key too long");
}
if (c== ' '){
currentKeyPair->key[j] = '\0';
break;
}else if (c=='\r' || c=='\n' || c == EOF){
currentKeyPair->key[j] = '\0';
currentKeyPair->replaceString[0] = '\0';
skipReplace = 1;
break;
}else{
currentKeyPair->key[j] = c;
j++;
}
}
if (skipReplace) continue;
c = getc(keyFile);
while(c=='\n' || c==' ' || c=='\r' || c=='\t'){
c = getc(keyFile);
}
for(int j = 0; ; c= getc(keyFile)){
if (j == MAX_RESOURCE_COUNT){
R_EXIT_FAILURE_MESSAGE("replace string too long");
}
if (c=='\r' || c=='\n' || c == EOF){
currentKeyPair->replaceString[j] = '\0';
break;
}else{
currentKeyPair->replaceString[j] = c;
j ++;
}
}
}
FREE_RESOURCE(keyFile);
for (int i = 0; i<keyCount; i++){
printf("'%s': '%s'\n", keyPairArray[i].key, keyPairArray[i].replaceString);
}
//Performing replacement
struct bufferedStream* bs = malloc(sizeof(struct bufferedStream));
PUSH_RESOURCE_MESSAGE(bs, free, "unable to allocate byteStream");
bs-> file = fopen("input", "r");
PUSH_RESOURCE_MESSAGE(bs->file, closeFile, "unable to open input file");
bs-> bufferSize = MAX_KEY_SIZE + 1;
bs-> buffer = malloc(sizeof(char) * bs->bufferSize);
PUSH_RESOURCE_MESSAGE(bs->buffer, free, "unable to allocate bytestream buffer")
bs->bytesAvailable = 0;
fillBuffer(bs);
FILE *output = fopen("output", "w");
PUSH_RESOURCE_MESSAGE(output, closeFile, "failed to open output file");
while(bs->bytesAvailable){
int replaceMade = 0;
for (int i = 0; i<keyCount; i++){
if (bufferContains(bs, keyPairArray[i].key)){
fprintf(output, keyPairArray[i].replaceString);
replaceMade = 1;
break;
}
}
if (replaceMade) continue;
char c = getNextByte(bs);
fprintf(output, "%c", c);
}
R_EXIT_SUCCESS
}
void fillBuffer(struct bufferedStream* bs){
while(!feof(bs->file) && (bs->bytesAvailable < bs->bufferSize)){
char c = getc(bs->file);
if (c == EOF) break;
bs->buffer[bs->bytesAvailable] = c;
bs->bytesAvailable++;
}
}
int bufferContains(struct bufferedStream* bs, char* str){
int i = 0;
while(1){
if (str[i] == '\0') break;
if (bs->bytesAvailable <= i) return 0;
if (bs->buffer[i] != str[i]) return 0;
i++;
}
for (int j = i; j < bs->bytesAvailable; j++){
bs->buffer[j-i] = bs->buffer[j];
}
bs->bytesAvailable -= i;
fillBuffer(bs);
return 1;
}
char getNextByte(struct bufferedStream*bs){
char c = bs->buffer[0];
for (int i = 0; i<bs->bytesAvailable-1; i++){
bs->buffer[i] = bs->buffer[i+1];
}
bs->bytesAvailable--;
fillBuffer(bs);
return c;
}