1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /* libcroco - Library for parsing and applying CSS
4 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
5 *
6 * This file is not part of the GNU gettext program, but is used with
7 * GNU gettext.
8 *
9 * The original copyright notice is as follows:
10 */
11
12 /*
13 * This file is part of The Croco Library
14 *
15 * Copyright (C) 2003-2004 Dodji Seketeli. All Rights Reserved.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of version 2.1 of the GNU Lesser General Public
19 * License as published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 * USA
30 *
31 * Author: Dodji Seketeli.
32 */
33
34 #include <config.h>
35 #include <string.h>
36 #include "cr-parsing-location.h"
37
38 /**
39 *@CRParsingLocation:
40 *
41 *Definition of the #CRparsingLocation class.
42 */
43
44
45 /**
46 * cr_parsing_location_new:
47 *Instanciates a new parsing location.
48 *
49 *Returns the newly instanciated #CRParsingLocation.
50 *Must be freed by cr_parsing_location_destroy()
51 */
52 CRParsingLocation *
cr_parsing_location_new(void)53 cr_parsing_location_new (void)
54 {
55 CRParsingLocation * result = NULL ;
56
57 result = g_try_malloc (sizeof (CRParsingLocation)) ;
58 if (!result) {
59 cr_utils_trace_info ("Out of memory error") ;
60 return NULL ;
61 }
62 cr_parsing_location_init (result) ;
63 return result ;
64 }
65
66 /**
67 * cr_parsing_location_init:
68 *@a_this: the current instance of #CRParsingLocation.
69 *
70 *Initializes the an instance of #CRparsingLocation.
71 *
72 *Returns CR_OK upon succesful completion, an error code otherwise.
73 */
74 enum CRStatus
cr_parsing_location_init(CRParsingLocation * a_this)75 cr_parsing_location_init (CRParsingLocation *a_this)
76 {
77 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
78
79 memset (a_this, 0, sizeof (CRParsingLocation)) ;
80 return CR_OK ;
81 }
82
83 /**
84 * cr_parsing_location_copy:
85 *@a_to: the destination of the copy.
86 *Must be allocated by the caller.
87 *@a_from: the source of the copy.
88 *
89 *Copies an instance of CRParsingLocation into another one.
90 *
91 *Returns CR_OK upon succesful completion, an error code
92 *otherwise.
93 */
94 enum CRStatus
cr_parsing_location_copy(CRParsingLocation * a_to,CRParsingLocation const * a_from)95 cr_parsing_location_copy (CRParsingLocation *a_to,
96 CRParsingLocation const *a_from)
97 {
98 g_return_val_if_fail (a_to && a_from, CR_BAD_PARAM_ERROR) ;
99
100 memcpy (a_to, a_from, sizeof (CRParsingLocation)) ;
101 return CR_OK ;
102 }
103
104 /**
105 * cr_parsing_location_to_string:
106 *@a_this: the current instance of #CRParsingLocation.
107 *@a_mask: a bitmap that defines which parts of the
108 *parsing location are to be serialized (line, column or byte offset)
109 *
110 *Returns the serialized string or NULL in case of an error.
111 */
112 gchar *
cr_parsing_location_to_string(CRParsingLocation const * a_this,enum CRParsingLocationSerialisationMask a_mask)113 cr_parsing_location_to_string (CRParsingLocation const *a_this,
114 enum CRParsingLocationSerialisationMask a_mask)
115 {
116 GString *result = NULL ;
117 gchar *str = NULL ;
118
119 g_return_val_if_fail (a_this, NULL) ;
120
121 if (!a_mask) {
122 a_mask = DUMP_LINE | DUMP_COLUMN | DUMP_BYTE_OFFSET ;
123 }
124 result =g_string_new (NULL) ;
125 if (!result)
126 return NULL ;
127 if (a_mask & DUMP_LINE) {
128 g_string_append_printf (result, "line:%d ",
129 a_this->line) ;
130 }
131 if (a_mask & DUMP_COLUMN) {
132 g_string_append_printf (result, "column:%d ",
133 a_this->column) ;
134 }
135 if (a_mask & DUMP_BYTE_OFFSET) {
136 g_string_append_printf (result, "byte offset:%d ",
137 a_this->byte_offset) ;
138 }
139 if (result->len) {
140 str = result->str ;
141 g_string_free (result, FALSE) ;
142 } else {
143 g_string_free (result, TRUE) ;
144 }
145 return str ;
146 }
147
148 /**
149 * cr_parsing_location_dump:
150 * @a_this: current instance of #CRParsingLocation
151 * @a_mask: the serialization mask.
152 * @a_fp: the file pointer to dump the parsing location to.
153 */
154 void
cr_parsing_location_dump(CRParsingLocation const * a_this,enum CRParsingLocationSerialisationMask a_mask,FILE * a_fp)155 cr_parsing_location_dump (CRParsingLocation const *a_this,
156 enum CRParsingLocationSerialisationMask a_mask,
157 FILE *a_fp)
158 {
159 gchar *str = NULL ;
160
161 g_return_if_fail (a_this && a_fp) ;
162 str = cr_parsing_location_to_string (a_this, a_mask) ;
163 if (str) {
164 fprintf (a_fp, "%s", str) ;
165 g_free (str) ;
166 str = NULL ;
167 }
168 }
169
170 /**
171 * cr_parsing_location_destroy:
172 *@a_this: the current instance of #CRParsingLocation. Must
173 *have been allocated with cr_parsing_location_new().
174 *
175 *Destroys the current instance of #CRParsingLocation
176 */
177 void
cr_parsing_location_destroy(CRParsingLocation * a_this)178 cr_parsing_location_destroy (CRParsingLocation *a_this)
179 {
180 g_return_if_fail (a_this) ;
181 g_free (a_this) ;
182 }
183
184