summaryrefslogtreecommitdiffstats
path: root/src/st/croco/cr-string.c
blob: 6a1667627e3b213dacb445d81c056e730eb5d7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */

/*
 * This file is part of The Croco Library
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 * 
 * Author: Dodji Seketeli.
 * See COPYRIGHTS file for copyright information.
 */

#include <string.h>
#include "cr-string.h"

/**
 *Instantiates a #CRString
 *@return the newly instantiated #CRString
 *Must be freed with cr_string_destroy().
 */
CRString *
cr_string_new (void)
{
	CRString *result = NULL ;

	result = g_try_malloc (sizeof (CRString)) ;
	if (!result) {
		cr_utils_trace_info ("Out of memory") ;
		return NULL ;
	}
	memset (result, 0, sizeof (CRString)) ;
        result->stryng = g_string_new (NULL) ;
	return result ;
}

/**
 *Instantiate a string and initialise it to
 *a_string.
 *@param a_string the initial string
 *@return the newly instantiated string.
 */
CRString  *
cr_string_new_from_string (const gchar * a_string)
{
	CRString *result = NULL ;

	result = cr_string_new () ;
	if (!result) {
		cr_utils_trace_info ("Out of memory") ;
		return NULL ;
	}
	if (a_string)
		g_string_append (result->stryng, a_string) ;
	return result ;
}

/**
 *Instantiates a #CRString from an instance of GString.
 *@param a_string the input string that will be copied into
 *the newly instantiated #CRString
 *@return the newly instantiated #CRString.
 */
CRString *
cr_string_new_from_gstring (GString const *a_string)
{
	CRString *result = NULL ;

	result = cr_string_new () ;
	if (!result) {
		cr_utils_trace_info ("Out of memory") ;
		return NULL ;
	}
	if (a_string) {
		g_string_append_len (result->stryng,
				     a_string->str,
				     a_string->len);

	}
	return result ;
}

CRString *
cr_string_dup (CRString const *a_this)
{
	CRString *result = NULL ;
	g_return_val_if_fail (a_this, NULL) ;

	result = cr_string_new_from_gstring (a_this->stryng) ;
	if (!result) {
		cr_utils_trace_info ("Out of memory") ;
		return NULL ;
	}
	cr_parsing_location_copy (&result->location,
                                  &a_this->location) ;
        return result ;
}

gchar *
cr_string_dup2 (CRString const *a_this)
{
        gchar *result = NULL ;

        g_return_val_if_fail (a_this, NULL) ;

        if (a_this 
            && a_this->stryng 
            && a_this->stryng->str) {
                result = g_strndup (a_this->stryng->str,
                                    a_this->stryng->len) ;
        }
        return result ;
}

/**
 *Returns a pointer to the internal raw NULL terminated string
 *of the current instance of #CRString.
 *@param a_this the current instance of #CRString
 */
const gchar *
cr_string_peek_raw_str (CRString const *a_this)
{
        g_return_val_if_fail (a_this, NULL) ;
        
        if (a_this->stryng && a_this->stryng->str)
                return a_this->stryng->str ;
        return NULL ;
}

/**
 *Returns the length of the internal raw NULL terminated
 *string of the current instance of #CRString.
 *@param a_this the current instance of #CRString.
 *@return the len of the internal raw NULL termninated string,
 *of -1 if no length can be returned.
 */
gint
cr_string_peek_raw_str_len (CRString const *a_this)
{
        g_return_val_if_fail (a_this && a_this->stryng,
                              -1) ;
        return a_this->stryng->len ;
}

/**
 *@param a_this the #CRString to destroy.
 */
void
cr_string_destroy (CRString *a_this)
{
	g_return_if_fail (a_this) ;

	if (a_this->stryng) {
		g_string_free (a_this->stryng, TRUE) ;
		a_this->stryng = NULL ;
	}
	g_free (a_this) ;
}