/gplsrc/ctype.c

https://bitbucket.org/foss4mv/scarletdme · C · 189 lines · 92 code · 34 blank · 63 comment · 23 complexity · de4352258fe0d411aecf5475718067be MD5 · raw file

  1. /* CTYPE.C
  2. * Character type handling and associated functions.
  3. * Copyright (c) 2006 Ladybridge Systems, All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  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 General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Ladybridge Systems can be contacted via the www.openqm.com web site.
  20. *
  21. * START-HISTORY:
  22. * 01 Jul 07 2.5-7 Extensive changes for PDA merge.
  23. * 15 Jan 06 2.3-4 0449 All uses of character maps need to be via unsigned
  24. * indices.
  25. * 28 Dec 05 2.3-3 New module.
  26. * 16 Sep 04 2.0-1 OpenQM launch. Earlier history details suppressed.
  27. * END-HISTORY
  28. *
  29. * START-DESCRIPTION:
  30. *
  31. * This module replaces all the standard casing and character type functions
  32. * to enable support of user defined collation sequences and upper/lower case
  33. * pairing rules. By encapsulating all these functions here, future changes
  34. * should be relatively easy to implement.
  35. *
  36. * Although the C library provides locale support functions, these are not
  37. * immediately applicable here because QM requires binary transparency and
  38. * the ability to sort right justified strings (amongst other problems).
  39. *
  40. * It is likely that QM will be adapted to support Unicode and all the
  41. * associated locale related operations in the long term future.
  42. *
  43. * END-DESCRIPTION
  44. *
  45. * START-CODE
  46. */
  47. #include "qm.h"
  48. /* ======================================================================
  49. Set default_character maps */
  50. void set_default_character_maps()
  51. {
  52. int i;
  53. int j;
  54. for(i = 0; i < 256; i++)
  55. {
  56. uc_chars[i] = (char)i;
  57. lc_chars[i] = (char)i;
  58. char_types[i] = 0;
  59. }
  60. for(i = 'a', j = 'A'; i <= 'z'; i++, j++)
  61. {
  62. uc_chars[i] = j;
  63. lc_chars[j] = i;
  64. char_types[i] |= CT_ALPHA;
  65. char_types[j] |= CT_ALPHA;
  66. }
  67. for(i = '0'; i <= '9'; i++)
  68. {
  69. char_types[i] |= CT_DIGIT;
  70. }
  71. for(i = 33; i <= 126; i++)
  72. {
  73. char_types[i] |= CT_GRAPH;
  74. }
  75. char_types[U_TEXT_MARK] |= CT_MARK;
  76. char_types[U_SUBVALUE_MARK] |= CT_MARK | CT_DELIM;
  77. char_types[U_VALUE_MARK] |= CT_MARK | CT_DELIM;
  78. char_types[U_FIELD_MARK] |= CT_MARK | CT_DELIM;
  79. char_types[U_ITEM_MARK] |= CT_MARK;
  80. }
  81. /* ======================================================================
  82. LowerCaseString() - Convert string to lower case */
  83. char * LowerCaseString(char * s)
  84. {
  85. char * p;
  86. p = s;
  87. while((*(p++) = LowerCase(*p)) != '\0') {}
  88. return s;
  89. }
  90. /* ======================================================================
  91. MemCompareNoCase() - Case insensitive variant of memcmp */
  92. int MemCompareNoCase(char * p, char * q, short int len)
  93. {
  94. signed char c;
  95. while(len--)
  96. {
  97. if ((c = UpperCase(*p) - UpperCase(*q)) != 0) return c;
  98. p++;
  99. q++;
  100. }
  101. return 0;
  102. }
  103. /* ======================================================================
  104. memichr() - Case insensitive variant of memchr() */
  105. char * memichr(char * s, char c, int n)
  106. {
  107. c = UpperCase(c);
  108. while(n--)
  109. {
  110. if (UpperCase(*s) == c) return s;
  111. s++;
  112. }
  113. return NULL;
  114. }
  115. /* ======================================================================
  116. memucpy() - Copy a specified number of bytes, converting to uppercase */
  117. void memucpy(char * tgt, char * src, short int len)
  118. {
  119. while(len--) *(tgt++) = UpperCase(*(src++));
  120. }
  121. /* ======================================================================
  122. StringCompLenNoCase() - Case insensitive variant of strncmp */
  123. int StringCompLenNoCase(char * p, char * q, short int len)
  124. {
  125. register char c;
  126. while(len--)
  127. {
  128. if (((c = UpperCase(*p) - UpperCase(*q)) != 0)
  129. || (*p == '\0') || (*q == '\0')) return c;
  130. p++;
  131. q++;
  132. }
  133. return 0;
  134. }
  135. /* ======================================================================
  136. UpperCaseMem() - Uppercase specified number of bytes */
  137. void UpperCaseMem(char * str, short int len)
  138. {
  139. register char c;
  140. while(len--)
  141. {
  142. c = UpperCase(*str);
  143. *(str++) = c;
  144. }
  145. }
  146. /* ======================================================================
  147. UpperCaseString() - Convert string to upper case */
  148. char * UpperCaseString(char * s)
  149. {
  150. char * p;
  151. p = s;
  152. while((*p = UpperCase(*p)) != '\0') {p++;}
  153. return s;
  154. }
  155. /* END-CODE */