4 * Change AutoDoc program
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.
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
51 extern Object
*LV_List
, *WD_Main
, *ST_Base
;
52 extern struct Window
*MainWindow
;
53 extern UBYTE FileName
[ 256 ], BaseName
[ 64 ], GenName
[ 256 ];
59 BOOL
SaveCadFile(BOOL window_opened
)
72 WindowBusy( WD_Main
);
78 if ( file
= Open( FileName
, MODE_NEWFILE
)) {
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
);
90 if ( Write( file
, ( UBYTE
* )&ch
, sizeof( CADHEADER
)) == sizeof( CADHEADER
)) {
92 * Now write the descriptions one by one.
94 cn
= ( CADNODE
* )FirstEntry( LV_List
);
100 strcpy( &cd
.cd_Name
[ 0 ], &cn
->cn_Name
[ 0 ] );
101 cd
.cd_Length
= cn
->cn_DataLength
;
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
) {
113 DosRequest( "OK", ISEQ_C
"Error writing\n\n`%s'", FileName
);
119 DosRequest( "OK", ISEQ_C
"Error writing\n\n`%s'", FileName
);
122 } while ( cn
= ( CADNODE
* )NextEntry( LV_List
, cn
));
125 DosRequest( "OK", ISEQ_C
"Error writing\n\n`%s'", FileName
);
133 * Delete the file on error.
140 DeleteFile( FileName
);
147 DosRequest( "OK", ISEQ_C
"Unable to open\n\n`%s'", FileName
);
155 WindowReady( WD_Main
);
157 return((BOOL
)(IoErr()==0));
161 * Load a single CADDESC.
163 STATIC CADNODE
*LoadCadDesc( BPTR file
)
172 if ( Read( file
, ( UBYTE
* )&cd
, sizeof( CADDESC
)) == sizeof( CADDESC
)) {
176 if ( cn
= ( CADNODE
* )AllocVec( sizeof( CADNODE
), MEMF_CLEAR
)) {
180 strcpy(cn
->cn_Name
, cd
.cd_Name
);
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
);
197 FreeVec(cn
->cn_Data
);
199 SetIoErr(ERROR_NO_FREE_STORE
);
204 SetIoErr(ERROR_NO_FREE_STORE
);
210 * Load a CADLIST file.
212 BOOL
LoadCadFile(BOOL window_opened
)
225 WindowBusy( WD_Main
);
228 BaseName
[ 0 ] = '\0';
230 ClearList( NULL
, LV_List
);
235 if ( file
= Open( FileName
, MODE_OLDFILE
)) {
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 ] );
253 for ( i
= 0; i
< ch
.ch_NumDesc
; i
++ ) {
257 if ( cn
= LoadCadDesc( file
)) {
259 * Add it to the list.
261 AddEntry( NULL
, LV_List
, cn
, LVAP_TAIL
);
266 DosRequest( "OK", ISEQ_C
"Error reading\n\n`%s'", FileName
);
272 MyRequest( "OK", ISEQ_C
"Unknown file version." );
276 MyRequest( "OK", ISEQ_C
"Unknown file type." );
281 DosRequest( "OK", ISEQ_C
"Error reading\n\n`%s'", FileName
);
287 DosRequest( "OK", ISEQ_C
"Unable to open\n\n`%s'", FileName
);
292 * Errors clear all data.
294 BaseName
[ 0 ] = '\0';
296 ClearList( NULL
, LV_List
);
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
);
307 RefreshList( MainWindow
, LV_List
);
312 WindowReady( WD_Main
);
320 BOOL
Generate(BOOL window_opened
, ULONG outputFormat
)
329 WindowBusy( WD_Main
);
332 switch( outputFormat
)
335 result
= GenerateDoc(window_opened
);
339 result
= GenerateGuide(window_opened
);
343 result
= GenerateHTML(window_opened
);
347 result
= GenerateXML(window_opened
);
351 result
= FALSE
; /* unsupported output format */
359 WindowReady( WD_Main
);
365 BOOL
GenerateDoc(BOOL window_opened
)
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)
380 * Write table of contents.
382 MyFPrintf( file
, "TABLE OF CONTENTS\n\n" );
384 cn
= ( CADNODE
* )FirstEntry( LV_List
);
386 MyFPrintf( file
, "%s/%s\n", BaseName
, &cn
->cn_Name
[ 0 ] );
387 } while ( cn
= ( CADNODE
* )NextEntry( LV_List
, cn
));
394 cn
= ( CADNODE
* )FirstEntry( LV_List
);
399 MyFPrintf( file
, "%lc%s/%s\n\n", 12, BaseName
, &cn
->cn_Name
[ 0 ] );
404 FWrite( file
, cn
->cn_Data
, cn
->cn_DataLength
, 1 );
416 DosRequest( "OK", ISEQ_C
"Error during generation of\n\n`%s'", GenName
);
419 } while ( cn
= ( CADNODE
* )NextEntry( LV_List
, cn
));
428 * Delete the file upon error.
432 DeleteFile( GenName
);
438 return((BOOL
)(IoErr()==0));
443 * Generate HTML autodoc. - Anton.
445 BOOL
GenerateHTML(BOOL window_opened
)
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
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
);
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
));
479 cn
= ( CADNODE
* )FirstEntry( LV_List
);
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.*/
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. */
502 DosRequest( "Go away!", ISEQ_C
"Error during generation of\n\n`%s'", GenName
);
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. */
515 * Delete the file upon error.
519 DeleteFile( GenName
);
525 return((BOOL
)(IoErr()==0));
529 * Generate XML autodoc. - Anton.
531 * This functin is incomplete.
533 BOOL
GenerateXML(BOOL window_opened
)
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)
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 '%'>\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. */
566 * Delete the file upon error.
570 DeleteFile( GenName
);
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
592 BOOL
GenerateGuide(BOOL window_opened
)
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)
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
));
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 */
642 cn
= ( CADNODE
* )FirstEntry( LV_List
);
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 */
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) */
672 DosRequest( "Go away!", ISEQ_C
"Error during generation of\n\n`%s'", GenName
);
675 } while ( cn
= ( CADNODE
* )NextEntry( LV_List
, cn
));
684 * Delete the file upon error.
688 DeleteFile( GenName
);
694 return((BOOL
)(IoErr()==0));
698 support function for GenerateGuide()
699 Copy source string to destination string, replacing slashes '/' with 'S'
702 UBYTE
*MapSlash(UBYTE
*source
, UBYTE
*destination
)
710 destination
[i
] = 'S';
714 destination
[i
] = 'C';
718 destination
[i
] = source
[i
];
720 }while(source
[i
++] != '\0');
722 return (destination
);