00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #include "common.h"
00024
00025 #include <ctype.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028
00029 #include "misc.h"
00030
00031 #include "buffer.h"
00032 #include "xmalloc.h"
00033
00034
00035
00036 int getNextValue(int *beg, int *end, unsigned int from, const char *str)
00037 {
00038
00039 int crlf = 0;
00040
00041
00042 if (str == NULL || from >= strlen(str)) return -1;
00043
00044 *beg = from;
00045 while (isspace((int) *(str+*beg))) (*beg)++;
00046
00047 if (*beg > 0)
00048 if(*(str+*beg-1) == '\n')
00049 crlf++;
00050
00051
00052 if (str[*beg] == '\0') return -1;
00053
00054 *end = (*beg) + 1;
00055 while (!isspace((int) *(str+*end)) && *(str+*end) != '\0') (*end)++;
00056
00057
00058 (*end)--;
00059 return crlf;
00060 }
00061
00062
00063
00064 #define SIZE_INC 256
00065
00066
00067 char* getLine(FILE *file)
00068 {
00069 struct Buffer line = bufInit((char*)pd_malloc(SIZE_INC), SIZE_INC, 0);
00070 int cur_chr;
00071
00072 while ((cur_chr = getc(file)) != EOF)
00073 {
00074 *(line.data + line.pos) = (char) cur_chr;
00075
00076 if (*(line.data + line.pos) == '\n')
00077 {
00078 *(line.data + line.pos) = '\0';
00079 return line.data;
00080 }
00081
00082
00083 if (++(line.pos) == line.len)
00084 {
00085 char* tmp = (char*)pd_realloc(line.data, line.pos + SIZE_INC);
00086
00087 if (tmp != NULL)
00088 {
00089 line.data = tmp;
00090 line.pos += SIZE_INC;
00091 }
00092 else
00093 {
00094 pd_free(line.data);
00095 return NULL;
00096 }
00097 }
00098 }
00099
00100 *(line.data + line.pos) = '\0';
00101 return line.data;
00102 }
00103