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 <config.h> 25#include <string.h> 26#include "cr-string.h" 27 28/** 29 *Instanciates a #CRString 30 *@return the newly instanciated #CRString 31 *Must be freed with cr_string_destroy(). 32 */ 33CRString * 34cr_string_new (void) 35{ 36 CRString *result = NULL ; 37 38 result = g_try_malloc (sizeof (CRString)) ; 39 if (!result) { 40 cr_utils_trace_info ("Out of memory") ; 41 return NULL ; 42 } 43 memset (result, 0, sizeof (CRString)) ; 44 result->stryng = g_string_new (NULL) ; 45 return result ; 46} 47 48/** 49 *Instanciate a string and initialise it to 50 *a_string. 51 *@param a_string the initial string 52 *@return the newly instanciated string. 53 */ 54CRString * 55cr_string_new_from_string (const gchar * a_string) 56{ 57 CRString *result = NULL ; 58 59 result = cr_string_new () ; 60 if (!result) { 61 cr_utils_trace_info ("Out of memory") ; 62 return NULL ; 63 } 64 if (a_string) 65 g_string_append (result->stryng, a_string) ; 66 return result ; 67} 68 69/** 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. 74 */ 75CRString * 76cr_string_new_from_gstring (GString *a_string) 77{ 78 CRString *result = NULL ; 79 80 result = cr_string_new () ; 81 if (!result) { 82 cr_utils_trace_info ("Out of memory") ; 83 return NULL ; 84 } 85 if (a_string) { 86 result->stryng = g_string_new_len 87 (a_string->str, a_string->len) ; 88 } else { 89 result->stryng = g_string_new (NULL) ; 90 } 91 return result ; 92} 93 94CRString * 95cr_string_dup (CRString *a_this) 96{ 97 CRString *result = NULL ; 98 g_return_val_if_fail (a_this, NULL) ; 99 100 result = cr_string_new_from_gstring (a_this->stryng) ; 101 if (!result) { 102 cr_utils_trace_info ("Out of memory") ; 103 return NULL ; 104 } 105 cr_parsing_location_copy (&result->location, 106 &a_this->location) ; 107 return result ; 108} 109 110gchar * 111cr_string_dup2 (CRString *a_this) 112{ 113 gchar *result = NULL ; 114 115 g_return_val_if_fail (a_this, NULL) ; 116 117 if (a_this 118 && a_this->stryng 119 && a_this->stryng->str) { 120 result = g_strndup (a_this->stryng->str, 121 a_this->stryng->len) ; 122 } 123 return result ; 124} 125 126/** 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 130 */ 131const gchar * 132cr_string_peek_raw_str (CRString *a_this) 133{ 134 g_return_val_if_fail (a_this, NULL) ; 135 136 if (a_this->stryng && a_this->stryng->str) 137 return a_this->stryng->str ; 138 return NULL ; 139} 140 141/** 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. 147 */ 148gint 149cr_string_peek_raw_str_len (CRString *a_this) 150{ 151 g_return_val_if_fail (a_this && a_this->stryng, 152 -1) ; 153 return a_this->stryng->len ; 154} 155 156/** 157 *@param a_this the #CRString to destroy. 158 */ 159void 160cr_string_destroy (CRString *a_this) 161{ 162 g_return_if_fail (a_this) ; 163 164 if (a_this->stryng) { 165 g_string_free (a_this->stryng, TRUE) ; 166 a_this->stryng = NULL ; 167 } 168 g_free (a_this) ; 169} 170