1 /****************************************************************************
2  * Copyright (C) 2011 by Matteo Franchin                                    *
3  *                                                                          *
4  * This file is part of Box.                                                *
5  *                                                                          *
6  *   Box is free software: you can redistribute it and/or modify it         *
7  *   under the terms of the GNU Lesser General Public License as published  *
8  *   by the Free Software Foundation, either version 3 of the License, or   *
9  *   (at your option) any later version.                                    *
10  *                                                                          *
11  *   Box is distributed in the hope that it will be useful,                 *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
14  *   GNU Lesser General Public License for more details.                    *
15  *                                                                          *
16  *   You should have received a copy of the GNU Lesser General Public       *
17  *   License along with Box.  If not, see <http://www.gnu.org/licenses/>.   *
18  ****************************************************************************/
19 
20 /** @file str.h
21  * @brief Implementation of the Str type in the Box language (C API).
22  *
23  * A nice description...
24  */
25 
26 #ifndef _BOX_STR_H
27 #  define _BOX_STR_H
28 
29 /** @brief The Str object.
30  */
31 typedef struct {
32   BoxInt length;      /**< the current length of the string */
33   BoxInt buffer_size; /**< the size of the block allocated to contain
34                            the string. */
35   char   *ptr;        /**< the pointer to the block allocated to contain
36                            the string. */
37 } BoxStr;
38 
39 /** Just for compatibility */
40 typedef BoxStr Str;
41 
42 BOXEXPORT void BoxStr_Init(BoxStr *s);
43 
44 BOXEXPORT void BoxStr_Finish(BoxStr *s);
45 
46 /** Set 'dest' with the content of 'src', deleting whatever was in 'dest'
47  * before.
48  * NOTE: this requires 'dest' to be a properly initialized BoxStr object.
49  */
50 BOXEXPORT BoxTask BoxStr_Set(BoxStr *dest, const BoxStr *src);
51 
52 /** Set 'dest' with the content of 'src', deleting whatever was in 'dest'
53  * before. This is similar to BoxStr_Set, but uses a C string as source.
54  * NOTE: this requires 'dest' to be a properly initialized BoxStr object.
55  */
56 BOXEXPORT BoxTask BoxStr_Set_From_C_String(BoxStr *dest, const char *src);
57 
58 BOXEXPORT BoxTask BoxStr_Large_Enough(BoxStr *s, BoxInt length);
59 
60 BOXEXPORT BoxTask BoxStr_Concat(BoxStr *dest, const BoxStr *src);
61 
62 BOXEXPORT BoxTask BoxStr_Concat_C_String(BoxStr *s, const char *ca);
63 
64 /** Initialise a string 'new_str' from another existing string 'src'. */
65 BOXEXPORT BoxTask BoxStr_Init_From(BoxStr *new_str, const BoxStr *src);
66 
67 /** Create a new C string from a BoxStr object. The string is freshly
68  * allocated with Box_Mem_Safe_Alloc.
69  */
70 BOXEXPORT char *BoxStr_To_C_String(BoxStr *s);
71 
72 
73 
74 /** Return the pointer to the raw data in the string. */
75 BOXEXPORT char *BoxStr_Get_Ptr(BoxStr *s);
76 
77 /** Return the size of the data in the string. */
78 BOXEXPORT size_t BoxStr_Get_Size(BoxStr *s);
79 
80 /** Compare two strings and return an integer less than, equal to, or greater
81  * than zero if s1 is found, respectively, to be less than, to match, or be
82  * greater than s2.
83  */
84 BOXEXPORT int BoxStr_Compare(const BoxStr *s1, const BoxStr *s2);
85 
86 #endif /* _BOX_STR_H */
87