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