Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / Task.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: Task.cc /main/2 1996/07/18 15:22:57 drk $ */
24 /* $XConsortium: Task.cc /main/2 1996/07/18 15:22:57 drk $ */
25
26 /* exported interfaces... */
27 #include "Task.h"
28
29 /* imported interfaces... */
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include "SGMLName.h"
34 #include "Token.h"
35 #include "AttributeRec.h"
36
37 ComplexTask::ComplexTask()
38 {
39   used = alloc = 0;
40   subtasks = NULL;
41 }
42
43 ComplexTask::~ComplexTask()
44 {
45   int i;
46
47   for(i = 0; i < used; i++){
48     delete subtasks[i];
49   }
50
51   if ( subtasks ) delete subtasks;
52 }
53
54 void ComplexTask::removeAllSubTasks()
55 {
56   for ( int i = 0; i < used; i++ ) {
57     delete subtasks[i];
58   }
59
60   if ( subtasks ) { delete subtasks; subtasks = 0; }
61   used = alloc = 0;
62 }
63
64 void ComplexTask::addSubTask(Task *t)
65 {
66   grow(used + 1);
67   subtasks[used++] = t;
68 }
69
70
71 void ComplexTask::stopSubTask(Task *t)
72 {
73   for(int i = 0; i < used; i++){
74
75     if(subtasks[i] == t){
76
77       while(i + 1 < used){
78         subtasks[i] = subtasks[i+1];
79         i++;
80       }
81
82       used--;
83
84       return;
85     }
86   }
87
88   fprintf(stderr, "Internal errnor: stop unknown task.");
89   abort();
90 }
91
92
93 void
94 ComplexTask::grow(int needed)
95 {
96   if(needed + 1 > alloc){
97     Task **born = new Task*[alloc = needed * 3 / 2 + 10];
98
99     if(used){
100       memcpy(born, subtasks, sizeof(Task*) * used);
101       delete subtasks; subtasks = 0;
102     }
103
104     subtasks = born;
105   }
106 }
107
108 void ComplexTask::markup(const Token& t)
109 {
110   int i;
111
112   for(i = 0; i < used; i++){
113     subtasks[i]->markup(t);
114   }
115 }
116
117 void ComplexTask::data(const char *d, size_t len)
118 {
119   int i;
120
121   for(i = 0; i < used; i++){
122     subtasks[i]->data(d, len);
123   }
124 }
125
126
127 #if TEST_TASK
128
129 #include <stdio.h>
130
131 void TestTask::markup(const Token &t)
132 {
133   switch(t.type()){
134   case START:
135     printf("\nStart Element: %s\n", t.giName());
136
137     const AttributeRec *a;
138
139     for(a = t.GetFirstAttr(); a ; a = t.GetNextAttr(a)){
140       const char *ty = SGMLName::lookup(a->getAttrType());
141       printf("Attribute: %s = [%s]`%s'\n",
142              SGMLName::lookup(a->getAttrName()), ty, a->getAttrValueString());
143     }
144
145     break;
146
147   case END:
148     printf("End Element: %s\n", t.giName());
149     break;
150
151   default:
152     printf("Unknown Token Type: %d\n", t.type());
153     abort();
154     break;
155     
156   }
157 }
158
159
160 void TestTask::data(const char *data, size_t)
161 {
162   printf("data: `%s'\n", data);
163 }
164
165 /*
166  * TestTask 2 is for OL-Data testing
167  */
168
169 #include <stdio.h>
170 #include "OLAF.h"
171 #include "OL-Data.h"
172 TestTask2::TestTask2()
173 {
174   f_base = -1;
175 }
176
177 void TestTask2::markup(const Token &t)
178 {
179
180   ComplexTask::markup( t );
181   
182   switch(t.type()){
183   case START:
184
185     /*
186      * See if any OLIAS Architecture Form exists
187      */
188
189     if ( t.AttributeMatch( OLAF::OLIAS, OLAF::Graphic ) ) {
190       f_base = t.level();
191
192       if ( t.LookupAttr( OLAF::OL_data ) ) 
193         ComplexTask::addSubTask( new OL_Data ( t, OLAF::OL_data ) );
194     }
195     break;
196
197   case END:
198     if ( f_base == t.level() ) {
199       for ( int i=0; i < ComplexTask::used; i++ ) {
200         OL_Data *t = ( OL_Data *) subtask(i);
201         cout << "OL_Data found = " << t->content() << endl;
202       }
203     }
204     break;
205
206   default :
207     break;
208
209   }
210 }
211
212
213 void TestTask2::data(const char *data, size_t t)
214 {
215   ComplexTask::data( data, t );
216 }
217
218 #endif