Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / FortuneParse.c
1 /* FortuneParse.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Routine FortuneParse
22  * parse fortunes from a fortune file
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: int FortuneParse(char *file, char ** fortune, int n);
28  * Read n fortunes from fortune file file, and put it into the
29  * string array fortune
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #include <sys/types.h>   /* primitive system data types */
36 #include <sys/stat.h>    /* file characteristics constants and functions */
37 #include <unistd.h>      /* unix standard library */
38 #include <stdio.h>       /* standard I/O library */
39 #include <stdlib.h>      /* C standard library */
40 #include <string.h>      /* C strings library */
41 #include <errno.h>       /* error definitions and routines */
42 #include <fcntl.h>       /* file control functions */
43
44 #include "macros.h"
45
46 /* Subroutines declaration */
47 extern void usage(void);
48
49 int FortuneParse(char *file, char **fortune, int n)
50 {
51 /* Variables definition */
52     FILE *fortunefile;
53     char line[80];
54     int i, len;
55     /* 
56      *  fortune file scanning, read string in memory 
57      */
58     fortunefile = fopen(file,"r");
59     if (fortunefile == NULL) {                       /* on open error exit */
60         printf("On file %s\n", file);
61         perror("Cannot open file");
62         exit(-1);
63     }
64     i = 0;
65     do {
66         if (!fgets(line, 80, fortunefile)) {
67             if (feof(fortunefile)) break;
68             perror("Read error");
69             exit(-1);
70         }
71         debug("i=%d, line=%s", i, line);
72         if (line[0]=='%') {
73             if (fortune[i]!=NULL) i++;
74             continue;
75         }
76         len = strlen(line) + 1;
77         if (fortune[i]==NULL) {
78             fortune[i] = malloc(len);
79             strncpy(fortune[i], line, len);
80         } else {
81             fortune[i] = realloc(fortune[i], strlen(fortune[i])+len+1);
82             strncat(fortune[i], line, len);
83         }
84     } while (i<n);
85     return i;
86 }