summaryrefslogtreecommitdiffstats
path: root/stack/xml/xml_bld.c
blob: 57a7347e98bea17cecae7068a3bf18598e703d36 (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
/*****************************************************************************
**
**  Name:       xml_bld_api.c
**
**  File:       Implements the XML Builder
**
**
**  Copyright (c) 2000-2004, WIDCOMM Inc., All Rights Reserved.
**  WIDCOMM Bluetooth Core. Proprietary and confidential.
**
*****************************************************************************/

#include <string.h>
#include <stdio.h>

#include "bt_target.h"
#include "xml_bld_api.h"

/* The XML Builder is dependent on the Object Store. At present
** the object store resides in GOEP and hence the builder is 
** dependent on GOEP. The builder only uses the Object Store
** in GOEP, so if the Object Store is separated from GOEP in the
** future, the builder will not be dependent on GOEP.
*/

/*****************************************************************************
**   Constants
*****************************************************************************/
#define XML_ST  "<"
#define XML_EM  "/"
#define XML_SP  " "
#define XML_CO  ":"
#define XML_EQ  "="
#define XML_GT  ">"
#define XML_QT  "\""

#define XML_LF  "\n"    /* Line feed */

/*****************************************************************************
**   Interface functions
*****************************************************************************/


/*****************************************************************************
**
**  Function:    XML_BufAddTag
**
**  Purpose:     Write a start or end tag and optional prefix.
**               
**  Parameters:  
**       UINT8       **pp_buf     reference to the storage to hold the XML object
**                                GOEP_WriteStore.
**       const UINT8*  prefix     tag prefix (namespace)
**       const UINT8*  tag        tag name
**       BOOLEAN       start_tag  TRUE = start tag, FALSE = end tag
**       BOOLEAN       has_attr   TRUE if the tag contains attributes
**       
**  Returns:     XML_BLD_SUCCESS if success
**
*****************************************************************************/
tXML_BLD_RESULT XML_BufAddTag (UINT8    **pp_buf, 
                            const UINT8 *prefix,
                            const UINT8 *tag, 
                            BOOLEAN     start_tag,
                            BOOLEAN     has_attr)
{
    UINT16 status = XML_BLD_ERROR;
    int n;

    if (tag != NULL)
    {
        if(start_tag)
            n = sprintf((char *)*pp_buf, XML_ST);
        else
            n = sprintf((char *)*pp_buf, XML_ST XML_EM);
        *pp_buf += n;

        if (prefix != NULL)
        {
            n = sprintf((char *)*pp_buf, "%s" XML_CO, prefix );
            *pp_buf += n;
        }
        n = sprintf((char *)*pp_buf, "%s" , tag );
        *pp_buf += n;
        if(!has_attr)
        {
            n = sprintf((char *)*pp_buf, XML_GT);
            *pp_buf += n;
            if (!start_tag)
            {
                n = sprintf((char *)*pp_buf, XML_LF);
                *pp_buf += n;
            }
        }

        status = XML_BLD_SUCCESS;
    }
    return status;
}



/*****************************************************************************
**
**  Function:    XML_BufAddAttribute
**
**  Purpose:     Write an attribute and optional prefix.
**               
**  Parameters:  
**       UINT8       **pp_buf     reference to the storage to hold the XML object
**       const UINT8*  prefix     attribute prefix (namespace)
**       const UINT8*  attr_name  attribute name
**       const UINT8*  attr_value attribute value
**       
**  Returns:     XML_BLD_SUCCESS if success
**
*****************************************************************************/
tXML_BLD_RESULT XML_BufAddAttribute (UINT8    **pp_buf, 
                                  const UINT8 *prefix,
                                  const UINT8 *attr_name, 
                                  const UINT8 *attr_value, 
                                  tXML_ATTR_END     last_attr)
{
    UINT16 status = XML_BLD_ERROR;
    int n;

    if (attr_name != NULL && attr_value != NULL)
    {
        n = sprintf((char *)*pp_buf, XML_SP);
        *pp_buf += n;
        if (prefix != NULL)
        {
            n = sprintf((char *)*pp_buf, "%s" XML_CO, prefix );
            *pp_buf += n;
        }
        n = sprintf((char *)*pp_buf, "%s" XML_EQ XML_QT "%s", attr_name, attr_value );
        *pp_buf += n;
        switch(last_attr)
        {
        case XML_ATTR_CONT:
            n = sprintf((char *)*pp_buf, XML_QT );
            break;
        case XML_ATTR_LAST:
            n = sprintf((char *)*pp_buf, XML_QT XML_GT XML_LF );
            break;
        case XML_ATTR_ETAG:
            n = sprintf((char *)*pp_buf, XML_QT XML_EM XML_GT XML_LF );
            break;
        default:
            n = 0;
            break;
        }
        *pp_buf += n;
        status = XML_BLD_SUCCESS;
    }
    else if(last_attr == XML_ATTR_ETAG)
    {
        /* allow the call to only add the closing attribute */
        n = sprintf((char *)*pp_buf, XML_EM XML_GT XML_LF );
        *pp_buf += n ;
        status = XML_BLD_SUCCESS;
    }
    return status;
}

/*****************************************************************************
**
**  Function:    XML_BufAddCharData
**
**  Purpose:     Write the element content.
**               
**  Parameters:  
**       UINT8       **pp_buf     reference to the storage to hold the XML object
**       const UINT8*  content    element content
**       
**  Returns:     XML_BLD_SUCCESS if success
**
*****************************************************************************/
tXML_BLD_RESULT XML_BufAddCharData (UINT8 **pp_buf, const UINT8 *charData)
{
    UINT16 status = XML_BLD_ERROR;
    int n;

    if (charData != NULL)
    {
        n = sprintf((char *)*pp_buf, "%s",  charData);
        *pp_buf += n;
        status = XML_BLD_SUCCESS;
    }
    return status;
}