unixsock plugin: Moved the `parse_identifier' code to `common.c'
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005-2007  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 #define sfree(ptr) \
30         if((ptr) != NULL) { \
31                 free(ptr); \
32         } \
33         (ptr) = NULL
34
35 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
36
37 void sstrncpy(char *d, const char *s, int len);
38 char *sstrdup(const char *s);
39 void *smalloc(size_t size);
40 char *sstrerror (int errnum, char *buf, size_t buflen);
41
42 /*
43  * NAME
44  *   sread
45  *
46  * DESCRIPTION
47  *   Reads exactly `n' bytes or failes. Syntax and other behavior is analogous
48  *   to `read(2)'. If EOF is received the file descriptor is closed and an
49  *   error is returned.
50  *
51  * PARAMETERS
52  *   `fd'          File descriptor to write to.
53  *   `buf'         Buffer that is to be written.
54  *   `count'       Numver of bytes in the buffer.
55  *
56  * RETURN VALUE
57  *   Zero upon success or non-zero if an error occured. `errno' is set in this
58  *   case.
59  */
60 ssize_t sread (int fd, void *buf, size_t count);
61
62 /*
63  * NAME
64  *   swrite
65  *
66  * DESCRIPTION
67  *   Writes exactly `n' bytes or failes. Syntax and other behavior is analogous
68  *   to `write(2)'.
69  *
70  * PARAMETERS
71  *   `fd'          File descriptor to write to.
72  *   `buf'         Buffer that is to be written.
73  *   `count'       Numver of bytes in the buffer.
74  *
75  * RETURN VALUE
76  *   Zero upon success or non-zero if an error occured. `errno' is set in this
77  *   case.
78  */
79 ssize_t swrite (int fd, const void *buf, size_t count);
80
81 /*
82  * NAME
83  *   strsplit
84  *
85  * DESCRIPTION
86  *   Splits a string into parts and stores pointers to the parts in `fields'.
87  *   The characters split at are ` ' (space) and "\t" (tab).
88  *
89  * PARAMETERS
90  *   `string'      String to split. This string will be modified. `fields' will
91  *                 contain pointers to parts of this string, so free'ing it
92  *                 will destroy `fields' as well.
93  *   `fields'      Array of strings where pointers to the parts will be stored.
94  *   `size'        Number of elements in the array. No more than `size'
95  *                 pointers will be stored in `fields'.
96  *
97  * RETURN VALUE
98  *    Returns the number of parts stored in `fields'.
99  */
100 int strsplit (char *string, char **fields, size_t size);
101
102 /*
103  * NAME
104  *   strjoin
105  *
106  * DESCRIPTION
107  *   Joins together several parts of a string using `sep' as a seperator. This
108  *   is equipollent to the perl buildin `join'.
109  *
110  * PARAMETERS
111  *   `dst'         Buffer where the result is stored.
112  *   `dst_len'     Length of the destination buffer. No more than this many
113  *                 bytes will be written to the memory pointed to by `dst',
114  *                 including the trailing null-byte.
115  *   `fields'      Array of strings to be joined.
116  *   `fields_num'  Number of elements in the `fields' array.
117  *   `sep'         String to be inserted between any two elements of `fields'.
118  *                 This string is neither prepended nor appended to the result.
119  *                 Instead of passing "" (empty string) one can pass NULL.
120  *
121  * RETURN VALUE
122  *   Returns the number of characters in `dst', NOT including the trailing
123  *   null-byte. If an error occured (empty array or `dst' too small) a value
124  *   smaller than zero will be returned.
125  */
126 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
127
128 /*
129  * NAME
130  *   escape_slashes
131  *
132  * DESCRIPTION
133  *   Removes slashes from the string `buf' and substitutes them with something
134  *   appropriate. This function should be used whenever a path is to be used as
135  *   (part of) an instance.
136  *
137  * PARAMETERS
138  *   `buf'         String to be escaped.
139  *   `buf_len'     Length of the buffer. No more then this many bytes will be
140  *   written to `buf', including the trailing null-byte.
141  *
142  * RETURN VALUE
143  *   Returns zero upon success and a value smaller than zero upon failure.
144  */
145 int escape_slashes (char *buf, int buf_len);
146
147 int strsubstitute (char *str, char c_from, char c_to);
148
149 /* FIXME: `timeval_sub_timespec' needs a description */
150 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
151
152 int check_create_dir (const char *file_orig);
153
154 #ifdef HAVE_LIBKSTAT
155 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
156 long long get_kstat_value (kstat_t *ksp, char *name);
157 #endif
158
159 unsigned long long ntohll (unsigned long long n);
160 unsigned long long htonll (unsigned long long n);
161
162 int format_name (char *ret, int ret_len,
163                 const char *hostname,
164                 const char *plugin, const char *plugin_instance,
165                 const char *type, const char *type_instance);
166 #define FORMAT_VL(ret, ret_len, vl, ds) \
167         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
168                         (ds)->type, (vl)->type_instance)
169
170 int parse_identifier (char *str, char **ret_host,
171                 char **ret_plugin, char **ret_plugin_instance,
172                 char **ret_type, char **ret_type_instance);
173 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
174
175 #endif /* COMMON_H */