streqvcmp.c 7.9 KB

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