File: util.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 (565 lines) | stat: -rw-r--r-- 12,554 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/****h* ROBODoc/Utilities
 * FUNCTION
 *   Set of general purpose utility functions that are used
 *   more than one module.
 *****
 * $Id: util.c,v 1.26 2003/12/30 17:39:36 gumpu Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>             /* for RB_Say() */
#include <time.h>
#include <assert.h>

#include "robodoc.h"
#include "globals.h"
#include "links.h"
#include "headers.h"
#include "util.h"


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


/****f* Utilities/RB_Alloc_Header [2.01]
 * FUNCTION
 *   allocate the struct RB_header
 * SYNOPSIS
 *   struct RB_header *RB_Alloc_Header( void )
 * RESULT
 *   struct RB_header *      -- all attributes/pointers set to zero
 * AUTHOR
 *   Koessi
 * SEE ALSO
 *   RB_Free_Header()
 * SOURCE
 */

struct RB_header   *
RB_Alloc_Header( void )
{
    struct RB_header   *new_header;

    if ( ( new_header = malloc( sizeof( struct RB_header ) ) ) != NULL )
    {
        memset( new_header, 0, sizeof( struct RB_header ) );
    }
    else
    {
        RB_Panic( "out of memory! [Alloc Header]\n" );
    }
    return ( new_header );
}

/********/


/****f* Utilities/RB_Free_Header [2.01]
 * NAME
 *   RB_Free_Header             -- oop
 * SYNOPSIS
 *   void RB_Free_Header( struct RB_header *header )
 * FUNCTION
 *   free struct RB_header and associated strings
 * INPUTS
 *   struct RB_header *header -- this one
 * AUTHOR
 *   Koessi
 * SEE ALSO
 *   RB_Alloc_Header(), RB_Close_The_Shop()
 * SOURCE
 */

void
RB_Free_Header( struct RB_header *header )
{
    if ( header )
    {
        if ( header->function_name )
        {
            free( header->function_name );
        }
        if ( header->version )
        {
            free( header->version );
        }
        if ( header->name )
        {
            free( header->name );
        }
        if ( header->unique_name )
        {
            free( header->unique_name );
        }
        if ( header->lines )
        {
            int                 i;

            for ( i = 0; i < header->no_lines; ++i )
            {
                free( header->lines[i] );
            }
            free( header->lines );
        }
        free( header );
    }
}

/************/


/****i* Utilities/RB_WordLen [2.01]
 * NAME
 *   RB_WordLen -- like strlen
 * SYNOPSIS
 *   int RB_WordLen( char *str )
 * FUNCTION
 *   get the amount of bytes until next space
 * INPUTS
 *   char *str -- the word
 * RESULT
 *   int -- length of the next word or 0
 * AUTHOR
 *   Koessi
 * SEE ALSO
 *   RB_Find_Header_Name()
 * SOURCE
 */

int
RB_WordLen( char *str )
{
    int                 len;
    char                c;

    for ( len = 0; ( ( c = *str ) != '\0' ) && !isspace( c ) && ( c != '\n' );
          ++str, ++len )
    {
        /* empty */
    }
    return ( len );
}

/*** RB_WordLen ***/


/****if* Utilities/RB_StrDup [2.01]
 * NAME
 *   RB_StrDup
 * SYNOPSIS
 *   char *RB_StrDup( char *str )
 * FUNCTION
 *   duplicate the given string
 * INPUTS
 *   char *str               -- source
 * RESULT
 *   char *                  -- destination
 * AUTHOR
 *   Koessi
 * SOURCE
 */

char               *
RB_StrDup( char *str )
{
    char               *dupstr;
    if ( ( dupstr =
           malloc( ( strlen( str ) + 1 ) * sizeof( char ) ) ) != NULL )
    {
        strcpy( dupstr, str );
    }
    else
    {
        RB_Panic( "out of memory! [StrDup]\n" );
    }
    return ( dupstr );
}

/*** RB_StrDup ***/

/****f* Utilities/RB_Say [2.01]
 * NAME
 *   RB_Say                     -- varargs
 * SYNOPSIS
 *   void RB_Say( char *what, char *why, ... )
 * FUNCTION
 *   Say what's going on.  Goes to stdout.
 * INPUTS
 *   char *format            -- formatstring
 *    ...                    -- parameters
 * AUTHOR
 *   Koessi
 * SOURCE
 */

void
RB_Say( char *format, ... )
{
    va_list             ap;

    if ( course_of_action & DO_TELL )
    {
        va_start( ap, format );
        printf( "%s: ", whoami );
        vprintf( format, ap );
        va_end( ap );
    }
}

/*** RB_Say ***/


/* TODO Documentation */
void
RB_SetCurrentFile( char *filename )
{
    current_file = filename;
}

/* TODO Documentation */
char               *
RB_GetCurrentFile(  )
{
    return current_file;
}


/****f* Utilities/RB_Panic [2.01]
 * NAME
 *   RB_Panic -- free resources and shut down
 * SYNOPSIS
 *   void RB_Panic( char *format, char *why, ... )
 * FUNCTION
 *   Print error message.  Frees all resources used by robodoc.
 *   Terminates program.  Output goes to stderr
 * INPUTS
 *   char *format            -- formatstring
 *   ...                     -- parameters
 * AUTHOR
 *   Koessi
 * SOURCE
 */

void
RB_Panic( char *format, ... )
{
    va_list             ap;
    char               *name;

    va_start( ap, format );

    name = RB_GetCurrentFile(  );

    if ( name )
    {
        fprintf( stderr, "%s: FATAL ERROR - %s:%d\n", whoami, name, line_number );
        fprintf( stderr, "%s: %s\n%s: ", whoami, line_buffer, whoami );
    }
    else
    {
        fprintf( stderr, "%s: ", whoami );
    }
    vfprintf( stderr, format, ap );
    fprintf( stderr, "%s: closing down...\n", whoami );
    va_end( ap );
    RB_Close_The_Shop(  );
    exit( EXIT_FAILURE );
}

/*** RB_Panic ***/




/****f* Utilities/RB_Str_Case_Cmp
 * FUNCTION
 *   Compare two strings, regardless of the case of the characters.
 * SYNOPSIS
 *   int      RB_Str_Case_Cmp(char *, char *)
 *   result = RB_Str_Case_Cmp(s, t)
 * RESULT
 *    0  s == t
 *   -1  s < t
 *    1  s > t
 * SOURCE
 */

int
RB_Str_Case_Cmp( char *s, char *t )
{
    assert( s );
    assert( t );
    for ( ; tolower( *s ) == tolower( *t ); s++, t++ )
    {
        if ( *s == '\0' )
        {
            return 0;
        }
    }
    return ( int ) ( tolower( *s ) - tolower( *t ) );
}

/*********/


/****f* Utilities/RB_TimeStamp
 * NAME
 *   RB_TimeStamp -- print a time stamp
 * SOURCE
 */

void
RB_TimeStamp( FILE * f )
{
    time_t              ttp;
    char                timeBuffer[255];

    time( &ttp );
    strftime( timeBuffer, 255, "%a %b %d %H:%M:%S %Y\n", localtime( &ttp ) );
    fprintf( f, "%s", timeBuffer );
}

/******/

/****f* Utilities/RB_Skip_Whitespace
 * SYNOPSIS
 *   char * RB_Skip_Whitespace(char *buf)
 * FUNCTION
 *   Skip space and tab chars from the start *buf. This is needed when
 *   searching for indented headers and items. 
 * NOTES
 *   We should extract some info about indentation level and save it to
 *   global variable in order to write out source items (that originate from
 *   indented headers) neatly. 
 * SEE ALSO
 *   RB_Find_Marker, RB_Find_End_Marker, RB_Find_Item, RB_Generate_Item_Body
 * SOURCE
 */

char               *
RB_Skip_Whitespace( char *buf )
{
    char               *c;

    for ( c = buf; *c; c++ )
    {
        switch ( *c )
        {
        case ' ':
        case '\t':
            break;
        default:
            return c;
        }
    }
    return buf;                 /* buf was null */
}

/*** RB_Skip_Whitespace ***/


/****f* Utilities/RB_FputcLatin1ToUtf8
 * NAME
 *   RB_FputcLatin1ToUtf8
 * SYNOPSIS
 *   void RB_FputcLatin1ToUtf8(FILE *fp, int c)
 * BUGS
 *   This wrongly assumes that input is always Latin-1.
 * SOURCE
 */

void
RB_FputcLatin1ToUtf8( FILE * fp, int c )
{
    if ( c < 0x80 )
    {
        if ( fputc( c, fp ) == EOF )
            RB_Panic( "RB_FputcLatin1ToUtf8: write error" );
    }
    else
    {
        if ( fputc( ( 0xC0 | ( c >> 6 ) ), fp ) == EOF )
            RB_Panic( "RB_FputcLatin1ToUtf8: write error" );
        if ( fputc( ( 0x80 | ( c & 0x3F ) ), fp ) == EOF )
            RB_Panic( "RB_FputcLatin1ToUtf8: write error" );
    }
}

/*** RB_FputcLatin1ToUtf8 ***/


/****f* Utilities/RB_CopyFile
 * NAME
 *   RB_CopyFile -- copy a file to another file
 * SYNOPSIS
 *   void RB_CopyFile( char* sourceFileName, char* destinationFileName )
 * RESULT
 *   Program Exit if one of the specified files did not open.
 * SOURCE
 */

void RB_CopyFile( char* sourceFileName, char* destinationFileName )
{
    FILE* source;

    source = fopen( sourceFileName, "r" );
    if ( source ) 
    {
        FILE* dest;
        dest = fopen( destinationFileName, "w" );
        if ( dest ) 
        {
            for (; fgets( line_buffer, MAX_LINE_LEN, source ); )
            {
                fputs( line_buffer, dest );
            }
        }
        else
        {
            fclose( source );
            RB_Panic( "can't open file %s for writing",
                    destinationFileName );
        }
    }
    else
    {
        RB_Panic( "can't open file %s for reading", sourceFileName );
    }
}

/*****/



/****f* Utilities/RB_Match
 * FUNCTION
 *   See if a string matches a wildcard expression.  The wildcard
 *   expression can consists of any literal character and the two
 *   wildcards characters '*' and '?'.  '*' matches the longest string
 *   of zero or more characters that fit.  '?' matches any single
 *   characters.
 *   Examples:
 *      "aapaapaapaap" matches "*aap"
 *      "linux"        matches "?inux"
 *      "linux"        matches "lin*ux"
 *      "linux"        matches "linux*"
 *   This is a recursive function.
 * SYNOPSIS
 *   int RB_Match( char* target, char* wildcard_expression )
 * INPUTS
 *   o target -- the string to be matched agains the
 *               wildcard_expression.
 *   o wildcard_expression -- the wildcard expression
 * RETURN VALUE
 *   TRUE  -- the target matches the wildcard expression
 *   FALSE -- it does not match.
 * SOURCE
 */

int RB_Match( char* target, char* wildcard_expression )
{
    if ( ( *wildcard_expression == '\0' ) &&
         ( *target == '\0' ) ) 
    {
        /* a match, since both strings are "" */
        return TRUE;
    }
    else if ( *wildcard_expression == '\0' ) 
    {
        /* we reached the end of the wildcard_expression,
         * but not the end of the target, this is not
         * a match.
         */
        return FALSE;
    }
    else if ( *target == '\0' )
    {
        /* we reached the end of the target but not the end of the
         * wildcard_expression.  Only if the whole wildcard_expression
         * consists of * we have a match.
         */
        unsigned int i;
        for ( i = 0; i < strlen( wildcard_expression ); ++i )
        {
            if ( wildcard_expression[ i ] != '*' )
            {
                return FALSE;
            }
        }
        return TRUE;
    }
    else
    {
        /* There are wildcard_expression characters left
         * and target characters left.
         */
        char wildcard = wildcard_expression[ 0 ];

        if ( wildcard == '?' )
        {
            /* Match a single character and see if the
             * rest of the target matches.
             */
            return RB_Match( target + 1, wildcard_expression + 1 );
        }
        else if ( wildcard == '*' )
        {
            int match = FALSE;
            int l = strlen( target );
            int i;
            /* First try to match all of the target string, and
             * then work back to the begin of the target string.
             * ( including the "" string. )
             */
            for ( i = l; i >= 0; --i )
            {
                if ( RB_Match( target + i, wildcard_expression + 1 ) )
                {
                    match = TRUE;
                    break;
                }
            }
            return match;
        }
        else 
        {
            int l_w = strlen( wildcard_expression );
            int l_t = strlen( target );
            /* The minimum of the length of the wildcard_expression
             * and target expression 
             */
            int l = ( l_w <= l_t ) ? l_w : l_t ;
            int i;
            for ( i = 0; i < l; ++i )
            {
                if( ( wildcard_expression[ i ] != '*' ) &&
                    ( wildcard_expression[ i ] != '?' ) )
                {
                    if ( wildcard_expression[ i ] != target[ i ] )
                    {
                        return FALSE;
                    }
                }
                else
                {
                    return RB_Match( target + i, wildcard_expression + i );
                }
            }
            /* The first l characters of the target and
             * wildcard_expression matched, now see if the rest
             * matches too.
             */
            return RB_Match( target + l, wildcard_expression + l );
        }
    }
}

/******/