I have to parse a custom configuration file with this structure:
# Begin with core containers.
ID = 0 # STX container
Name = "" # No name is necessary for a STX container.
Flags = {STX}
Attributes = {Status=0}
So I've written the following code in order to remove those comments (starting with #) and the inline comments. I don't know if I'm doing it the right way, it just seems (for me, not too much experience) a stupid way of doing it with many while loops.
void NextToken() {
// Let's begin taking comments away!
FILE* Source = fopen("defs.dat", "r");
char* Buffer = calloc(MAXBUFLEN, sizeof(char));
while (fgets(Buffer, MAXBUFLEN, Source)) {
// If line starts with '#' it is a comment, so it must be ignored. If it's a newine it just has to be skipped.
if (Buffer[0] == 35 || Buffer[0] == '\n')
continue;
// Now we have to delete inline comments
int Pos = 0;
while (Pos < strlen(Buffer)) {
if(Buffer[Pos] == '#') {
Pos++;
while(Buffer[Pos] != '\n') {
Pos++;
continue;
}
continue;
}
//Cool! now just let's print the output.
printf("%c", Buffer[Pos++]);
}
}
}
The max size of the buffer is 256 characters.