61 lines
1.1 KiB
C++
61 lines
1.1 KiB
C++
#include "stdafx.h"
|
|
|
|
char *trimwhitespace(char *str)
|
|
{
|
|
char *end;
|
|
|
|
// Trim leading space
|
|
while (isspace((unsigned char)*str)) str++;
|
|
|
|
if (*str == 0) // All spaces?
|
|
return str;
|
|
|
|
// Trim trailing space
|
|
end = str + strlen(str) - 1;
|
|
while (end > str && isspace((unsigned char)*end)) end--;
|
|
|
|
// Write new null terminator
|
|
*(end + 1) = 0;
|
|
|
|
return str;
|
|
}
|
|
|
|
char *readLine(FILE *file) {
|
|
|
|
if (file == NULL) {
|
|
printf("\nError: file pointer is null.");
|
|
exit(1);
|
|
}
|
|
|
|
int maximumLineLength = 128;
|
|
char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength);
|
|
|
|
if (lineBuffer == NULL) {
|
|
printf("\nError allocating memory for line buffer.");
|
|
exit(1);
|
|
}
|
|
|
|
char ch = getc(file);
|
|
int count = 0;
|
|
|
|
while ((ch != '\n') && (ch != EOF)) {
|
|
if (count == maximumLineLength) {
|
|
maximumLineLength += 128;
|
|
lineBuffer = (char *)realloc(lineBuffer, maximumLineLength);
|
|
if (lineBuffer == NULL) {
|
|
printf("\nError reallocating space for line buffer.");
|
|
exit(1);
|
|
}
|
|
}
|
|
lineBuffer[count] = ch;
|
|
count++;
|
|
|
|
ch = getc(file);
|
|
}
|
|
|
|
lineBuffer[count] = '\0';
|
|
if (count > 0)
|
|
return lineBuffer;
|
|
else
|
|
return NULL;
|
|
} |