summaryrefslogtreecommitdiffstats
path: root/src/st/croco/cr-parsing-location.c
blob: 2b40974130fafb04a71f3575c86448a3c13156bd (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
169
170
171
/* -*- 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 the COPYRIGHTS file for copyright information.
 */

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

/**
 *@CRParsingLocation:
 *
 *Definition of the #CRparsingLocation class.
 */


/**
 * cr_parsing_location_new:
 *Instantiates a new parsing location.
 *
 *Returns the newly instantiated #CRParsingLocation.
 *Must be freed by cr_parsing_location_destroy()
 */
CRParsingLocation * 
cr_parsing_location_new (void)
{
	CRParsingLocation * result = NULL ;

	result = g_try_malloc (sizeof (CRParsingLocation)) ;
	if (!result) {
		cr_utils_trace_info ("Out of memory error") ;
		return NULL ;
	}
	cr_parsing_location_init (result) ;
	return result ;
}

/**
 * cr_parsing_location_init:
 *@a_this: the current instance of #CRParsingLocation.
 *
 *Initializes the an instance of #CRparsingLocation.
 *
 *Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus 
cr_parsing_location_init (CRParsingLocation *a_this)
{
	g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;

	memset (a_this, 0, sizeof (CRParsingLocation)) ;
	return CR_OK ;
}

/**
 * cr_parsing_location_copy:
 *@a_to: the destination of the copy. 
 *Must be allocated by the caller.
 *@a_from: the source of the copy.
 *
 *Copies an instance of CRParsingLocation into another one.
 *
 *Returns CR_OK upon successful completion, an error code
 *otherwise.
 */
enum CRStatus 
cr_parsing_location_copy (CRParsingLocation *a_to,
			  CRParsingLocation const *a_from)
{
	g_return_val_if_fail (a_to && a_from, CR_BAD_PARAM_ERROR) ;

	memcpy (a_to, a_from, sizeof (CRParsingLocation)) ;
	return CR_OK ;
}

/**
 * cr_parsing_location_to_string:
 *@a_this: the current instance of #CRParsingLocation.
 *@a_mask: a bitmap that defines which parts of the
 *parsing location are to be serialized (line, column or byte offset)
 *
 *Returns the serialized string or NULL in case of an error.
 */
gchar * 
cr_parsing_location_to_string (CRParsingLocation const *a_this,
			       enum CRParsingLocationSerialisationMask a_mask)
{
	GString *result = NULL ;
	gchar *str = NULL ;

	g_return_val_if_fail (a_this, NULL) ;

	if (!a_mask) {
		a_mask = DUMP_LINE | DUMP_COLUMN | DUMP_BYTE_OFFSET ;
	}
	result =g_string_new (NULL) ;
	if (!result)
		return NULL ;
	if (a_mask & DUMP_LINE) {
		g_string_append_printf (result, "line:%d ", 
					a_this->line) ;
	}
	if (a_mask & DUMP_COLUMN) {
		g_string_append_printf (result, "column:%d ", 
					a_this->column) ;
	}
	if (a_mask & DUMP_BYTE_OFFSET) {
		g_string_append_printf (result, "byte offset:%d ", 
					a_this->byte_offset) ;
	}
	if (result->len) {
		str = g_string_free (result, FALSE) ;
	} else {
		g_string_free (result, TRUE) ;
	}
	return str ;
}

/**
 * cr_parsing_location_dump:
 * @a_this: current instance of #CRParsingLocation
 * @a_mask: the serialization mask.
 * @a_fp: the file pointer to dump the parsing location to.
 */
void
cr_parsing_location_dump (CRParsingLocation const *a_this,
			  enum CRParsingLocationSerialisationMask a_mask,
			  FILE *a_fp)
{
	gchar *str = NULL ;

	g_return_if_fail (a_this && a_fp) ;
	str = cr_parsing_location_to_string (a_this, a_mask) ;
	if (str) {
		fprintf (a_fp, "%s", str) ;
		g_free (str) ;
		str = NULL ;
	}
}

/**
 * cr_parsing_location_destroy:
 *@a_this: the current instance of #CRParsingLocation. Must
 *have been allocated with cr_parsing_location_new().
 *
 *Destroys the current instance of #CRParsingLocation
 */
void 
cr_parsing_location_destroy (CRParsingLocation *a_this)
{
	g_return_if_fail (a_this) ;
	g_free (a_this) ;
}