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