Initial import.
[cave.git] / src / file.c
blobc28c7d307f7a6c3573d51adc6f9d46f9c62502d7
1 /* CAVE (Character Animation Viewer for Everyone)
2 Copyright (C) 2001-2002 Ben Kibbey <bjk@arbornet.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #ifdef HAVE_ZLIB_H
29 #include <zlib.h>
30 #endif
32 #include "common.h"
34 static long framedelim(const char *line)
36 if (regexec(&regex, line, 1, 0, 0) == 0) {
37 if (regexdelay)
38 return atol(line);
39 else
40 return 0;
43 return -1;
46 SCENE *readfile(char *filename)
48 #ifdef HAVE_LIBZ
49 gzFile *fp;
50 #else
51 FILE *fp;
52 #endif
53 unsigned rows = 0;
54 long n, last = -1;
55 char *line;
56 int septop = 0;
57 SCENE *scene, *snode, *sprev;
58 FRAME *fnode;
60 line = Calloc(1, LINE_MAX);
62 #ifdef HAVE_LIBZ
63 if ((fp = gzopen(filename, "rb")) == NULL) {
64 #else
65 if ((fp = fopen(filename, "r")) == NULL) {
66 #endif
67 free(line);
68 return NULL;
71 #ifdef HAVE_LIBZ
72 if ((line = gzgets(fp, line, LINE_MAX)) == NULL) {
73 #else
74 if ((line = fgets(line, LINE_MAX, fp)) == NULL) {
75 #endif
76 free(line);
77 return NULL;
80 line[strlen(line) - 1] = 0;
81 septop = (framedelim(line) != -1) ? 1 : 0;
83 #ifdef HAVE_LIBZ
84 gzrewind(fp);
85 #else
86 rewind(fp);
87 #endif
89 message(NULL, NULL, LOADING, sepfile(filename));
91 scene = Calloc(1, sizeof(SCENE));
92 snode = scene;
93 snode->frame = Calloc(1, sizeof(FRAME));
94 fnode = snode->frame;
95 frames = delaylen = 0;
97 #ifdef HAVE_LIBZ
98 while ((line = gzgets(fp, line, LINE_MAX)) != NULL) {
99 #else
100 while ((line = fgets(line, LINE_MAX, fp)) != NULL) {
101 #endif
102 int len = strlen(line) - 1;
104 line[len] = 0;
105 n = framedelim(line);
107 if (n != -1) {
108 if (septop) {
109 if (last == -1) {
110 last = n;
111 continue;
114 else
115 last = n;
117 if (regexdelay) {
118 delaylen += last;
119 snode->timepos = delaylen;
120 snode->delay = last;
123 last = n;
124 rows = 0;
125 snode->frameid = ++frames;
126 fnode->next = NULL;
127 snode->next = Calloc(1, sizeof(SCENE));
128 sprev = snode;
129 snode = snode->next;
130 snode->prev = sprev;
131 snode->frame = Calloc(1, sizeof(FRAME));
132 fnode = snode->frame;
134 continue;
137 fnode->line = strdup(line);
138 fnode->next = Calloc(1, sizeof(FRAME));
139 fnode = fnode->next;
140 snode->rows = ++rows;
142 if (maxx < len)
143 maxx = len;
145 if (maxy < rows)
146 maxy = rows;
149 if (septop) {
150 if (regexdelay) {
151 delaylen += last;
152 snode->timepos = delaylen;
153 snode->delay = last;
156 last = n;
157 rows = 0;
158 snode->frameid = ++frames;
159 fnode->next = NULL;
160 snode->next = Calloc(1, sizeof(SCENE));
161 sprev = snode;
162 snode = snode->next;
163 snode->prev = sprev;
164 snode->frame = Calloc(1, sizeof(FRAME));
165 fnode = snode->frame;
168 snode->next = scene;
169 scene->prev = snode->prev;
171 #ifdef HAVE_LIBZ
172 gzclose(fp);
173 #else
174 fclose(fp);
175 #endif
177 free(line);
179 message(NULL, NULL, NULL);
180 return scene;