Libcroco
cr-parsing-location.c
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: nil; 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  *@CRParsingLocation:
29  *
30  *Definition of the #CRparsingLocation class.
31  */
32 
33 
34 /**
35  * cr_parsing_location_new:
36  *Instanciates a new parsing location.
37  *
38  *Returns the newly instanciated #CRParsingLocation.
39  *Must be freed by cr_parsing_location_destroy()
40  */
43 {
44  CRParsingLocation * result = NULL ;
45 
46  result = g_try_malloc (sizeof (CRParsingLocation)) ;
47  if (!result) {
48  cr_utils_trace_info ("Out of memory error") ;
49  return NULL ;
50  }
51  cr_parsing_location_init (result) ;
52  return result ;
53 }
54 
55 /**
56  * cr_parsing_location_init:
57  *@a_this: the current instance of #CRParsingLocation.
58  *
59  *Initializes the an instance of #CRparsingLocation.
60  *
61  *Returns CR_OK upon succesful completion, an error code otherwise.
62  */
63 enum CRStatus
65 {
66  g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
67 
68  memset (a_this, 0, sizeof (CRParsingLocation)) ;
69  return CR_OK ;
70 }
71 
72 /**
73  * cr_parsing_location_copy:
74  *@a_to: the destination of the copy.
75  *Must be allocated by the caller.
76  *@a_from: the source of the copy.
77  *
78  *Copies an instance of CRParsingLocation into another one.
79  *
80  *Returns CR_OK upon succesful completion, an error code
81  *otherwise.
82  */
83 enum CRStatus
85  CRParsingLocation const *a_from)
86 {
87  g_return_val_if_fail (a_to && a_from, CR_BAD_PARAM_ERROR) ;
88 
89  memcpy (a_to, a_from, sizeof (CRParsingLocation)) ;
90  return CR_OK ;
91 }
92 
93 /**
94  * cr_parsing_location_to_string:
95  *@a_this: the current instance of #CRParsingLocation.
96  *@a_mask: a bitmap that defines which parts of the
97  *parsing location are to be serialized (line, column or byte offset)
98  *
99  *Returns the serialized string or NULL in case of an error.
100  */
101 gchar *
104 {
105  GString *result = NULL ;
106  gchar *str = NULL ;
107 
108  g_return_val_if_fail (a_this, NULL) ;
109 
110  if (!a_mask) {
111  a_mask = DUMP_LINE | DUMP_COLUMN | DUMP_BYTE_OFFSET ;
112  }
113  result =g_string_new (NULL) ;
114  if (!result)
115  return NULL ;
116  if (a_mask & DUMP_LINE) {
117  g_string_append_printf (result, "line:%d ",
118  a_this->line) ;
119  }
120  if (a_mask & DUMP_COLUMN) {
121  g_string_append_printf (result, "column:%d ",
122  a_this->column) ;
123  }
124  if (a_mask & DUMP_BYTE_OFFSET) {
125  g_string_append_printf (result, "byte offset:%d ",
126  a_this->byte_offset) ;
127  }
128  if (result->len) {
129  str = result->str ;
130  g_string_free (result, FALSE) ;
131  } else {
132  g_string_free (result, TRUE) ;
133  }
134  return str ;
135 }
136 
137 /**
138  * cr_parsing_location_dump:
139  * @a_this: current instance of #CRParsingLocation
140  * @a_mask: the serialization mask.
141  * @a_fp: the file pointer to dump the parsing location to.
142  */
143 void
146  FILE *a_fp)
147 {
148  gchar *str = NULL ;
149 
150  g_return_if_fail (a_this && a_fp) ;
151  str = cr_parsing_location_to_string (a_this, a_mask) ;
152  if (str) {
153  fprintf (a_fp, "%s", str) ;
154  g_free (str) ;
155  str = NULL ;
156  }
157 }
158 
159 /**
160  * cr_parsing_location_destroy:
161  *@a_this: the current instance of #CRParsingLocation. Must
162  *have been allocated with cr_parsing_location_new().
163  *
164  *Destroys the current instance of #CRParsingLocation
165  */
166 void
168 {
169  g_return_if_fail (a_this) ;
170  g_free (a_this) ;
171 }
172 
CRParsingLocation * cr_parsing_location_new(void)
@CRParsingLocation:
gchar * cr_parsing_location_to_string(CRParsingLocation const *a_this, enum CRParsingLocationSerialisationMask a_mask)
cr_parsing_location_to_string: @a_this: the current instance of CRParsingLocation.
The declaration of the CRParsingLocation object.
CRParsingLocationSerialisationMask
CRStatus
The status type returned by the methods of the croco library.
Definition: cr-utils.h:43
Definition: cr-utils.h:44
enum CRStatus cr_parsing_location_init(CRParsingLocation *a_this)
cr_parsing_location_init: @a_this: the current instance of CRParsingLocation.
void cr_parsing_location_destroy(CRParsingLocation *a_this)
cr_parsing_location_destroy: @a_this: the current instance of CRParsingLocation.
void cr_parsing_location_dump(CRParsingLocation const *a_this, enum CRParsingLocationSerialisationMask a_mask, FILE *a_fp)
cr_parsing_location_dump: @a_this: current instance of CRParsingLocation @a_mask: the serialization m...
enum CRStatus cr_parsing_location_copy(CRParsingLocation *a_to, CRParsingLocation const *a_from)
cr_parsing_location_copy: @a_to: the destination of the copy.
#define cr_utils_trace_info(a_msg)
Traces an info message.
Definition: cr-utils.h:127