disable the unrecognized nls and x flags
[AROS-Contrib.git] / bgui / CAD / cad_file.c
blob37e35fe839c77e39ad64bc8029cdb15aab33ce73
1 /*
2 * @(#) $Header$
4 * Change AutoDoc program
5 * cad_file.c
7 * (C) Copyright 1998 Manuel Lemos.
8 * (C) Copyright 1996-1997 Ian J. Einman.
9 * (C) Copyright 1993-1996 Jaba Development.
10 * (C) Copyright 1993-1996 Jan van den Baard.
11 * All Rights Reserved.
13 * $Log$
14 * Revision 42.0 2000/05/09 22:10:51 mlemos
15 * Bumped to revision 42.0 before handing BGUI to AROS team
17 * Revision 41.11 2000/05/09 20:23:13 mlemos
18 * Bumped to revision 41.11
20 * Revision 1.2 2000/05/09 19:55:37 mlemos
21 * Merged with the branch Manuel_Lemos_fixes.
23 * Revision 1.1.2.5 1999/08/21 19:26:19 mlemos
24 * Integrated Anton Rolls changes to fix the command line argument template
25 * handling and XML format support.
27 * Revision 1.1.2.4 1999/05/31 02:52:47 mlemos
28 * Integrated Anton Rolls changes to generate documentation files in
29 * AmigaGuide and HTML formats.
31 * Revision 1.1.2.3 1998/11/28 14:43:45 mlemos
32 * Changed LoadCadDesc() so that it was no longer inserting tildes (~) and
33 * stripping whitespace from the files as they were loaded.
35 * Revision 1.1.2.2 1998/10/01 04:41:35 mlemos
36 * Made the file handling functions work when the program main window is not
37 * opened and return a success value.
39 * Revision 1.1.2.1 1998/09/19 01:50:16 mlemos
40 * Ian sources.
46 #include "cad.h"
49 * Import data.
51 extern Object *LV_List, *WD_Main, *ST_Base;
52 extern struct Window *MainWindow;
53 extern UBYTE FileName[ 256 ], BaseName[ 64 ], GenName[ 256 ];
54 extern ULONG NumDesc;
57 * Save the CAD file.
59 BOOL SaveCadFile(BOOL window_opened)
61 BPTR file;
62 CADHEADER ch;
63 CADDESC cd;
64 CADNODE *cn;
66 SetIoErr(0);
67 if(window_opened)
70 * Lock the window.
72 WindowBusy( WD_Main );
76 * Open the file.
78 if ( file = Open( FileName, MODE_NEWFILE )) {
80 * Prepare header.
82 ch.ch_Ident = ID_CADF;
83 ch.ch_Version = CADF_VERSION;
84 ch.ch_NumDesc = NumDesc;
85 strcpy( ( char * )&ch.ch_BaseName[ 0 ], BaseName );
88 * Write the header.
90 if ( Write( file, ( UBYTE * )&ch, sizeof( CADHEADER )) == sizeof( CADHEADER )) {
92 * Now write the descriptions one by one.
94 cn = ( CADNODE * )FirstEntry( LV_List );
96 do {
98 * Prepare CADDESC.
100 strcpy( &cd.cd_Name[ 0 ], &cn->cn_Name[ 0 ] );
101 cd.cd_Length = cn->cn_DataLength;
104 * Write the CADDESC.
106 if ( Write( file, ( UBYTE * )&cd, sizeof( CADDESC )) == sizeof( CADDESC )) {
108 * And the corresponding data.
110 if ( cn->cn_DataLength ) {
111 if ( Write( file, cn->cn_Data, cn->cn_DataLength ) != cn->cn_DataLength ) {
112 if(window_opened)
113 DosRequest( "OK", ISEQ_C "Error writing\n\n`%s'", FileName );
114 break;
117 } else {
118 if(window_opened)
119 DosRequest( "OK", ISEQ_C "Error writing\n\n`%s'", FileName );
120 break;
122 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
123 } else {
124 if(window_opened)
125 DosRequest( "OK", ISEQ_C "Error writing\n\n`%s'", FileName );
129 * Close the file.
131 Close( file );
133 * Delete the file on error.
136 LONG error;
138 if ((error=IoErr()))
140 DeleteFile( FileName );
141 SetIoErr(error);
145 } else {
146 if(window_opened)
147 DosRequest( "OK", ISEQ_C "Unable to open\n\n`%s'", FileName );
150 if(window_opened)
153 * Unlock the window.
155 WindowReady( WD_Main );
157 return((BOOL)(IoErr()==0));
161 * Load a single CADDESC.
163 STATIC CADNODE *LoadCadDesc( BPTR file )
165 CADDESC cd;
166 CADNODE *cn;
167 UBYTE *buffer;
170 * Read the CADDESC.
172 if ( Read( file, ( UBYTE * )&cd, sizeof( CADDESC )) == sizeof( CADDESC )) {
174 * Allocate node.
176 if ( cn = ( CADNODE * )AllocVec( sizeof( CADNODE ), MEMF_CLEAR )) {
178 * Setup node name.
180 strcpy(cn->cn_Name, cd.cd_Name);
183 * Allocate data.
185 if (cd.cd_Length)
187 if (buffer = (UBYTE *)AllocVec(cd.cd_Length + 1, MEMF_CLEAR))
189 if (Read(file, buffer, cd.cd_Length) == cd.cd_Length)
191 cn->cn_DataLength = strlen(buffer);
192 cn->cn_Data = AllocVec(cn->cn_DataLength, 0);
193 strncpy(cn->cn_Data, buffer, cn->cn_DataLength);
194 FreeVec(buffer);
195 return cn;
197 FreeVec(cn->cn_Data);
198 } else
199 SetIoErr(ERROR_NO_FREE_STORE);
200 } else
201 return(cn);
202 FreeVec(cn);
203 } else
204 SetIoErr(ERROR_NO_FREE_STORE);
206 return( NULL );
210 * Load a CADLIST file.
212 BOOL LoadCadFile(BOOL window_opened)
214 BPTR file;
215 CADHEADER ch;
216 CADNODE *cn;
217 BOOL err = FALSE;
218 ULONG i;
220 if(window_opened)
223 * Lock the window.
225 WindowBusy( WD_Main );
228 BaseName[ 0 ] = '\0';
229 NumDesc = 0;
230 ClearList( NULL, LV_List );
233 * Open the file.
235 if ( file = Open( FileName, MODE_OLDFILE )) {
237 * Load header.
239 if ( Read( file, ( UBYTE * )&ch, sizeof( CADHEADER )) == sizeof( CADHEADER )) {
241 * Check identifier and version.
243 if ( ch.ch_Ident == ID_CADF ) {
244 if ( ch.ch_Version <= CADF_VERSION ) {
246 * Copy the base name.
248 strcpy( BaseName, &ch.ch_BaseName[ 0 ] );
251 * Read all CADDESCs.
253 for ( i = 0; i < ch.ch_NumDesc; i++ ) {
255 * Read CADDESC.
257 if ( cn = LoadCadDesc( file )) {
259 * Add it to the list.
261 AddEntry( NULL, LV_List, cn, LVAP_TAIL );
262 NumDesc++;
263 } else {
264 err = TRUE;
265 if(window_opened)
266 DosRequest( "OK", ISEQ_C "Error reading\n\n`%s'", FileName );
267 break;
270 } else {
271 err = TRUE;
272 MyRequest( "OK", ISEQ_C "Unknown file version." );
274 } else {
275 err = TRUE;
276 MyRequest( "OK", ISEQ_C "Unknown file type." );
278 } else {
279 err = TRUE;
280 if(window_opened)
281 DosRequest( "OK", ISEQ_C "Error reading\n\n`%s'", FileName );
283 Close( file );
284 } else {
285 err = TRUE;
286 if(window_opened)
287 DosRequest( "OK", ISEQ_C "Unable to open\n\n`%s'", FileName );
290 if ( err ) {
292 * Errors clear all data.
294 BaseName[ 0 ] = '\0';
295 NumDesc = 0;
296 ClearList( NULL, LV_List );
300 * Setup GUI.
302 SetGadgetAttrs(( struct Gadget * )ST_Base, MainWindow, NULL, STRINGA_TextVal, BaseName, TAG_END );
303 SetGadgetAttrs(( struct Gadget * )LV_List, MainWindow, NULL, LISTV_Select, LISTV_Select_First, TAG_END );
305 if(window_opened)
307 RefreshList( MainWindow, LV_List );
310 * Unlock the window.
312 WindowReady( WD_Main );
314 return((BOOL)!err);
318 * Generate autodoc.
320 BOOL Generate(BOOL window_opened, ULONG outputFormat)
322 BOOL result;
324 if(window_opened)
327 * Lock the window.
329 WindowBusy( WD_Main );
332 switch( outputFormat )
334 case MATCH_DOC:
335 result = GenerateDoc(window_opened);
336 break;
338 case MATCH_GUIDE:
339 result = GenerateGuide(window_opened);
340 break;
342 case MATCH_HTML:
343 result = GenerateHTML(window_opened);
344 break;
346 case MATCH_XML:
347 result = GenerateXML(window_opened);
348 break;
350 default:
351 result = FALSE; /* unsupported output format */
354 if(window_opened)
357 * Unlock the window.
359 WindowReady( WD_Main );
362 return( result );
365 BOOL GenerateDoc(BOOL window_opened)
367 CADNODE *cn;
368 BPTR file;
371 * Open the output file.
373 if ( file = Open( GenName, MODE_NEWFILE )) {
374 /* set secondary error code to 0 - Anton
375 (Who cares if the file doesn't exist yet? need to check this, though)
377 SetIoErr(0);
380 * Write table of contents.
382 MyFPrintf( file, "TABLE OF CONTENTS\n\n" );
384 cn = ( CADNODE * )FirstEntry( LV_List );
385 do {
386 MyFPrintf( file, "%s/%s\n", BaseName, &cn->cn_Name[ 0 ] );
387 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
389 FPutC( file, '\n' );
392 * Write entries...
394 cn = ( CADNODE * )FirstEntry( LV_List );
395 do {
397 * Header.
399 MyFPrintf( file, "%lc%s/%s\n\n", 12, BaseName, &cn->cn_Name[ 0 ] );
402 * Data.
404 FWrite( file, cn->cn_Data, cn->cn_DataLength, 1 );
407 * Add an empty line.
409 FPutC( file, '\n' );
412 * Break on an error.
414 if ( IoErr()) {
415 if(window_opened)
416 DosRequest( "OK", ISEQ_C "Error during generation of\n\n`%s'", GenName );
417 break;
419 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
422 * Close the file.
424 Close( file );
426 LONG error;
428 * Delete the file upon error.
430 if ((error=IoErr()))
432 DeleteFile( GenName );
433 SetIoErr(error);
438 return((BOOL)(IoErr()==0));
443 * Generate HTML autodoc. - Anton.
445 BOOL GenerateHTML(BOOL window_opened)
447 CADNODE *cn;
448 BPTR file;
451 * Open the output file.
453 if ( file = Open( GenName, MODE_NEWFILE )) {
454 /* set secondary error code to 0
455 (Who cares if the file doesn't exist yet? need to check this, though) - Anton
457 SetIoErr(0);
459 /* Write html header stuff - Anton */
460 MyFPrintf( file, "<html>\n <head><title>");
461 MyFPrintf(file, BaseName); /* put BaseName into html document title */
462 MyFPrintf( file, "</title>\n </head>\n<body>\n");
465 * Write table of contents.
467 MyFPrintf( file, "TABLE OF CONTENTS<br>\n<br>\n" );
469 cn = ( CADNODE * )FirstEntry( LV_List );
470 do {
471 /* name the table of contents entry and link to the actual doc entry */
472 MyFPrintf( file, "<a name=toc%s><a href=#%s>", cn->cn_Name, cn->cn_Name);
473 MyFPrintf( file, "%s/%s</a></a><br>\n", BaseName, cn->cn_Name );
474 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
477 * Write entries...
479 cn = ( CADNODE * )FirstEntry( LV_List );
480 do {
481 /* html stuff before the actual node data. */
482 MyFPrintf( file, "<a name=%s>", cn->cn_Name); /* name the link target here - Anton */
483 MyFPrintf( file, "\n<hr>\n" ); /* horizontal rule and newline - Anton */
484 /* link back to the table of contents - Anton */
485 MyFPrintf( file, "\n<br>\n<a href=#toc%s>top</a> ", cn->cn_Name );
486 MyFPrintf( file, "%s/%s<br>\n", BaseName, cn->cn_Name );
487 MyFPrintf( file, "<pre>\n" ); /* preformatted text tag - Anton.*/
489 /* Data. */
490 FWrite( file, cn->cn_Data, cn->cn_DataLength, 1 );
492 /* to do: search through the data, find "SEE ALSO" and match anything after this
493 with any node names. If they match, make a link to it.
496 /* end preformatted text tag, end <a name> tag, newline and horizontal rule- Anton */
497 MyFPrintf( file, "</pre>\n</a><br>\n" );
499 /* Break on an error. */
500 if ( IoErr()) {
501 if(window_opened)
502 DosRequest( "Go away!", ISEQ_C "Error during generation of\n\n`%s'", GenName );
503 break;
505 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
507 MyFPrintf( file, "\n<hr>\n" ); /* final horizontal rule and newline - Anton */
508 MyFPrintf( file, "</body>\n</html>"); /* finish off the html stuff. - Anton */
510 /* Close the file. */
511 Close( file );
513 LONG error;
515 * Delete the file upon error.
517 if ((error=IoErr()))
519 DeleteFile( GenName );
520 SetIoErr(error);
525 return((BOOL)(IoErr()==0));
529 * Generate XML autodoc. - Anton.
531 * This functin is incomplete.
533 BOOL GenerateXML(BOOL window_opened)
535 BPTR file;
538 * Open the output file.
540 if ( file = Open( GenName, MODE_NEWFILE )) {
541 /* set secondary error code to 0
542 (Who cares if the file doesn't exist yet? need to check this, though)
544 SetIoErr(0);
546 /* Write xml header stuff */
547 /* QUOTE is defined in cad.h */
548 /* encoding: ascii is a subset of UTF-8 and does not need to be specified. */
549 /* Manuel recommends ISO-8859-1 encoding so it recognizes 8-bit characters */
550 MyFPrintf( file, "<?xml version='1.0' encoding=\"ISO-8859-1\"?>\n\n");
552 /* stuff goes here */
553 MyFPrintf( file, "<!DOCTYPE test [\n");
554 MyFPrintf( file, "<!ELEMENT test (#PCDATA) >\n");
555 MyFPrintf( file, "<!ENTITY % xx '&#37;'>\n"); /* not used - example of entity */
556 MyFPrintf( file, "<test>This format has yet to be implemented</test>\n");
557 MyFPrintf( file, "]>\n");
559 MyFPrintf( file, "\n</xml>"); /* finish off the xml stuff. */
561 /* Close the file. */
562 Close( file );
564 LONG error;
566 * Delete the file upon error.
568 if ((error=IoErr()))
570 DeleteFile( GenName );
571 SetIoErr(error);
576 return((BOOL)(IoErr()==0));
580 * Generate amigaguide. (just the same as text for now.) - Anton
582 * Currently cad node names with slashes are a problem.
583 * (interpreted by amigaguide as directory.)
584 * I've remapped slashes now, but double clicking on a link with a slash still won't work.
585 * (not a big problem).
587 * Ok, this will be confusing, because there are CADNODEs and there are
588 * amigaguide nodes in the resulting file. So read carefully and don't
589 * be fooled.
592 BOOL GenerateGuide(BOOL window_opened)
594 CADNODE *cn;
595 BPTR file;
596 UBYTE tempNodeName[32]; /* This will be copied from the CADNODE and slashes replaced */
599 * Open the output file.
601 if ( file = Open( GenName, MODE_NEWFILE )) {
602 /* set secondary error code to 0 - Anton
603 (Who cares if the file doesn't exist yet? need to check this, though)
605 SetIoErr(0);
607 /* guide header stuff */
608 /* @database to identify this file as amigaguide
609 @tab 2 to make tabs look reasonable - a global attribute
610 @node main to begin the main node (which contains the Table of Contents)
612 MyFPrintf( file, "@database\n@tab 2\n@node main "QUOTE"%s"QUOTE"\n", FilePart(GenName));
614 /* if( We-Want-A-SearchGuide-Button ) { */
615 /* add a nice search button */
616 MyFPrintf( file, "@{"QUOTE" Search "QUOTE" SYSTEM "QUOTE"RUN >nil: SearchGuide %s"QUOTE"}\n", FilePart(GenName));
618 /* } */
621 * Write table of contents.
623 MyFPrintf( file, "TABLE OF CONTENTS\n\n" );
625 cn = ( CADNODE * )FirstEntry( LV_List );
628 MyFPrintf( file, "@{"QUOTE"%s/%s"QUOTE" LINK ", BaseName, cn->cn_Name); /* button text */
630 /* if the node name contains a slash then we are in trouble */
631 MapSlash( cn->cn_Name, tempNodeName ); /* This should get us out of trouble */
632 MyFPrintf( file, QUOTE"%s"QUOTE"}", tempNodeName); /* node name */
634 MyFPrintf( file, "\n"); /* newline */
635 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
637 MyFPrintf( file, "@endnode\n" ); /* end of the main node */
640 * Write entries...
642 cn = ( CADNODE * )FirstEntry( LV_List );
643 do {
645 * Header.
648 MyFPrintf( file, "\n@node "QUOTE ); /* begin quote */
650 /* if the node name contains a slash then we are in trouble */
651 MapSlash( cn->cn_Name, tempNodeName ); /* This should get us out of trouble */
652 MyFPrintf( file, "%s", tempNodeName ); /* node name */
654 MyFPrintf( file, QUOTE" "QUOTE ); /* end quote, begin quote */
655 MyFPrintf( file, "%s/%s", BaseName, cn->cn_Name ); /* node title */
656 MyFPrintf( file, QUOTE"\n\n" ); /* end quote, newline */
659 * Data.
661 FWrite( file, cn->cn_Data, cn->cn_DataLength, 1 );
664 MyFPrintf( file, "@endnode\n" ); /* end node, new line */
665 /* (there must be a linefeed after the last node) */
668 * Break on an error.
670 if ( IoErr()) {
671 if(window_opened)
672 DosRequest( "Go away!", ISEQ_C "Error during generation of\n\n`%s'", GenName );
673 break;
675 } while ( cn = ( CADNODE * )NextEntry( LV_List, cn ));
678 * Close the file.
680 Close( file );
682 LONG error;
684 * Delete the file upon error.
686 if ((error=IoErr()))
688 DeleteFile( GenName );
689 SetIoErr(error);
694 return((BOOL)(IoErr()==0));
698 support function for GenerateGuide()
699 Copy source string to destination string, replacing slashes '/' with 'S'
700 and ':' with 'C'
702 UBYTE *MapSlash(UBYTE *source, UBYTE *destination)
704 int i=0;
707 switch(source[i])
709 case '/':
710 destination[i] = 'S';
711 break;
713 case ':':
714 destination[i] = 'C';
715 break;
717 default:
718 destination[i] = source[i];
720 }while(source[i++] != '\0');
722 return (destination);