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