even nicer indentation, thanks to LRN's indent patch
[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 filesize into a fancy human-readable format.
55  *
56  * @param size number of bytes
57  * @return fancy representation of the size (possibly rounded) for humans
58  */
59 char *
60 GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
61
62
63 /**
64  * Convert the len characters long character sequence
65  * given in input that is in the given charset
66  * to UTF-8.
67  *
68  * @param input the input string (not necessarily 0-terminated)
69  * @param len the number of bytes in the input
70  * @param charset character set to convert from
71  * @return the converted string (0-terminated)
72  */
73 char *
74 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset);
75
76
77 /**
78  * Complete filename (a la shell) from abbrevition.
79  *
80  * @param fil the name of the file, may contain ~/ or
81  *        be relative to the current directory
82  * @return the full file name,
83  *          NULL is returned on error
84  */
85 char *
86 GNUNET_STRINGS_filename_expand (const char *fil);
87
88 /**
89  * Fill a buffer of the given size with
90  * count 0-terminated strings (given as varargs).
91  * If "buffer" is NULL, only compute the amount of
92  * space required (sum of "strlen(arg)+1").
93  *
94  * Unlike using "snprintf" with "%s", this function
95  * will add 0-terminators after each string.  The
96  * "GNUNET_string_buffer_tokenize" function can be
97  * used to parse the buffer back into individual
98  * strings.
99  *
100  * @param buffer the buffer to fill with strings, can
101  *               be NULL in which case only the necessary
102  *               amount of space will be calculated
103  * @param size number of bytes available in buffer
104  * @param count number of strings that follow
105  * @param ... count 0-terminated strings to copy to buffer
106  * @return number of bytes written to the buffer
107  *         (or number of bytes that would have been written)
108  */
109 size_t
110 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...);
111
112
113 /**
114  * Given a buffer of a given size, find "count"
115  * 0-terminated strings in the buffer and assign
116  * the count (varargs) of type "const char**" to the
117  * locations of the respective strings in the
118  * buffer.
119  *
120  * @param buffer the buffer to parse
121  * @param size size of the buffer
122  * @param count number of strings to locate
123  * @param ... pointers to where to store the strings
124  * @return offset of the character after the last 0-termination
125  *         in the buffer, or 0 on error.
126  */
127 unsigned int
128 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
129                                 unsigned int count, ...);
130
131
132
133 /**
134  * "man ctime_r", except for GNUnet time; also, unlike ctime, the
135  * return value does not include the newline character.
136  *
137  * @param t the absolute time to convert
138  * @return timestamp in human-readable form
139  */
140 char *
141 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
142
143
144 /**
145  * Give relative time in human-readable fancy format.
146  *
147  * @param delta time in milli seconds
148  * @return string in human-readable form
149  */
150 char *
151 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta);
152
153 #if 0                           /* keep Emacsens' auto-indent happy */
154 {
155 #endif
156 #ifdef __cplusplus
157 }
158 #endif
159
160
161 /* ifndef GNUNET_UTIL_STRING_H */
162 #endif
163 /* end of gnunet_util_string.h */