home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / CHAR.C < prev    next >
C/C++ Source or Header  |  1991-02-17  |  8KB  |  291 lines

  1. /*    CHAR.C:    Character handling functions for
  2.         MicroEMACS 3.10
  3.         (C)Copyright 1988 by Daniel Lawrence
  4.  
  5.         ALL THE CODE HERE IS FOR VARIOUS FORMS OF ASCII AND
  6.         WILL HAVE TO BE MODIFIED FOR EBCDIC
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. /*    isletter()
  16.         Is the character a letter?  We presume a letter must
  17.     be either in the upper or lower case tables (even if it gets
  18.     translated to itself).
  19. */
  20.  
  21. int PASCAL NEAR isletter(ch)
  22.  
  23. register unsigned int ch;
  24.  
  25. {
  26.     return(isupper(ch) || islower(ch));
  27. }
  28.  
  29. /*    islower()
  30.         Is the character a lower case letter?  This looks
  31.     in the lower to uppercase translation table.
  32. */
  33.  
  34. int PASCAL NEAR islower(ch)
  35. register unsigned int    ch;
  36. {
  37.     return(lowcase[ch] != 0);
  38. }
  39.  
  40. /*    isupper()
  41.         Is the character a upper case letter?  This looks
  42.     in the upper to lowercase translation table.
  43. */
  44.  
  45. int PASCAL NEAR isupper(ch)
  46. register unsigned int    ch;
  47. {
  48.     return(upcase[ch] != 0);
  49. }
  50.  
  51. /*    chcase()
  52.  
  53.         Change the case of the current character.
  54.     First check lower and then upper.  If it is not a letter,
  55.     it gets returned unchanged.
  56. */
  57.  
  58. unsigned int PASCAL NEAR chcase(ch)
  59. register unsigned int    ch;
  60. {
  61.     /* translate lowercase */
  62.     if (islower(ch))
  63.         return(lowcase[ch]);
  64.  
  65.     /* translate uppercase */
  66.     if (isupper(ch))
  67.         return(upcase[ch]);
  68.  
  69.     /* let the rest pass */
  70.     return(ch);
  71. }
  72.  
  73. /* change *cp to an upper case character */
  74.  
  75. uppercase(cp)
  76.  
  77. char *cp;    /* ptr to character to uppercase */
  78.  
  79. {
  80.     /* translate lowercase */
  81.     if (islower(*cp))
  82.         *cp = lowcase[*cp];
  83. }
  84.  
  85. /* change *cp to an lower case character */
  86.  
  87. lowercase(cp)
  88.  
  89. char *cp;    /* ptr to character to lowercase */
  90.  
  91. {
  92.     /* translate lowercase */
  93.     if (isupper(*cp))
  94.         *cp = upcase[*cp];
  95. }
  96.  
  97. #if    PROTO
  98. char PASCAL NEAR upperc(char ch) /* return the upper case equivalant of a character */
  99. #else
  100. char PASCAL NEAR upperc(ch)    /* return the upper case equivalant of a character */
  101.  
  102. char ch;    /* character to get uppercase euivalant of */
  103. #endif
  104. {
  105.     if (islower(ch))
  106.         return(lowcase[ch]);
  107.     else
  108.         return(ch);
  109. }
  110.  
  111. #if    PROTO
  112. char PASCAL NEAR lowerc(char ch) /* return the lower case equivalant of a character */
  113. #else
  114. char PASCAL NEAR lowerc(ch)    /* return the lower case equivalant of a character */
  115.  
  116. char ch;    /* character to get lowercase equivalant of */
  117. #endif
  118. {
  119.     if (isupper(ch))
  120.         return(upcase[ch]);
  121.     else
  122.         return(ch);
  123. }
  124.  
  125. PASCAL NEAR initchars()    /* initialize the character upper/lower case tables */
  126.  
  127. {
  128.     register int index;    /* index into tables */
  129.  
  130.     /* all of both tables to zero */
  131.     for (index = 0; index < HICHAR; index++) {
  132.         lowcase[index] = 0;
  133.         upcase[index] = 0;
  134.     }
  135.  
  136.     /* lower to upper, upper to lower */
  137.     for (index = 'a'; index <= 'z'; index++) {
  138.         lowcase[index] = index ^ DIFCASE;
  139.         upcase[index ^ DIFCASE] = index;
  140.     }
  141.  
  142. #if    MSDOS
  143.     /* setup various extended IBM-PC characters */
  144.     upcase[0x80]  = 0x87;    /* C with a cedilla */
  145.     lowcase[0x81] = 0x9a;    /* U with an umlaut */
  146.     lowcase[0x82] = 0x90;    /* E with an acute accent */
  147.     lowcase[0x83] = 0x83;    /* A with a circumflex */
  148.     lowcase[0x84] = 0x8e;    /* A with an umlaut */
  149.     lowcase[0x85] = 0x85;    /* A with a grave accent */
  150.     lowcase[0x86] = 0x8f;    /* A with a circle */
  151.     lowcase[0x87] = 0x80;    /* C with a cedilla */
  152.     lowcase[0x88] = 0x88;    /* E with a circumflex */
  153.     lowcase[0x89] = 0x89;    /* E with an umlaut */
  154.     lowcase[0x8a] = 0x8a;    /* E with a grave accent */
  155.     lowcase[0x8b] = 0x8b;    /* I with an umlaut */
  156.     lowcase[0x8c] = 0x8c;    /* I with a circumflex */
  157.     lowcase[0x8d] = 0x8d;    /* I with a grave accent */
  158.     upcase[0x8e]  = 0x84;    /* A with an umlaut */
  159.     upcase[0x8f]  = 0x86;    /* A with a circle */
  160.     upcase[0x90]  = 0x82;    /* E with an acute accent */
  161.     lowcase[0x91] = 0x92;    /* AE diphthong */
  162.     upcase[0x92]  = 0x91;    /* AE diphthong */
  163.     lowcase[0x93] = 0x93;    /* O with a circumflex */
  164.     lowcase[0x94] = 0x99;    /* O with an umlaut */
  165.     lowcase[0x95] = 0x95;    /* O with an acute accent */
  166.     lowcase[0x96] = 0x96;    /* u with a circumflex */
  167.     lowcase[0x97] = 0x97;    /* U with an grave accent */
  168.     lowcase[0x98] = 0x98;    /* y with an umlaut */
  169.     upcase[0x99]  = 0x94;    /* O with an umlaut */
  170.     upcase[0x9a]  = 0x81;    /* U with an umlaut */
  171.     lowcase[0xa0] = 0xa0;    /* a with an acute accent */
  172.     lowcase[0xa1] = 0xa1;    /* i with an acute accent */
  173.     lowcase[0xa2] = 0xa2;    /* o with an acute accent */
  174.     lowcase[0xa3] = 0xa3;    /* u with an acute accent */
  175.     lowcase[0xa4] = 0xa5;    /* n with a tilde */
  176.     upcase[0xa5]  = 0xa4;    /* N with a tilde */
  177. #endif
  178. #if    VMS
  179.     /* setup DEC Multinational Character Set */
  180.     upcase[ 192]  = 224;    /* A with a grave accent */
  181.     upcase[ 193]  = 225;    /* A with an acute accent */
  182.     upcase[ 194]  = 226;    /* A with a circumflex */
  183.     upcase[ 195]  = 227;    /* A with a tilde */
  184.     upcase[ 196]  = 228;    /* A with an umlaut */
  185.     upcase[ 197]  = 229;    /* A with a ring */
  186.     upcase[ 198]  = 230;    /* AE diphthong */
  187.     upcase[ 199]  = 231;    /* C with a cedilla */
  188.     upcase[ 200]  = 232;    /* E with a grave accent */
  189.     upcase[ 201]  = 233;    /* E with an acute accent */
  190.     upcase[ 202]  = 234;    /* E with circumflex */
  191.     upcase[ 203]  = 235;    /* E with an umlaut */
  192.     upcase[ 204]  = 236;    /* I with a grave accent */
  193.     upcase[ 205]  = 237;    /* I with an acute accent */
  194.     upcase[ 206]  = 238;    /* I with circumflex */
  195.     upcase[ 207]  = 239;    /* I with an umlaut */
  196.     upcase[ 209]  = 241;    /* N with a tilde */
  197.     upcase[ 210]  = 242;    /* O with a grave accent */
  198.     upcase[ 211]  = 243;    /* O with an acute accent */
  199.     upcase[ 212]  = 244;    /* O with circumflex */
  200.     upcase[ 213]  = 245;    /* O with a tilde */
  201.     upcase[ 214]  = 246;    /* O with an umlaut */
  202.     upcase[ 215]  = 247;    /* OE ligature */
  203.     upcase[ 216]  = 248;    /* O with a slash */
  204.     upcase[ 217]  = 249;    /* U with a grave accent */
  205.     upcase[ 218]  = 250;    /* U with an acute accent */
  206.     upcase[ 219]  = 251;    /* U with circumflex */
  207.     upcase[ 220]  = 252;    /* U with an umlaut */
  208.     upcase[ 221]  = 253;    /* Y with an umlaut */
  209.  
  210.     lowcase[ 223]  = 223;    /* German lowercase sharp s */
  211.  
  212.     lowcase[ 224]  = 192;    /* a with a grave accent */
  213.     lowcase[ 225]  = 193;    /* a with an acute accent */
  214.     lowcase[ 226]  = 194;    /* a with a circumflex */
  215.     lowcase[ 227]  = 195;    /* a with a tilde */
  216.     lowcase[ 228]  = 196;    /* a with an umlaut */
  217.     lowcase[ 229]  = 197;    /* a with a ring */
  218.     lowcase[ 230]  = 198;    /* ae diphthong */
  219.     lowcase[ 231]  = 199;    /* c with a cedilla */
  220.     lowcase[ 232]  = 200;    /* e with a grave accent */
  221.     lowcase[ 233]  = 201;    /* e with an acute accent */
  222.     lowcase[ 234]  = 202;    /* e with circumflex */
  223.     lowcase[ 235]  = 203;    /* e with an umlaut */
  224.     lowcase[ 236]  = 204;    /* i with a grave accent */
  225.     lowcase[ 237]  = 205;    /* i with an acute accent */
  226.     lowcase[ 238]  = 206;    /* i with circumflex */
  227.     lowcase[ 239]  = 207;    /* i with an umlaut */
  228.     lowcase[ 241]  = 209;    /* n with a tilde */
  229.     lowcase[ 242]  = 210;    /* o with a grave accent */
  230.     lowcase[ 243]  = 211;    /* o with an acute accent */
  231.     lowcase[ 244]  = 212;    /* o with circumflex */
  232.     lowcase[ 245]  = 213;    /* o with a tilde */
  233.     lowcase[ 246]  = 214;    /* o with an umlaut */
  234.     lowcase[ 247]  = 215;    /* oe ligature */
  235.     lowcase[ 248]  = 216;    /* o with a slash */
  236.     lowcase[ 249]  = 217;    /* u with a grave accent */
  237.     lowcase[ 250]  = 218;    /* u with an acute accent */
  238.     lowcase[ 251]  = 219;    /* u with circumflex */
  239.     lowcase[ 252]  = 220;    /* u with an umlaut */
  240.     lowcase[ 253]  = 221;    /* y with an umlaut */
  241.  
  242. #endif
  243. }
  244.  
  245. /*    Set a character in the lowercase map */
  246.  
  247. int PASCAL NEAR setlower(ch, val)
  248.  
  249. char *ch;    /* ptr to character to set */
  250. char *val;    /* value to set it to */
  251.  
  252. {
  253.     return(lowcase[*ch & 255] = *val & 255);
  254. }
  255.  
  256. /*    Set a character in the uppercase map */
  257.  
  258. int PASCAL NEAR setupper(ch, val)
  259.  
  260. char *ch;    /* ptr to character to set */
  261. char *val;    /* value to set it to */
  262.  
  263. {
  264.     return(upcase[*ch & 255] = *val & 255);
  265. }
  266.  
  267. #if    DBCS
  268. /* is this character a 2 byte character prefix code? */
  269.  
  270. int PASCAL NEAR is2byte(sp, cp)
  271.  
  272. char *sp;    /* ptr to beginning of string containing character to test */
  273. char *cp;    /* ptr to charector to test */
  274.  
  275. {
  276.     register char *cc;    /* pointer to current character */
  277.  
  278.     cc = sp;
  279.     while (*cc) {
  280.         if (cc > cp)
  281.             return(FALSE);
  282.         if (cc == cp)
  283.             return(is2char(*cp));
  284.         if (is2char(*cc))
  285.             ++cc;
  286.         ++cc;
  287.     }
  288.     return(FALSE);
  289. }
  290. #endif
  291.