Parses the config file
Added parse_config and determine_config_entry_value functions and entry types definitions in order to parse the config filestable
parent
44ce9ae07b
commit
f318271a28
62
src/rhosts.c
62
src/rhosts.c
|
|
@ -42,22 +42,56 @@ int parse_config(struct entry **entries){
|
||||||
configfile = fopen(CONFIGFILE, "r+");
|
configfile = fopen(CONFIGFILE, "r+");
|
||||||
if (configfile == NULL){return 1;}
|
if (configfile == NULL){return 1;}
|
||||||
*entries = malloc(sizeof(struct entry));
|
*entries = malloc(sizeof(struct entry));
|
||||||
|
if (entries == NULL){return 1;}
|
||||||
entries[0]->entrytype = 0;
|
entries[0]->entrytype = 0;
|
||||||
char c='\0';
|
char c='\0';
|
||||||
char *buff = malloc(sizeof(char));
|
char buff[500];
|
||||||
short int valtyp = 1;
|
buff[0]='\0';
|
||||||
int *j = &entries[0]->entrytype; // Used to make easier to read
|
short int valtyp = CONTENTTYPE_BLANK;
|
||||||
if (entries == NULL){return 1;}
|
int *j = NULL;
|
||||||
|
j = &(*entries)[0].entrytype; // Used to make easier to read
|
||||||
|
|
||||||
do{
|
do{
|
||||||
c = getc(configfile);
|
c = fgetc(configfile);
|
||||||
buff = realloc(buff, sizeof(buff) + sizeof(char));
|
// Detect if a comment
|
||||||
buff[sizeof(buff)-1] = c;
|
if (strncmp(buff, "#",(long unsigned int)1) == 0 && \
|
||||||
if (c == ':' && valtyp == 1){
|
valtyp == CONTENTTYPE_BLANK){
|
||||||
*j += 1;
|
while (c != '\n' && c != EOF){c =fgetc(configfile);}
|
||||||
|
}
|
||||||
|
// Detect end of value type string
|
||||||
|
if (c == '=' && valtyp == CONTENTTYPE_BLANK){
|
||||||
|
valtyp = determine_config_entry_value(buff);
|
||||||
|
if (valtyp == CONTENTTYPE_ERROR){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
buff[0]='\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect end of entry
|
||||||
}while (c != '\0');
|
else if ((c == '\n' || c == EOF) \
|
||||||
|
&& valtyp != CONTENTTYPE_BLANK){
|
||||||
|
(*entries)[0].entrytype++;
|
||||||
|
*entries = (struct entry *)reallocarray(*entries,\
|
||||||
|
(*j + 1), sizeof(struct entry));
|
||||||
|
if (*entries == NULL){return 1;}
|
||||||
|
j = &(*entries)[0].entrytype;
|
||||||
|
(*entries)[*j].entrytype=valtyp;
|
||||||
|
strcpy((*entries)[*j].entry,buff);
|
||||||
|
buff[0] = '\0';
|
||||||
|
valtyp = CONTENTTYPE_BLANK;
|
||||||
|
}
|
||||||
|
else if (c == '\n' || c == EOF){ // Clear blank lines
|
||||||
|
buff[0] = '\0';
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
strncat(buff, &c, 1);
|
||||||
|
}
|
||||||
|
}while (c != EOF);
|
||||||
|
// int k = 1;
|
||||||
|
// for (k=1;k<*j;k++){
|
||||||
|
// printf("%d - %s\n",(*entries)[k].entrytype,\
|
||||||
|
// (*entries)[k].entry);
|
||||||
|
// }
|
||||||
|
|
||||||
rc = fclose(configfile);
|
rc = fclose(configfile);
|
||||||
if (rc != 0){return 1;}
|
if (rc != 0){return 1;}
|
||||||
|
|
@ -80,3 +114,9 @@ int openfile(FILE **file, char *mode, char *location){
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
short int determine_config_entry_value(char *buff){
|
||||||
|
if (strncmp(buff,"#", 1) == 0){return CONTENTTYPE_COMMENT;}
|
||||||
|
else if (strcmp(buff,"site") == 0){return CONTENTTYPE_SITE;}
|
||||||
|
else if (strcmp(buff,"download") == 0){return CONTENTTYPE_DOWNLOAD;}
|
||||||
|
else {return CONTENTTYPE_ERROR;}
|
||||||
|
}
|
||||||
|
|
|
||||||
11
src/rhosts.h
11
src/rhosts.h
|
|
@ -20,16 +20,21 @@
|
||||||
#else
|
#else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// entry types
|
||||||
#define STATIC 0
|
#define CONTENTTYPE_ERROR 5
|
||||||
|
#define CONTENTTYPE_BLANK 0
|
||||||
|
#define CONTENTTYPE_SITE 1
|
||||||
|
#define CONTENTTYPE_DOWNLOAD 2
|
||||||
|
#define CONTENTTYPE_COMMENT 3
|
||||||
|
|
||||||
struct entry{
|
struct entry{
|
||||||
int entrytype;
|
int entrytype;
|
||||||
char *entry;
|
char entry[500];
|
||||||
};
|
};
|
||||||
|
|
||||||
int parse_config(struct entry **entries);
|
int parse_config(struct entry **entries);
|
||||||
|
|
||||||
int openfile(FILE **file, char *mode, char *location);
|
int openfile(FILE **file, char *mode, char *location);
|
||||||
int closefile(FILE **file, char *location);
|
int closefile(FILE **file, char *location);
|
||||||
|
short int determine_config_entry_value(char *buff);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue