1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
4 * This file is part of The Croco Library
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2.1 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * Author: Dodji Seketeli.
21 * See COPYRIGHTS file for copyright information.
26 #include "cr-string.h"
29 *Instanciates a #CRString
30 *@return the newly instanciated #CRString
31 *Must be freed with cr_string_destroy().
36 CRString *result = NULL ;
38 result = g_try_malloc (sizeof (CRString)) ;
40 cr_utils_trace_info ("Out of memory") ;
43 memset (result, 0, sizeof (CRString)) ;
44 result->stryng = g_string_new (NULL) ;
49 *Instanciate a string and initialise it to
51 *@param a_string the initial string
52 *@return the newly instanciated string.
55 cr_string_new_from_string (const gchar * a_string)
57 CRString *result = NULL ;
59 result = cr_string_new () ;
61 cr_utils_trace_info ("Out of memory") ;
65 g_string_append (result->stryng, a_string) ;
70 *Instanciates a #CRString from an instance of GString.
71 *@param a_string the input string that will be copied into
72 *the newly instanciated #CRString
73 *@return the newly instanciated #CRString.
76 cr_string_new_from_gstring (GString *a_string)
78 CRString *result = NULL ;
80 result = cr_string_new () ;
82 cr_utils_trace_info ("Out of memory") ;
86 result->stryng = g_string_new_len
87 (a_string->str, a_string->len) ;
89 result->stryng = g_string_new (NULL) ;
95 cr_string_dup (CRString *a_this)
97 CRString *result = NULL ;
98 g_return_val_if_fail (a_this, NULL) ;
100 result = cr_string_new_from_gstring (a_this->stryng) ;
102 cr_utils_trace_info ("Out of memory") ;
105 cr_parsing_location_copy (&result->location,
111 cr_string_dup2 (CRString *a_this)
113 gchar *result = NULL ;
115 g_return_val_if_fail (a_this, NULL) ;
119 && a_this->stryng->str) {
120 result = g_strndup (a_this->stryng->str,
121 a_this->stryng->len) ;
127 *Returns a pointer to the internal raw NULL terminated string
128 *of the current instance of #CRString.
129 *@param a_this the current instance of #CRString
132 cr_string_peek_raw_str (CRString *a_this)
134 g_return_val_if_fail (a_this, NULL) ;
136 if (a_this->stryng && a_this->stryng->str)
137 return a_this->stryng->str ;
142 *Returns the length of the internal raw NULL terminated
143 *string of the current instance of #CRString.
144 *@param a_this the current instance of #CRString.
145 *@return the len of the internal raw NULL termninated string,
146 *of -1 if no length can be returned.
149 cr_string_peek_raw_str_len (CRString *a_this)
151 g_return_val_if_fail (a_this && a_this->stryng,
153 return a_this->stryng->len ;
157 *@param a_this the #CRString to destroy.
160 cr_string_destroy (CRString *a_this)
162 g_return_if_fail (a_this) ;
164 if (a_this->stryng) {
165 g_string_free (a_this->stryng, TRUE) ;
166 a_this->stryng = NULL ;