curly wars / auto-indentation
[oweals/gnunet.git] / src / include / gnunet_strings_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_strings_lib.h
23  * @brief strings and string handling functions (including malloc
24  *        and string tokenizing)
25  *
26  * @author Christian Grothoff
27  * @author Krista Bennett
28  * @author Gerd Knorr <kraxel@bytesex.org>
29  * @author Ioana Patrascu
30  * @author Tzvetan Horozov
31  */
32
33 #ifndef GNUNET_STRINGS_LIB_H
34 #define GNUNET_STRINGS_LIB_H
35
36 /* we need size_t, and since it can be both unsigned int
37    or unsigned long long, this IS platform dependent;
38    but "stdlib.h" should be portable 'enough' to be
39    unconditionally available... */
40 #include <stdlib.h>
41
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #if 0                           /* keep Emacsens' auto-indent happy */
46 }
47 #endif
48 #endif
49
50 #include "gnunet_time_lib.h"
51
52
53 /**
54  * Convert a given fancy human-readable size to bytes.
55  *
56  * @param fancy_size human readable string (i.e. 1 MB)
57  * @param size set to the size in bytes
58  * @return GNUNET_OK on success, GNUNET_SYSERR on error
59  */
60 int
61 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
62                                     unsigned long long *size);
63
64
65 /**
66  * Convert a given fancy human-readable time to our internal
67  * representation.
68  *
69  * @param fancy_size human readable string (i.e. 1 minute)
70  * @param rtime set to the relative time
71  * @return GNUNET_OK on success, GNUNET_SYSERR on error
72  */
73 int
74 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
75                                        struct GNUNET_TIME_Relative *rtime);
76
77
78 /**
79  * Convert a given filesize into a fancy human-readable format.
80  *
81  * @param size number of bytes
82  * @return fancy representation of the size (possibly rounded) for humans
83  */
84 char *
85 GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
86
87
88 /**
89  * Convert the len characters long character sequence
90  * given in input that is in the given charset
91  * to UTF-8.
92  *
93  * @param input the input string (not necessarily 0-terminated)
94  * @param len the number of bytes in the input
95  * @param charset character set to convert from
96  * @return the converted string (0-terminated)
97  */
98 char *
99 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset);
100
101
102 /**
103  * Complete filename (a la shell) from abbrevition.
104  *
105  * @param fil the name of the file, may contain ~/ or
106  *        be relative to the current directory
107  * @return the full file name,
108  *          NULL is returned on error
109  */
110 char *
111 GNUNET_STRINGS_filename_expand (const char *fil);
112
113
114 /**
115  * Fill a buffer of the given size with
116  * count 0-terminated strings (given as varargs).
117  * If "buffer" is NULL, only compute the amount of
118  * space required (sum of "strlen(arg)+1").
119  *
120  * Unlike using "snprintf" with "%s", this function
121  * will add 0-terminators after each string.  The
122  * "GNUNET_string_buffer_tokenize" function can be
123  * used to parse the buffer back into individual
124  * strings.
125  *
126  * @param buffer the buffer to fill with strings, can
127  *               be NULL in which case only the necessary
128  *               amount of space will be calculated
129  * @param size number of bytes available in buffer
130  * @param count number of strings that follow
131  * @param ... count 0-terminated strings to copy to buffer
132  * @return number of bytes written to the buffer
133  *         (or number of bytes that would have been written)
134  */
135 size_t
136 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...);
137
138
139 /**
140  * Given a buffer of a given size, find "count"
141  * 0-terminated strings in the buffer and assign
142  * the count (varargs) of type "const char**" to the
143  * locations of the respective strings in the
144  * buffer.
145  *
146  * @param buffer the buffer to parse
147  * @param size size of the buffer
148  * @param count number of strings to locate
149  * @param ... pointers to where to store the strings
150  * @return offset of the character after the last 0-termination
151  *         in the buffer, or 0 on error.
152  */
153 unsigned int
154 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
155                                 unsigned int count, ...);
156
157
158
159 /**
160  * "man ctime_r", except for GNUnet time; also, unlike ctime, the
161  * return value does not include the newline character.
162  *
163  * @param t the absolute time to convert
164  * @return timestamp in human-readable form
165  */
166 char *
167 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
168
169
170 /**
171  * Give relative time in human-readable fancy format.
172  *
173  * @param delta time in milli seconds
174  * @return string in human-readable form
175  */
176 char *
177 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta);
178
179 #if 0                           /* keep Emacsens' auto-indent happy */
180 {
181 #endif
182 #ifdef __cplusplus
183 }
184 #endif
185
186
187 /* ifndef GNUNET_UTIL_STRING_H */
188 #endif
189 /* end of gnunet_util_string.h */