Unified string handling.
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005-2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21 **/
22
23 #ifndef COMMON_H
24 #define COMMON_H
25
26 #include "collectd.h"
27 #include "plugin.h"
28
29 #if HAVE_PWD_H
30 # include <pwd.h>
31 #endif
32
33 #define sfree(ptr) \
34         if((ptr) != NULL) { \
35                 free(ptr); \
36         } \
37         (ptr) = NULL
38
39 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
40
41 char *sstrncpy (char *dest, const char *src, size_t n);
42 int ssnprintf (char *dest, size_t n, const char *format, ...);
43 char *sstrdup(const char *s);
44 void *smalloc(size_t size);
45 char *sstrerror (int errnum, char *buf, size_t buflen);
46
47 /*
48  * NAME
49  *   sread
50  *
51  * DESCRIPTION
52  *   Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
53  *   to `read(2)'. If EOF is received the file descriptor is closed and an
54  *   error is returned.
55  *
56  * PARAMETERS
57  *   `fd'          File descriptor to write to.
58  *   `buf'         Buffer that is to be written.
59  *   `count'       Number of bytes in the buffer.
60  *
61  * RETURN VALUE
62  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
63  *   case.
64  */
65 ssize_t sread (int fd, void *buf, size_t count);
66
67 /*
68  * NAME
69  *   swrite
70  *
71  * DESCRIPTION
72  *   Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
73  *   to `write(2)'.
74  *
75  * PARAMETERS
76  *   `fd'          File descriptor to write to.
77  *   `buf'         Buffer that is to be written.
78  *   `count'       Number of bytes in the buffer.
79  *
80  * RETURN VALUE
81  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
82  *   case.
83  */
84 ssize_t swrite (int fd, const void *buf, size_t count);
85
86 /*
87  * NAME
88  *   strsplit
89  *
90  * DESCRIPTION
91  *   Splits a string into parts and stores pointers to the parts in `fields'.
92  *   The characters split at are ` ' (space) and "\t" (tab).
93  *
94  * PARAMETERS
95  *   `string'      String to split. This string will be modified. `fields' will
96  *                 contain pointers to parts of this string, so free'ing it
97  *                 will destroy `fields' as well.
98  *   `fields'      Array of strings where pointers to the parts will be stored.
99  *   `size'        Number of elements in the array. No more than `size'
100  *                 pointers will be stored in `fields'.
101  *
102  * RETURN VALUE
103  *    Returns the number of parts stored in `fields'.
104  */
105 int strsplit (char *string, char **fields, size_t size);
106
107 /*
108  * NAME
109  *   strjoin
110  *
111  * DESCRIPTION
112  *   Joins together several parts of a string using `sep' as a separator. This
113  *   is equivalent to the Perl built-in `join'.
114  *
115  * PARAMETERS
116  *   `dst'         Buffer where the result is stored.
117  *   `dst_len'     Length of the destination buffer. No more than this many
118  *                 bytes will be written to the memory pointed to by `dst',
119  *                 including the trailing null-byte.
120  *   `fields'      Array of strings to be joined.
121  *   `fields_num'  Number of elements in the `fields' array.
122  *   `sep'         String to be inserted between any two elements of `fields'.
123  *                 This string is neither prepended nor appended to the result.
124  *                 Instead of passing "" (empty string) one can pass NULL.
125  *
126  * RETURN VALUE
127  *   Returns the number of characters in `dst', NOT including the trailing
128  *   null-byte. If an error occurred (empty array or `dst' too small) a value
129  *   smaller than zero will be returned.
130  */
131 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
132
133 /*
134  * NAME
135  *   escape_slashes
136  *
137  * DESCRIPTION
138  *   Removes slashes from the string `buf' and substitutes them with something
139  *   appropriate. This function should be used whenever a path is to be used as
140  *   (part of) an instance.
141  *
142  * PARAMETERS
143  *   `buf'         String to be escaped.
144  *   `buf_len'     Length of the buffer. No more then this many bytes will be
145  *   written to `buf', including the trailing null-byte.
146  *
147  * RETURN VALUE
148  *   Returns zero upon success and a value smaller than zero upon failure.
149  */
150 int escape_slashes (char *buf, int buf_len);
151
152 int strsubstitute (char *str, char c_from, char c_to);
153
154 /* FIXME: `timeval_sub_timespec' needs a description */
155 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
156
157 int check_create_dir (const char *file_orig);
158
159 #ifdef HAVE_LIBKSTAT
160 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
161 long long get_kstat_value (kstat_t *ksp, char *name);
162 #endif
163
164 unsigned long long ntohll (unsigned long long n);
165 unsigned long long htonll (unsigned long long n);
166
167 #if FP_LAYOUT_NEED_NOTHING
168 # define ntohd(d) (d)
169 # define htond(d) (d)
170 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
171 double ntohd (double d);
172 double htond (double d);
173 #else
174 # error "Don't know how to convert between host and network representation of doubles."
175 #endif
176
177 int format_name (char *ret, int ret_len,
178                 const char *hostname,
179                 const char *plugin, const char *plugin_instance,
180                 const char *type, const char *type_instance);
181 #define FORMAT_VL(ret, ret_len, vl, ds) \
182         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
183                         (ds)->type, (vl)->type_instance)
184
185 int parse_identifier (char *str, char **ret_host,
186                 char **ret_plugin, char **ret_plugin_instance,
187                 char **ret_type, char **ret_type_instance);
188 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
189
190 #if !HAVE_GETPWNAM_R
191 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
192                 size_t buflen, struct passwd **pwbufp);
193 #endif
194
195 int notification_init (notification_t *n, int severity, const char *message,
196                 const char *host,
197                 const char *plugin, const char *plugin_instance,
198                 const char *type, const char *type_instance);
199 #define NOTIFICATION_INIT_VL(n, vl, ds) \
200         notification_init (n, NOTIF_FAILURE, NULL, \
201                         (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
202                         (ds)->type, (vl)->type_instance)
203 #endif /* COMMON_H */