-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtocMaker.c
344 lines (287 loc) · 7.41 KB
/
tocMaker.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#define TOC_LIMITER "<!-- [toc] -->"
//Declare Functions
int isHeadline(char* line);
char* getName(char* headline);
char* getLink(char* headline);
char* createTopic(char* line);
void trim(char* str);
void replace(char* str, char oldChar, char newChar);
void removeNeedle(char* str, char* needle);
void removeNeedles(char* str, char** needle, int numNeedles);
void addLink(char* str);
bool hasItem(char* str);
void cleanupLinks();
void toLower(char* str);
void appendBuf(char* str);
void cleanupBuffer();
bool readLine(FILE *file, char* buffer, int maxSize);
bool isEmptyLine(char* str);
void strRemove(char* str, int start, int n);
//declare variables for a link-list
char** links;
int linksize;
int linksEnd;
//declare buffer for output file
char** outputBuffer;
int buffSize;
int buffEnd;
char* needles[] = {"(", ")", "[", "]", "{", "}", "<", ">",
"!", "\"", "§", "$", "%", "&", "/", "=",
"?", "´", "`", "\\", "€", "@", "*", "+",
"~", "'", ",", ";", ".", ":", "^", "°",
"|"};
int main(int argc, char** argv) {
links = malloc(sizeof(char*));
linksize = 1;
linksEnd = 0;
outputBuffer = malloc(sizeof(char*));
buffSize = 1;
buffEnd = 0;
char cwd[1024];
char* filePath = NULL;
if (getcwd(cwd, sizeof(cwd)) != NULL && argc > 1) {
//check if absolute or relative path of file is given
if (argv[1][0] != '/') {
filePath = malloc(strlen(cwd) + strlen(argv[1]) + 2);
sprintf(filePath, "%s/%s%c", cwd, argv[1], '\0');
} else {
filePath = malloc(strlen(argv[1]) + 1);
strcpy(filePath, argv[1]);
}
}
//check if filepath is ok
if (filePath != NULL) {
FILE *file;
file = fopen(filePath, "r");
if (file) {
//read file
char lineBuffer[1024];
bool skip = false;
while (readLine(file, lineBuffer, 1024)) {
//skip table of content
if (skip) {
skip = strcmp(lineBuffer, TOC_LIMITER);
} else {
if (strcmp(lineBuffer, TOC_LIMITER)) {
appendBuf(lineBuffer);
} else {
skip = true;
}
}
}
//write file and create table of content
file = freopen(filePath, "w", file);
//skip initial headline
int i = 0;
while (i < buffEnd && (strlen(outputBuffer[i]) < 2
|| outputBuffer[i][0] != '#' || outputBuffer[i][1] != '#')) {
fprintf(file, "%s\n", outputBuffer[i]);
i++;
}
if (i >= buffEnd) {
printf("Error: could not create table of content, due to missing ##$headline.\n");
printf("Please note, that at least one headline with two # should be present to create a table of content.\n");
return 1;
}
//create toc:
//check if an empty line is present and insert if not
if (!isEmptyLine(outputBuffer[i - 1]))
fprintf(file, "\n");
fprintf(file, "%s\n", TOC_LIMITER);
fprintf(file, "## Table of Contents\n\n");
for (int index = i; index < buffEnd; index++) {
char* topic = createTopic(outputBuffer[index]);
if (topic) {
fprintf(file, "%s\n", topic);
free(topic);
}
}
fprintf(file, "\n%s\n", TOC_LIMITER);
//write the rest to the file
while (i < buffEnd) {
fprintf(file, "%s\n", outputBuffer[i]);
i++;
}
fclose(file);
} else {
printf("Error: could not open file\n");
}
} else {
printf("Error: %s\n", argc < 2?"need a README file":"filepath is too long");
}
if (filePath)
free(filePath);
cleanupLinks();
return 0;
}
bool readLine(FILE *file, char* buffer, int maxSize) {
int i = 0;
int curChar;
while ((curChar = fgetc(file)) != EOF && curChar != '\n' && i < (maxSize - 1)) {
if (curChar >= 0 && curChar <= 255)
buffer[i] = (char) curChar;
i++;
}
buffer[i] = '\0';
return curChar != EOF || i > 0;
}
int isHeadline(char* line) {
int hashtags = 0;
hashtags = strspn(line, "#");
return hashtags;
}
char* getName(char* headline) {
char* name = malloc(strlen(headline) + 1);
strcpy(name, headline);
removeNeedle(name, "#");
trim(name);
//remove images from name
char* ptr;
while ((ptr = strstr(name, "!["))) {
char* end = strstr(ptr, ")");
if (end)
strRemove(ptr, 0, (int) (end - ptr) + 1);
}
//remove links
ptr = name;
while ((ptr = strstr(name, "["))) {
if (strstr(name, "]") == strstr(name, "](") && strstr(name, "]")) {
char* end = strstr(ptr, ")");
if (end)
strRemove(ptr, 0, (int) (end - ptr) + 1);
}
}
return name;
}
char* getLink(char* headline) {
char *tmpName = getName(headline);
removeNeedles(tmpName, needles, 33);
replace(tmpName, ' ', '-');
//allocate space (+2 for # and \0, +3 for possible extensions like _1)
char* link = malloc(strlen(tmpName) + 5);
link[0] = '#';
strcpy(link + 1, tmpName);
free(tmpName);
toLower(link);
//check if link-name is already in use and modify if necessary
int linksEnd = strlen(link);
int counter = 1;
char buf[5];
while (hasItem(link)) {
sprintf(buf, "-%d%c", counter++, '\0');
strcpy(link + linksEnd, buf);
}
addLink(link);
return link;
}
char* createTopic(char* line) {
int numHashtag = isHeadline(line);
if (numHashtag) {
int numSpaces = 2 * numHashtag -1;
char *name = getName(line);
char *link = getLink(line);
char *topic = malloc(strlen(name) + strlen(link) + numSpaces + 8);
for(int i = 0; i < numSpaces; i++)
topic[i] = ' ';
sprintf(topic + numSpaces, "* [%s](%s)%c", name, link, '\0');
free(name);
return topic;
} else
return NULL;
}
void trim(char* str) {
//trim front
int linksEnd = strlen(str);
if (strchr(str, ' ') == str) {
int numSpaces = strspn(str, " ");
for (int i = numSpaces; i <= linksEnd; i++)
str[i - numSpaces] = str[i];
}
linksEnd = strlen(str);
//trim back
while (linksEnd > 0 && str[linksEnd - 1] == ' ') {
str[linksEnd - 1] = str[linksEnd];
linksEnd--;
}
}
void replace(char* str, char oldChar, char newChar) {
char* ptr = strchr(str, oldChar);
while (ptr) {
*ptr = newChar;
ptr = strchr(str, oldChar);
}
}
void removeNeedle(char* str, char* needle) {
char *ptr;
while ((ptr = strstr(str, needle))) {
strRemove(ptr, 0, strlen(needle));
}
}
void removeNeedles(char* str, char** needles, int numNeedles) {
for (int i = 0; i < numNeedles; i++)
removeNeedle(str, needles[i]);
}
void strRemove(char* str, int start, int n) {
if (start < strlen(str)) {
str += start;
for (int i = n; i <= strlen(str); i++) {
str[i - n] = str[i];
}
}
}
void addLink(char* str) {
//enlarge array if necessary
if (linksEnd == linksize) {
char** newBuffer = realloc(links, 2 * sizeof(char*) * linksize);
linksize *= 2;
links = newBuffer;
}
//add item to buffer
links[linksEnd++] = str;
}
bool hasItem(char* str) {
for (int i = 0; i < linksEnd; i++)
if (!strcmp(links[i], str))
return true;
return false;
}
void toLower(char* str) {
for (;*str ; str++)
if (*str > 64 && *str < 91)
*str += 32;
//TODO: should implement umlauts here
}
void cleanupLinks() {
for (int i = 0; i < linksEnd; i++)
free(links[i]);
free(links);
}
void appendBuf(char* str) {
//enlarge array if necessary
if (buffEnd == buffSize) {
char** newBuffer = realloc(outputBuffer, 2 * sizeof(char*) * buffSize);
buffSize *= 2;
outputBuffer = newBuffer;
}
//add item to buffer
char* strCpy = malloc(strlen(str) + 1);
strcpy(strCpy, str);
outputBuffer[buffEnd++] = strCpy;
}
void cleanupBuffer() {
for (int i = 0; i < buffEnd; i++)
free(outputBuffer[i]);
free(outputBuffer);
}
bool isEmptyLine(char* str) {
for ( ; *str; str++) {
if ((unsigned char) *str > 32)
return false;
}
return true;
}