streqvcmp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * $Id: streqvcmp.c,v 4.10 2007/04/28 22:19:23 bkorb Exp $
  3. * Time-stamp: "2006-07-26 18:25:53 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. /*
  13. * Automated Options copyright 1992-2007 Bruce Korb
  14. *
  15. * Automated Options is free software.
  16. * You may redistribute it and/or modify it under the terms of the
  17. * GNU General Public License, as published by the Free Software
  18. * Foundation; either version 2, or (at your option) any later version.
  19. *
  20. * Automated Options is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with Automated Options. See the file "COPYING". If not,
  27. * write to: The Free Software Foundation, Inc.,
  28. * 51 Franklin Street, Fifth Floor,
  29. * Boston, MA 02110-1301, USA.
  30. *
  31. * As a special exception, Bruce Korb gives permission for additional
  32. * uses of the text contained in his release of AutoOpts.
  33. *
  34. * The exception is that, if you link the AutoOpts library with other
  35. * files to produce an executable, this does not by itself cause the
  36. * resulting executable to be covered by the GNU General Public License.
  37. * Your use of that executable is in no way restricted on account of
  38. * linking the AutoOpts library code into it.
  39. *
  40. * This exception does not however invalidate any other reasons why
  41. * the executable file might be covered by the GNU General Public License.
  42. *
  43. * This exception applies only to the code released by Bruce Korb under
  44. * the name AutoOpts. If you copy code from other sources under the
  45. * General Public License into a copy of AutoOpts, as the General Public
  46. * License permits, the exception does not apply to the code that you add
  47. * in this way. To avoid misleading anyone as to the status of such
  48. * modified files, you must delete this exception notice from them.
  49. *
  50. * If you write modifications of your own for AutoOpts, it is your choice
  51. * whether to permit this exception to apply to your modifications.
  52. * If you do not wish that, delete this exception notice.
  53. */
  54. /*
  55. * This array is designed for mapping upper and lower case letter
  56. * together for a case independent comparison. The mappings are
  57. * based upon ascii character sequences.
  58. */
  59. static unsigned char charmap[] = {
  60. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, '\a',
  61. '\b', '\t', '\n', '\v', '\f', '\r', 0x0E, 0x0F,
  62. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  63. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  64. ' ', '!', '"', '#', '$', '%', '&', '\'',
  65. '(', ')', '*', '+', ',', '-', '.', '/',
  66. '0', '1', '2', '3', '4', '5', '6', '7',
  67. '8', '9', ':', ';', '<', '=', '>', '?',
  68. '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  69. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  70. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  71. 'x', 'y', 'z', '[', '\\', ']', '^', '_',
  72. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  73. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  74. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  75. 'x', 'y', 'z', '{', '|', '}', '~', 0x7f,
  76. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  77. 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
  78. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  79. 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
  80. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  81. 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  82. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
  83. 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  84. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  85. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
  86. 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
  87. 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
  88. 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
  89. 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
  90. 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  91. 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
  92. };
  93. /*=export_func strneqvcmp
  94. *
  95. * what: compare two strings with an equivalence mapping
  96. *
  97. * arg: + char const* + str1 + first string +
  98. * arg: + char const* + str2 + second string +
  99. * arg: + int + ct + compare length +
  100. *
  101. * ret_type: int
  102. * ret_desc: the difference between two differing characters
  103. *
  104. * doc:
  105. *
  106. * Using a character mapping, two strings are compared for "equivalence".
  107. * Each input character is mapped to a comparison character and the
  108. * mapped-to characters are compared for the two NUL terminated input strings.
  109. * The comparison is limited to @code{ct} bytes.
  110. * This function name is mapped to option_strneqvcmp so as to not conflict
  111. * with the POSIX name space.
  112. *
  113. * err: none checked. Caller responsible for seg faults.
  114. =*/
  115. int
  116. strneqvcmp( tCC* s1, tCC* s2, int ct )
  117. {
  118. for (; ct > 0; --ct) {
  119. unsigned char u1 = (unsigned char) *s1++;
  120. unsigned char u2 = (unsigned char) *s2++;
  121. int dif = charmap[ u1 ] - charmap[ u2 ];
  122. if (dif != 0)
  123. return dif;
  124. if (u1 == NUL)
  125. return 0;
  126. }
  127. return 0;
  128. }
  129. /*=export_func streqvcmp
  130. *
  131. * what: compare two strings with an equivalence mapping
  132. *
  133. * arg: + char const* + str1 + first string +
  134. * arg: + char const* + str2 + second string +
  135. *
  136. * ret_type: int
  137. * ret_desc: the difference between two differing characters
  138. *
  139. * doc:
  140. *
  141. * Using a character mapping, two strings are compared for "equivalence".
  142. * Each input character is mapped to a comparison character and the
  143. * mapped-to characters are compared for the two NUL terminated input strings.
  144. * This function name is mapped to option_streqvcmp so as to not conflict
  145. * with the POSIX name space.
  146. *
  147. * err: none checked. Caller responsible for seg faults.
  148. =*/
  149. int
  150. streqvcmp( tCC* s1, tCC* s2 )
  151. {
  152. for (;;) {
  153. unsigned char u1 = (unsigned char) *s1++;
  154. unsigned char u2 = (unsigned char) *s2++;
  155. int dif = charmap[ u1 ] - charmap[ u2 ];
  156. if (dif != 0)
  157. return dif;
  158. if (u1 == NUL)
  159. return 0;
  160. }
  161. }
  162. /*=export_func streqvmap
  163. *
  164. * what: Set the character mappings for the streqv functions
  165. *
  166. * arg: + char + From + Input character +
  167. * arg: + char + To + Mapped-to character +
  168. * arg: + int + ct + compare length +
  169. *
  170. * doc:
  171. *
  172. * Set the character mapping. If the count (@code{ct}) is set to zero, then
  173. * the map is cleared by setting all entries in the map to their index
  174. * value. Otherwise, the "@code{From}" character is mapped to the "@code{To}"
  175. * character. If @code{ct} is greater than 1, then @code{From} and @code{To}
  176. * are incremented and the process repeated until @code{ct} entries have been
  177. * set. For example,
  178. * @example
  179. * streqvmap( 'a', 'A', 26 );
  180. * @end example
  181. * @noindent
  182. * will alter the mapping so that all English lower case letters
  183. * will map to upper case.
  184. *
  185. * This function name is mapped to option_streqvmap so as to not conflict
  186. * with the POSIX name space.
  187. *
  188. * err: none.
  189. =*/
  190. void
  191. streqvmap( char From, char To, int ct )
  192. {
  193. if (ct == 0) {
  194. ct = sizeof( charmap ) - 1;
  195. do {
  196. charmap[ ct ] = ct;
  197. } while (--ct >= 0);
  198. }
  199. else {
  200. int chTo = (int)To & 0xFF;
  201. int chFrom = (int)From & 0xFF;
  202. do {
  203. charmap[ chFrom ] = (unsigned)chTo;
  204. chFrom++;
  205. chTo++;
  206. if ((chFrom >= sizeof( charmap )) || (chTo >= sizeof( charmap )))
  207. break;
  208. } while (--ct > 0);
  209. }
  210. }
  211. /*=export_func strequate
  212. *
  213. * what: map a list of characters to the same value
  214. *
  215. * arg: + char const* + ch_list + characters to equivalence +
  216. *
  217. * doc:
  218. *
  219. * Each character in the input string get mapped to the first character
  220. * in the string.
  221. * This function name is mapped to option_strequate so as to not conflict
  222. * with the POSIX name space.
  223. *
  224. * err: none.
  225. =*/
  226. void
  227. strequate( char const* s )
  228. {
  229. if ((s != NULL) && (*s != NUL)) {
  230. unsigned char equiv = (unsigned)*s;
  231. while (*s != NUL)
  232. charmap[ (unsigned)*(s++) ] = equiv;
  233. }
  234. }
  235. /*=export_func strtransform
  236. *
  237. * what: convert a string into its mapped-to value
  238. *
  239. * arg: + char* + dest + output string +
  240. * arg: + char const* + src + input string +
  241. *
  242. * doc:
  243. *
  244. * Each character in the input string is mapped and the mapped-to
  245. * character is put into the output.
  246. * This function name is mapped to option_strtransform so as to not conflict
  247. * with the POSIX name space.
  248. *
  249. * err: none.
  250. =*/
  251. void
  252. strtransform( char* d, char const* s )
  253. {
  254. do {
  255. *(d++) = (char)charmap[ (unsigned)*s ];
  256. } while (*(s++) != NUL);
  257. }
  258. /*
  259. * Local Variables:
  260. * mode: C
  261. * c-file-style: "stroustrup"
  262. * indent-tabs-mode: nil
  263. * End:
  264. * end of autoopts/streqvcmp.c */