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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*
File IO
Functions:
char *FReadNextLineAlloc(FILE *fp, char comment_char)
char *FReadNextLineAllocCount(
FILE *fp,
char comment_char,
int *line_count
)
---
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "../include/fio.h"
/*
* Allocate memory while reading a line from file in chunk size
* of this many bytes:
*/
#define FREAD_ALLOC_CHUNK_SIZE 8
/*
* Returns an allocated string containing the entire
* line or NULL on error or EOF.
*
* If comment_char is '\0' then the next line is read regardless
* if it is a comment or not.
*
* Calling function must free() the returned pointer.
*/
char *FReadNextLineAlloc(FILE *fp, char comment_char)
{
return(FReadNextLineAllocCount(fp, comment_char, NULL));
}
char *FReadNextLineAllocCount(
FILE *fp,
char comment_char,
int *line_count
)
{
int i, m, n;
char *strptr;
if(fp == NULL)
return(NULL);
/* Is comment character specified? */
if(comment_char != '\0')
{
/* Comment character is specified. */
/* Read past spaces, newlines, and comments. */
i = fgetc(fp);
if(i == EOF)
return(NULL);
while((i == ' ') || (i == '\t') || (i == '\n') || (i == '\r') ||
(i == comment_char)
)
{
if(i == EOF)
return(NULL);
/* If newline, then increment line count. */
if((i == '\n') ||
(i == '\r')
)
{
if(line_count != NULL)
*line_count += 1;
}
/* If comment, then skip to next line. */
if(i == comment_char)
{
i = fgetc(fp);
while((i != '\n') && (i != '\r'))
{
if(i == EOF)
return(NULL);
i = fgetc(fp);
}
if(line_count != NULL)
*line_count += 1;
}
/* Get next character. */
i = fgetc(fp);
}
/* Begin adding characters to string. */
m = 0; /* mem size. */
n = 1; /* chars read. */
strptr = NULL;
while((i != '\n') && (i != '\r') && (i != '\0'))
{
/* Escape character? */
if(i == '\\')
{
/* Read next character. */
i = fgetc(fp);
/* Skip newlines internally. */
if((i == '\n') || (i == '\r'))
{
i = fgetc(fp);
/* Still counts as a line though! */
if(line_count != NULL)
*line_count += 1;
}
}
if(i == EOF)
break;
/* Allocate more memory as needed. */
if(m < n)
{
/* Allocate FREAD_ALLOC_CHUNK_SIZE more bytes. */
m += FREAD_ALLOC_CHUNK_SIZE;
strptr = (char *)realloc(strptr, m * sizeof(char));
if(strptr == NULL)
return(NULL);
}
strptr[n - 1] = (char)i;
/* Read next character from file. */
i = fgetc(fp);
n++; /* Increment characters read. */
}
/* Add newline and null terminate. */
m += 2; /* 2 more chars. */
strptr = (char *)realloc(strptr, m * sizeof(char));
if(strptr == NULL)
return(NULL);
strptr[n - 1] = '\n';
strptr[n] = '\0';
/* Increment line count. */
if(line_count != NULL)
*line_count += 1;
}
else
{
/* Comment character is not specified. */
i = fgetc(fp);
if(i == EOF)
return(NULL);
/* Begin adding characters to string. */
m = 0; /* Memory size. */
n = 1; /* Characters read. */
strptr = NULL; /* Return string. */
while((i != '\n') && (i != '\r') && (i != '\0'))
{
/* Escape character? */
if(i == '\\')
{
/* Read next character. */
i = fgetc(fp);
/* Skip newlines internally. */
if((i == '\n') || (i == '\r'))
{
i = fgetc(fp);
/* Still counts as a line though! */
if(line_count != NULL)
*line_count += 1;
}
}
if(i == EOF)
break;
/* Allocate more memory as needed. */
if(m < n)
{
/* Allocate FREAD_ALLOC_CHUNK_SIZE more bytes. */
m += FREAD_ALLOC_CHUNK_SIZE;
strptr = (char *)realloc(strptr, m * sizeof(char));
if(strptr == NULL)
return(NULL);
}
strptr[n - 1] = (char)i;
/* Read next character from file. */
i = fgetc(fp);
n++; /* Increment characters read. */
}
/* Add newline and null terminate. */
m += 2; /* 2 more chars. */
strptr = (char *)realloc(strptr, m * sizeof(char));
strptr[n - 1] = '\n';
strptr[n] = '\0';
/* Increment line count. */
if(line_count != NULL)
*line_count += 1;
}
return(strptr);
}
|