Added the functions `strjoin' and `escape_slashes' to `common.c'.
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/
6  * or modify it under the terms of the GNU General Public Li-
7  * cence as published by the Free Software Foundation; either
8  * version 2 of the Licence, or any later version.
9  *
10  * This program is distributed in the hope that it will be use-
11  * ful, but WITHOUT ANY WARRANTY; without even the implied war-
12  * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public Licence for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * Licence along with this program; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
18  * USA.
19  *
20  * Authors:
21  *   Florian octo Forster <octo at verplant.org>
22  *   Niki W. Waibel <niki.waibel@gmx.net>
23 **/
24
25 #ifndef COMMON_H
26 #define COMMON_H
27
28 #include "collectd.h"
29
30 #define sfree(ptr) \
31         if((ptr) != NULL) { \
32                 free(ptr); \
33         } \
34         (ptr) = NULL
35
36 void sstrncpy(char *d, const char *s, int len);
37 char *sstrdup(const char *s);
38 void *smalloc(size_t size);
39
40 /*
41  * NAME
42  *   strsplit
43  *
44  * DESCRIPTION
45  *   Splits a string into parts and stores pointers to the parts in `fields'.
46  *   The characters split at are ` ' (space) and "\t" (tab).
47  *
48  * PARAMETERS
49  *   `string'      String to split. This string will be modified. `fields' will
50  *                 contain pointers to parts of this string, so free'ing it
51  *                 will destroy `fields' as well.
52  *   `fields'      Array of strings where pointers to the parts will be stored.
53  *   `size'        Number of elements in the array. No more than `size'
54  *                 pointers will be stored in `fields'.
55  *
56  * RETURN VALUE
57  *    Returns the number of parts stored in `fields'.
58  */
59 int strsplit (char *string, char **fields, size_t size);
60
61 /*
62  * NAME
63  *   strjoin
64  *
65  * DESCRIPTION
66  *   Joins together several parts of a string using `sep' as a seperator. This
67  *   is equipollent to the perl buildin `join'.
68  *
69  * PARAMETERS
70  *   `dst'         Buffer where the result is stored.
71  *   `dst_len'     Length of the destination buffer. No more than this many
72  *                 bytes will be written to the memory pointed to by `dst',
73  *                 including the trailing null-byte.
74  *   `fields'      Array of strings to be joined.
75  *   `fields_num'  Number of elements in the `fields' array.
76  *   `sep'         String to be inserted between any two elements of `fields'.
77  *                 This string is neither prepended nor appended to the result.
78  *                 Instead of passing "" (empty string) one can pass NULL.
79  *
80  * RETURN VALUE
81  *   Returns the number of characters in `dst', NOT including the trailing
82  *   null-byte. If an error occured (empty array or `dst' too small) a value
83  *   smaller than zero will be returned.
84  */
85 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
86
87 /*
88  * NAME
89  *   escape_slashes
90  *
91  * DESCRIPTION
92  *   Removes slashes from the string `buf' and substitutes them with something
93  *   appropriate. This function should be used whenever a path is to be used as
94  *   (part of) an instance.
95  *
96  * PARAMETERS
97  *   `buf'         String to be escaped.
98  *   `buf_len'     Length of the buffer. No more then this many bytes will be
99  *   written to `buf', including the trailing null-byte.
100  *
101  * RETURN VALUE
102  *   Returns zero upon success and a value smaller than zero upon failure.
103  */
104 int escape_slashes (char *buf, int buf_len);
105
106 int rrd_update_file (char *host, char *file, char *values, char **ds_def,
107                 int ds_num);
108
109 #ifdef HAVE_LIBKSTAT
110 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
111 long long get_kstat_value (kstat_t *ksp, char *name);
112 #endif
113
114 #endif /* COMMON_H */