Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / OL-Data.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: OL-Data.C /main/4 1996/08/21 15:47:10 drk $
24  *
25  * (c) Copyright 1996 Digital Equipment Corporation.
26  * (c) Copyright 1996 Hewlett-Packard Company.
27  * (c) Copyright 1996 International Business Machines Corp.
28  * (c) Copyright 1996 Sun Microsystems, Inc.
29  * (c) Copyright 1996 Novell, Inc. 
30  * (c) Copyright 1996 FUJITSU LIMITED.
31  * (c) Copyright 1996 Hitachi.
32  */
33
34 /* exported interfaces */
35 #include "OL-Data.h"
36
37 /* imported interfaces */
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41
42 #include <misc/unique_id.h>
43 #include "Expression.h"
44 #include "AttributeRec.h"
45 #include "OLAF.h"
46 #include "SGMLName.h"
47 #include "Token.h"
48 #include "Task.h"
49 #include "Dispatch.h"
50 #include "SGMLDefn.h"
51
52
53 #include "AttributeData.h"
54 #include "Content.h"
55 #include "FirstOf.h"
56 #include "GenericId.h"
57 #include "Literal.h"
58 #include "ConcatTask.h"
59
60
61 // Debugging macro
62 #ifdef DEBUG
63 #define DBG(level) if ( dbgLevel >= level)
64 #else
65 #define DBG(level) if (0)
66 #endif
67
68 static int dbgLevel=-1;
69
70 //---------------------------------------------------------------------------
71 static char *
72 FilteredSpaces( const char *str )
73 {
74   
75   char *head;
76   char *tail = (char *)str + strlen( str ) - 1;
77   char *ptr = (char *)str;
78
79   while (1) {
80     if ( *ptr != ' ' && *ptr != '\n' && *ptr != '\t' ) {
81       head = ptr;
82       break;
83     }
84     else {
85       ptr++;
86     }
87   }
88
89   while (1) {
90     if ( *tail != ' ' && *tail != '\n' && *tail != '\t' ) {
91       *(tail + 1) = '\0';
92       break;
93     }
94     else { tail--; }
95   }
96
97   return ( head );
98 }
99 //-------------------------------------------------------------------------
100 OL_Data::OL_Data( const Token &t,
101                   int DataType,
102                   ActionType mode ):BaseData(t,mode) 
103 {
104
105   
106   char *dbgStr;
107   dbgStr = getenv("OL_DEBUG");
108   dbgLevel = ( dbgStr ? atoi ( dbgStr ) : 0 );
109
110   /* first generate grep all the mode info */
111   istat = mode & IGNORE_ON;
112   removeSpaces = mode & REMOVE_SPACES;
113
114   f_name = strdup( t.file() );
115   line_num = t.line();
116
117   if ( istat && !Dispatch::OutsideIgnoreScope() ) {
118     data_avail = 0;
119     return;
120   }
121   
122   const AttributeRec *attRec;
123   if ( !(attRec = t.LookupAttr( DataType )) ) {
124
125     if ( mode & GENERATE_ID ) {
126       const char *str = unique_id();
127       
128       ValueBuffer.writeStr( str );
129       data_avail = 1;
130       data_complete = 1;
131     }
132     else {
133
134       /*
135        * The default rule kicks in, i.e use #CONTENT
136        */
137
138       addSubTask( new Content( t, mode) );
139     }
140
141     return;
142   }
143     
144   tokContent.Parse( ( char *)attRec->getAttrValueString() );
145
146
147   const OL_Expression *eptr = tokContent.exprlist;
148   assert(eptr != NULL);
149   
150   switch ( eptr->type() ) {
151
152     case REFERENCE:
153       {
154         BaseData *sub_data = new AttributeData(t, eptr->name(), mode );
155         if ( sub_data ) {
156           if (  data_avail= sub_data->DataWillBeAvailable() ) {
157             ValueBuffer.write( sub_data->content(),
158                                sub_data->content_size() );
159             data_complete = 1;
160           }
161           delete sub_data;
162         }
163       }
164
165       break;
166         
167     case CONTENT:
168       addSubTask( new Content(t, mode) );
169       break;
170
171     case CONCAT:
172       addSubTask( new Concat( t, 
173                               (ExprList *)eptr->data_list(),
174                               mode) );
175       break;
176
177     case FIRSTOF:
178       addSubTask( new FirstOf( t, 
179                                (ExprList *)eptr->data_list(),
180                                mode) );
181       break;
182
183     case GENERIC_ID:
184       addSubTask( new GenericId( t, 
185                                  eptr->name(),
186                                  mode) );
187       break;
188
189     case LITERAL:
190       data_avail = 1;
191       data_complete = 1;
192       ValueBuffer.writeStr( ( const char *)eptr->data_list() );
193       break;
194     
195     default:
196       abort();
197   }
198
199 }
200
201 //-------------------------------------------------------------------------
202 void
203 OL_Data::markup( const Token &t )
204 {
205
206   DBG(80) cerr << "(DEBUG) OL_Data::markup() " << endl;
207   DBG(80) cerr << "        t.giName() = " << t.giName() << endl;
208
209   if ( istat && !Dispatch::OutsideIgnoreScope() ) {
210     return;
211   }
212
213   if ( f_base > 0 ) {
214
215     ComplexTask::markup( t );
216     if ( t.type() == END ) {
217
218       if ( f_base == t.level() ) {
219
220         data_complete = 1;
221
222         for ( int i=0; i < ComplexTask::used; i++ ) {
223           BaseData *task = ( BaseData *)ComplexTask::subtask(i);
224           ValueBuffer.write( task->content(), task->content_size() );
225         }
226
227
228         if ( removeSpaces && ValueBuffer.GetSize() > 0 ) {
229           const char *filtered_string = FilteredSpaces( ValueBuffer.GetBuffer() );
230           /* rewrite ValueBuffer with the filtered string */
231           ValueBuffer.reset();
232           ValueBuffer.writeStr( filtered_string );
233         }
234           
235           
236         DBG(10) printf("Data for <%s> = %s\n", t.giName(),
237                        ValueBuffer.GetBuffer() );
238
239         reset();
240
241       }
242     }
243
244   }  /* if level > 0 */
245 }
246
247 //-------------------------------------------------------------------------
248 void
249 OL_Data::data( const char *str, size_t t )
250 {
251
252   if ( istat && !Dispatch::OutsideIgnoreScope() ) {
253     return;
254   }
255
256   if ( f_base > 0 ) {
257     ComplexTask::data( str, t );
258   }
259 }
260
261 //-------------------------------------------------------------------------
262 void
263 OL_Data::reset()
264 {
265   f_base = -1;
266   istat = removeSpaces = 0;
267   removeAllSubTasks();
268 }
269   
270