1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2 
3 /*
4  * This file is part of The Croco Library
5  *
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.
9  *
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.
14  *
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
18  * USA
19  *
20  * Author: Dodji Seketeli.
21  * See COPYRIGHTS file for copyright information.
22  */
23 
24 #include <string.h>
25 #include "cr-string.h"
26 
27 /**
28  *Instanciates a #CRString
29  *@return the newly instanciated #CRString
30  *Must be freed with cr_string_destroy().
31  */
32 CRString *
cr_string_new(void)33 cr_string_new (void)
34 {
35 	CRString *result = NULL ;
36 
37 	result = g_try_malloc (sizeof (CRString)) ;
38 	if (!result) {
39 		cr_utils_trace_info ("Out of memory") ;
40 		return NULL ;
41 	}
42 	memset (result, 0, sizeof (CRString)) ;
43         result->stryng = g_string_new (NULL) ;
44 	return result ;
45 }
46 
47 /**
48  *Instanciate a string and initialise it to
49  *a_string.
50  *@param a_string the initial string
51  *@return the newly instanciated string.
52  */
53 CRString  *
cr_string_new_from_string(const gchar * a_string)54 cr_string_new_from_string (const gchar * a_string)
55 {
56 	CRString *result = NULL ;
57 
58 	result = cr_string_new () ;
59 	if (!result) {
60 		cr_utils_trace_info ("Out of memory") ;
61 		return NULL ;
62 	}
63 	if (a_string)
64 		g_string_append (result->stryng, a_string) ;
65 	return result ;
66 }
67 
68 /**
69  *Instanciates a #CRString from an instance of GString.
70  *@param a_string the input string that will be copied into
71  *the newly instanciated #CRString
72  *@return the newly instanciated #CRString.
73  */
74 CRString *
cr_string_new_from_gstring(GString const * a_string)75 cr_string_new_from_gstring (GString const *a_string)
76 {
77 	CRString *result = NULL ;
78 
79 	result = cr_string_new () ;
80 	if (!result) {
81 		cr_utils_trace_info ("Out of memory") ;
82 		return NULL ;
83 	}
84 	if (a_string) {
85 		g_string_append_len (result->stryng,
86 				     a_string->str,
87 				     a_string->len);
88 
89 	}
90 	return result ;
91 }
92 
93 CRString *
cr_string_dup(CRString const * a_this)94 cr_string_dup (CRString const *a_this)
95 {
96 	CRString *result = NULL ;
97 	g_return_val_if_fail (a_this, NULL) ;
98 
99 	result = cr_string_new_from_gstring (a_this->stryng) ;
100 	if (!result) {
101 		cr_utils_trace_info ("Out of memory") ;
102 		return NULL ;
103 	}
104 	cr_parsing_location_copy (&result->location,
105                                   &a_this->location) ;
106         return result ;
107 }
108 
109 gchar *
cr_string_dup2(CRString const * a_this)110 cr_string_dup2 (CRString const *a_this)
111 {
112         gchar *result = NULL ;
113 
114         g_return_val_if_fail (a_this, NULL) ;
115 
116         if (a_this
117             && a_this->stryng
118             && a_this->stryng->str) {
119                 result = g_strndup (a_this->stryng->str,
120                                     a_this->stryng->len) ;
121         }
122         return result ;
123 }
124 
125 /**
126  *Returns a pointer to the internal raw NULL terminated string
127  *of the current instance of #CRString.
128  *@param a_this the current instance of #CRString
129  */
130 const gchar *
cr_string_peek_raw_str(CRString const * a_this)131 cr_string_peek_raw_str (CRString const *a_this)
132 {
133         g_return_val_if_fail (a_this, NULL) ;
134 
135         if (a_this->stryng && a_this->stryng->str)
136                 return a_this->stryng->str ;
137         return NULL ;
138 }
139 
140 /**
141  *Returns the length of the internal raw NULL terminated
142  *string of the current instance of #CRString.
143  *@param a_this the current instance of #CRString.
144  *@return the len of the internal raw NULL termninated string,
145  *of -1 if no length can be returned.
146  */
147 gint
cr_string_peek_raw_str_len(CRString const * a_this)148 cr_string_peek_raw_str_len (CRString const *a_this)
149 {
150         g_return_val_if_fail (a_this && a_this->stryng,
151                               -1) ;
152         return a_this->stryng->len ;
153 }
154 
155 /**
156  *@param a_this the #CRString to destroy.
157  */
158 void
cr_string_destroy(CRString * a_this)159 cr_string_destroy (CRString *a_this)
160 {
161 	g_return_if_fail (a_this) ;
162 
163 	if (a_this->stryng) {
164 		g_string_free (a_this->stryng, TRUE) ;
165 		a_this->stryng = NULL ;
166 	}
167 	g_free (a_this) ;
168 }
169