PageRenderTime 136ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/ cfstox/CFStox/ta-lib-0.4.0-msvc/ta-lib/excel/src/xlw/trio/strio.c

http://cfstox.googlecode.com/
C | 581 lines | 425 code | 58 blank | 98 comment | 130 complexity | 112103f3938ba3c5f1c9c0fd34297db6 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*************************************************************************
  2. *
  3. * $Id: strio.c 3 2002-07-16 05:43:26Z fortier $
  4. *
  5. * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  12. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  13. * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
  14. * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
  15. *
  16. ************************************************************************/
  17. /*
  18. * TODO
  19. * - StrToLongDouble
  20. */
  21. static const char rcsid[] = "@(#)$Id: strio.c 3 2002-07-16 05:43:26Z fortier $";
  22. #if defined(unix) || defined(__xlC__) || defined(__QNX__)
  23. # define PLATFORM_UNIX
  24. #elif defined(WIN32) || defined(_WIN32)
  25. # define PLATFORM_WIN32
  26. #elif defined(AMIGA) && defined(__GNUC__)
  27. # define PLATFORM_UNIX
  28. #endif
  29. #if defined(__STDC__) && (__STDC_VERSION__ >= 199901L)
  30. # define TRIO_C99
  31. #endif
  32. #include "strio.h"
  33. #include <string.h>
  34. #include <locale.h>
  35. #include <ctype.h>
  36. #include <stdarg.h>
  37. #include <time.h>
  38. #include <math.h>
  39. #ifndef DEBUG
  40. # define NDEBUG
  41. #endif
  42. #include <assert.h>
  43. #ifndef NULL
  44. # define NULL 0
  45. #endif
  46. #define NIL ((char)0)
  47. #ifndef FALSE
  48. # define FALSE (1 == 0)
  49. # define TRUE (! FALSE)
  50. #endif
  51. #define VALID(x) (NULL != (x))
  52. #define INVALID(x) (NULL == (x))
  53. #if defined(PLATFORM_UNIX)
  54. # define USE_STRCASECMP
  55. # define USE_STRNCASECMP
  56. # define USE_STRERROR
  57. # if defined(__QNX__)
  58. # define strcasecmp(x,y) stricmp(x,y)
  59. # define strncasecmp(x,y,n) strnicmp(x,y,n)
  60. # endif
  61. #elif defined(PLATFORM_WIN32)
  62. # define USE_STRCASECMP
  63. # define strcasecmp(x,y) strcmpi(x,y)
  64. #endif
  65. /*************************************************************************
  66. * StrAppendMax
  67. */
  68. char *StrAppendMax(char *target, size_t max, const char *source)
  69. {
  70. assert(VALID(target));
  71. assert(VALID(source));
  72. assert(max > 0);
  73. max -= StrLength(target) + 1;
  74. return (max > 0) ? strncat(target, source, max) : target;
  75. }
  76. /*************************************************************************
  77. * StrCopyMax
  78. */
  79. char *StrCopyMax(char *target, size_t max, const char *source)
  80. {
  81. assert(VALID(target));
  82. assert(VALID(source));
  83. assert(max > 0); /* Includes != 0 */
  84. target = strncpy(target, source, max - 1);
  85. target[max - 1] = (char)0;
  86. return target;
  87. }
  88. /*************************************************************************
  89. * StrDuplicate
  90. */
  91. char *StrDuplicate(const char *source)
  92. {
  93. char *target;
  94. assert(VALID(source));
  95. target = StrAlloc(StrLength(source) + 1);
  96. if (target)
  97. {
  98. StrCopy(target, source);
  99. }
  100. return target;
  101. }
  102. /*************************************************************************
  103. * StrDuplicateMax
  104. */
  105. char *StrDuplicateMax(const char *source, size_t max)
  106. {
  107. char *target;
  108. size_t len;
  109. assert(VALID(source));
  110. assert(max > 0);
  111. /* Make room for string plus a terminating zero */
  112. len = StrLength(source) + 1;
  113. if (len > max)
  114. {
  115. len = max;
  116. }
  117. target = StrAlloc(len);
  118. if (target)
  119. {
  120. StrCopyMax(target, len, source);
  121. }
  122. return target;
  123. }
  124. /*************************************************************************
  125. * StrEqual
  126. */
  127. int StrEqual(const char *first, const char *second)
  128. {
  129. assert(VALID(first));
  130. assert(VALID(second));
  131. if (VALID(first) && VALID(second))
  132. {
  133. #if defined(USE_STRCASECMP)
  134. return (0 == strcasecmp(first, second));
  135. #else
  136. while ((*first != NIL) && (*second != NIL))
  137. {
  138. if (toupper(*first) != toupper(*second))
  139. {
  140. break;
  141. }
  142. first++;
  143. second++;
  144. }
  145. return ((*first == NIL) && (*second == NIL));
  146. #endif
  147. }
  148. return FALSE;
  149. }
  150. /*************************************************************************
  151. * StrEqualCase
  152. */
  153. int StrEqualCase(const char *first, const char *second)
  154. {
  155. assert(VALID(first));
  156. assert(VALID(second));
  157. if (VALID(first) && VALID(second))
  158. {
  159. return (0 == strcmp(first, second));
  160. }
  161. return FALSE;
  162. }
  163. /*************************************************************************
  164. * StrEqualCaseMax
  165. */
  166. int StrEqualCaseMax(const char *first, size_t max, const char *second)
  167. {
  168. assert(VALID(first));
  169. assert(VALID(second));
  170. if (VALID(first) && VALID(second))
  171. {
  172. return (0 == strncmp(first, second, max));
  173. }
  174. return FALSE;
  175. }
  176. /*************************************************************************
  177. * StrEqualLocale
  178. */
  179. int StrEqualLocale(const char *first, const char *second)
  180. {
  181. assert(VALID(first));
  182. assert(VALID(second));
  183. #if defined(LC_COLLATE)
  184. return (strcoll(first, second) == 0);
  185. #else
  186. return StrEqual(first, second);
  187. #endif
  188. }
  189. /*************************************************************************
  190. * StrEqualMax
  191. */
  192. int StrEqualMax(const char *first, size_t max, const char *second)
  193. {
  194. assert(VALID(first));
  195. assert(VALID(second));
  196. if (VALID(first) && VALID(second))
  197. {
  198. #if defined(USE_STRNCASECMP)
  199. return (0 == strncasecmp(first, second, max));
  200. #else
  201. /* Not adequately tested yet */
  202. size_t cnt = 0;
  203. while ((*first != NIL) && (*second != NIL) && (cnt <= max))
  204. {
  205. if (toupper(*first) != toupper(*second))
  206. {
  207. break;
  208. }
  209. first++;
  210. second++;
  211. cnt++;
  212. }
  213. return ((cnt == max) || ((*first == NIL) && (*second == NIL)));
  214. #endif
  215. }
  216. return FALSE;
  217. }
  218. /*************************************************************************
  219. * StrError
  220. */
  221. const char *StrError(int errorNumber)
  222. {
  223. #if defined(USE_STRERROR)
  224. return strerror(errorNumber);
  225. #else
  226. return "unknown";
  227. #endif
  228. }
  229. /*************************************************************************
  230. * StrFormatDate
  231. */
  232. size_t StrFormatDateMax(char *target,
  233. size_t max,
  234. const char *format,
  235. const struct tm *datetime)
  236. {
  237. assert(VALID(target));
  238. assert(VALID(format));
  239. assert(VALID(datetime));
  240. assert(max > 0);
  241. return strftime(target, max, format, datetime);
  242. }
  243. /*************************************************************************
  244. * StrHash
  245. */
  246. unsigned long StrHash(const char *string, int type)
  247. {
  248. unsigned long value = 0L;
  249. char ch;
  250. assert(VALID(string));
  251. switch (type)
  252. {
  253. case STRIO_HASH_PLAIN:
  254. while ( (ch = *string++) != NIL )
  255. {
  256. value *= 31;
  257. value += (unsigned long)ch;
  258. }
  259. break;
  260. default:
  261. assert(FALSE);
  262. break;
  263. }
  264. return value;
  265. }
  266. /*************************************************************************
  267. * StrMatch
  268. */
  269. int StrMatch(char *string, char *pattern)
  270. {
  271. assert(VALID(string));
  272. assert(VALID(pattern));
  273. for (; ('*' != *pattern); ++pattern, ++string)
  274. {
  275. if (NIL == *string)
  276. {
  277. return (NIL == *pattern);
  278. }
  279. if ((toupper((int)*string) != toupper((int)*pattern))
  280. && ('?' != *pattern))
  281. {
  282. return FALSE;
  283. }
  284. }
  285. /* two-line patch to prevent *too* much recursiveness: */
  286. while ('*' == pattern[1])
  287. pattern++;
  288. do
  289. {
  290. if ( StrMatch(string, &pattern[1]) )
  291. {
  292. return TRUE;
  293. }
  294. }
  295. while (*string++);
  296. return FALSE;
  297. }
  298. /*************************************************************************
  299. * StrMatchCase
  300. */
  301. int StrMatchCase(char *string, char *pattern)
  302. {
  303. assert(VALID(string));
  304. assert(VALID(pattern));
  305. for (; ('*' != *pattern); ++pattern, ++string)
  306. {
  307. if (NIL == *string)
  308. {
  309. return (NIL == *pattern);
  310. }
  311. if ((*string != *pattern)
  312. && ('?' != *pattern))
  313. {
  314. return FALSE;
  315. }
  316. }
  317. /* two-line patch to prevent *too* much recursiveness: */
  318. while ('*' == pattern[1])
  319. pattern++;
  320. do
  321. {
  322. if ( StrMatchCase(string, &pattern[1]) )
  323. {
  324. return TRUE;
  325. }
  326. }
  327. while (*string++);
  328. return FALSE;
  329. }
  330. /*************************************************************************
  331. * StrSpanFunction
  332. *
  333. * Untested
  334. */
  335. size_t StrSpanFunction(char *source, int (*Function)(int))
  336. {
  337. size_t count = 0;
  338. assert(VALID(source));
  339. assert(VALID(Function));
  340. while (*source != NIL)
  341. {
  342. if (Function(*source))
  343. break; /* while */
  344. source++;
  345. count++;
  346. }
  347. return count;
  348. }
  349. /*************************************************************************
  350. * StrSubstringMax
  351. */
  352. char *StrSubstringMax(const char *string, size_t max, const char *find)
  353. {
  354. size_t count;
  355. size_t size;
  356. char *result = NULL;
  357. assert(VALID(string));
  358. assert(VALID(find));
  359. size = StrLength(find);
  360. if (size <= max)
  361. {
  362. for (count = 0; count <= max - size; count++)
  363. {
  364. if (StrEqualMax(find, size, &string[count]))
  365. {
  366. result = (char *)&string[count];
  367. break;
  368. }
  369. }
  370. }
  371. return result;
  372. }
  373. /*************************************************************************
  374. * StrToDouble
  375. *
  376. * double ::= [ <sign> ]
  377. * ( <number> |
  378. * <number> <decimal_point> <number> |
  379. * <decimal_point> <number> )
  380. * [ <exponential> [ <sign> ] <number> ]
  381. * number ::= 1*( <digit> )
  382. * digit ::= ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )
  383. * exponential ::= ( 'e' | 'E' )
  384. * sign ::= ( '-' | '+' )
  385. * decimal_point ::= '.'
  386. */
  387. double StrToDouble(const char *source, const char **endp)
  388. {
  389. #if defined(TRIO_C99)
  390. return strtod(source, (char **)endp);
  391. #else
  392. /* Preliminary code */
  393. int isNegative = FALSE;
  394. int isExponentNegative = FALSE;
  395. unsigned long integer = 0;
  396. unsigned long fraction = 0;
  397. unsigned long fracdiv = 1;
  398. unsigned long exponent = 0;
  399. double value = 0.0;
  400. /* First try hex-floats */
  401. if ((source[0] == '0') && ((source[1] == 'x') || (source[1] == 'X')))
  402. {
  403. source += 2;
  404. while (isxdigit((int)*source))
  405. {
  406. integer *= 16;
  407. integer += (isdigit((int)*source)
  408. ? (*source - '0')
  409. : 10 + (toupper((int)*source) - 'A'));
  410. source++;
  411. }
  412. if (*source == '.')
  413. {
  414. source++;
  415. while (isxdigit((int)*source))
  416. {
  417. fraction *= 16;
  418. fraction += (isdigit((int)*source)
  419. ? (*source - '0')
  420. : 10 + (toupper((int)*source) - 'A'));
  421. fracdiv *= 16;
  422. source++;
  423. }
  424. if ((*source == 'p') || (*source == 'P'))
  425. {
  426. source++;
  427. if ((*source == '+') || (*source == '-'))
  428. {
  429. isExponentNegative = (*source == '-');
  430. source++;
  431. }
  432. while (isdigit((int)*source))
  433. {
  434. exponent *= 10;
  435. exponent += (*source - '0');
  436. source++;
  437. }
  438. }
  439. }
  440. }
  441. else /* Then try normal decimal floats */
  442. {
  443. isNegative = (*source == '-');
  444. /* Skip sign */
  445. if ((*source == '+') || (*source == '-'))
  446. source++;
  447. /* Integer part */
  448. while (isdigit((int)*source))
  449. {
  450. integer *= 10;
  451. integer += (*source - '0');
  452. source++;
  453. }
  454. if (*source == '.')
  455. {
  456. source++; /* skip decimal point */
  457. while (isdigit((int)*source))
  458. {
  459. fraction *= 10;
  460. fraction += (*source - '0');
  461. fracdiv *= 10;
  462. source++;
  463. }
  464. }
  465. if ((*source == 'e') || (*source == 'E'))
  466. {
  467. source++; /* Skip exponential indicator */
  468. isExponentNegative = (*source == '-');
  469. if ((*source == '+') || (*source == '-'))
  470. source++;
  471. while (isdigit((int)*source))
  472. {
  473. exponent *= 10;
  474. exponent += (*source - '0');
  475. source++;
  476. }
  477. }
  478. }
  479. value = (double)integer;
  480. if (fraction != 0)
  481. {
  482. value += (double)fraction / (double)fracdiv;
  483. }
  484. if (exponent != 0)
  485. {
  486. if (isExponentNegative)
  487. value /= pow((double)10, (double)exponent);
  488. else
  489. value *= pow((double)10, (double)exponent);
  490. }
  491. if (isNegative)
  492. value = -value;
  493. if (endp)
  494. *endp = source;
  495. return value;
  496. #endif
  497. }
  498. /*************************************************************************
  499. * StrToFloat
  500. */
  501. float StrToFloat(const char *source, const char **endp)
  502. {
  503. #if defined(TRIO_C99)
  504. return strtof(source, (char **)endp);
  505. #else
  506. return (float)StrToDouble(source, endp);
  507. #endif
  508. }
  509. /*************************************************************************
  510. * StrToUpper
  511. */
  512. int StrToUpper(char *target)
  513. {
  514. int i = 0;
  515. assert(VALID(target));
  516. while (NIL != *target)
  517. {
  518. *target = toupper((int)*target);
  519. target++;
  520. i++;
  521. }
  522. return i;
  523. }