-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlformat.c
138 lines (132 loc) · 3.91 KB
/
htmlformat.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
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//needed for me to understand my own code
#define TRUE 1
#define FALSE 0
//checks if a file is part of the blacklist
int checkBlackList(char *inputStr){
char *blacklist[] = {".",".."};
int blacklistLen = 2;
int i=0;
while (blacklistLen != i){
//check if the string is = than the blacklist string
if (strcmp(inputStr,blacklist[i])==0){return FALSE;}
i++;}
return TRUE;
}
/*check if the file extension is a image
* fix later use the blacklist function for now */
int checkValidImage(char *inputStr,char **checkValid,int checkValidLen){
int line = 0;
for (;line<=checkValidLen;line+=1){
if(strstr(inputStr,checkValid[line])!=NULL){
return TRUE;
}
}
return FALSE;
}
void checkNull(void* pointer){if(pointer==NULL){fprintf(stderr,"out of mem");exit(1);}}
char** readFile(char *fileStr){
FILE *confFile;
if ((confFile = fopen(fileStr,"r")) == NULL){fprintf(stderr,"file \"%s\" not found",fileStr);exit(1);}
int i = 0;
int arrayLen = 10;
char *array = (char *) malloc(arrayLen*sizeof(char *));
checkNull(array);
int c;
while ((c = fgetc(confFile)) != EOF){
if (i == arrayLen -2){
arrayLen = arrayLen*2;
array = (char *) realloc(array,arrayLen*sizeof(char *));
checkNull(array);
}
array[i] = c;
i++;
}
array[i]='\0'; //end str
fclose(confFile);
arrayLen = 5;
i=0;//1
char **arrayOut = (char **) malloc(arrayLen*sizeof(char **));
char *temp;
temp = strtok(array,"\n");
while(temp) { // breaks if temp == '\0' so if we exit the loop
arrayOut[i] = strcat(temp, "\0");
i += 1;
temp = strtok(NULL,"\n");
if(i==arrayLen){
arrayLen = arrayLen*2;
arrayOut = (char **) realloc(arrayOut,arrayLen*sizeof(char **));
checkNull(arrayOut);
checkNull(arrayOut[i]);
}
}
free(array);
return arrayOut;
}
int main(int argc, char *argv[]) {
//errors out if the user does not input a file, uses relative path
if (argc == 2) { fprintf(stderr,"needs <CONFIG FILE> <DIR>");return 1;}
char **format;
format = (char**) readFile(argv[1]);
int validCheckLen = 2;
char **validCheck= (char**) malloc(validCheckLen*sizeof(char **));
DIR *targetDir;
int fileNameHtmlLen = 10;
int line = 0;
int loopStartLine = 0;
char *fileNameHtml = (char *) malloc(fileNameHtmlLen*sizeof(char *));
char *temp = (char *) malloc(fileNameHtmlLen*sizeof(char *));
struct dirent *dir;
targetDir = opendir(argv[2]);
for(int i=0;strstr(format[line],"START") == NULL;line+=1){
if(i+1==validCheckLen){
validCheckLen = validCheckLen * 2;
validCheck = (char **) realloc(validCheck,validCheckLen*sizeof(char **));
}
validCheck[i] = format[line];
i+=1;
}
line +=1;
validCheckLen = line-1;
for(;strstr(format[line],"<FILE>") == NULL;line += 1){
printf("%s\n",format[line]);
}
line += 1;
loopStartLine = line;
if (targetDir) {
while ((dir = readdir(targetDir)) != NULL) {
line=loopStartLine;
if (checkValidImage(dir->d_name,validCheck,validCheckLen) == TRUE){
for(loopStartLine = line;strstr(format[line],"</FILE>")==NULL;line+=1){
if (strlen(format[line]) + strlen(dir->d_name) >= fileNameHtmlLen) {
fileNameHtmlLen = strlen(format[line]) + strlen(dir->d_name)+1;
fileNameHtml = (char *) realloc(fileNameHtml,fileNameHtmlLen*sizeof(char*));
temp = (char *) realloc(temp,fileNameHtmlLen*sizeof(char*));
checkNull(fileNameHtml);
checkNull(temp);
}
strcpy(temp,format[line]);
strcpy(fileNameHtml,strtok(temp,"FILE"));
strcat(fileNameHtml,dir->d_name);
if((strcpy(temp,strtok(NULL,"FILE"))) != NULL){
strcat(fileNameHtml,temp);
}
printf("%s\n",fileNameHtml);
}
}
}
closedir(targetDir);
free(validCheck);
free(fileNameHtml);
free(temp);
line +=1;
}else{fprintf(stderr,"dir %s not found",argv[2]);exit(1);}
for(;strstr(format[line],"END")== NULL;line+=1){
printf("%s\n",format[line]);
}
free(format);
return(0);
}