11 int chdir(const char *);
\r
12 char *getcwd(char *, int);
\r
17 void deep_ifiction_verify(char *md, int f);
\r
18 void * my_malloc(int32, char *);
\r
19 char *blorb_chunk_for_name(char *name);
\r
20 #ifndef THREE_LETTER_EXTENSIONS
\r
21 static char *ext_table[] = { "zcode", ".zblorb",
\r
26 static char *ext_table[] = { "zcode", ".zlb",
\r
32 char *blorb_ext_for_name(char *fmt)
\r
35 for(i=0;ext_table[i];i+=2)
\r
36 if (strcmp(ext_table[i],fmt)==0) return ext_table[i+1];
\r
37 #ifndef THREE_LETTER_EXTENSIONS
\r
44 char *deep_complete_ifiction(char *fn, char *ifid, char *format)
\r
52 if (!f) { fprintf(stderr,"Error: Can not open file %s\n",fn);
\r
55 fseek(f,0,SEEK_END);
\r
57 fseek(f,0,SEEK_SET);
\r
58 md=(char *) my_malloc(i+1,"Metadata buffer");
\r
61 id=strstr(md,"</ifindex>");
\r
66 /* Find the identification chunk */
\r
69 bp=strstr(md,"<identification>");
\r
72 idb=(char *)my_malloc(TREATY_MINIMUM_EXTENT+128,"ident buffer");
\r
73 sprintf(idb,"<format>%s</format>\n", format);
\r
78 ep=strstr(bp,"</identification>");
\r
79 idb=(char *)my_malloc(TREATY_MINIMUM_EXTENT+128+(ep-bp),"ident buffer");
\r
80 for(ii=16;bp+ii<ep;ii++)
\r
83 for(ep+=18;*ep;ep++)
\r
86 bp=strstr(idb,"<format>");
\r
88 if (memcmp(bp+8,format,strlen(format)))
\r
89 fprintf(stderr,"Error: Format in sparse .iFiction does not match story\n");
\r
94 /* Insert the new ifids */
\r
97 char bfr[TREATY_MINIMUM_EXTENT];
\r
98 sprintf(bfr,"<ifid>%s</ifid>",idp);
\r
99 if (!strstr(idb,bfr)) { strcat(idb,bfr); strcat(idb,"\n"); }
\r
100 idp=strtok(NULL,",");
\r
104 idp=(char *) my_malloc(strlen(md)+strlen(idb)+64, "Output metadata");
\r
105 /* printf("%d bytes for metadata\n",strlen(md)+strlen(idb)+64);*/
\r
106 id=strstr(md,"<story>");
\r
110 strcat(idp,"<story>\n <identification>\n");
\r
113 strcat(idp," </identification>\n");
\r
117 deep_ifiction_verify(md, 0);
\r
121 void write_int(int32 i, FILE *f)
\r
124 bf[0]=(((unsigned) i) >> 24) & 0xFF;
\r
125 bf[1]=(((unsigned) i) >> 16) & 0xFF;
\r
126 bf[2]=(((unsigned) i) >> 8) & 0xFF;
\r
127 bf[3]=(((unsigned) i)) & 0xFF;
\r
130 static void _babel_multi_blorb(char *outfile, char **args, char *todir , int argc)
\r
132 int32 total, storyl, coverl, i;
\r
133 char buffer[TREATY_MINIMUM_EXTENT+10];
\r
134 char b2[TREATY_MINIMUM_EXTENT];
\r
137 char *cover, *md, *cvrf, *ep;
\r
140 if (argc!=2 && argc !=3)
\r
142 fprintf(stderr,"Invalid usage\n");
\r
145 if (!babel_init(args[0]))
\r
147 fprintf(stderr,"Error: Could not determine the format of file %s\n",args[0]);
\r
150 if (babel_treaty(GET_STORY_FILE_IFID_SEL,buffer,TREATY_MINIMUM_EXTENT)<=0 ||
\r
151 babel_treaty(GET_FORMAT_NAME_SEL,b2,TREATY_MINIMUM_EXTENT)<0
\r
154 fprintf(stderr,"Error: Could not deduce an IFID for file %s\n",args[0]);
\r
157 if (babel_get_length() != babel_get_story_length())
\r
159 fprintf(stderr,"Warning: Story file will be extacted from container before blorbing\n");
\r
161 /* printf("Completing ifiction\n");*/
\r
162 md=deep_complete_ifiction(args[1],buffer,b2);
\r
163 /* printf("Ifiction is %d bytes long\n",strlen(md));*/
\r
164 ep=strchr(buffer,',');
\r
167 strcpy(buffer,outfile);
\r
168 strcat(buffer,blorb_ext_for_name(b2));
\r
171 f=fopen(buffer,"wb");
\r
175 fprintf(stderr,"Error: Error writing to file %s\n",buffer);
\r
178 storyl=babel_get_story_length();
\r
179 total=storyl + (storyl%2) + 36;
\r
180 if (md) total+=8+strlen(md)+strlen(md)%2;
\r
183 c=fopen(args[2],"rb");
\r
186 fseek(c,0,SEEK_END);
\r
190 cover=(char *) my_malloc(coverl+2,"Cover art buffer");
\r
191 fseek(c,0,SEEK_SET);
\r
192 fread(cover,1,coverl,c);
\r
193 if (memcmp(cover+1,"PNG",3)==0) cvrf="PNG ";
\r
195 total += 32+coverl + (coverl%2);
\r
202 /* printf("Writing header\n;");*/
\r
203 fwrite("FORM",1,4,f);
\r
204 write_int(total,f);
\r
205 /* printf("Writing index\n;");*/
\r
206 fwrite("IFRSRIdx",1,8,f);
\r
207 write_int(argc==3 ? 28:16,f);
\r
208 write_int(argc==3 ? 2:1,f);
\r
209 /* printf("Writing story\n;");*/
\r
210 fwrite("Exec", 1,4,f);
\r
212 write_int(argc==3 ? 48:36,f);
\r
215 /* printf("Writing image\n;"); */
\r
216 fwrite("Pict", 1,4,f);
\r
218 write_int(56+storyl+(storyl%2),f);
\r
220 /* printf("Invoking chunk for name %s\n",b2); */
\r
221 fwrite(blorb_chunk_for_name(b2),1,4,f);
\r
222 write_int(storyl,f);
\r
223 /* printf("Writing story data\n"); */
\r
224 fwrite(babel_get_story_file(),1,storyl,f);
\r
225 if (storyl%2) fwrite("\0",1,1,f);
\r
228 /* printf("Writing cover data header %s\n",cvrf); */
\r
229 fwrite(cvrf,1,4,f);
\r
230 /* printf("Writing cover data size %d\n",coverl); */
\r
231 write_int(coverl,f);
\r
232 /* printf("Writing cover data\n"); */
\r
233 fwrite(cover,1,coverl,f);
\r
234 if (coverl%2) fwrite("\0",1,1,f);
\r
235 /* printf("Done with cover\n");*/
\r
237 /* printf("Writing frontispiece\n;");*/
\r
238 fwrite("Fspc\0\0\0\004\0\0\0\001",1,12,f);
\r
242 /* printf("Writing metadata\n;");*/
\r
243 fwrite("IFmd",1,4,f);
\r
244 write_int(strlen(md),f);
\r
245 fwrite(md,1,strlen(md),f);
\r
247 fwrite("\0",1,1,f);
\r
252 printf("Created %s\n",buffer);
\r
255 void babel_multi_complete(char **args, char *todir, int argc)
\r
257 char buffer[TREATY_MINIMUM_EXTENT+10];
\r
258 char b2[TREATY_MINIMUM_EXTENT];
\r
264 fprintf(stderr,"Invalid usage\n");
\r
267 if (!babel_init(args[0]))
\r
269 fprintf(stderr,"Error: Could not determine the format of file %s\n",args[0]);
\r
272 if (babel_treaty(GET_STORY_FILE_IFID_SEL,buffer,TREATY_MINIMUM_EXTENT)<=0
\r
273 || babel_treaty(GET_FORMAT_NAME_SEL,b2,TREATY_MINIMUM_EXTENT)<0)
\r
275 fprintf(stderr,"Error: Could not deduce an IFID for file %s\n",args[0]);
\r
278 md=deep_complete_ifiction(args[1],buffer, b2);
\r
280 ep=strchr(buffer,',');
\r
282 strcat(buffer,".iFiction");
\r
285 f=fopen(buffer,"w");
\r
287 if (!f || !fputs(md,f))
\r
289 fprintf(stderr,"Error: Error writing to file %s\n",buffer);
\r
294 printf("Created %s\n",buffer);
\r
296 void babel_multi_blorb(char **args, char *todir , int argc)
\r
298 _babel_multi_blorb(NULL,args,todir,argc);
\r
300 void babel_multi_blorb1(char **args, char *todir , int argc)
\r
304 buf=(char *)my_malloc(strlen(args[0])+1,"blorb name buffer");
\r
305 strcpy(buf,args[0]);
\r
306 bb=strrchr(buf,'.');
\r
308 _babel_multi_blorb(buf,args,todir,argc);
\r