summaryrefslogtreecommitdiff
path: root/ext/syck/emitter.c
blob: 1927b7e1e288f77214247b1882a015acf82f1b2d (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
/*
 * emitter.c
 *
 * $Author$
 * $Date$
 *
 * Copyright (C) 2003 why the lucky stiff
 */
#include <stdio.h>
#include <string.h>

#include "syck.h"

#define DEFAULT_ANCHOR_FORMAT "id%03d"

struct adjust_arg {
    /* Position to start adjusting */
    long startpos;
    /* Adjusting by an offset */
    int offset;
};

/*
 * Allocate an emitter
 */
SyckEmitter *
syck_new_emitter()
{
    SyckEmitter *e;
    e = S_ALLOC( SyckEmitter );
    e->headless = 0;
    e->seq_map = 0;
    e->use_header = 0;
    e->use_version = 0;
    e->sort_keys = 0;
    e->anchor_format = NULL;
    e->explicit_typing = 0;
    e->best_width = 80;
    e->block_style = block_arbitrary;
    e->stage = doc_open;
    e->indent = 2;
    e->level = -1;
    e->ignore_id = 0;
    e->anchors = NULL;
    e->markers = NULL;
    e->bufsize = SYCK_BUFFERSIZE;
    e->buffer = NULL;
    e->marker = NULL;
    e->bufpos = 0;
    e->handler = NULL;
    e->bonus = NULL;
    return e;
}

int
syck_st_free_anchors( char *key, char *name, char *arg )
{
    S_FREE( name );
    return ST_CONTINUE;
}

int
syck_st_free_markers( char *key, SyckEmitterNode *n, char *arg )
{
    S_FREE( n );
    return ST_CONTINUE;
}

void
syck_emitter_st_free( SyckEmitter *e )
{
    /*
     * Free the anchor tables
     */
    if ( e->anchors != NULL )
    {
        st_foreach( e->anchors, syck_st_free_anchors, 0 );
        st_free_table( e->anchors );
        e->anchors = NULL;
    }

    /*
     * Free the markers tables
     */
    if ( e->markers != NULL )
    {
        st_foreach( e->markers, syck_st_free_markers, 0 );
        st_free_table( e->markers );
        e->markers = NULL;
    }
}

void
syck_emitter_ignore_id( SyckEmitter *e, SYMID id )
{
    e->ignore_id = id;
}

void
syck_emitter_handler( SyckEmitter *e, SyckOutputHandler hdlr )
{
    e->handler = hdlr;
}

void
syck_free_emitter( SyckEmitter *e )
{
    /*
     * Free tables
     */
    syck_emitter_st_free( e );
    if ( e->buffer != NULL )
    {
        S_FREE( e->buffer );
    }
    S_FREE( e );
}

void
syck_emitter_clear( SyckEmitter *e )
{
    if ( e->buffer == NULL )
    {
        e->buffer = S_ALLOC_N( char, e->bufsize );
        S_MEMZERO( e->buffer, char, e->bufsize );
    }
    e->buffer[0] = '\0';
    e->marker = e->buffer;
    e->bufpos = 0;
}

/*
 * Raw write to the emitter buffer.
 */
void
syck_emitter_write( SyckEmitter *e, char *str, long len )
{
    long at;
    ASSERT( str != NULL )
    if ( e->buffer == NULL )
    {
        syck_emitter_clear( e );
    }
    
    /*
     * Flush if at end of buffer
     */
    at = e->marker - e->buffer;
    if ( len + at > e->bufsize )
    {
        syck_emitter_flush( e );
    }

    /*
     * Write to buffer
     */
    S_MEMCPY( e->marker, str, char, len );
    e->marker += len;
}

/*
 * Write a chunk of data out.
 */
void
syck_emitter_flush( SyckEmitter *e )
{
    if ( ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) || 
         e->stage == doc_need_header )
    {
        if ( e->use_version == 1 )
        {
            char *header = S_ALLOC_N( char, 64 );
            S_MEMZERO( header, char, 64 );
            sprintf( header, "--- %YAML:%d.%d ", SYCK_YAML_MAJOR, SYCK_YAML_MINOR );
            (e->handler)( e, header, strlen( header ) );
            S_FREE( header );
        }
        else
        {
            (e->handler)( e, "--- ", 4 );
        }
        e->stage = doc_processing;
    }
    (e->handler)( e, e->buffer, e->marker - e->buffer );
    e->bufpos += e->marker - e->buffer;
    e->marker = e->buffer;
}

/*
 * Emit a simple, unquoted string.
 */
void
syck_emitter_simple( SyckEmitter *e, char *str, long len )
{
    e->seq_map = 0;
    syck_emitter_write( e, str, len );
}

/*
 * Shift the offsets of all applicable anchors
 */
int
syck_adjust_anchors( char *key, SyckEmitterNode *n, struct adjust_arg *arg )
{
    if ( arg->startpos >= n->pos )
    {
        n->pos += arg->offset + 1;
    }
    return ST_CONTINUE;
}

/*
 * call on start of an object's marshalling
 * (handles anchors, returns an alias)
 */
char *
syck_emitter_start_obj( SyckEmitter *e, SYMID oid )
{
    SyckEmitterNode *n = NULL;
    char *anchor_name = NULL;

    e->level++;
    if ( oid != e->ignore_id )
    {
        /*
         * Look for anchors
         */
        if ( e->markers == NULL )
        {
            e->markers = st_init_numtable();
        }

        /*
         * Markers table initially marks the string position of the
         * object.  Doesn't yet create an anchor, simply notes the
         * position.
         */
        if ( ! st_lookup( e->markers, (st_data_t)oid, (st_data_t *)&n ) )
        {
            /*
             * Store all markers
             */
            n = S_ALLOC( SyckEmitterNode );
            n->is_shortcut = 0;
            n->indent = e->level * e->indent;
            n->pos = e->bufpos + ( e->marker - e->buffer );
            st_insert( e->markers, (st_data_t)oid, (st_data_t)n );
        }
        else
        {
            if ( e->anchors == NULL )
            {
                e->anchors = st_init_numtable();
            }

            if ( ! st_lookup( e->anchors, (st_data_t)oid, (st_data_t *)&anchor_name ) )
            {
                int idx = 0;
                /*
                 * Second time hitting this object, let's give it an anchor
                 */
                idx = e->anchors->num_entries + 1;

                /*
                 * Create the anchor tag
                 */
                if ( n->pos >= e->bufpos )
                {
                    int alen;
                    struct adjust_arg *args = S_ALLOC( struct adjust_arg );
                    char *start = e->buffer + ( n->pos - e->bufpos );

                    char *anc = ( e->anchor_format == NULL ? DEFAULT_ANCHOR_FORMAT : e->anchor_format );
                    char *aname = S_ALLOC_N( char, strlen( anc ) + 10 );
                    S_MEMZERO( aname, char, strlen( anc ) + 10 );
                    sprintf( aname, anc, idx );

                    /*
                     * Write the anchor into the buffer
                     * FIXME: Need to flush the buffer some, if there is not room for the anchor.
                     */
                    alen = strlen( aname );
                    S_MEMMOVE( start + alen + 1, start, char, e->marker - start );
                    S_MEMCPY( start + 1, aname, char, alen );
                    start[0] = '&';
                    e->marker += alen + 1;

                    /*
                     * Cycle through anchors, modify for the size of the anchor.
                     */
                    args->startpos = n->pos;
                    args->offset = alen + 1;
                    st_foreach( e->anchors, syck_adjust_anchors, (st_data_t)args );
                    S_FREE( args );

                    /*
                     * Insert into anchors table
                     */
                    st_insert( e->anchors, (st_data_t)oid, (st_data_t)aname );
                }
            }

        }
    }

    return anchor_name;
}

/*
 * call on completion of an object's marshalling
 */
void
syck_emitter_end_obj( SyckEmitter *e )
{
    e->level--;
}