summaryrefslogtreecommitdiffstats
path: root/runtime/map-str.c
blob: 2a6e84b6c2c8adb2e172bf509129ebfc0f391e94 (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
/* -*- linux-c -*- 
 * Map String Functions
 * Copyright (C) 2005 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

/** @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;
}

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

	if (map == NULL)
		return -2;

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

		m = __stp_map_create (map);
		if (!m)
			return -1;
		
		/* 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 -2;
		
		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);
		}
	}
	return 0;
}

/** 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.
 * @returns \li \c 0 on success \li \c -1 on overflow \li \c -2 on bad map or key
 * @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.
 * @returns \li \c 0 on success \li \c -1 on overflow \li \c -2 on bad map or key
 * @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.
 * @returns 0 on success, -1 on error.
 * @sa _stp_map_set()
 * @ingroup map_set
 */

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