-
Notifications
You must be signed in to change notification settings - Fork 7
/
babel_ifiction_functions.c
179 lines (153 loc) · 3.65 KB
/
babel_ifiction_functions.c
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
/* babel_ifiction_functions.c babel top-level operations for ifiction
* (c) 2006 By L. Ross Raszewski
*
* This code is freely usable for all purposes.
*
* This work is licensed under the Creative Commons Attribution 4.0 License.
* To view a copy of this license, visit
* https://creativecommons.org/licenses/by/4.0/ or send a letter to
* Creative Commons,
* PO Box 1866,
* Mountain View, CA 94042, USA.
*
* This file depends upon babel.c (for rv), babel.h, misc.c and ifiction.c
*/
#include "babel.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#ifndef THREE_LETTER_EXTENSIONS
#define IFICTION_EXT ".iFiction"
#else
#define IFICTION_EXT ".ifi"
#endif
void *my_malloc(int, char *);
struct IFiction_Info
{
char ifid[256];
int wmode;
};
static void write_story_to_disk(struct XMLTag *xtg, void *ctx)
{
char *b, *ep;
char *begin, *end;
char buffer[TREATY_MINIMUM_EXTENT];
int32 l, j;
if (ctx) { }
if (strcmp(xtg->tag,"story")==0)
{
begin=xtg->begin;
end=xtg->end;
l=end-begin+1;
b=(char *)my_malloc(l,"XML buffer");
memcpy(b,begin,l-1);
b[l]=0;
j=ifiction_get_IFID(b,buffer,TREATY_MINIMUM_EXTENT);
if (!j)
{
fprintf(stderr,"No IFID found for this story\n");
free(b);
return;
}
ep=strtok(buffer,",");
while(ep)
{
char buf2[256];
FILE *f;
sprintf(buf2,"%s%s",ep,IFICTION_EXT);
f=fopen(buf2,"w");
if (!f ||
fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!-- Metadata extracted by Babel -->"
"<ifindex version=\"1.0\" xmlns=\"http://babel.ifarchive.org/protocol/iFiction/\">\n"
" <story>",
f)==EOF ||
fputs(b,f)==EOF ||
fputs("/<story>\n</ifindex>\n",f)==EOF
)
{
fprintf(stderr,"Error writing to file %s\n",buf2);
} else
printf("Extracted %s\n",buf2);
if (f) fclose(f);
ep=strtok(NULL,",");
}
free(b);
}
}
void babel_ifiction_ifid(char *md)
{
char output[TREATY_MINIMUM_EXTENT];
int i;
char *ep;
i=ifiction_get_IFID(md,output,TREATY_MINIMUM_EXTENT);
if (!i)
{
fprintf(stderr,"Error: No IFIDs found in iFiction file\n");
return;
}
ep=strtok(output,",");
while(ep)
{
printf("IFID: %s\n",ep);
ep=strtok(NULL,",");
}
}
static int haserrors, haswarnings;
static void examine_tag(struct XMLTag *xtg, void *ctx)
{
struct IFiction_Info *xti=(struct IFiction_Info *)ctx;
if (strcmp("ifid",xtg->tag)==0 && strcmp(xti->ifid,"UNKNOWN")==0)
{
memcpy(xti->ifid,xtg->begin,xtg->end-xtg->begin);
xti->ifid[xtg->end-xtg->begin]=0;
}
}
/* The ctx argument is -1 to ignore errors and warnings; 0 to only show
errors; 1 to show errors and warnings. */
static void verify_eh(char *e, void *ctx)
{
if (*((int *)ctx) < 0) return;
if (strncmp(e,"Warning",7)) {
/* Error */
haserrors=1;
fprintf(stderr, "%s\n",e);
}
else {
/* Warning */
haswarnings=1;
if (*((int *)ctx))
fprintf(stderr, "%s\n",e);
}
}
void babel_ifiction_fish(char *md)
{
int i=-1;
ifiction_parse(md,write_story_to_disk,NULL,verify_eh,&i);
}
void deep_ifiction_verify(char *md, int f)
{
struct IFiction_Info ii;
int errmode=0;
ii.wmode=0;
haserrors = haswarnings = 0;
strcpy(ii.ifid,"UNKNOWN");
ifiction_parse(md,examine_tag,&ii,verify_eh,&errmode);
if (f&& !haserrors) printf("Verified %s\n",ii.ifid);
}
void babel_ifiction_verify(char *md)
{
deep_ifiction_verify(md,1);
}
void babel_ifiction_lint(char *md)
{
struct IFiction_Info ii;
int errmode=show_warnings;
ii.wmode=1;
haserrors = haswarnings = 0;
strcpy(ii.ifid,"UNKNOWN");
ifiction_parse(md,examine_tag,&ii,verify_eh,&errmode);
if (!(haserrors || haswarnings)) printf("%s conforms to iFiction style guidelines\n",ii.ifid);
else if (!haserrors) printf("%s does not conform to guidelines\n",ii.ifid);
}