Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

cr-parsing-location.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: ni; 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  *@file
00029  *Definition of the #CRparsingLocation class.
00030  */
00031 
00032 
00033 /**
00034  *Instanciates a new parsing location.
00035  *@return the newly instanciated #CRParsingLocation.
00036  *Must be freed by cr_parsing_location_destroy()
00037  */
00038 CRParsingLocation * 
00039 cr_parsing_location_new (void)
00040 {
00041         CRParsingLocation * result = NULL ;
00042 
00043         result = g_try_malloc (sizeof (CRParsingLocation)) ;
00044         if (!result) {
00045                 cr_utils_trace_info ("Out of memory error") ;
00046                 return NULL ;
00047         }
00048         cr_parsing_location_init (result) ;
00049         return result ;
00050 }
00051 
00052 /**
00053  *Initializes the an instance of #CRparsingLocation.
00054  *@param a_this the current instance of #CRParsingLocation.
00055  *@return CR_OK upon
00056  */
00057 enum CRStatus 
00058 cr_parsing_location_init (CRParsingLocation *a_this)
00059 {
00060         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
00061 
00062         memset (a_this, 0, sizeof (CRParsingLocation)) ;
00063         return CR_OK ;
00064 }
00065 
00066 /**
00067  *Copies an instance of CRParsingLocation into another one.
00068  *@param a_to the destination of the copy. 
00069  *Must be allocated by the caller.
00070  *@param a_from the source of the copy.
00071  *@return CR_OK upon succesful completion, an error code
00072  *otherwise.
00073  */
00074 enum CRStatus 
00075 cr_parsing_location_copy (CRParsingLocation *a_to,
00076                           CRParsingLocation *a_from)
00077 {
00078         g_return_val_if_fail (a_to && a_from, CR_BAD_PARAM_ERROR) ;
00079 
00080         memcpy (a_to, a_from, sizeof (CRParsingLocation)) ;
00081         return CR_OK ;
00082 }
00083 
00084 /**
00085  *@param a_this the current instance of #CRParsingLocation.
00086  *@param a_mask a bitmap that defines which parts of the
00087  *parsing location are to be serialized (line, column or byte offset)
00088  *@return the serialized string or NULL in case of an error.
00089  */
00090 gchar * 
00091 cr_parsing_location_to_string (CRParsingLocation *a_this,
00092                                enum CRParsingLocationSerialisationMask a_mask)
00093 {
00094         GString *result = NULL ;
00095         gchar *str = NULL ;
00096 
00097         g_return_val_if_fail (a_this, NULL) ;
00098 
00099         if (!a_mask) {
00100                 a_mask = DUMP_LINE | DUMP_COLUMN | DUMP_BYTE_OFFSET ;
00101         }
00102         result =g_string_new (NULL) ;
00103         if (!result)
00104                 return NULL ;
00105         if (a_mask & DUMP_LINE) {
00106                 g_string_append_printf (result, "line:%d ", 
00107                                         a_this->line) ;
00108         }
00109         if (a_mask & DUMP_COLUMN) {
00110                 g_string_append_printf (result, "column:%d ", 
00111                                         a_this->column) ;
00112         }
00113         if (a_mask & DUMP_BYTE_OFFSET) {
00114                 g_string_append_printf (result, "byte offset:%d ", 
00115                                         a_this->byte_offset) ;
00116         }
00117         if (result->len) {
00118                 str = result->str ;
00119                 g_string_free (result, FALSE) ;
00120         } else {
00121                 g_string_free (result, TRUE) ;
00122         }
00123         return str ;
00124 }
00125 
00126 void
00127 cr_parsing_location_dump (CRParsingLocation *a_this,
00128                           enum CRParsingLocationSerialisationMask a_mask,
00129                           FILE *a_fp)
00130 {
00131         gchar *str = NULL ;
00132 
00133         g_return_if_fail (a_this && a_fp) ;
00134         str = cr_parsing_location_to_string (a_this, a_mask) ;
00135         if (str) {
00136                 fprintf (a_fp, "%s", str) ;
00137                 g_free (str) ;
00138                 str = NULL ;
00139         }
00140 }
00141 
00142 /**
00143  *Destroys the current instance of #CRParsingLocation
00144  *@param a_this the current instance of #CRParsingLocation. Must
00145  *have been allocated with cr_parsing_location_new().
00146  */
00147 void 
00148 cr_parsing_location_destroy (CRParsingLocation *a_this)
00149 {
00150         g_return_if_fail (a_this) ;
00151         g_free (a_this) ;
00152 }
00153 

Generated on Fri Oct 29 08:29:12 2004 for Libcroco by  doxygen 1.3.9.1