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
|
#include "ed/inc/tok.h"
#include<stdio.h>
#include<stdlib.h>
static char *get_line (FILE * fp)
{
size_t i = 0;
size_t size = 1;
char *s = NULL;
int c;
while ((c = fgetc (fp)) != EOF)
{
if (i == size - 1)
{
void *tmp = realloc (s, size * 2);
if (tmp != NULL)
{
size *= 2;
s = tmp;
}
else
{
free (s), s = NULL;
break; /* sortie d'urgence */
}
}
if (c != '\n')
{
s[i] = c;
i++;
}
else
{
s[i] = 0;
break;
}
}
return s;
}
static int parse (int argc, char const **argv)
{
int ret = 0;
long val = strtol(argv[1], NULL, 10);
printf ("'%s' %ld\n", argv[0], val);
return ret;
}
static void process_line (char const *line)
{
sTOK *ptok = TOK_create (line, "=", NULL);
if (ptok != NULL)
{
sTOK_INFO info;
TOK_get (ptok, &info);
parse (info.argc, info.argv);
TOK_delete (ptok), ptok = NULL;
}
}
int main ()
{
#define fname "data.txt"
FILE *fp = fopen (fname, "r");
if (fp != NULL)
{
char *line;
while ((line = get_line (fp)) != NULL)
{
process_line (line);
free (line), line = NULL;
}
fclose (fp), fp = NULL;
}
else
{
perror (fname);
}
return 0;
} |
Partager