#120815 - Dracker - Tue Mar 06, 2007 6:48 am
I've written a function called infinite_fgets() to make file input a lot easier.
It seems I've let a bug sneak in somewhere, because it seems that sometimes a program can hang in infinite_fgets()'s loop and never get out. It also appears that this causes a memory leak, as when left in this state, the DS screens eventually blank.
I can't find any bugs in here - can you? I'm fairly certain the bug is in infinite_fgets() but it is possible that it's elsewhere.
Code blocks seem to screw up indentation, not to mention lack of highlighting. A much prettier version is available at http://pastebin.ca/383227
The .h file only includes a prototype.
_________________
--Drack
Onyx DS Lite, M3 Perfect Lite, Flashme V7, Flashed using SocketMe
Gamecube DOL-001 with Qoob Pro
Wii, not modded.
It seems I've let a bug sneak in somewhere, because it seems that sometimes a program can hang in infinite_fgets()'s loop and never get out. It also appears that this causes a memory leak, as when left in this state, the DS screens eventually blank.
I can't find any bugs in here - can you? I'm fairly certain the bug is in infinite_fgets() but it is possible that it's elsewhere.
Code blocks seem to screw up indentation, not to mention lack of highlighting. A much prettier version is available at http://pastebin.ca/383227
Code: |
#include <stdio.h>
#include <stdlib.h> #include "infinite_fgets.h" /* * Basically, this function works like fgets except it handles * an indefinite length input line. Reads until it hits \n or EOF. * Any line of text returned ends with \0. * * None of the lines returned contain/end with a newline! * * Returns NULL on error (malloc returns NULL, etc) and 0 if * EOF is reached before reading any input. If end-of-line * or some other non-EOF termination is reached before reading * any input, returns NULL. */ char* infinite_fgets(FILE * stream) { char *buffer; short int i; size_t size = sizeof(char); i = fgetc(stream); /* read in one character */ if ( i == EOF ) { /* EOF is -1, cannot store in char */ return 0; /* This retval must be handled by whatever called this */ } else if ( i == '\0' || /* Null */ i == NULL || /* Null */ i == '\255' || /* Null */ i == '\r' || /* carriage return */ i == '\n') return NULL; /* Line Feed */ buffer = malloc(size); /* size = sizeof(char) at this point */ if (buffer == NULL) { /* malloc error */ return NULL; } *buffer = (char)i; /* set first character */ for(;;) { i = fgetc(stream); /* read in one character */ if ( i == EOF || i == '\0' || /* Null */ i == NULL || /* Null */ i == '\255' || /* Null */ i == '\r' || /* carriage return */ i == '\n') break; /* Line Feed */ buffer = realloc(buffer,size+sizeof(char)); /* Buffer is now 1 char longer than its contents */ if (buffer == NULL) { /* realloc error */ return NULL; } /* END IF */ size+=sizeof(char); buffer[size-1] = (char)i; /* Add the character to the buffer * This assumes sizeof(char) == 1 * Fortunately, this is true for all * platforms I use this on */ } /* END FOR */ buffer = realloc(buffer,size+sizeof(char)); /* Make room for null termination */ if (buffer == NULL) { /* realloc error */ return NULL; } /* END IF */ buffer[size] = '\0'; /* Null-terminate the string */ return buffer; } /* END INFINITE_FGETS */ |
The .h file only includes a prototype.
_________________
--Drack
Onyx DS Lite, M3 Perfect Lite, Flashme V7, Flashed using SocketMe
Gamecube DOL-001 with Qoob Pro
Wii, not modded.