PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cssed-0.4.0/libcroco/parser/cr-parsing-location.c

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