1 /* -*- Mode: C; indent-tabs-mode: ni; 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 the COPYRIGHTS file for copyright information.
22 */
23
24 #include <string.h>
25 #include "cr-parsing-location.h"
26
27 /**
28 *@file
29 *Definition of the #CRparsingLocation class.
30 */
31
32
33 /**
34 *Instanciates a new parsing location.
35 *@return the newly instanciated #CRParsingLocation.
36 *Must be freed by cr_parsing_location_destroy()
37 */
38 CRParsingLocation *
cr_parsing_location_new(void)39 cr_parsing_location_new (void)
40 {
41 CRParsingLocation * result = NULL ;
42
43 result = g_try_malloc (sizeof (CRParsingLocation)) ;
44 if (!result) {
45 cr_utils_trace_info ("Out of memory error") ;
46 return NULL ;
47 }
48 cr_parsing_location_init (result) ;
49 return result ;
50 }
51
52 /**
53 *Initializes the an instance of #CRparsingLocation.
54 *@param a_this the current instance of #CRParsingLocation.
55 *@return CR_OK upon
56 */
57 enum CRStatus
cr_parsing_location_init(CRParsingLocation * a_this)58 cr_parsing_location_init (CRParsingLocation *a_this)
59 {
60 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
61
62 memset (a_this, 0, sizeof (CRParsingLocation)) ;
63 return CR_OK ;
64 }
65
66 /**
67 *Copies an instance of CRParsingLocation into another one.
68 *@param a_to the destination of the copy.
69 *Must be allocated by the caller.
70 *@param a_from the source of the copy.
71 *@return CR_OK upon succesful completion, an error code
72 *otherwise.
73 */
74 enum CRStatus
cr_parsing_location_copy(CRParsingLocation * a_to,CRParsingLocation * a_from)75 cr_parsing_location_copy (CRParsingLocation *a_to,
76 CRParsingLocation *a_from)
77 {
78 g_return_val_if_fail (a_to && a_from, CR_BAD_PARAM_ERROR) ;
79
80 memcpy (a_to, a_from, sizeof (CRParsingLocation)) ;
81 return CR_OK ;
82 }
83
84 /**
85 *@param a_this the current instance of #CRParsingLocation.
86 *@param a_mask a bitmap that defines which parts of the
87 *parsing location are to be serialized (line, column or byte offset)
88 *@return the serialized string or NULL in case of an error.
89 */
90 gchar *
cr_parsing_location_to_string(CRParsingLocation * a_this,enum CRParsingLocationSerialisationMask a_mask)91 cr_parsing_location_to_string (CRParsingLocation *a_this,
92 enum CRParsingLocationSerialisationMask a_mask)
93 {
94 GString *result = NULL ;
95 gchar *str = NULL ;
96
97 g_return_val_if_fail (a_this, NULL) ;
98
99 if (!a_mask) {
100 a_mask = DUMP_LINE | DUMP_COLUMN | DUMP_BYTE_OFFSET ;
101 }
102 result =g_string_new (NULL) ;
103 if (!result)
104 return NULL ;
105 if (a_mask & DUMP_LINE) {
106 g_string_append_printf (result, "line:%d ",
107 a_this->line) ;
108 }
109 if (a_mask & DUMP_COLUMN) {
110 g_string_append_printf (result, "column:%d ",
111 a_this->column) ;
112 }
113 if (a_mask & DUMP_BYTE_OFFSET) {
114 g_string_append_printf (result, "byte offset:%d ",
115 a_this->byte_offset) ;
116 }
117 if (result->len) {
118 str = result->str ;
119 g_string_free (result, FALSE) ;
120 } else {
121 g_string_free (result, TRUE) ;
122 }
123 return str ;
124 }
125
126 void
cr_parsing_location_dump(CRParsingLocation * a_this,enum CRParsingLocationSerialisationMask a_mask,FILE * a_fp)127 cr_parsing_location_dump (CRParsingLocation *a_this,
128 enum CRParsingLocationSerialisationMask a_mask,
129 FILE *a_fp)
130 {
131 gchar *str = NULL ;
132
133 g_return_if_fail (a_this && a_fp) ;
134 str = cr_parsing_location_to_string (a_this, a_mask) ;
135 if (str) {
136 fprintf (a_fp, "%s", str) ;
137 g_free (str) ;
138 str = NULL ;
139 }
140 }
141
142 /**
143 *Destroys the current instance of #CRParsingLocation
144 *@param a_this the current instance of #CRParsingLocation. Must
145 *have been allocated with cr_parsing_location_new().
146 */
147 void
cr_parsing_location_destroy(CRParsingLocation * a_this)148 cr_parsing_location_destroy (CRParsingLocation *a_this)
149 {
150 g_return_if_fail (a_this) ;
151 g_free (a_this) ;
152 }
153
154