summaryrefslogtreecommitdiff
path: root/sformat.c
blob: 08a18bd21cb1478e1065135ff7e7e2dc3831582a (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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* Sysprof -- Sampling, systemwide CPU profiler
 * Copyright 2004, Red Hat, Inc.
 * Copyright 2004, 2005, 2006, Soeren Sandmann
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <string.h>

#include "sformat.h"

typedef struct State State;
typedef struct Transition Transition;
typedef struct Fragment Fragment;

/*
 * Format
 */
struct SFormat
{
    State *	begin;
    State *	end;
    
    GQueue *	types;
    GQueue *	transitions;
    GQueue *	states;
};

/*
 * Type
 */
typedef enum
{
    TYPE_POINTER,
    TYPE_STRING,
    TYPE_INTEGER,
    TYPE_RECORD,
    TYPE_LIST,
    TYPE_FORWARD
} TypeKind;

struct SType
{
    TypeKind	kind;
    char       *name;
    Transition *enter, *exit;
    SType      *target;		    /* If kind is TYPE_POINTER */
};

static void type_free (SType *type);

/*
 * Transition
 */
typedef enum
{
    BEGIN,
    VALUE,
    END
} TransitionKind;

struct Transition
{
    SType *		type;
    TransitionKind	kind;
    State *		to;
};

static Transition *transition_new   (SFormat        *format,
				     TransitionKind  kind,
				     SType          *type,
				     State          *from,
				     State          *to);
static void transition_free         (Transition     *transition);
static void transition_set_to_state (Transition     *transition,
				     State          *to_state);

/*
 * State
 */ 

struct State
{
    GQueue *transitions;
};

static State *state_new            (SFormat    *format);
static void   state_add_transition (State      *state,
				    Transition *transition);
static void   state_free           (State      *state);

/*
 * Format
 */
SFormat *
sformat_new (void)
{
    /* FIXME: should probably be refcounted, and an SContext
     * should have a ref on the format
     */
    SFormat *format = g_new0 (SFormat, 1);
    
    format->begin = NULL;
    format->end = NULL;
    
    format->types = g_queue_new ();
    format->states = g_queue_new ();
    format->transitions = g_queue_new ();
    
    return format;
}

void
sformat_free (SFormat *format)
{
    GList *list;
    
    for (list = format->types->head; list; list = list->next)
    {
	SType *type = list->data;
	
	type_free (type);
    }
    g_queue_free (format->types);
    
    for (list = format->states->head; list; list = list->next)
    {
	State *state = list->data;
	
	state_free (state);
    }
    g_queue_free (format->states);
    
    for (list = format->transitions->head; list; list = list->next)
    {
	Transition *transition = list->data;
	
	transition_free (transition);
    }
    g_queue_free (format->transitions);
    
    g_free (format);
}

void
sformat_set_type (SFormat *format,
		  SType   *type)
{
    format->begin = state_new (format);
    format->end = state_new (format);
    
    state_add_transition (format->begin, type->enter);
    transition_set_to_state (type->exit, format->end);
}

/*
 * Type
 */
static SType *
type_new (SFormat *format,
	  TypeKind kind,
	  const char *name)
{
    SType *type = g_new0 (SType, 1);
    
    type->kind = kind;
    type->name = name? g_strdup (name) : NULL;
    type->enter = NULL;
    type->exit = NULL;
    type->target = NULL;
    
    g_queue_push_tail (format->types, type);
    
    return type;
}

static SType *
type_new_from_forward (SFormat *format,
		       TypeKind kind,
		       const char *name,
		       SForward *forward)
{
    SType *type;
    
    if (forward)
    {
	type = (SType *)forward;
	type->kind = kind;
	type->name = g_strdup (name);
    }
    else
    {
	type = type_new (format, kind, name);
    }
    
    return type;
}


static SType *
type_new_value (SFormat *format, TypeKind kind, const char *name)
{
    SType *type = type_new (format, kind, name);
    State *before, *after;
    Transition *value;
    
    before = state_new (format);
    after = state_new (format);
    
    type->enter = transition_new (format, BEGIN, type, NULL, before);
    type->exit  = transition_new (format, END, type, after, NULL);
    value = transition_new (format, VALUE, type, before, after);
    
    return type;
}

static void
type_free (SType *type)
{
    g_free (type->name);
    g_free (type);
}

SForward *
sformat_declare_forward (SFormat *format)
{
    SType *type = type_new (format, TYPE_FORWARD, NULL);
    
    return (SForward *)type;
}


static GQueue *
expand_varargs (SType *content1,
		va_list args)
{
    GQueue *types = g_queue_new ();
    SType *type;
    
    g_queue_push_tail (types, content1);
    
    type = va_arg (args, SType *);
    while (type)
    {
	g_queue_push_tail (types, type);
	type = va_arg (args, SType *);
    }
    
    return types;
}

SType *
sformat_make_record (SFormat *format,
		     const char *name,
		     SForward *forward,
		     SType *content,
		     ...)
{
    SType *type;
    va_list args;
    GQueue *types;
    GList *list;
    State *begin, *state;
    
    /* Build queue of child types */
    va_start (args, content);
    types = expand_varargs (content, args);
    va_end (args);
    
    /* chain types together */
    state = begin = state_new (format);
    
    for (list = types->head; list != NULL; list = list->next)
    {
        SType *child_type = list->data;
	
        state_add_transition (state, child_type->enter);
	
        state = state_new (format);
	
	transition_set_to_state (child_type->exit, state);
    }
    
    g_queue_free (types);
    
    /* create and return the new type */
    type = type_new_from_forward (format, TYPE_RECORD, name, forward);
    type->enter = transition_new (format, BEGIN, type, NULL, begin);
    type->exit = transition_new (format, END, type, state, NULL);
    
    return type;
}

SType *
sformat_make_list   (SFormat *format,
		     const char *name,
		     SForward *forward,
		     SType   *child_type)
{
    SType *type;
    State *list_state;

    type = type_new_from_forward (format, TYPE_LIST, name, forward);

    list_state = state_new (format);
    
    type->enter = transition_new (format, BEGIN, type, NULL, list_state);
    type->exit = transition_new (format, END, type, list_state, NULL);
    
    state_add_transition (list_state, child_type->enter);
    transition_set_to_state (child_type->exit, list_state);
    
    return type;
}

SType *
sformat_make_pointer (SFormat     *format,
		      const char  *name,
		      SForward    *forward)
{
    SType *type = type_new_value (format, TYPE_POINTER, name);
    type->target = (SType *)forward;
    
    return type;
}

SType *
sformat_make_integer (SFormat *format,
		      const    char *name)
{
    return type_new_value (format, TYPE_INTEGER, name);
}

SType *
sformat_make_string  (SFormat *format,
		      const    char *name)
{
    return type_new_value (format, TYPE_STRING, name);
}



gboolean
stype_is_record (SType *type)
{
    return type->kind == TYPE_RECORD;
}

gboolean
stype_is_list (SType *type)
{
    return type->kind == TYPE_LIST;
}

gboolean
stype_is_pointer (SType *type)
{
    return type->kind == TYPE_POINTER;
}

gboolean
stype_is_integer (SType *type)
{
    return type->kind == TYPE_INTEGER;
}

gboolean
stype_is_string (SType *type)
{
    return type->kind == TYPE_STRING;
}

SType *
stype_get_target_type (SType *type)
{
    g_return_val_if_fail (stype_is_pointer (type), NULL);
    
    return type->target;
}

const char *
stype_get_name (SType *type)
{
    return type->name;
}

/* Consider adding unions at some point
 *
 * To be useful they should probably be anonymous, so that
 * the union itself doesn't have a representation in the
 * xml file.
 *
 *     API:
 *            sformat_new_union (gpointer content1, ...);
 *
 *            char *content = begin_get_union ();
 *            if (strcmp (content, ...) == 0)
 *                      get_pointer ();
 *            else if (strcmp (content, ...) == 0)
 *
 *               ;
 *
 * Annoying though, that we then won't have the nice one-to-one
 * correspondence between begin()/end() calls and <element></element>s
 * Actually, we will probably have to have <union>asdlfkj</union>
 * elements. That will make things a lot easier, and unions are
 * still pretty useful if you put big things like lists in them.
 *
 * Or maybe just give them a name ...
 *
 * We may also consider adding anonymous records. These will
 * not be able to have pointers associated with them though
 * (because there wouldn't be a natural place
 *
 *
 * Also consider adding the following data types:
 *
 * - Binary blobs of data, stored as base64 perhaps
 * 
 * - floating point values. How do we store those portably
 *     without losing precision? Gnumeric may know.
 * 
 * - enums, stored as strings
 *
 * - booleans.
 */


/*
 * State
 */
static State *
state_new (SFormat *format)
{
    State *state = g_new0 (State, 1);
    state->transitions = g_queue_new ();
    
    g_queue_push_tail (format->states, state);
    
    return state;
}

static void
state_add_transition (State *state,
		      Transition *transition)
{
    g_queue_push_tail (state->transitions, transition);
}

static void
state_free (State *state)
{
    g_queue_free (state->transitions);
    g_free (state);
}

/*
 * Transition
 */
static Transition *
transition_new (SFormat *format,
		TransitionKind kind,
		SType *type,
		State *from,
		State *to)
{
    Transition *transition = g_new0 (Transition, 1);
    
    transition->type = type;
    transition->kind = kind;
    transition->to = to;
    
    if (from)
	state_add_transition (from, transition);
    
    g_queue_push_tail (format->transitions, transition);
    
    return transition;
}

static void
transition_free (Transition *transition)
{
    g_free (transition);
}

static void
transition_set_to_state (Transition *transition,
			 State      *to_state)
{
    transition->to = to_state;
}

/* Context */
struct SContext
{
    SFormat *format;
    State *state;
};

SContext *
scontext_new (SFormat *format)
{
    SContext *context = g_new0 (SContext, 1);
    
    context->format = format;
    context->state = format->begin;
    
    return context;
}

static SType *
do_transition (SContext *context,
	       TransitionKind kind,
	       const char *element)
{
    GList *list;
    
    for (list = context->state->transitions->head; list; list = list->next)
    {
	Transition *transition = list->data;
	
	if (transition->kind == kind)
	{
	    if (kind == VALUE || strcmp (transition->type->name, element) == 0)
	    {
		context->state = transition->to;
		return transition->type;
	    }
	}
    }
    
    return NULL;
}

SType *
scontext_begin (SContext *context,
		const char *element)
{
    return do_transition (context, BEGIN, element);
}

SType *
scontext_text (SContext *context)
{
    return do_transition (context, VALUE, NULL);
}

SType *
scontext_end (SContext *context,
	      const char *element)
{
    return do_transition (context, END, element);
}

gboolean
scontext_is_finished (SContext *context)
{
    return context->state == context->format->end;
}

void
scontext_free (SContext *context)
{
    g_free (context);
}



/* assorted stuff */
#if 0
static const State *
state_transition_check (const State *state,
                        const char *element,
                        TransitionKind kind,
                        SType *type)
{
    GList *list;
    
    for (list = state->transitions->head; list; list = list->next)
    {
        Transition *transition = list->data;
	
        if (transition->kind == kind                    &&
            strcmp (element, transition->element) == 0)
        {
            *type = transition->type;
            return transition->to;
        }
    }
    
    return NULL;
}

static const State *
state_transition_begin (const State *state, const char *element, SType *type)
{
    return state_transition_check (state, element, BEGIN, type);
}

static const State *
state_transition_end (const State *state, const char *element, SType *type)
{
    return state_transition_check (state, element, END, type);
}

static const State *
state_transition_text (const State *state, SType *type, SType *target_type)
{
    GList *list;
    
    for (list = state->transitions->head; list; list = list->next)
    {
        Transition *transition = list->data;
	
        if (transition->kind == VALUE)
        {
            *type = transition->type;
	    
	    if (*type == TYPE_POINTER && target_type)
		*target_type = transition->target_type;
	    
            /* There will never be more than one allowed value transition for
             * a given state
             */
            return transition->to;
        }
    }
    
    return NULL;
}

#endif