aboutsummaryrefslogtreecommitdiff
path: root/src/storycode.c
blob: 4edc39ef79b040844229538cdfd429e67aee9cc5 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * storycode.c
 *
 * Colloquium - A tiny presentation program
 *
 * Copyright (c) 2012 Thomas White <taw@bitwiz.org.uk>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "storycode.h"
#include "presentation.h"


struct _scblocklist
{
	int n_blocks;
	char **blocks;
	int max_blocks;
};


struct _scblocklistiterator
{
	int pos;
};


static int allocate_blocks(SCBlockList *bl)
{
	char **blocks_new;

	blocks_new = realloc(bl->blocks, bl->max_blocks*sizeof(char *));
	if ( blocks_new == NULL ) {
		return 1;
	}

	bl->blocks = blocks_new;

	return 0;
}


SCBlockList *sc_block_list_new()
{
	SCBlockList *bl;

	bl = calloc(1, sizeof(SCBlockList));
	if ( bl == NULL ) return NULL;

	bl->n_blocks = 0;
	bl->max_blocks = 64;
	bl->blocks = NULL;
	if ( allocate_blocks(bl) ) {
		free(bl);
		return NULL;
	}

	return bl;
}



void sc_block_list_free(SCBlockList *bl)
{
	int i;

	for ( i=0; i<bl->n_blocks; i++ ) {
		free(bl->blocks[i]);
	}
	free(bl->blocks);
	free(bl);
}


char *sc_block_list_first(SCBlockList *bl, SCBlockListIterator **piter)
{
	SCBlockListIterator *iter;

	if ( bl->n_blocks == 0 ) return NULL;

	iter = calloc(1, sizeof(SCBlockListIterator));
	if ( iter == NULL ) return NULL;

	iter->pos = 0;
	*piter = iter;

	return bl->blocks[0];
}


char *sc_block_list_next(SCBlockList *bl, SCBlockListIterator *iter)
{
	iter->pos++;
	if ( iter->pos == bl->n_blocks ) {
		free(iter);
		return NULL;
	}

	return bl->blocks[iter->pos];
}


static int sc_block_list_add(SCBlockList *bl, char *text)
{
	if ( bl->n_blocks == bl->max_blocks ) {
		bl->max_blocks += 64;
		if ( allocate_blocks(bl) ) return 1;
	}

	bl->blocks[bl->n_blocks] = text;
	bl->n_blocks++;
	return 0;
}


SCBlockList *sc_find_blocks(const char *sc, const char *blockname)
{
	SCBlockList *bl;
	const char *pos;
	char label[1024];

	bl = sc_block_list_new();
	if ( bl == NULL ) return NULL;

	if ( strlen(blockname) > 1021 ) {
		fprintf(stderr, "Block name '%s' too long.\n", blockname);
		return NULL;
	}

	strcat(label, "\\");
	strcat(label, blockname);
	strcat(label, "{");

	pos = sc;
	do {

		pos = strstr(pos, label);

		if ( pos != NULL ) {

			 int i;
			 int bct = 1;
			 int found = 0;
			 int ml = strlen(pos);

			pos += strlen(label);

			for ( i=0; i<ml; i++ ) {
				if ( pos[i] == '{' ) {
					bct++;
				} else if ( pos[i] == '}' ) {
					bct--;
				}
				if ( bct == 0 ) {
					found = 1;
					break;
				}
			}

			if ( (!found) || (bct != 0) ) {
				fprintf(stderr, "Parse error while looking for"
				                " block '%s'\n", blockname);
				sc_block_list_free(bl);
				return NULL;
			}

			if ( sc_block_list_add(bl, strndup(pos, i)) ) {
				fprintf(stderr, "Failed to add block.\n");
				sc_block_list_free(bl);
				return NULL;
			}

			pos += i+1;
			printf("Remaining text '%s'\n", pos);

		}

	} while ( pos != NULL );

	return bl;
}


/* Unpack level 2 StoryCode (content + subframes) into frames */
struct frame *sc_unpack(const char *sc)
{
	struct frame *fr;

	fr = calloc(1, sizeof(struct frame));
	if ( fr == NULL ) return NULL;



	return fr;
}