PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/cssed-0.4.0/libcroco/parser/cr-num.c

#
C | 295 lines | 153 code | 53 blank | 89 comment | 11 complexity | bd4e42edbd9d741915219efc102ab7a4 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
  2. /*
  3. * This file is part of The Croco Library
  4. *
  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 COPYRIGHTS file for copyrights information.
  22. */
  23. /**
  24. *@file
  25. *The definition
  26. *of the #CRNum class.
  27. */
  28. #include "cr-num.h"
  29. #include "string.h"
  30. /**
  31. *The default constructor of
  32. *#CRNum.
  33. *@return the newly built instance of
  34. *#CRNum.
  35. */
  36. CRNum *
  37. cr_num_new (void)
  38. {
  39. CRNum *result = NULL;
  40. result = g_try_malloc (sizeof (CRNum));
  41. if (result == NULL) {
  42. cr_utils_trace_info ("Out of memory");
  43. return NULL;
  44. }
  45. memset (result, 0, sizeof (CRNum));
  46. return result;
  47. }
  48. /**
  49. *A constructor of #CRNum.
  50. *@param a_is_natural indicates whether the intance of #CRNum is
  51. *a natural number or not.
  52. *@param a_integer_part the integer part of the instance
  53. *of #CRNum
  54. *@param a_decimal_part in case the instance of #CRNum
  55. *natural number (but a decimal one) this parameter
  56. *is the decimal part of the instance of #CRNum.
  57. *@return the newly built instance of #CRNum or
  58. *NULL if an error arises.
  59. */
  60. CRNum *
  61. cr_num_new_with_val (gdouble a_val, enum CRNumType a_type)
  62. {
  63. CRNum *result = NULL;
  64. result = cr_num_new ();
  65. g_return_val_if_fail (result, NULL);
  66. result->val = a_val;
  67. result->type = a_type;
  68. return result;
  69. }
  70. /**
  71. *Returns the string representation of the
  72. *current instance of #CRNum.
  73. *@param a_this the current instance of #CRNum.
  74. *@return the newly built string representation
  75. *of the current instance of #CRNum. The returned
  76. *string is NULL terminated. The caller *must*
  77. *free the returned string.
  78. */
  79. guchar *
  80. cr_num_to_string (CRNum * a_this)
  81. {
  82. gdouble test_val = 0.0;
  83. guchar *tmp_char1 = NULL,
  84. *tmp_char2 = NULL,
  85. *result = NULL;
  86. g_return_val_if_fail (a_this, NULL);
  87. test_val = a_this->val - (glong) a_this->val;
  88. if (!test_val) {
  89. tmp_char1 = g_strdup_printf ("%ld", (glong) a_this->val);
  90. } else {
  91. tmp_char1 = g_strdup_printf ("%.3f", a_this->val);
  92. }
  93. g_return_val_if_fail (tmp_char1, NULL);
  94. switch (a_this->type) {
  95. case NUM_LENGTH_EM:
  96. tmp_char2 = (guchar *) "em";
  97. break;
  98. case NUM_LENGTH_EX:
  99. tmp_char2 = (guchar *) "ex";
  100. break;
  101. case NUM_LENGTH_PX:
  102. tmp_char2 = (guchar *) "px";
  103. break;
  104. case NUM_LENGTH_IN:
  105. tmp_char2 = (guchar *) "in";
  106. break;
  107. case NUM_LENGTH_CM:
  108. tmp_char2 = (guchar *) "cm";
  109. break;
  110. case NUM_LENGTH_MM:
  111. tmp_char2 = (guchar *) "mm";
  112. break;
  113. case NUM_LENGTH_PT:
  114. tmp_char2 = (guchar *) "pt";
  115. break;
  116. case NUM_LENGTH_PC:
  117. tmp_char2 = (guchar *) "pc";
  118. break;
  119. case NUM_ANGLE_DEG:
  120. tmp_char2 = (guchar *) "deg";
  121. break;
  122. case NUM_ANGLE_RAD:
  123. tmp_char2 = (guchar *) "rad";
  124. break;
  125. case NUM_ANGLE_GRAD:
  126. tmp_char2 = (guchar *) "grad";
  127. break;
  128. case NUM_TIME_MS:
  129. tmp_char2 = (guchar *) "ms";
  130. break;
  131. case NUM_TIME_S:
  132. tmp_char2 = (guchar *) "s";
  133. break;
  134. case NUM_FREQ_HZ:
  135. tmp_char2 = (guchar *) "Hz";
  136. break;
  137. case NUM_FREQ_KHZ:
  138. tmp_char2 = (guchar *) "KHz";
  139. break;
  140. case NUM_PERCENTAGE:
  141. tmp_char2 = (guchar *) "%";
  142. break;
  143. case NUM_INHERIT:
  144. tmp_char2 = (guchar *) "inherit";
  145. break ;
  146. case NUM_AUTO:
  147. tmp_char2 = (guchar *) "auto";
  148. break ;
  149. case NUM_GENERIC:
  150. tmp_char2 = NULL ;
  151. break ;
  152. default:
  153. tmp_char2 = (guchar *) "unknown";
  154. break;
  155. }
  156. if (tmp_char2) {
  157. result = g_strconcat (tmp_char1, tmp_char2, NULL);
  158. g_free (tmp_char1);
  159. } else {
  160. result = tmp_char1;
  161. }
  162. return result;
  163. }
  164. /**
  165. *Copies an instance of #CRNum.
  166. *@param a_src the instance of #CRNum to copy.
  167. *Must be non NULL.
  168. *@param a_dst the destination of the copy.
  169. *Must be non NULL
  170. *@return CR_OK upon successful completion, an
  171. *error code otherwise.
  172. */
  173. enum CRStatus
  174. cr_num_copy (CRNum * a_dest, CRNum * a_src)
  175. {
  176. g_return_val_if_fail (a_dest && a_src, CR_BAD_PARAM_ERROR);
  177. memcpy (a_dest, a_src, sizeof (CRNum));
  178. return CR_OK;
  179. }
  180. /**
  181. *Duplicates an instance of #CRNum
  182. *@param a_this the instance of #CRNum to duplicate.
  183. *@return the newly created (duplicated) instance of #CRNum.
  184. *Must be freed by cr_num_destroy().
  185. */
  186. CRNum *
  187. cr_num_dup (CRNum * a_this)
  188. {
  189. CRNum *result = NULL;
  190. enum CRStatus status = CR_OK;
  191. g_return_val_if_fail (a_this, NULL);
  192. result = cr_num_new ();
  193. g_return_val_if_fail (result, NULL);
  194. status = cr_num_copy (result, a_this);
  195. g_return_val_if_fail (status == CR_OK, NULL);
  196. return result;
  197. }
  198. /**
  199. *Sets an instance of #CRNum.
  200. *@param a_this the current instance of #CRNum to be set.
  201. *@param a_val the new numerical value to be hold by the current
  202. *instance of #CRNum
  203. *@param a_type the new type of #CRNum.
  204. */
  205. enum CRStatus
  206. cr_num_set (CRNum * a_this, gdouble a_val, enum CRNumType a_type)
  207. {
  208. g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
  209. a_this->val = a_val;
  210. a_this->type = a_type;
  211. return CR_OK;
  212. }
  213. /**
  214. *Tests if the current instance of #CRNum is a fixed
  215. *length value or not. Typically a fixed length value
  216. *is anything from NUM_LENGTH_EM to NUM_LENGTH_PC.
  217. *See the definition of #CRNumType to see what we mean.
  218. *@return TRUE if the instance of #CRNum is a fixed length number,
  219. *FALSE otherwise.
  220. */
  221. gboolean
  222. cr_num_is_fixed_length (CRNum * a_this)
  223. {
  224. gboolean result = FALSE;
  225. g_return_val_if_fail (a_this, FALSE);
  226. if (a_this->type >= NUM_LENGTH_EM
  227. && a_this->type <= NUM_LENGTH_PC) {
  228. result = TRUE ;
  229. }
  230. return result ;
  231. }
  232. /**
  233. *The destructor of #CRNum.
  234. *@param a_this the this pointer of
  235. *the current instance of #CRNum.
  236. */
  237. void
  238. cr_num_destroy (CRNum * a_this)
  239. {
  240. g_return_if_fail (a_this);
  241. g_free (a_this);
  242. }