home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / memacs400_src.lzh / MEMACS400 / SRC / char.c < prev    next >
Text File  |  1996-04-25  |  9KB  |  327 lines

  1. /*    CHAR.C:    Character handling functions for
  2.         MicroEMACS 4.00
  3.         (C)Copyright 1995 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. /*    is_letter()
  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 is_letter(ch)
  22.  
  23. register char ch;
  24.  
  25. {
  26.     return(is_upper(ch) || is_lower(ch));
  27. }
  28.  
  29. /*    is_lower()
  30.         Is the character a lower case letter?  This looks
  31.     in the lower to uppercase translation table.
  32. */
  33.  
  34. int PASCAL NEAR is_lower(ch)
  35.  
  36. register char ch;
  37.  
  38. {
  39.     return(lowcase[ch & 255] != 0);
  40. }
  41.  
  42. /*    is_upper()
  43.         Is the character a upper case letter?  This looks
  44.     in the upper to lowercase translation table.
  45. */
  46.  
  47. int PASCAL NEAR is_upper(ch)
  48.  
  49. register char ch;
  50.  
  51. {
  52.     return(upcase[ch & 255] != 0);
  53. }
  54.  
  55. /*    chcase()
  56.  
  57.         Change the case of the current character.
  58.     First check lower and then upper.  If it is not a letter,
  59.     it gets returned unchanged.
  60. */
  61.  
  62. unsigned int PASCAL NEAR chcase(ch)
  63. register unsigned int    ch;
  64. {
  65.     /* translate lowercase */
  66.     if (is_lower(ch))
  67.         return(lowcase[ch & 255]);
  68.  
  69.     /* translate uppercase */
  70.     if (is_upper(ch))
  71.         return(upcase[ch & 255]);
  72.  
  73.     /* let the rest pass */
  74.     return(ch);
  75. }
  76.  
  77. /* change *cp to an upper case character */
  78.  
  79. VOID PASCAL NEAR uppercase(cp)
  80.  
  81. unsigned char *cp;    /* ptr to character to uppercase */
  82.  
  83. {
  84.     /* translate uppercase */
  85.     if (is_lower(*cp))
  86.         *cp = lowcase[*cp & 255];
  87. }
  88.  
  89. /* change *cp to an lower case character */
  90.  
  91. VOID PASCAL NEAR lowercase(cp)
  92.  
  93. unsigned char *cp;    /* ptr to character to lowercase */
  94.  
  95. {
  96.     /* translate lowercase */
  97.     if (is_upper(*cp))
  98.         *cp = upcase[*cp & 255];
  99. }
  100.  
  101. #if    PROTO
  102. int PASCAL NEAR upperc(char ch) /* return the upper case equivalant of a character */
  103. #else
  104. int PASCAL NEAR upperc(ch)    /* return the upper case equivalant of a character */
  105.  
  106. unsigned char ch;    /* character to get uppercase euivalant of */
  107. #endif
  108. {
  109.     if (is_lower(ch))
  110.         return(lowcase[ch & 255]);
  111.     else
  112.         return(ch);
  113. }
  114.  
  115. #if    PROTO
  116. int PASCAL NEAR lowerc(char ch) /* return the lower case equivalant of a character */
  117. #else
  118. int PASCAL NEAR lowerc(ch)    /* return the lower case equivalant of a character */
  119.  
  120. unsigned char ch;    /* character to get lowercase equivalant of */
  121. #endif
  122. {
  123.     if (is_upper(ch))
  124.         return(upcase[ch & 255]);
  125.     else
  126.         return(ch);
  127. }
  128.  
  129. VOID PASCAL NEAR initchars()    /* initialize the character upper/lower case tables */
  130.  
  131. {
  132.     register int index;    /* index into tables */
  133.  
  134.     /* all of both tables to zero */
  135.     for (index = 0; index < HICHAR; index++) {
  136.         lowcase[index] = 0;
  137.         upcase[index] = 0;
  138.     }
  139.  
  140.     /* lower to upper, upper to lower */
  141.     for (index = 'a'; index <= 'z'; index++) {
  142.         lowcase[index] = index ^ DIFCASE;
  143.         upcase[index ^ DIFCASE] = index;
  144.     }
  145. #if BSD || FREEBSD || USG || AIX || AUX || SMOS || HPUX8 || HPUX9 || SUN || XENIX || AVIION
  146.     /* and for those international characters! */
  147.     for (index = (unsigned char)'\340';
  148.          index <= (unsigned char)'\375'; index++) {
  149.         lowcase[index] = index ^ DIFCASE;
  150.         upcase[index ^ DIFCASE] = index;
  151.     }
  152. #endif /*  BSD || FREEBSD || USG || AIX || AUX || SMOS || HPUX8 || HPUX9 || SUN || XENIX || AVIION */
  153.  
  154. #if    MSDOS
  155.     /* setup various extended IBM-PC characters */
  156.     upcase[0x80]  = 0x87;    /* C with a cedilla */
  157.     lowcase[0x81] = 0x9a;    /* U with an umlaut */
  158.     lowcase[0x82] = 0x90;    /* E with an acute accent */
  159.     lowcase[0x83] = 0x83;    /* A with a circumflex */
  160.     lowcase[0x84] = 0x8e;    /* A with an umlaut */
  161.     lowcase[0x85] = 0x85;    /* A with a grave accent */
  162.     lowcase[0x86] = 0x8f;    /* A with a circle */
  163.     lowcase[0x87] = 0x80;    /* C with a cedilla */
  164.     lowcase[0x88] = 0x88;    /* E with a circumflex */
  165.     lowcase[0x89] = 0x89;    /* E with an umlaut */
  166.     lowcase[0x8a] = 0x8a;    /* E with a grave accent */
  167.     lowcase[0x8b] = 0x8b;    /* I with an umlaut */
  168.     lowcase[0x8c] = 0x8c;    /* I with a circumflex */
  169.     lowcase[0x8d] = 0x8d;    /* I with a grave accent */
  170.     upcase[0x8e]  = 0x84;    /* A with an umlaut */
  171.     upcase[0x8f]  = 0x86;    /* A with a circle */
  172.     upcase[0x90]  = 0x82;    /* E with an acute accent */
  173.     lowcase[0x91] = 0x92;    /* AE diphthong */
  174.     upcase[0x92]  = 0x91;    /* AE diphthong */
  175.     lowcase[0x93] = 0x93;    /* O with a circumflex */
  176.     lowcase[0x94] = 0x99;    /* O with an umlaut */
  177.     lowcase[0x95] = 0x95;    /* O with an acute accent */
  178.     lowcase[0x96] = 0x96;    /* u with a circumflex */
  179.     lowcase[0x97] = 0x97;    /* U with an grave accent */
  180.     lowcase[0x98] = 0x98;    /* y with an umlaut */
  181.     upcase[0x99]  = 0x94;    /* O with an umlaut */
  182.     upcase[0x9a]  = 0x81;    /* U with an umlaut */
  183.     lowcase[0xa0] = 0xa0;    /* a with an acute accent */
  184.     lowcase[0xa1] = 0xa1;    /* i with an acute accent */
  185.     lowcase[0xa2] = 0xa2;    /* o with an acute accent */
  186.     lowcase[0xa3] = 0xa3;    /* u with an acute accent */
  187.     lowcase[0xa4] = 0xa5;    /* n with a tilde */
  188.     upcase[0xa5]  = 0xa4;    /* N with a tilde */
  189. #endif
  190. #if    VMS
  191.     /* setup DEC Multinational Character Set */
  192.     upcase[ 192]  = 224;    /* A with a grave accent */
  193.     upcase[ 193]  = 225;    /* A with an acute accent */
  194.     upcase[ 194]  = 226;    /* A with a circumflex */
  195.     upcase[ 195]  = 227;    /* A with a tilde */
  196.     upcase[ 196]  = 228;    /* A with an umlaut */
  197.     upcase[ 197]  = 229;    /* A with a ring */
  198.     upcase[ 198]  = 230;    /* AE diphthong */
  199.     upcase[ 199]  = 231;    /* C with a cedilla */
  200.     upcase[ 200]  = 232;    /* E with a grave accent */
  201.     upcase[ 201]  = 233;    /* E with an acute accent */
  202.     upcase[ 202]  = 234;    /* E with circumflex */
  203.     upcase[ 203]  = 235;    /* E with an umlaut */
  204.     upcase[ 204]  = 236;    /* I with a grave accent */
  205.     upcase[ 205]  = 237;    /* I with an acute accent */
  206.     upcase[ 206]  = 238;    /* I with circumflex */
  207.     upcase[ 207]  = 239;    /* I with an umlaut */
  208.     upcase[ 209]  = 241;    /* N with a tilde */
  209.     upcase[ 210]  = 242;    /* O with a grave accent */
  210.     upcase[ 211]  = 243;    /* O with an acute accent */
  211.     upcase[ 212]  = 244;    /* O with circumflex */
  212.     upcase[ 213]  = 245;    /* O with a tilde */
  213.     upcase[ 214]  = 246;    /* O with an umlaut */
  214.     upcase[ 215]  = 247;    /* OE ligature */
  215.     upcase[ 216]  = 248;    /* O with a slash */
  216.     upcase[ 217]  = 249;    /* U with a grave accent */
  217.     upcase[ 218]  = 250;    /* U with an acute accent */
  218.     upcase[ 219]  = 251;    /* U with circumflex */
  219.     upcase[ 220]  = 252;    /* U with an umlaut */
  220.     upcase[ 221]  = 253;    /* Y with an umlaut */
  221.  
  222.     lowcase[ 223]  = 223;    /* German lowercase sharp s */
  223.  
  224.     lowcase[ 224]  = 192;    /* a with a grave accent */
  225.     lowcase[ 225]  = 193;    /* a with an acute accent */
  226.     lowcase[ 226]  = 194;    /* a with a circumflex */
  227.     lowcase[ 227]  = 195;    /* a with a tilde */
  228.     lowcase[ 228]  = 196;    /* a with an umlaut */
  229.     lowcase[ 229]  = 197;    /* a with a ring */
  230.     lowcase[ 230]  = 198;    /* ae diphthong */
  231.     lowcase[ 231]  = 199;    /* c with a cedilla */
  232.     lowcase[ 232]  = 200;    /* e with a grave accent */
  233.     lowcase[ 233]  = 201;    /* e with an acute accent */
  234.     lowcase[ 234]  = 202;    /* e with circumflex */
  235.     lowcase[ 235]  = 203;    /* e with an umlaut */
  236.     lowcase[ 236]  = 204;    /* i with a grave accent */
  237.     lowcase[ 237]  = 205;    /* i with an acute accent */
  238.     lowcase[ 238]  = 206;    /* i with circumflex */
  239.     lowcase[ 239]  = 207;    /* i with an umlaut */
  240.     lowcase[ 241]  = 209;    /* n with a tilde */
  241.     lowcase[ 242]  = 210;    /* o with a grave accent */
  242.     lowcase[ 243]  = 211;    /* o with an acute accent */
  243.     lowcase[ 244]  = 212;    /* o with circumflex */
  244.     lowcase[ 245]  = 213;    /* o with a tilde */
  245.     lowcase[ 246]  = 214;    /* o with an umlaut */
  246.     lowcase[ 247]  = 215;    /* oe ligature */
  247.     lowcase[ 248]  = 216;    /* o with a slash */
  248.     lowcase[ 249]  = 217;    /* u with a grave accent */
  249.     lowcase[ 250]  = 218;    /* u with an acute accent */
  250.     lowcase[ 251]  = 219;    /* u with circumflex */
  251.     lowcase[ 252]  = 220;    /* u with an umlaut */
  252.     lowcase[ 253]  = 221;    /* y with an umlaut */
  253.  
  254. #endif
  255. }
  256.  
  257. /*    Set a character in the lowercase map */
  258.  
  259. int PASCAL NEAR setlower(ch, val)
  260.  
  261. char *ch;    /* ptr to character to set */
  262. char *val;    /* value to set it to */
  263.  
  264. {
  265.     return(lowcase[*ch & 255] = *val & 255);
  266. }
  267.  
  268. /*    Set a character in the uppercase map */
  269.  
  270. int PASCAL NEAR setupper(ch, val)
  271.  
  272. char *ch;    /* ptr to character to set */
  273. char *val;    /* value to set it to */
  274.  
  275. {
  276.     return(upcase[*ch & 255] = *val & 255);
  277. }
  278.  
  279. #if (ZTC | TURBO | MSC) == 0
  280. /*
  281.  * strrev -- Reverse string in place.  Code here for those compilers
  282.  *    that do not have the function in their own library.
  283.  */
  284. char *strrev(our_str)
  285. char *our_str;
  286. {
  287.     register char    *beg_str, *end_str;
  288.     register char    the_char;
  289.  
  290.     end_str = beg_str = our_str;
  291.     end_str += strlen(beg_str);
  292.  
  293.     do {
  294.         the_char = *--end_str;
  295.         *end_str = *beg_str;
  296.         *beg_str++ = the_char;
  297.     } while (end_str > beg_str);
  298.  
  299.     return(our_str);
  300. }
  301. #endif
  302.  
  303. #if    DBCS
  304. /* is this character a 2 byte character prefix code? */
  305.  
  306. int PASCAL NEAR is2byte(sp, cp)
  307.  
  308. char *sp;    /* ptr to beginning of string containing character to test */
  309. char *cp;    /* ptr to charactor to test */
  310.  
  311. {
  312.     register char *cc;    /* pointer to current character */
  313.  
  314.     cc = sp;
  315.     while (*cc) {
  316.         if (cc > cp)
  317.             return(FALSE);
  318.         if (cc == cp)
  319.             return(is2char(*cp));
  320.         if (is2char(*cc))
  321.             ++cc;
  322.         ++cc;
  323.     }
  324.     return(FALSE);
  325. }
  326. #endif
  327.