Libcroco
|
00001 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */ 00002 00003 /* 00004 * This file is part of The Croco Library 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of version 2.1 of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00018 * USA 00019 * 00020 * Author: Dodji Seketeli. 00021 * See COPYRIGHTS file for copyright information. 00022 */ 00023 00024 #include <string.h> 00025 #include "cr-string.h" 00026 00027 /** 00028 *Instanciates a #CRString 00029 *@return the newly instanciated #CRString 00030 *Must be freed with cr_string_destroy(). 00031 */ 00032 CRString * 00033 cr_string_new (void) 00034 { 00035 CRString *result = NULL ; 00036 00037 result = g_try_malloc (sizeof (CRString)) ; 00038 if (!result) { 00039 cr_utils_trace_info ("Out of memory") ; 00040 return NULL ; 00041 } 00042 memset (result, 0, sizeof (CRString)) ; 00043 result->stryng = g_string_new (NULL) ; 00044 return result ; 00045 } 00046 00047 /** 00048 *Instanciate a string and initialise it to 00049 *a_string. 00050 *@param a_string the initial string 00051 *@return the newly instanciated string. 00052 */ 00053 CRString * 00054 cr_string_new_from_string (const gchar * a_string) 00055 { 00056 CRString *result = NULL ; 00057 00058 result = cr_string_new () ; 00059 if (!result) { 00060 cr_utils_trace_info ("Out of memory") ; 00061 return NULL ; 00062 } 00063 if (a_string) 00064 g_string_append (result->stryng, a_string) ; 00065 return result ; 00066 } 00067 00068 /** 00069 *Instanciates a #CRString from an instance of GString. 00070 *@param a_string the input string that will be copied into 00071 *the newly instanciated #CRString 00072 *@return the newly instanciated #CRString. 00073 */ 00074 CRString * 00075 cr_string_new_from_gstring (GString const *a_string) 00076 { 00077 CRString *result = NULL ; 00078 00079 result = cr_string_new () ; 00080 if (!result) { 00081 cr_utils_trace_info ("Out of memory") ; 00082 return NULL ; 00083 } 00084 if (a_string) { 00085 g_string_append_len (result->stryng, 00086 a_string->str, 00087 a_string->len); 00088 00089 } 00090 return result ; 00091 } 00092 00093 CRString * 00094 cr_string_dup (CRString const *a_this) 00095 { 00096 CRString *result = NULL ; 00097 g_return_val_if_fail (a_this, NULL) ; 00098 00099 result = cr_string_new_from_gstring (a_this->stryng) ; 00100 if (!result) { 00101 cr_utils_trace_info ("Out of memory") ; 00102 return NULL ; 00103 } 00104 cr_parsing_location_copy (&result->location, 00105 &a_this->location) ; 00106 return result ; 00107 } 00108 00109 gchar * 00110 cr_string_dup2 (CRString const *a_this) 00111 { 00112 gchar *result = NULL ; 00113 00114 g_return_val_if_fail (a_this, NULL) ; 00115 00116 if (a_this 00117 && a_this->stryng 00118 && a_this->stryng->str) { 00119 result = g_strndup (a_this->stryng->str, 00120 a_this->stryng->len) ; 00121 } 00122 return result ; 00123 } 00124 00125 /** 00126 *Returns a pointer to the internal raw NULL terminated string 00127 *of the current instance of #CRString. 00128 *@param a_this the current instance of #CRString 00129 */ 00130 const gchar * 00131 cr_string_peek_raw_str (CRString const *a_this) 00132 { 00133 g_return_val_if_fail (a_this, NULL) ; 00134 00135 if (a_this->stryng && a_this->stryng->str) 00136 return a_this->stryng->str ; 00137 return NULL ; 00138 } 00139 00140 /** 00141 *Returns the length of the internal raw NULL terminated 00142 *string of the current instance of #CRString. 00143 *@param a_this the current instance of #CRString. 00144 *@return the len of the internal raw NULL termninated string, 00145 *of -1 if no length can be returned. 00146 */ 00147 gint 00148 cr_string_peek_raw_str_len (CRString const *a_this) 00149 { 00150 g_return_val_if_fail (a_this && a_this->stryng, 00151 -1) ; 00152 return a_this->stryng->len ; 00153 } 00154 00155 /** 00156 *@param a_this the #CRString to destroy. 00157 */ 00158 void 00159 cr_string_destroy (CRString *a_this) 00160 { 00161 g_return_if_fail (a_this) ; 00162 00163 if (a_this->stryng) { 00164 g_string_free (a_this->stryng, TRUE) ; 00165 a_this->stryng = NULL ; 00166 } 00167 g_free (a_this) ; 00168 }