aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libvotable/votAttr.c
blob: f94f84c0ebd62f1b5dc463ee35efdfc4b118a2df (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
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
/**
 *  VOTATTRIBUTE.C -- (Private) Methods to manage XML attributes.
 *
 *  @file       votAttribute.c
 *  @author     Mike Fitzpatrick and Eric Timmermann
 *  @date       8/03/09
 *
 *  @brief  	(Private) Methods to manage XML attributes.
 */

#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "votParseP.h"

extern char  *strcasestr();


/** 
 *  vot_attrSet -- Set/Create an attributes (private method).
 *
 *  @brief  Set/Create an attributes (private method)
 *  @fn	    status = vot_attrSet (AttrBlock *ablock, char *name, char *value)
 *
 *  @param  ablock 	An AttrBlock to insert these attributes.
 *  @param  name 	A string that hold the name of an attribute.
 *  @param  value 	A string that hold the value of an attribute.
 *  @return 		The status of the request.  1 Success, 0=FAIL.
 *
 *  @warning If an attribute has no name/value, this will not create it.
 */
int 
vot_attrSet (AttrBlock *ablock, char *name, char *value)
{
    char  *name_m = NULL;
    int   value_found = 0, value_existing = 0;
    AttrList *attr = (ablock ? ablock->attributes : (AttrList *) NULL);

    
    if (name == NULL)
        return (0);
    name_m = strdup (name);
    
    /* Check for namespace qualifiers on the attribute.
     */
    if ((name_m[0] && strchr(name_m, (int)':')) || strcmp("xmlns", name_m) == 0)
        value_found = 1;

    /* Check for an 'xtype' attribute in the v1.1+ spec.
     */
    if (name_m[0] && strcmp("xtype", name_m) == 0)
        value_found = 1;

    if (ablock->req && strcasestr (ablock->req, name_m) != NULL)
	value_found = 1;
    else if (ablock->opt && strcasestr (ablock->opt, name_m) != NULL)
	value_found = 1;

    if (!value_found) {
#ifdef USE_STRICT
	fprintf (stderr, "Error: '%s' not a valid Attribute.\n", name);
        return (0);
#else
        return (1);
#endif

    } else {
	while (attr != NULL) {
            if (name_m[0] && strcasecmp (attr->name, name_m) == 0) {
                strncpy (attr->value, value, min (strlen (value), SZ_ATTRVAL));
                value_existing = 1;
            }
            attr = attr->next;
        }

        if (!value_existing) {
            attr = (AttrList *) calloc (1, sizeof(AttrList));
            if (ablock->attributes == NULL) {
		attr->next = NULL;
                strncpy (attr->value, value, min (strlen (value), SZ_ATTRVAL));
                strcpy (attr->name, name_m);
	    } else {
                attr = (AttrList *) calloc (1, sizeof(AttrList));
                attr->next = ablock->attributes;
                strncpy (attr->value, value, min (strlen (value), SZ_ATTRVAL));
                strcpy (attr->name, name_m);
	    }
	    ablock->attributes = attr;
        }
    }	
    
    if (name_m != NULL)
        free (name_m);
    
    return (1);
}


/** 
 *  vot_attrGet -- Get an attribute's value (private method).
 *
 *  @brief  Get an attribute's value (private method)
 *  @fn	    char *vot_attrGet (AttrBlock *ablock, char *name)
 *
 *  @param  *ablock 	An AttrBlock to insert these attributes
 *  @param  *name 	A string that hold the name of an attribute
 *  @return 		Value of the attribute or NULL
 */
char *
vot_attrGet (AttrBlock *ablock, char *name)
{
    char *value;
    AttrList *attr = (ablock ? ablock->attributes : (AttrList *) NULL);
    
    while (attr != NULL) {
        if (strcasecmp (attr->name, name) == 0) {
            value = (char *) calloc (SZ_ATTRNAME, strlen(attr->value)+1);
            
            strncpy (value, attr->value, strlen (attr->value));
	    if (value && value[0])
                return (value);
	    else 
		return (NULL);
        }
        attr = attr->next;
    }
    
    return (NULL);
}


/** 
 *  vot_attrXML -- Get the attributes for an XML tag (private method).
 *
 *  @brief  Get the attributes for an XML tag (private method)
 *  @fn	    char *vot_attrXML (AttrBlock *ablock)
 *
 *  @param *ablock 	An AttrBlock to insert these attributes
 *  @return 		A string containing the attributes for an XML tag
 */
char * 
vot_attrXML (AttrBlock *ablock)
{
    char  *out = (char *) calloc (SZ_XMLTAG, sizeof (char));
    AttrList *attr = (ablock ? ablock->attributes : (AttrList *) NULL);
    
    while (attr != NULL) {

        /* Privately used attribute.  It is not valid. */
        if (strcasecmp (attr->name, "NCOLS") != 0 &&
            strcasecmp (attr->name, "NROWS") != 0) {
		    
		if ((attr->value && attr->value[0]) ||
		    (strcasecmp (attr->name, "value") == 0)) {
                        strcat (out, " ");
                        strcat (out, attr->name);
                        strcat (out, "=\"");
                        strcat (out, attr->value);
                        strcat (out, "\"");
		}
        }
        
        attr = attr->next;
    }
    
    return (out);
}