summaryrefslogtreecommitdiff
path: root/objects/objects_parser.cc
blob: 9278b56ae5a6855be9a4b748f13a4a08521a8685 (plain)
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
/*
 * RTFL
 *
 * Copyright 2013-2015 Sebastian Geerken <sgeerken@dillo.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version; with the following exception:
 *
 * The copyright holders of RTFL give you permission to link this file
 * statically or dynamically against all versions of the graphviz
 * library, which are published by AT&T Corp. under one of the following
 * licenses:
 *
 * - Common Public License version 1.0 as published by International
 *   Business Machines Corporation (IBM), or
 * - Eclipse Public License version 1.0 as published by the Eclipse
 *   Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "objects_parser.hh"

#if 0
#   define PRINT(fmt) printf ("---- [%p] " fmt "\n", this)
#   define PRINTF(fmt, ...) printf ("---- [%p] " fmt "\n", this, __VA_ARGS__)
#else
#   define PRINT(fmt)
#   define PRINTF(fmt, ...)
#endif

using namespace rtfl::tools;

namespace rtfl {

namespace objects {

namespace timeout {
   
inline int getActualType (int type)
{
   return type >> 8;
}

inline int getCount (int type)
{
   return type & 0xff;
}

inline int makeType (int actualType, int count)
{
   return count | (actualType << 8);
}

inline int incType (int type)
{
   return makeType (getActualType (type), getCount (type) + 1);
}

inline int decType (int type)
{
   return makeType (getActualType (type), getCount (type) - 1);
}

} // namespace timeout

ObjectsControllerBase::ObjectsControllerBase()
{
   predessor = NULL;
   successor = NULL;
}

void ObjectsControllerBase::addTimeout (double secs, int type)
{
   PRINTF ("ObjectsControllerBase::addTimeout (%g, %d)", secs, type);

   if (predessor)
      predessor->addTimeout (secs, timeout::incType (type));
   else
      fprintf (stderr, "addTimeout (%g, %d): no predessor\n", secs, type); 
}

void ObjectsControllerBase::removeTimeout (int type)
{
   if (predessor)
      predessor->removeTimeout (timeout::incType (type));
   else
      fprintf (stderr, "removeTimeout (%d): no predessor\n", type); 
}

void ObjectsControllerBase::setObjectsSource (ObjectsSource *source)
{
   predessor = source;
}

void ObjectsControllerBase::timeout (int type)
{
   PRINTF ("ObjectsControllerBase::timeout (%d)", type);

   // We start sending count == 1 to the predessor, so we get back count == 1
   // from it at the end.
   if (timeout::getCount (type) == 1)
      ownTimeout (timeout::getActualType (type));
   else {
      if (successor)
         successor->timeout (timeout::decType (type));
      else
         fprintf (stderr, "timeout (%d): no successor\n", type);
   }
}

void ObjectsControllerBase::finish ()
{
   ownFinish ();
   
   if (successor)
      successor->finish ();
}

void ObjectsControllerBase::setObjectsSink (ObjectsSink *sink)
{
   successor = sink;
}

void ObjectsControllerBase::addOwnTimeout (double secs, int type)
{
   PRINTF ("ObjectsControllerBase::addOwnTimeout (%g, %d)", secs, type);
   addTimeout (secs, timeout::makeType (0, type));
}

void ObjectsControllerBase::removeOwnTimeout (int type)
{
   removeTimeout (timeout::makeType (0, type));
}
   
void ObjectsControllerBase::ownTimeout (int type)
{
   // No implementation.
}

void ObjectsControllerBase::ownFinish ()
{
   // No implementation.
}

// ----------------------------------------------------------------------

ObjectsParser::ObjectsParser (ObjectsController *controller)
{
   this->controller = controller;
   source = NULL;
   controller->setObjectsSource (this);
}

ObjectsParser::~ObjectsParser ()
{
   controller->setObjectsSource (NULL);
}

void ObjectsParser::processCommand (CommonLineInfo *info, char *cmd, char *args)
{
   char **parts = NULL;

   if (args == NULL)
      // All commands need arguments here.
      fprintf (stderr, "Missing arguments:%s\n", info->completeLine);
   else if (strcmp (cmd, "obj-msg") == 0) {
      parts = split (args, 4);
      if (parts[1] && parts[2] && parts[3])
         controller->objMsg (info, parts[0], parts[1], atoi(parts[2]),
                             parts[3]);
      else
         fprintf (stderr, "Incomplete line (obj-msg):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-mark") == 0) {
      parts = split (args, 4);
      if (parts[1] && parts[2] && parts[3])
         controller->objMark (info, parts[0], parts[1], atoi(parts[2]),
                              parts[3]);
      else
         fprintf (stderr, "Incomplete line (obj-mark):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-msg-start") == 0)
      controller->objMsgStart (info, args);
   else if (strcmp (cmd, "obj-msg-end") == 0)
      controller->objMsgEnd (info, args);
   else if (strcmp (cmd, "obj-enter") == 0) {
      parts = split (args, 5);
      if (parts[1] && parts[2] && parts[3] && parts[4])
         controller->objEnter (info, parts[0], parts[1], atoi(parts[2]),
                               parts[3], parts[4]);
      else
         fprintf (stderr, "Incomplete line (obj-enter):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-leave") == 0)
      // Pre-version "obj-leave" does not support values.
      controller->objLeave (info, args, NULL);
   else if (strcmp (cmd, "obj-create") == 0) {
      parts = split (args, 2);
      if (parts[1])
         controller->objCreate (info, parts[0], parts[1]);
      else
         fprintf (stderr, "Incomplete line (obj-create):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-ident") == 0) {
      parts = split (args, 2);
      if (parts[1])
         controller->objIdent (info, parts[0], parts[1]);
      else
         fprintf (stderr, "Incomplete line (obj-ident):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-assoc") == 0) {
      parts = split (args, 2);
      if (parts[1])
         controller->objAssoc (info, parts[0], parts[1]);
      else
         fprintf (stderr, "Incomplete line (obj-assoc):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-set") == 0) {
      parts = split (args, 3);
      if (parts[1] && parts[2])
         controller->objSet (info, parts[0], parts[1], parts[2]);
      else
         fprintf (stderr, "Incomplete line (obj-set):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-color") == 0) {
      parts = split (args, 2);
      if (parts[1]) {
         fprintf (stderr, "Warning: obj-color is deprecated; use "
                  "obj-class-color instead:\n%s\n", info->completeLine);
         controller->objClassColor (info, parts[1], parts[0]);
      } else
         fprintf (stderr, "Incomplete line (obj-color):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-class-color") == 0) {
      parts = split (args, 2);
      if (parts[1])
         controller->objClassColor (info, parts[1], parts[0]);
      else
         fprintf (stderr, "Incomplete line (obj-class-color):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-object-color") == 0) {
      parts = split (args, 2);
      if (parts[1])
         controller->objObjectColor (info, parts[0], parts[1]);
      else
         fprintf (stderr, "Incomplete line (obj-object-color):\n%s\n",
                  info->completeLine);
   } else if (strcmp (cmd, "obj-delete") == 0)
      controller->objDelete (info, args);
   else
      fprintf (stderr, "Unknown command identifier '%s':\n%s\n", cmd,
               info->completeLine);

   if (parts)
      freeSplit (parts);
}

void ObjectsParser::processVCommand (CommonLineInfo *info, const char *module,
                                     int majorVersion, int minorVersion,
                                     const char *cmd, char **args)
{
   if (strcmp (module, "obj") == 0) {
      if (majorVersion > 1)
         fprintf (stderr, "Last supported version is 1.0:\n%s\n",
                  info->completeLine);
      if (args[0] == NULL) {
         if (strcmp (cmd, "noident") == 0)
            controller->objNoIdent (info);
         else
            // All other commands need arguments.
            fprintf (stderr, "Missing arguments:%s\n", info->completeLine);
      } else if (strcmp (cmd, "msg") == 0) {
         if (args[1] && args[2] && args[3])
            controller->objMsg (info, args[0], args[1], atoi(args[2]), args[3]);
         else
            fprintf (stderr, "Incomplete line (msg):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "mark") == 0) {
         if (args[1] && args[2] && args[3])
            controller->objMark (info, args[0], args[1], atoi(args[2]),
                                 args[3]);
         else
            fprintf (stderr, "Incomplete line (mark):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "msg-start") == 0)
         controller->objMsgStart (info, args[0]);
      else if (strcmp (cmd, "msg-end") == 0)
         controller->objMsgEnd (info, args[0]);
      else if (strcmp (cmd, "enter") == 0) {
         if (args[1] && args[2] && args[3] && args[4])
            controller->objEnter (info, args[0], args[1], atoi(args[2]),
                                  args[3], args[4]);
         else
            fprintf (stderr, "Incomplete line (enter):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "leave") == 0)
         // Args[1] may be NULL.
         controller->objLeave (info, args[0], args[1]);
      else if (strcmp (cmd, "create") == 0) {
         if (args[1])
            controller->objCreate (info, args[0], args[1]);
         else
            fprintf (stderr, "Incomplete line (create):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "ident") == 0) {
         if (args[1])
            controller->objIdent (info, args[0], args[1]);
         else
            fprintf (stderr, "Incomplete line (ident):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "assoc") == 0) {
         if (args[1])
            controller->objAssoc (info, args[0], args[1]);
         else
            fprintf (stderr, "Incomplete line (assoc):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "set") == 0) {
         if (args[1] && args[2])
            controller->objSet (info, args[0], args[1], args[2]);
         else
            fprintf (stderr, "Incomplete line (set):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "class-color") == 0) {
         if (args[1])
            // Notice the changed order.
            controller->objClassColor (info, args[0], args[1]);
         else
            fprintf (stderr, "Incomplete line (class-color):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "object-color") == 0) {
         if (args[1])
            controller->objObjectColor (info, args[0], args[1]);
         else
            fprintf (stderr, "Incomplete line (object-color):\n%s\n",
                     info->completeLine);
      } else if (strcmp (cmd, "delete") == 0)
         controller->objDelete (info, args[0]);
      else
         fprintf (stderr, "Unknown command identifier '%s':\n%s\n", cmd,
                  info->completeLine);
   }      
}

void ObjectsParser::setLinesSource (LinesSource *source)
{
   this->source = source;
}

void ObjectsParser::timeout (int type)
{
   PRINTF ("ObjectsParser::timeout (%d)", type);

   if (timeout::getCount (type) == 0)
      ; // ownTimeout (timeout::getActualType (type));
   else
      controller->timeout (timeout::decType (type));
}

void ObjectsParser::finish ()
{
   controller->finish ();
}

void ObjectsParser::setObjectsSink (ObjectsSink *sink)
{
   // This would be the controller passed in the constructor.
}

void ObjectsParser::addTimeout (double secs, int type)
{
   PRINTF ("ObjectsParser::addTimeout (%g, %d)", secs, type);

   if (source)
      source->addTimeout (secs, timeout::incType (type));
   else
      fprintf (stderr, "addTimeout (%g, %d): no source\n", secs, type);
}

void ObjectsParser::removeTimeout (int type)
{
   if (source)
      source->removeTimeout (timeout::incType (type));
   else
      fprintf (stderr, "removeTimeout (%d): no source\n", type);
}

} // namespace objects

} // namespace rtfl