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