X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FFortuneParse.c;fp=sources%2FFortuneParse.c;h=da1f68d61346d0ffa3f368d2e91bbce0c34ec44f;hp=0000000000000000000000000000000000000000;hb=b1ead1b930038e4f7cd6ee7f737d7ee4699a068c;hpb=d12bc3e1e4b3ee762036d1c226c3b2ba1a720fb9 diff --git a/sources/FortuneParse.c b/sources/FortuneParse.c new file mode 100644 index 0000000..da1f68d --- /dev/null +++ b/sources/FortuneParse.c @@ -0,0 +1,88 @@ +/* FortuneParse.c + * + * Copyright (C) 2002 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Routine FortuneParse + * parse fortunes from a fortune file + * + * Author: Simone Piccardi + * Aug. 2002 + * + * Usage: int FortuneParse(char *file, char ** fortune, int n); + * Read n fortunes from fortune file file, and put it into the + * string array fortune + * + * $Id: FortuneParse.c,v 1.5 2003/05/02 09:55:13 piccardi Exp $ + * + ****************************************************************/ +/* + * Include needed headers + */ +#include /* predefined types */ +#include /* */ +#include /* include unix standard library */ +#include /* include standard I/O library */ +#include /* standard library */ +#include /* ANSI C standard string */ +#include /* error definitions */ +#include /* */ + +#include "macros.h" + +/* Subroutines declaration */ +extern void usage(void); + +int FortuneParse(char *file, char **fortune, int n) +{ +/* Variables definition */ + FILE *fortunefile; + char line[80]; + int i, len; + /* + * fortune file scanning, read string in memory + */ + fortunefile = fopen(file,"r"); + if (fortunefile == NULL) { /* on open error exit */ + printf("On file %s\n", file); + perror("Cannot open file"); + exit(-1); + } + i = 0; + do { + if (!fgets(line, 80, fortunefile)) { + if (feof(fortunefile)) break; + perror("Read error"); + exit(-1); + } + debug("i=%d, line=%s", i, line); + if (line[0]=='%') { + if (fortune[i]!=NULL) i++; + continue; + } + len = strlen(line) + 1; + if (fortune[i]==NULL) { + fortune[i] = malloc(len); + strncpy(fortune[i], line, len); + } else { + fortune[i] = realloc(fortune[i], strlen(fortune[i])+len+1); + strncat(fortune[i], line, len); + } + } while (i