Added options to make Robodoc more customizable.
[robodoc.git] / Source / items.c
1 // vi: ff=unix spell 
2
3 /*
4 Copyright (C) 1994-2007  Frans Slothouber, Jacco van Weert, Petteri Kettunen,
5 Bernd Koesling, Thomas Aglassinger, Anthon Pang, Stefan Kost, David Druffner,
6 Sasha Vasko, Kai Hofmann, Thierry Pierron, Friedrich Haase, and Gergely Budai.
7
8 This file is part of ROBODoc
9
10 ROBODoc is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 */
24
25
26 /****h* ROBODoc/Items
27  * FUNCTION
28  *   This module contains functions that deal with items.  The
29  *   documentation consists of headers, and headers contains one of
30  *   more items.  Each item has a name and a body.  All possible items
31  *   are listed in configuration.items.  A uses can specify that
32  *   certain items are not to be added to the documentation.  These
33  *   items are listed in configuration.ignore_items.
34  * AUTHOR
35  *   Frans Slothouber
36  *******
37  */
38
39 #include <stddef.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <assert.h>
45
46 #include "globals.h"
47 #include "robodoc.h"
48 #include "items.h"
49 #include "roboconfig.h"
50 #include "util.h"
51
52 /****v* Items/item_name_buffer
53  * FUNCTION
54  *   Stores the name of the last item that was found.
55  * SOURCE
56  */
57
58 #define MAX_ITEM_NAME_LENGTH 1024
59 char                item_name_buffer[MAX_ITEM_NAME_LENGTH];
60
61 /*****/
62
63 /* TODO Documentation */
64 char               *RB_Get_Item_Name(
65     void )
66 {
67     return item_name_buffer;
68 }
69
70 /****f* Items/RB_Create_Item
71  *
72  * SOURCE
73  */
74
75 struct RB_Item     *RB_Create_Item(
76     enum ItemType arg_item_type )
77 {
78     struct RB_Item     *item = malloc( sizeof( struct RB_Item ) );
79
80     assert( item );
81
82     item->next = 0;
83     item->type = arg_item_type;
84     item->begin_index = 0;
85     item->end_index = 0;
86
87     return item;
88 }
89
90 /*****/
91
92 /****f* Items/RB_Get_Item_Type [3.0b]
93  * FUNCTION
94  *   return the item_type represented by the given string.
95  * SYNOPSIS
96  *   int RB_Get_Item_Type( char *cmp_name )
97  * INPUTS
98  *   char *cmp_name -- item_name to evaluate
99  * RESULT
100  *   int            -- the right item_type or NO_ITEM
101  * SOURCE
102  */
103
104 int RB_Get_Item_Type(
105     char *cmp_name )
106 {
107     unsigned int        item_type;
108
109     assert( configuration.items.number );
110     for ( item_type = 0; item_type < configuration.items.number; ++item_type )
111     {
112         char               *item = configuration.items.names[item_type];
113
114         /* Skip preformat mark */
115         if ( *item == '-' )
116             item++;
117         if ( !strcmp( item, cmp_name ) )
118         {
119             return ( item_type );
120         }
121     }
122     return ( NO_ITEM );
123 }
124
125 /*** RB_Get_Item_Type ***/
126
127
128
129 /****f* Items/RB_Is_ItemName
130  * FUNCTION
131  *   Is there an itemname in the line.  Ignores leading spaces and
132  *   remark markers.
133  * INPUTS
134  *   line -- line to be searched.
135  * RESULT
136  *   The kind of item that was found or NO_ITEM if no item could be found.
137  *   The name of the item will be stored in item_name_buffer.
138  * NOTES
139  *   We used to check for misspelled items names by testing if
140  *   the item name buffer consists of only upper case characters.
141  *   However checking for a misspelled item name this way results in
142  *   many false positives. For instance many warnings are given for
143  *   FORTRAN code as all the keywords are in uppercase.  We need to
144  *   find a better method for this.
145  * SOURCE
146  */
147
148 enum ItemType RB_Is_ItemName(
149     char *line )
150 {
151     char               *cur_char = line;
152     int                 i = 0;
153
154     cur_char = RB_Skip_Whitespace( cur_char );
155     if ( RB_Has_Remark_Marker( cur_char ) )
156     {
157         cur_char = RB_Skip_Remark_Marker( cur_char );
158         cur_char = RB_Skip_Whitespace( cur_char );
159         /* It there anything left? */
160         if ( strlen( cur_char ) )
161         {
162             enum ItemType       item_type = NO_ITEM;
163
164             /* Copy the name */
165             strcpy( item_name_buffer, cur_char );
166             /* remove any trailing spaces */
167             for ( i = strlen( item_name_buffer ) - 1;
168                   i >= 0 && utf8_isspace( item_name_buffer[i] ); --i )
169             {
170                 item_name_buffer[i] = '\0';
171             }
172             /* No check and see if this is an item name */
173             if ( strlen( item_name_buffer ) )
174             {
175                 item_type = RB_Get_Item_Type( item_name_buffer );
176 #if 0                           /* Until we find a better method */
177                 if ( item_type == NO_ITEM )
178                 {
179                     /* Check if it is a misspelled item name */
180                     item_type = POSSIBLE_ITEM;
181                     for ( i = 0; i < strlen( item_name_buffer ); ++i )
182                     {
183                         if ( !( utf8_isupper( item_name_buffer[i] ) ||
184                                 utf8_isspace( item_name_buffer[i] ) ) )
185                         {
186                             /* No it is not */
187                             item_type = NO_ITEM;
188                             break;
189                         }
190                     }
191                 }
192 #endif
193             }
194             return item_type;
195         }
196         else
197         {
198             return NO_ITEM;
199         }
200     }
201     else
202     {
203         return NO_ITEM;
204     }
205 }
206
207 /******/
208
209 /* TODO Documentation */
210 int Is_Ignore_Item(
211     char *name )
212 {
213     unsigned int        i;
214
215     for ( i = 0; i < configuration.ignore_items.number; ++i )
216     {
217         if ( !strcmp( configuration.ignore_items.names[i], name ) )
218         {
219             return TRUE;
220         }
221     }
222     return FALSE;
223 }
224
225
226 /****f* HeaderTypes/Works_Like_SourceItem
227  * FUNCTION
228  *   Tells wether this item works similar to the
229  *   source item, that is weather it copies it's
230  *   content verbatim to the output document.
231  * SYNPOPSIS
232  */
233 int Works_Like_SourceItem(
234     enum ItemType item_type )
235 /*
236  * INPUTS
237  *   item_type -- Type of item (also the index to the item name)
238  * RESULT
239  *   TRUE  -- Item works like a SOURCE item
240  *   FALSE -- Item does NOT work like a SOURCE item
241  * SOURCE
242  */
243 {
244     unsigned int        i;
245
246     // Check if it is a SOURCE item
247     if ( item_type == SOURCECODE_ITEM )
248     {
249         return TRUE;
250     }
251
252     // Lookup if it works like a SOURCE item
253     for ( i = 0; i < configuration.source_items.number; ++i )
254     {
255         if ( !strcmp
256              ( configuration.source_items.names[i],
257                configuration.items.names[item_type] ) )
258         {
259             return TRUE;
260         }
261     }
262
263     // Neither SOURCE item, nor works like it
264     return FALSE;
265 }
266
267 /******/
268
269 /****f* HeaderTypes/Is_Preformatted_Item
270  * FUNCTION
271  *   Tells wether this item should be automatically preformatted in the
272  *   output.
273  * SYNPOPSIS
274  */
275 int Is_Preformatted_Item(
276     enum ItemType item_type )
277 /*
278  * INPUTS
279  *   item_type -- Type of item (also the index to the item name)
280  * RESULT
281  *   TRUE  -- Item should be automatically preformatted
282  *   FALSE -- Item should NOT be automatically preformatted
283  * SOURCE
284  */
285 {
286     unsigned int        i;
287
288     // Lookup if item should be preformatted
289     for ( i = 0; i < configuration.preformatted_items.number; ++i )
290     {
291         if ( !strcmp
292              ( configuration.preformatted_items.names[i],
293                configuration.items.names[item_type] ) )
294         {
295             // Item name match, it sould be preformatted
296             return TRUE;
297         }
298     }
299
300     // Do not automatically preformat item
301     return FALSE;
302 }
303
304 /******/
305
306 /****f* HeaderTypes/Is_Format_Item
307  * FUNCTION
308  *   Tells wether this item should be formatted by the browser
309  * SYNPOPSIS
310  */
311 int Is_Format_Item(
312     enum ItemType item_type )
313 /*
314  * INPUTS
315  *   item_type -- Type of item (also the index to the item name)
316  * RESULT
317  *   TRUE  -- Item should be formatted by the browser
318  *   FALSE -- Item should be left alone
319  * SOURCE
320  */
321 {
322     unsigned int        i;
323
324     // Lookup if item should be formatted by the browser
325     for ( i = 0; i < configuration.format_items.number; ++i )
326     {
327         if ( !strcmp
328              ( configuration.format_items.names[i],
329                configuration.items.names[item_type] ) )
330         {
331             // Item name match, it sould be formatted by the browser
332             return TRUE;
333         }
334     }
335
336     // Leave item alone
337     return FALSE;
338 }
339
340 /******/