summaryrefslogtreecommitdiffstats
path: root/runtime/map-str.c
blob: e5e5b5e4e1b7060f4a46c4e4b42c7a426f048584 (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
/* -*- linux-c -*- */

/** @file map-str.c
 * @brief Map functions to set and get strings
 */

/* from map.c */
void str_copy(char *dest, char *src);

void str_add(void *dest, char *val)
{
	char *dst = (char *)dest;
	int len = strlen(val);
	int len1 = strlen(dst);
	int num = MAP_STRING_LENGTH - 1 - len1;

	if (len > num)
		len = num;
	strncpy (&dst[len1], val, len);
	dst[len + len1] = 0;
}

void __stp_map_set_str (MAP map, char *val, int add)
{
	struct map_node *m;

	if (map == NULL)
		return;

	if (map->create) {
		if (val == 0 && !map->no_wrap)
			return;

		m = __stp_map_create (map);
		if (!m)
			return;
		
		/* set the value */
		//dbug ("m=%lx offset=%lx\n", (long)m, (long)map->data_offset);
		str_copy((void *)((long)m + map->data_offset), val);
	} else {
		if (map->key == NULL)
			return;
		
		if (val) {
			if (add)
				str_add((void *)((long)map->key + map->data_offset), val);
			else
				str_copy((void *)((long)map->key + map->data_offset), val);
		} else if (!add) {
			/* setting value to 0 is the same as deleting */
			_stp_map_key_del(map);
		}
	}
}

/** Set the current element's value to a string.
 * This sets the current element's value to a string. The map must have been created
 * to hold strings using <i>_stp_map_new(xxx, STRING)</i>
 *
 * If the element doesn't exist, it is created.  If no current element (key)
 * is set for the map, this function does nothing.
 * @param map
 * @param str String containing new value.
 * @sa _stp_map_set()
 * @ingroup map_set
 */
#define _stp_map_set_str(map,val) __stp_map_set_str(map,val,0)
/** Add to the current element's string value.
 * This sets the current element's value to a string consisting of the old
 * contents followed by the new string. The map must have been created
 * to hold strings using <i>_stp_map_new(xxx, STRING)</i>
 *
 * If the element doesn't exist, it is created.  If no current element (key)
 * is set for the map, this function does nothing.
 * @param map
 * @param val String containing value to append.
 * @ingroup map_set
 */
#define _stp_map_add_str(map,val) __stp_map_set_str(map,val,1)

/** Get the current element's string value.
 * This gets the current element's string value. The map must have been created
 * to hold strings using <i>_stp_map_new(xxx, STRING)</i>
 *
 * If no current element (key) is set for the map, this function 
 * returns NULL.
 * @param map
 * @sa _stp_map_set()
 * @ingroup map_set
 */
char *_stp_map_get_str (MAP map)
{
	struct map_node *m;
	if (map == NULL || map->create || map->key == NULL)
		return 0;
	//dbug ("key %lx\n", (long)map->key);
	m = (struct map_node *)map->key;
	return (char *)((long)m + map->data_offset);
}

/** Set the current element's value to String.
 * This sets the current element's value to a String. The map must have been created
 * to hold strings using <i>_stp_map_new(xxx, STRING)</i>
 *
 * If the element doesn't exist, it is created.  If no current element (key)
 * is set for the map, this function does nothing.
 * @param map
 * @param str String containing new value.
 * @sa _stp_map_set()
 * @ingroup map_set
 */

void _stp_map_set_string (MAP map, String str)
{
	__stp_map_set_str (map, str->buf, 0);
}