2 * << Haru Free PDF Library >> -- hpdf_utils.c
4 * URL: http://libharu.org
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
14 * It is provided "as is" without express or implied warranty.
19 #include "hpdf_utils.h"
20 #include "hpdf_consts.h"
22 /*---------------------------------------------------------------------------*/
25 HPDF_AToI (const char *s)
27 HPDF_BOOL flg = HPDF_FALSE;
34 /* increment pointer until the charactor of 's' is not
35 * white-space-charactor.
38 if (HPDF_IS_WHITE_SPACE(*s))
49 while (*s >= '0' && *s <= '9') {
63 HPDF_AToF (const char *s)
65 HPDF_BOOL flg = HPDF_FALSE;
70 /* increment pointer until the charactor of 's' is not
71 * white-space-charactor.
74 if (HPDF_IS_WHITE_SPACE(*s))
85 while (*s >= '0' && *s <= '9') {
96 while (*s >= '0' && *s <= '9') {
107 v = (HPDF_DOUBLE)i / tmp;
122 char buf[HPDF_INT_LEN + 1];
125 if (val < HPDF_LIMIT_MIN_INT)
126 val = HPDF_LIMIT_MIN_INT;
129 } else if (val > HPDF_LIMIT_MAX_INT) {
130 val = HPDF_LIMIT_MAX_INT;
131 } else if (val == 0) {
135 t = buf + HPDF_INT_LEN;
139 *t = (char)((char)(val % 10) + '0');
145 while (s < eptr && *t != 0)
161 if (val > HPDF_LIMIT_MAX_INT)
162 val = HPDF_LIMIT_MAX_INT;
167 while (val > 0 && t >= s) {
168 *t = (char)((char)(val % 10) + '0');
186 HPDF_INT32 fpart_val;
187 char buf[HPDF_REAL_LEN + 1];
192 if (val > HPDF_LIMIT_MAX_REAL)
193 val = HPDF_LIMIT_MAX_REAL;
195 if (val < HPDF_LIMIT_MIN_REAL)
196 val = HPDF_LIMIT_MIN_REAL;
198 t = buf + HPDF_REAL_LEN;
206 /* separate an integer part and a decimal part. */
207 int_val = (HPDF_INT32)(val + 0.000005);
208 fpart_val = (HPDF_INT32)((HPDF_REAL)(val - int_val + 0.000005) * 100000);
210 /* process decimal part */
211 for (i = 0; i < 5; i++) {
212 *t = (char)((char)(fpart_val % 10) + '0');
217 /* process integer part */
223 while (int_val > 0) {
224 *t = (char)((char)(int_val % 10) + '0');
230 while (s <= eptr && *t != 0)
234 /* delete an excessive decimal portion. */
246 return (*s == 0) ? s : ++s;
251 HPDF_MemCpy (HPDF_BYTE* out,
265 HPDF_StrCpy (char *out,
270 while (eptr > out && *in != 0)
276 return (HPDF_BYTE *)out;
281 HPDF_MemCmp (const HPDF_BYTE *s1,
301 HPDF_StrCmp (const char *s1,
314 if (*s1 == 0 || *s2 == 0)
318 return (HPDF_BYTE)*s1 - (HPDF_BYTE)*s2;
323 HPDF_MemSet (void *s,
327 HPDF_BYTE* b = (HPDF_BYTE*)s;
340 HPDF_StrLen (const char *s,
348 while (*s != 0 && (maxlen < 0 || len < maxlen)) {
353 return (HPDF_UINT)len;
358 HPDF_StrStr (const char *s1,
362 HPDF_UINT len = HPDF_StrLen (s2, -1);
371 maxlen = HPDF_StrLen (s1, -1);
380 if (HPDF_MemCmp ((HPDF_BYTE *)s1, (HPDF_BYTE *)s2, len) == 0)
392 HPDF_ToBox (HPDF_INT16 left,
409 HPDF_ToPoint (HPDF_INT16 x,
421 HPDF_ToRect (HPDF_REAL left,
429 rect.bottom = bottom;
438 HPDF_UInt16Swap (HPDF_UINT16 *value)
442 HPDF_MemCpy (u, (HPDF_BYTE*)value, 2);
443 *value = (HPDF_UINT16)((HPDF_UINT16)u[0] << 8 | (HPDF_UINT16)u[1]);