Code

moving trunk for module inkscape
[inkscape.git] / src / libcroco / cr-string.c
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 = NULL ;
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 }
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  *
54 cr_string_new_from_string (const gchar * a_string)
55 {
56         CRString *result = NULL ;
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 }
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 *
75 cr_string_new_from_gstring (GString *a_string)
76 {
77         CRString *result = NULL ;
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                 result->stryng = g_string_new_len
86                         (a_string->str, a_string->len) ;
87         } else {
88                 result->stryng = g_string_new (NULL) ;
89         }
90         return result ;
91 }
93 CRString *
94 cr_string_dup (CRString *a_this)
95 {
96         CRString *result = NULL ;
97         g_return_val_if_fail (a_this, NULL) ;
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 ;
109 gchar *
110 cr_string_dup2 (CRString *a_this)
112         gchar *result = NULL ;
114         g_return_val_if_fail (a_this, NULL) ;
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 ;
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 *
131 cr_string_peek_raw_str (CRString *a_this)
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 ;
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
148 cr_string_peek_raw_str_len (CRString *a_this)
150         g_return_val_if_fail (a_this && a_this->stryng,
151                               -1) ;
152         return a_this->stryng->len ;
155 /**
156  *@param a_this the #CRString to destroy.
157  */
158 void
159 cr_string_destroy (CRString *a_this)
161         g_return_if_fail (a_this) ;
163         if (a_this->stryng) {
164                 g_string_free (a_this->stryng, TRUE) ;
165                 a_this->stryng = NULL ;
166         }
167         g_free (a_this) ;