File: document.c

package info (click to toggle)
robodoc 4.0.18-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 924 kB
  • ctags: 669
  • sloc: ansic: 8,386; xml: 953; sh: 335; makefile: 144; perl: 68
file content (520 lines) | stat: -rw-r--r-- 13,399 bytes parent folder | download
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/****h* ROBODoc/Document
 * FUNCTION
 *   This module contains functions to manipulate the central data
 *   structure (RB_Document) that contains information about the
 *   source files, and documentation files, and headers.
 *
 *   The name is a bit confusing because it sort of implies that
 *   it contains the documentation extracted from the sourcefiles.
 *
 *   For each run a RB_Document structure is created, it is filled
 *   by the analyser and directory module and then used by the
 *   generator module to create the documentation.
 * MODIFICATION HISTORY
 *   ????-??-??   Frans Slothouber  V1.0
 *   2003-02-03   Frans Slothouber  Refactoring
 *   2003-10-30   David White       Removed unistd.h for Borland
 *******
 */

#include <assert.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#if defined (RB_MSVC)
#  include <direct.h>
#else
#  if defined (RB_BCC)
#  else
#  include <unistd.h> /* Not used for Borland */
#  endif
#endif

#include "robodoc.h"
#include "document.h"
#include "part.h"
#include "path.h"
#include "directory.h"
#include "headers.h"
#include "links.h"
#include "util.h"
#include <string.h>
#include "generator.h"
#include "file.h"


#ifdef DMALLOC
#include <dmalloc.h>
#endif


/****f* Document/RB_Document_Add_Part
 * FUNCTION
 *   Add a new part to the document.
 * INPUTS
 *   o document  -- the document the part is to be added to.
 *   o part      -- the part to be added
 * SOURCE
 */

void
RB_Document_Add_Part( struct RB_Document *document, struct RB_Part *part )
{
    part->next = document->parts;
    document->parts = part;
}

/*****/


/* TODO Documentation */
void
RB_Free_RB_Document( struct RB_Document *document )
{
    if ( document->parts )
    {
        struct RB_Part     *a_part = NULL;
        struct RB_Part     *a_part2 = NULL;

        for ( a_part = document->parts; a_part; a_part = a_part2 )
        {
            a_part2 = a_part->next;
            RB_Free_RB_Part( a_part );
        }
    }
    if ( document->headers )
    {
        unsigned long     i;

        for ( i = 0; i < document->no_headers; ++i )
        {
            RB_Free_Header( document->headers[i] );

        }
        free( document->headers );
    }
    free( document );
}

/****f* Document/RB_Document_Create_Parts
 * FUNCTION
 *   Create all the parts of a document based on the sourcefiles in
 *   the source tree.  This creates a new RB_Part for each file in
 *   the source tree.
 * INPUTS
 *    o document -- the document for which the parts are generated.
 * SOURCE
 */

void
RB_Document_Create_Parts( struct RB_Document *document )
{
    struct RB_Filename *i_file = NULL;

    assert( document );
    assert( document->srctree );

    for ( i_file = document->srctree->first; i_file; i_file = i_file->next )
    {
        struct RB_Part     *rbpart;

        rbpart = RB_Get_RB_Part(  );
        RB_Part_Add_Source( rbpart, i_file );
        RB_Document_Add_Part( document, rbpart );
    }
}

/*******/


/****f* Document/RB_Fill_Header_Filename
 * FUNCTION
 *   Fill the file_name attribute of all headers based either on the
 *   part or the singledoc name.   The file_name tells in which file
 *   the documentation for the header is to be stored.
 * SYNOPSIS
 *   void RB_Fill_Header_Filename ( struct RB_Document* document )
 * SOURCE
 */

void
RB_Fill_Header_Filename( struct RB_Document *document )
{
    struct RB_Part     *i_part;

    for ( i_part = document->parts; i_part; i_part = i_part->next )
    {
        struct RB_header   *i_header;

        for ( i_header = i_part->headers;
              i_header; i_header = i_header->next )
        {
            if ( document->actions & DO_SINGLEDOC )
            {
                i_header->file_name = document->singledoc_name;
            }
            else if ( document->actions & DO_MULTIDOC )
            {
                i_header->file_name = RB_Get_FullDocname( i_part->filename );
            }
            else if ( document->actions & DO_SINGLEFILE )
            {
                i_header->file_name = document->singledoc_name;
            }
            else
            {
                assert( 0 );
            }
        }
    }
}

/******/


/****f* Document/RB_Document_Determine_DocFilePaths
 * FUNCTION
 *   Determine the path of each of the documentation files based on
 *   the path of the source file and the documentation root path and
 *   the source root path.
 * SYNOPSIS
 *   void RB_Document_Determine_DocFilePaths( struct RB_Document* document )
 * EXAMPLE
 *   srcpath = ./test/mysrc/sub1/sub2
 *   srcroot = ./test/mysrc/
 *   docroot = ./test/mydoc/
 *     ==>
 *   docpath = ./test/mydoc/sub1/sub2
 * SOURCE
 */

void
RB_Document_Determine_DocFilePaths( struct RB_Document *document )
{
    struct RB_Path     *path;
    int                 docroot_length;
    int                 srcroot_length;
    int                 length;

    assert( document->srctree );
    assert( document->srcroot );
    assert( document->docroot );

    docroot_length = strlen( document->docroot->name );
    srcroot_length = strlen( document->srcroot->name );

    for ( path = document->srctree->first_path; path; path = path->next )
    {
        char               *name;
        char               *new_name;
        char               *tail;

        name = path->name;
        length = strlen( name );
        assert( length >= srcroot_length );
        tail = name + srcroot_length;
        new_name = calloc( docroot_length +
                           ( length - srcroot_length ) + 1, sizeof( char ) );
        assert( new_name );
        strcat( new_name, document->docroot->name );
        strcat( new_name, tail );
        path->docname = new_name;
    }
}

/******/


/****f* Document/RB_Document_Create_DocFilePaths
 * FUNCTION
 *   This function creates the whole document directory
 *   tree.  It tests if the directories exist and if they
 *   do not the directory is created.
 * SYNOPSIS
 *   void RB_Document_Create_DocFilePaths( struct RB_Document* document )
 * INPUTS
 *   o document -- the document for which the tree is created.
 * SOURCE
 */

void
RB_Document_Create_DocFilePaths( struct RB_Document *document )
{
    struct RB_Path     *path;

    for ( path = document->srctree->first_path; path; path = path->next )
    {
        char               *pathname = NULL;
        char               *c2 = NULL;

        RB_Say( "Trying to create directory %s\n", path->docname );
        /* Don't want to modify the docname in the path
           structure. So we make a copy that we later
           destroy. */

        pathname = RB_StrDup( path->docname );
        for ( c2 = pathname + 1;        /* We skip the leading '/' */
              *c2; ++c2 )
        {
            if ( *c2 == '/' )
            {
                struct stat         dirstat;

                *c2 = '\0';     /* Replace the '/' with a '\0'. */
                /* We now have one of the paths leading up to the
                   total path. Test if it exists. */
                if ( stat( pathname, &dirstat ) == 0 )
                {
                    /* Path exists. */
                }
                else
                {
                    int                 result;

#if defined(RB_MSVC) || defined(_XCOMPILE_MINGW) || defined(__MINGW32_VERSION)
                    result = mkdir( pathname );
#else
                    result = mkdir( pathname, 0770 );
#endif
                    if ( result == 0 )
                    {
                        /* Path was created. */
                    }
                    else
                    {
                        assert( 0 );    /* Path creation failed. */
                    }
                }
                /* Put the '/' back in it's place. */
                *c2 = '/';
            }
        }
        free( pathname );
    }
}

/*******/


/****f* Document/RB_Document_Collect_Headers
 * FUNCTION
 *   Create a table of pointers to all headers.  This is done to
 *   have easy access to all heades without having to scan all
 *   RB_Parts.
 * INPUTS
 *   o document -- the document for which the table is created.
 * OUTPUT
 *   o document->headers
 *   o document->no_headers
 * SOURCE
 */

void
RB_Document_Collect_Headers( struct RB_Document *document )
{
    struct RB_Part     *i_part;
    struct RB_header   *( *headers );   /* Pointer to an array of pointers RB_headers. */
    unsigned long       count = 0;
    unsigned long       i = 0;

    RB_Say( "Collecting headers\n" );
    for ( i_part = document->parts; i_part; i_part = i_part->next )
    {
        struct RB_header   *i_header;

        for ( i_header = i_part->headers;
              i_header; i_header = i_header->next )
        {
            count++;
        }
    }
    headers =
        ( struct RB_header ** ) calloc( count, sizeof( struct RB_header * ) );
    for ( i_part = document->parts; i_part; i_part = i_part->next )
    {
        struct RB_header   *i_header;

        for ( i_header = i_part->headers;
              i_header; i_header = i_header->next )
        {
            headers[i] = i_header;
            i++;
        }
    }
    document->headers = headers;
    document->no_headers = count;
}

/*******/


/*
 If A is called   qqqq/ffff and B is called  ffff/zzzz then A is the
 parent of B
*/

void
RB_Document_Link_Headers( struct RB_Document *document )
{
    unsigned long                i;
    unsigned long                j;
    struct RB_header   *parent;
    struct RB_header   *child;
    char               *parent_name;
    char               *child_name;

    RB_Say( "Linking headers\n" );
    for ( i = 0; i < document->no_headers; i++ )
    {
        parent = ( document->headers )[i];
        parent_name = parent->function_name;
        for ( j = 0; j < document->no_headers; j++ )
        {
            if ( i != j )
            {
                child = ( document->headers )[j];
                child_name = child->module_name;
                if ( strcmp( child_name, parent_name ) == 0 )
                {
                    child->parent = parent;
                }
            }
        }
    }
}

/* TODO Documentation */
void
RB_Document_Determine_DocFileNames( struct RB_Document *document )
{
    struct RB_Filename *filename;
    unsigned int       length = 0;
    char               *name;
    char               *c;

    struct RB_Part     *part;

    for ( part = document->parts; part; part = part->next )
    {

        filename = part->filename;
        /* turn  mysource.c  into  mysource_c.html
           First find the total length. */
        length = strlen( filename->name );
        /* add one for the '.' */
        ++length;
        length += RB_Get_Len_Extension( document->extension );
        /* plus one for the '\0' */
        ++length;
        name = ( char * ) calloc( length, sizeof( char ) );
        assert( name );
        strcat( name, filename->name );
        for ( c = name; *c != '\0'; c++ )
        {
            if ( *c == '.' )
            {
                *c = '_';
            }
        }
        RB_Add_Extension( document->extension, name );
        part->filename->docname = name;
    }
}

/****f* Document/RB_Open_SingleDocumentation
 * FUNCTION
 *   Open the file that will contain the documentation in
 *   case we create a single document.
 * SYNOPSIS
 *   FILE* RB_Open_SingleDocumentation( struct RB_Document* document )
 * RESULT
 *   An opened file.
 *****
 */

FILE               *
RB_Open_SingleDocumentation( struct RB_Document *document )
{
    FILE               *file;
    static char        *default_name = "singledoc";
    char               *name = NULL;
    size_t              size = 0;

    if ( document->singledoc_name )
    {
        size += strlen( document->singledoc_name );
    }
    else
    {
        size += strlen( default_name );
    }
    size++;                     /* and the '\0'; */
    size += RB_Get_Len_Extension( document->extension );

    name = ( char * ) calloc( size, sizeof( char ) );
    assert( name );
    if ( document->singledoc_name )
    {
        strcat( name, document->singledoc_name );
    }
    else
    {
        strcat( name, default_name );
    }
    RB_Add_Extension( document->extension, name );

    file = fopen( name, "w" );
    if ( file )
    {
        /* File opened  */
    }
    else
    {
        RB_Panic( "Can't open %s\n", name );
    }
    return file;
}


/****f* Document/RB_Get_RB_Document
 * FUNCTION
 *   Allocate and initialize an RB_Document structure.
 * SYNOPSIS
 *   struct RB_Document* RB_Get_RB_Document()
 * RESULT
 *   An initialized document structure.
 * SOURCE
 */

struct RB_Document *
RB_Get_RB_Document( void )
{
    struct RB_Document *document = 0;
    document =
        ( struct RB_Document * ) malloc( sizeof( struct RB_Document ) );
    if ( document )
    {
        document->cur_part = NULL;
        document->parts = NULL;
        document->links = NULL;
        document->headers = NULL;
        document->doctype = UNKNOWN;
        document->actions = 0;
        document->srctree = NULL;
        document->srcroot = NULL;
        document->docroot = NULL;
        document->singledoc_name = NULL;
        document->no_headers = 0;
        document->charset = NULL;
        document->extension = NULL;
    }
    else
    {
        RB_Panic( "out of memory" );
    }
    return document;
}

/*******/