Libcroco
|
00001 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ 00002 00003 /* 00004 * This file is part of The Croco Library 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of version 2.1 of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00018 * USA 00019 * 00020 * Author: Dodji Seketeli. 00021 * See the COPYRIGHTS file for copyright information. 00022 */ 00023 00024 #include <string.h> 00025 #include "cr-parsing-location.h" 00026 00027 /** 00028 *@CRParsingLocation: 00029 * 00030 *Definition of the #CRparsingLocation class. 00031 */ 00032 00033 00034 /** 00035 * cr_parsing_location_new: 00036 *Instanciates a new parsing location. 00037 * 00038 *Returns the newly instanciated #CRParsingLocation. 00039 *Must be freed by cr_parsing_location_destroy() 00040 */ 00041 CRParsingLocation * 00042 cr_parsing_location_new (void) 00043 { 00044 CRParsingLocation * result = NULL ; 00045 00046 result = g_try_malloc (sizeof (CRParsingLocation)) ; 00047 if (!result) { 00048 cr_utils_trace_info ("Out of memory error") ; 00049 return NULL ; 00050 } 00051 cr_parsing_location_init (result) ; 00052 return result ; 00053 } 00054 00055 /** 00056 * cr_parsing_location_init: 00057 *@a_this: the current instance of #CRParsingLocation. 00058 * 00059 *Initializes the an instance of #CRparsingLocation. 00060 * 00061 *Returns CR_OK upon succesful completion, an error code otherwise. 00062 */ 00063 enum CRStatus 00064 cr_parsing_location_init (CRParsingLocation *a_this) 00065 { 00066 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ; 00067 00068 memset (a_this, 0, sizeof (CRParsingLocation)) ; 00069 return CR_OK ; 00070 } 00071 00072 /** 00073 * cr_parsing_location_copy: 00074 *@a_to: the destination of the copy. 00075 *Must be allocated by the caller. 00076 *@a_from: the source of the copy. 00077 * 00078 *Copies an instance of CRParsingLocation into another one. 00079 * 00080 *Returns CR_OK upon succesful completion, an error code 00081 *otherwise. 00082 */ 00083 enum CRStatus 00084 cr_parsing_location_copy (CRParsingLocation *a_to, 00085 CRParsingLocation const *a_from) 00086 { 00087 g_return_val_if_fail (a_to && a_from, CR_BAD_PARAM_ERROR) ; 00088 00089 memcpy (a_to, a_from, sizeof (CRParsingLocation)) ; 00090 return CR_OK ; 00091 } 00092 00093 /** 00094 * cr_parsing_location_to_string: 00095 *@a_this: the current instance of #CRParsingLocation. 00096 *@a_mask: a bitmap that defines which parts of the 00097 *parsing location are to be serialized (line, column or byte offset) 00098 * 00099 *Returns the serialized string or NULL in case of an error. 00100 */ 00101 gchar * 00102 cr_parsing_location_to_string (CRParsingLocation const *a_this, 00103 enum CRParsingLocationSerialisationMask a_mask) 00104 { 00105 GString *result = NULL ; 00106 gchar *str = NULL ; 00107 00108 g_return_val_if_fail (a_this, NULL) ; 00109 00110 if (!a_mask) { 00111 a_mask = DUMP_LINE | DUMP_COLUMN | DUMP_BYTE_OFFSET ; 00112 } 00113 result =g_string_new (NULL) ; 00114 if (!result) 00115 return NULL ; 00116 if (a_mask & DUMP_LINE) { 00117 g_string_append_printf (result, "line:%d ", 00118 a_this->line) ; 00119 } 00120 if (a_mask & DUMP_COLUMN) { 00121 g_string_append_printf (result, "column:%d ", 00122 a_this->column) ; 00123 } 00124 if (a_mask & DUMP_BYTE_OFFSET) { 00125 g_string_append_printf (result, "byte offset:%d ", 00126 a_this->byte_offset) ; 00127 } 00128 if (result->len) { 00129 str = result->str ; 00130 g_string_free (result, FALSE) ; 00131 } else { 00132 g_string_free (result, TRUE) ; 00133 } 00134 return str ; 00135 } 00136 00137 /** 00138 * cr_parsing_location_dump: 00139 * @a_this: current instance of #CRParsingLocation 00140 * @a_mask: the serialization mask. 00141 * @a_fp: the file pointer to dump the parsing location to. 00142 */ 00143 void 00144 cr_parsing_location_dump (CRParsingLocation const *a_this, 00145 enum CRParsingLocationSerialisationMask a_mask, 00146 FILE *a_fp) 00147 { 00148 gchar *str = NULL ; 00149 00150 g_return_if_fail (a_this && a_fp) ; 00151 str = cr_parsing_location_to_string (a_this, a_mask) ; 00152 if (str) { 00153 fprintf (a_fp, "%s", str) ; 00154 g_free (str) ; 00155 str = NULL ; 00156 } 00157 } 00158 00159 /** 00160 * cr_parsing_location_destroy: 00161 *@a_this: the current instance of #CRParsingLocation. Must 00162 *have been allocated with cr_parsing_location_new(). 00163 * 00164 *Destroys the current instance of #CRParsingLocation 00165 */ 00166 void 00167 cr_parsing_location_destroy (CRParsingLocation *a_this) 00168 { 00169 g_return_if_fail (a_this) ; 00170 g_free (a_this) ; 00171 } 00172