-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphext.c
More file actions
415 lines (374 loc) · 9.45 KB
/
phext.c
File metadata and controls
415 lines (374 loc) · 9.45 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "phext.h"
#include <stdio.h>
/* phext - internal module functions */
/* ------------------------------------------------------------------------ */
int advance_coordinate(int* spot)
{
int test = *spot;
if (test < PHEXT_MAXIMUM_INDEX)
{
*spot = test + 1;
return 1;
}
return 0;
}
void check_region_end(char* p, char** end, int* stage)
{
char next = *p;
if (*stage == 1)
{
*end = p - 1;
*stage = 2;
}
}
/* phext public functions */
/* ------------------------------------------------------------------------ */
char* phext_fetch_text(char *phext, phext_coordinate coord)
{
phext_coordinate walker;
phext_initialize_coordinate(&walker);
char* p = phext;
char* start = 0;
char* end = 0;
int stage = 0;
while (*p != 0)
{
char next = *p;
if (next == PHEXT_SCROLL_BREAK)
{
check_region_end(p, &end, &stage);
phext_scroll_break(&walker);
++p;
continue;
}
if (next == PHEXT_SECTION_BREAK)
{
check_region_end(p, &end, &stage);
phext_section_break(&walker);
++p;
continue;
}
if (next == PHEXT_CHAPTER_BREAK)
{
check_region_end(p, &end, &stage);
phext_chapter_break(&walker);
++p;
continue;
}
if (next == PHEXT_BOOK_BREAK)
{
check_region_end(p, &end, &stage);
phext_book_break(&walker);
++p;
continue;
}
if (next == PHEXT_VOLUME_BREAK)
{
check_region_end(p, &end, &stage);
phext_volume_break(&walker);
++p;
continue;
}
if (next == PHEXT_COLLECTION_BREAK)
{
check_region_end(p, &end, &stage);
phext_collection_break(&walker);
++p;
continue;
}
if (next == PHEXT_SERIES_BREAK)
{
check_region_end(p, &end, &stage);
phext_series_break(&walker);
++p;
continue;
}
if (next == PHEXT_SHELF_BREAK)
{
check_region_end(p, &end, &stage);
phext_shelf_break(&walker);
++p;
continue;
}
if (next == PHEXT_LIBRARY_BREAK)
{
check_region_end(p, &end, &stage);
phext_library_break(&walker);
++p;
continue;
}
if (stage == 0 &&
coord.Z.library == walker.Z.library &&
coord.Z.shelf == walker.Z.shelf &&
coord.Z.series == walker.Z.series &&
coord.Y.collection == walker.Y.collection &&
coord.Y.volume == walker.Y.volume &&
coord.Y.book == walker.Y.book &&
coord.X.chapter == walker.X.chapter &&
coord.X.section == walker.X.section &&
coord.X.scroll == walker.X.scroll)
{
start = p;
stage = 1;
}
++p;
if (stage == 1 && *p == 0)
{
end = p - 1;
stage = 2;
}
}
int length = end - start + 1;
int buffer_length = length + 1;
if (start != 0 && end != 0 && length > 0)
{
if (length > 0)
{
char* buffer = (char*) malloc(buffer_length);
memcpy(buffer, start, length);
buffer[length] = 0;
return buffer;
}
}
return 0;
}
/* ------------------------------------------------------------------------ */
void phext_initialize_coordinate(phext_coordinate *coord)
{
if (coord == 0)
{
return;
}
coord->Z.library = 1;
coord->Z.shelf = 1;
coord->Z.series = 1;
coord->Y.collection = 1;
coord->Y.volume = 1;
coord->Y.book = 1;
coord->X.chapter = 1;
coord->X.section = 1;
coord->X.scroll = 1;
}
/* ------------------------------------------------------------------------ */
void set_coordinate_by_state(int state, phext_coordinate* coord, int value)
{
switch (state)
{
case 8:
coord->X.scroll = value;
break;
case 7:
coord->X.section = value;
break;
case 6:
coord->X.chapter = value;
break;
case 5:
coord->Y.book = value;
break;
case 4:
coord->Y.volume = value;
break;
case 3:
coord->Y.collection = value;
break;
case 2:
coord->Z.series = value;
break;
case 1:
coord->Z.shelf = value;
break;
case 0:
default:
coord->Z.library = value;
break;
}
}
/* ------------------------------------------------------------------------ */
void phext_set_coordinate(phext_coordinate *coord, char *address)
{
char* a = address;
int i = 0;
int state = 0;
char buffer[8];
char next;
int value;
while (*a != 0)
{
next = *a;
if (next == '.' || next == '/')
{
buffer[i] = 0;
value = atoi(buffer);
set_coordinate_by_state(state, coord, value);
memset(buffer, 0, 8);
i = 0;
++state;
++a;
continue;
}
buffer[i] = next;
++i;
if (i > 7)
{
break;
}
++a;
}
buffer[i] = 0;
value = atoi(buffer);
set_coordinate_by_state(state, coord, value);
memset(buffer, 0, 8);
}
/* ------------------------------------------------------------------------ */
char *phext_get_address(phext_coordinate coord)
{
char* buffer = (char*) malloc(PHEXT_BUFFER_SIZE);
sprintf(buffer, "%d.%d.%d/%d.%d.%d/%d.%d.%d",
coord.Z.library, coord.Z.shelf, coord.Z.series,
coord.Y.collection, coord.Y.volume, coord.Y.book,
coord.X.chapter, coord.X.section, coord.X.scroll);
return buffer;
}
/* ------------------------------------------------------------------------ */
int phext_validate_coordinate(phext_coordinate* coord)
{
if (coord == 0)
{
return PHEXT_INVALID_COORDINATE;
}
if (coord->Z.library < PHEXT_MINIMUM_INDEX ||
coord->Z.shelf < PHEXT_MINIMUM_INDEX ||
coord->Z.series < PHEXT_MINIMUM_INDEX ||
coord->Y.collection < PHEXT_MINIMUM_INDEX ||
coord->Y.volume < PHEXT_MINIMUM_INDEX ||
coord->Y.book < PHEXT_MINIMUM_INDEX ||
coord->X.chapter < PHEXT_MINIMUM_INDEX ||
coord->X.section < PHEXT_MINIMUM_INDEX ||
coord->X.scroll < PHEXT_MINIMUM_INDEX)
{
return PHEXT_INVALID_COORDINATE;
}
return PHEXT_NO_ERROR;
}
/* ------------------------------------------------------------------------ */
int phext_scroll_break(phext_coordinate *coord)
{
if (coord == 0)
{
return 0;
}
if (advance_coordinate(&(coord->X.scroll)))
{
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_section_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->X.section)))
{
coord->X.scroll = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_chapter_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->X.chapter)))
{
coord->X.scroll = 1;
coord->X.section = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_book_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->Y.book)))
{
coord->X.scroll = 1;
coord->X.section = 1;
coord->X.chapter = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_volume_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->Y.volume)))
{
coord->X.scroll = 1;
coord->X.section = 1;
coord->X.chapter = 1;
coord->Y.book = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_collection_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->Y.collection)))
{
coord->X.scroll = 1;
coord->X.section = 1;
coord->X.chapter = 1;
coord->Y.book = 1;
coord->Y.volume = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_series_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->Z.series)))
{
coord->X.scroll = 1;
coord->X.section = 1;
coord->X.chapter = 1;
coord->Y.book = 1;
coord->Y.volume = 1;
coord->Y.collection = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_shelf_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->Z.shelf)))
{
coord->X.scroll = 1;
coord->X.section = 1;
coord->X.chapter = 1;
coord->Y.book = 1;
coord->Y.volume = 1;
coord->Y.collection = 1;
coord->Z.series = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}
/* ------------------------------------------------------------------------ */
int phext_library_break(phext_coordinate *coord)
{
if (advance_coordinate(&(coord->Z.library)))
{
coord->X.scroll = 1;
coord->X.section = 1;
coord->X.chapter = 1;
coord->Y.book = 1;
coord->Y.volume = 1;
coord->Y.collection = 1;
coord->Z.series = 1;
coord->Z.shelf = 1;
return PHEXT_SUCCESS;
}
return PHEXT_FAILURE;
}