aboutsummaryrefslogtreecommitdiff
path: root/xml.c
blob: cd04b34dc06a759324e5d9cc5fb938495b80eaff (plain) (blame)
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
#include <string.h>
#include <mxml.h>

#include "types.h"
#include "xml.h"

char *                    /* O - Buffer */
get_value(mxml_node_t *node,        /* I - Node to get */
         void        *buffer,        /* I - Buffer */
     int         buflen)        /* I - Size of buffer */
{
    char        *ptr,            /* Pointer into buffer */
    *end;            /* End of buffer */
    int        len;            /* Length of node */
    mxml_node_t    *current;        /* Current node */
    
    
    ptr = (char*)buffer;
    end = (char*)buffer + buflen - 1;
    char tempbuf[256];
    current = node->child;
    for (current = node->child; current && ptr < end; current = current->next)
    {
        if (current->type == MXML_TEXT)
        {
            if (current->value.text.whitespace)
                *ptr++ = ' ';
            
            len = (int)strlen(current->value.text.string);
            if (len > (int)(end - ptr))
                len = (int)(end - ptr);
            
            memcpy(ptr, current->value.text.string, len);
            ptr += len;
        }
        else if (current->type == MXML_OPAQUE)
        {
            len = (int)strlen(current->value.opaque);
            if (len > (int)(end - ptr))
                len = (int)(end - ptr);
            
            memcpy(ptr, current->value.opaque, len);
            ptr += len;
        }
        else if (current->type == MXML_INTEGER)
        {
            sprintf(tempbuf, "%d", current->value.integer);
            len = (int)strlen(tempbuf);
            if (len > (int)(end - ptr))
                len = (int)(end - ptr);
            
            memcpy(ptr, tempbuf, len);
            ptr += len;
        }
        else if (current->type == MXML_REAL)
        {
            sprintf(tempbuf, "%f", current->value.real);
            len = (int)strlen(tempbuf);
            if (len > (int)(end - ptr))
                len = (int)(end - ptr);
            
            memcpy(ptr, tempbuf, len);
            ptr += len;
        }
    }
    *ptr = 0;
    return buffer;
}