streqvcmp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * $Id: streqvcmp.c,v 4.12 2007/07/04 21:36:38 bkorb Exp $
  3. * Time-stamp: "2007-07-04 11:35:12 bkorb"
  4. *
  5. * String Equivalence Comparison
  6. *
  7. * These routines allow any character to be mapped to any other
  8. * character before comparison. In processing long option names,
  9. * the characters "-", "_" and "^" all need to be equivalent
  10. * (because they are treated so by different development environments).
  11. *
  12. * This file is part of AutoOpts, a companion to AutoGen.
  13. * AutoOpts is free software.
  14. * AutoOpts is copyright (c) 1992-2007 by Bruce Korb - all rights reserved
  15. *
  16. * AutoOpts is available under any one of two licenses. The license
  17. * in use must be one of these two and the choice is under the control
  18. * of the user of the license.
  19. *
  20. * The GNU Lesser General Public License, version 3 or later
  21. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  22. *
  23. * The Modified Berkeley Software Distribution License
  24. * See the file "COPYING.mbsd"
  25. *
  26. * These files have the following md5sums:
  27. *
  28. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  29. * fa82ca978890795162346e661b47161a pkg/libopts/COPYING.lgplv3
  30. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  31. *
  32. * This array is designed for mapping upper and lower case letter
  33. * together for a case independent comparison. The mappings are
  34. * based upon ascii character sequences.
  35. */
  36. static unsigned char charmap[] = {
  37. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, '\a',
  38. '\b', '\t', '\n', '\v', '\f', '\r', 0x0E, 0x0F,
  39. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  40. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  41. ' ', '!', '"', '#', '$', '%', '&', '\'',
  42. '(', ')', '*', '+', ',', '-', '.', '/',
  43. '0', '1', '2', '3', '4', '5', '6', '7',
  44. '8', '9', ':', ';', '<', '=', '>', '?',
  45. '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  46. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  47. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  48. 'x', 'y', 'z', '[', '\\', ']', '^', '_',
  49. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  50. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  51. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  52. 'x', 'y', 'z', '{', '|', '}', '~', 0x7f,
  53. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  54. 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
  55. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  56. 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
  57. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  58. 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  59. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
  60. 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  61. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  62. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
  63. 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
  64. 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
  65. 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  66. 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
  67. 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  68. 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
  69. };
  70. /*=export_func strneqvcmp
  71. *
  72. * what: compare two strings with an equivalence mapping
  73. *
  74. * arg: + char const* + str1 + first string +
  75. * arg: + char const* + str2 + second string +
  76. * arg: + int + ct + compare length +
  77. *
  78. * ret_type: int
  79. * ret_desc: the difference between two differing characters
  80. *
  81. * doc:
  82. *
  83. * Using a character mapping, two strings are compared for "equivalence".
  84. * Each input character is mapped to a comparison character and the
  85. * mapped-to characters are compared for the two NUL terminated input strings.
  86. * The comparison is limited to @code{ct} bytes.
  87. * This function name is mapped to option_strneqvcmp so as to not conflict
  88. * with the POSIX name space.
  89. *
  90. * err: none checked. Caller responsible for seg faults.
  91. =*/
  92. int
  93. strneqvcmp( tCC* s1, tCC* s2, int ct )
  94. {
  95. for (; ct > 0; --ct) {
  96. unsigned char u1 = (unsigned char) *s1++;
  97. unsigned char u2 = (unsigned char) *s2++;
  98. int dif = charmap[ u1 ] - charmap[ u2 ];
  99. if (dif != 0)
  100. return dif;
  101. if (u1 == NUL)
  102. return 0;
  103. }
  104. return 0;
  105. }
  106. /*=export_func streqvcmp
  107. *
  108. * what: compare two strings with an equivalence mapping
  109. *
  110. * arg: + char const* + str1 + first string +
  111. * arg: + char const* + str2 + second string +
  112. *
  113. * ret_type: int
  114. * ret_desc: the difference between two differing characters
  115. *
  116. * doc:
  117. *
  118. * Using a character mapping, two strings are compared for "equivalence".
  119. * Each input character is mapped to a comparison character and the
  120. * mapped-to characters are compared for the two NUL terminated input strings.
  121. * This function name is mapped to option_streqvcmp so as to not conflict
  122. * with the POSIX name space.
  123. *
  124. * err: none checked. Caller responsible for seg faults.
  125. =*/
  126. int
  127. streqvcmp( tCC* s1, tCC* s2 )
  128. {
  129. for (;;) {
  130. unsigned char u1 = (unsigned char) *s1++;
  131. unsigned char u2 = (unsigned char) *s2++;
  132. int dif = charmap[ u1 ] - charmap[ u2 ];
  133. if (dif != 0)
  134. return dif;
  135. if (u1 == NUL)
  136. return 0;
  137. }
  138. }
  139. /*=export_func streqvmap
  140. *
  141. * what: Set the character mappings for the streqv functions
  142. *
  143. * arg: + char + From + Input character +
  144. * arg: + char + To + Mapped-to character +
  145. * arg: + int + ct + compare length +
  146. *
  147. * doc:
  148. *
  149. * Set the character mapping. If the count (@code{ct}) is set to zero, then
  150. * the map is cleared by setting all entries in the map to their index
  151. * value. Otherwise, the "@code{From}" character is mapped to the "@code{To}"
  152. * character. If @code{ct} is greater than 1, then @code{From} and @code{To}
  153. * are incremented and the process repeated until @code{ct} entries have been
  154. * set. For example,
  155. * @example
  156. * streqvmap( 'a', 'A', 26 );
  157. * @end example
  158. * @noindent
  159. * will alter the mapping so that all English lower case letters
  160. * will map to upper case.
  161. *
  162. * This function name is mapped to option_streqvmap so as to not conflict
  163. * with the POSIX name space.
  164. *
  165. * err: none.
  166. =*/
  167. void
  168. streqvmap( char From, char To, int ct )
  169. {
  170. if (ct == 0) {
  171. ct = sizeof( charmap ) - 1;
  172. do {
  173. charmap[ ct ] = ct;
  174. } while (--ct >= 0);
  175. }
  176. else {
  177. int chTo = (int)To & 0xFF;
  178. int chFrom = (int)From & 0xFF;
  179. do {
  180. charmap[ chFrom ] = (unsigned)chTo;
  181. chFrom++;
  182. chTo++;
  183. if ((chFrom >= sizeof( charmap )) || (chTo >= sizeof( charmap )))
  184. break;
  185. } while (--ct > 0);
  186. }
  187. }
  188. /*=export_func strequate
  189. *
  190. * what: map a list of characters to the same value
  191. *
  192. * arg: + char const* + ch_list + characters to equivalence +
  193. *
  194. * doc:
  195. *
  196. * Each character in the input string get mapped to the first character
  197. * in the string.
  198. * This function name is mapped to option_strequate so as to not conflict
  199. * with the POSIX name space.
  200. *
  201. * err: none.
  202. =*/
  203. void
  204. strequate( char const* s )
  205. {
  206. if ((s != NULL) && (*s != NUL)) {
  207. unsigned char equiv = (unsigned)*s;
  208. while (*s != NUL)
  209. charmap[ (unsigned)*(s++) ] = equiv;
  210. }
  211. }
  212. /*=export_func strtransform
  213. *
  214. * what: convert a string into its mapped-to value
  215. *
  216. * arg: + char* + dest + output string +
  217. * arg: + char const* + src + input string +
  218. *
  219. * doc:
  220. *
  221. * Each character in the input string is mapped and the mapped-to
  222. * character is put into the output.
  223. * This function name is mapped to option_strtransform so as to not conflict
  224. * with the POSIX name space.
  225. *
  226. * err: none.
  227. =*/
  228. void
  229. strtransform( char* d, char const* s )
  230. {
  231. do {
  232. *(d++) = (char)charmap[ (unsigned)*s ];
  233. } while (*(s++) != NUL);
  234. }
  235. /*
  236. * Local Variables:
  237. * mode: C
  238. * c-file-style: "stroustrup"
  239. * indent-tabs-mode: nil
  240. * End:
  241. * end of autoopts/streqvcmp.c */