summaryrefslogtreecommitdiffstats
path: root/tinyXCAP/src/txcap_node.c
blob: ee1963f7b8fd7c62496cc684e428246aec985af5 (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
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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.
*
* DOUBANGO 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 DOUBANGO.
*
*/
/**@file txcap_node.c
 * @brief XCAP nodes.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *

 */
#include "tinyxcap/txcap_node.h"

#include "tsk_string.h"
#include "tsk_buffer.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

#include <string.h> /* strlen() */

/**@ingroup txcap_selector_group
* Gets the Node Url.
* @param auid_id The id of the AUID (e.g 'resource-lists').
* @param ... Node selection steps. You must use @a TXCAP_SELECTOR_NODE_SET*() macros to set these steps.
* The list of parameters must end with @ref TXCAP_SELECTOR_NODE_SET_NULL() even if there is no step.<br>
* @retval The Url of the node (e.g. 'resource-lists/list[2]').
*
* @code
char* node = txcap_selector_get_node("resource-lists",
		TXCAP_SELECTOR_NODE_SET_ATTRIBUTE("list", "name", "rcs"),
		TXCAP_SELECTOR_NODE_SET_ATTRIBUTE("entry", "uri", "sip:bob@example.com"),
		TXCAP_SELECTOR_NODE_SET_NULL());

		TSK_FREE(node);
* @endcode
*
* @sa @ref txcap_selector_get_node_2<br>@ref txcap_selector_get_document<br> @ref txcap_selector_get_document_2<br>@ref txcap_selector_get_url
*/
char* txcap_selector_get_node(const char* auid_id, ...)
{
    char* ret = tsk_null;
    va_list ap;

    va_start(ap, auid_id);
    ret = txcap_selector_get_node_2(auid_id, &ap);
    va_end(ap);

    return ret;
}

/**@ingroup txcap_selector_group
* Gets the Node Url.
* @param auid_id The id of the AUID (e.g 'resource-lists').
* @param app Node selection steps. You must use @a TXCAP_SELECTOR_NODE_SET*() macros to set these steps.
* The list of parameters must end with @ref TXCAP_SELECTOR_NODE_SET_NULL() even if there is no step.<br>
* @retval The Url of the node (e.g. 'resource-lists/list[2]').
*
* @code
* @endcode
*
* @sa @ref txcap_selector_get_node<br>@ref txcap_selector_get_document<br> @ref txcap_selector_get_document_2<br>@ref txcap_selector_get_url
*/
char* txcap_selector_get_node_2(const char* auid_id, va_list* app)
{
    char* ret = tsk_null;
    char* namespace = tsk_null;
    tsk_buffer_t* buffer = tsk_buffer_create_null();
    txcap_selector_param_type_t step;

    while((step = va_arg(*app, txcap_selector_param_type_t)) != xcapp_node_null) {
        switch(step) {
        case xcapp_node_name: {
            /* (const char*)QNAME_STR */
            const char* QNAME_STR = va_arg(*app, const char*);
            if(tsk_buffer_append_2(buffer, "/%s", QNAME_STR)) {
                goto bail;
            }
            break;
        }
        case xcapp_node_pos: {
            /* (const char*)QNAME_STR, (unsigned int)POS_UINT */
            const char* QNAME_STR = va_arg(*app, const char*);
            unsigned int POS_UINT = va_arg(*app, unsigned int);
            tsk_buffer_append_2(buffer, "/%s%%5B%u%%5D",
                                QNAME_STR, POS_UINT);
            break;
        }
        case xcapp_node_attribute: {
            /* (const char*)QNAME_STR, (const char*)ATT_QNAME_STR, (const char*)ATT_VALUE_STR */
            const char* QNAME_STR = va_arg(*app, const char*);
            const char* ATT_QNAME_STR = va_arg(*app, const char*);
            const char* ATT_VALUE_STR = va_arg(*app, const char*);
            tsk_buffer_append_2(buffer, "/%s%%5B@%s=%%22%s%%22%%5D",
                                QNAME_STR, ATT_QNAME_STR, ATT_VALUE_STR);
            break;
        }
        case xcapp_node_pos_n_attribute: {
            /* (const char*)QNAME_STR, (unsigned int)POS_UINT, (const char*)ATT_QNAME_STR, (const char*)ATT_VALUE_STR */
            const char* QNAME_STR = va_arg(*app, const char*);
            unsigned int POS_UINT = va_arg(*app, unsigned int);
            const char* ATT_QNAME_STR = va_arg(*app, const char*);
            const char* ATT_VALUE_STR = va_arg(*app, const char*);
            tsk_buffer_append_2(buffer, "/%s%%5B%u%%5D%%5B@%s=%%22%s%%22%%5D",
                                QNAME_STR, POS_UINT, ATT_QNAME_STR, ATT_VALUE_STR);
            break;
        }
        case xcapp_node_namespace: {
            /* (const char*)PREFIX_STR, (const char*)VALUE_STR */
            const char* PREFIX_STR = va_arg(*app, const char*);
            const char* VALUE_STR = va_arg(*app, const char*);
            char* temp = tsk_null;
            tsk_sprintf(&temp, "%sxmlns(%s=%%22%s%%22)",
                        namespace?"":"%3F",PREFIX_STR, VALUE_STR);
            tsk_strcat(&namespace, temp);
            TSK_FREE(temp);
            break;
        }
        default: {
            TSK_DEBUG_ERROR("NOT SUPPORTED.");
            goto bail;
        }
        } /* switch */
    } /* while */

    /* append the namespace */
    if(namespace) {
        tsk_buffer_append(buffer, namespace, tsk_strlen(namespace));
        TSK_FREE(namespace);
    }

bail:
    if(TSK_BUFFER_DATA(buffer) && TSK_BUFFER_SIZE(buffer)) {
        ret = tsk_strndup(TSK_BUFFER_DATA(buffer), TSK_BUFFER_SIZE(buffer));
    }
    TSK_OBJECT_SAFE_FREE(buffer);
    return ret;
}
OpenPOWER on IntegriCloud