home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / func / advc / string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-29  |  4.9 KB  |  241 lines

  1. #include <ctype.h>
  2.  
  3. int any2dec(str,radix)                /* convert string to integer */
  4.    char *str;
  5.    unsigned int radix;
  6. {
  7.    unsigned int digit, number = 0;
  8.    while (*str) {
  9.       if (isdigit(*str)) digit = *str - '0';
  10.       else if (islower(digit)) digit = *str - 'a' + 10;
  11.       else digit = *str - 'A' + 10;
  12.       number = number * radix + digit;
  13.       str++; }
  14.    return(number);
  15. }
  16.  
  17.  
  18.  
  19. unsigned char *bsqueeze(line)         /* squeeze blanks from a string */
  20.    unsigned char *line;
  21. {
  22.    static unsigned char sqbuf[126];
  23.    unsigned char *sqptr = sqbuf;
  24.    int repcnt = 0;
  25.  
  26.    while (*line) {
  27.       while(*line == ' ') {
  28.          line++;
  29.          repcnt++; }
  30.       if (repcnt == 2)
  31.          *sqptr++ = ' ' | 128;
  32.       else if (repcnt > 2) {
  33.          *sqptr++ = 128;
  34.          *sqptr++ = repcnt | 128; }
  35.       else if (repcnt) {
  36.          if (sqptr == &sqbuf[0])
  37.             *sqptr++ = ' ';
  38.          else *(sqptr - 1) += 128; }
  39.       repcnt = 0;
  40.       *sqptr++ = *line++; }
  41.    *sqptr = '\0';
  42.    return(&sqbuf[0]);
  43. }
  44.  
  45.  
  46.  
  47. unsigned char *bunsqueeze(line)       /* unsqueeze a blank-squeezed string */
  48.    unsigned char *line;
  49. {
  50.    static unsigned char usqbuf[126];
  51.    unsigned char *usqptr = usqbuf;
  52.    int repcnt = 0;
  53.  
  54.    while (*line) {
  55.       if (*line < 128)
  56.          *usqptr++ = *line;
  57.       else if (*line == 128) {
  58.          repcnt = *++line - 128;
  59.          while (repcnt--)
  60.              *usqptr++ = ' '; }
  61.       else {
  62.          *usqptr++ = *line - 128;
  63.          *usqptr++ = ' '; }
  64.       line++; }
  65.    *usqptr = '\0';
  66.    return(&usqbuf[0]);
  67. }
  68.  
  69.  
  70.  
  71. char *dec2any(number,radix)           /* convert integer to string */
  72.    unsigned int number, radix;
  73. {
  74.    static char buffer[64];      /* allow for up to 64-bit integers */
  75.    char *bufptr = buffer + 64;  /* point to end of buffer */
  76.    int digit;
  77.    *bufptr-- = '\0';
  78.    do {
  79.       digit = number % radix;
  80.       number /= radix;
  81.       *bufptr-- = (digit < 10) ? (digit + '0') : (digit + 'A' - 10); }
  82.    while (number);
  83.    return(++bufptr);
  84. }
  85.  
  86.  
  87.  
  88. char *extract(str,delim,element)    /* extract element from delimited string */
  89.    char *str, delim;
  90.    int element;
  91. {
  92.    static char elembuf[80];
  93.    char *elem = elembuf, *temp;
  94.  
  95.    temp = str;
  96.    while (*str && element)
  97.       if (*str++ == delim)
  98.          if (--element) temp = str;
  99.    if ((element == 1) && (*str == '\0') || (element == 0))
  100.       while (*temp && (*temp != delim))
  101.          *elem++ = *temp++;
  102.    *elem = '\0';
  103.    return(&elembuf[0]);
  104. }
  105.  
  106.  
  107.  
  108. char *locase(str)                     /* convert string to lowercase */
  109.    char *str;
  110. {
  111.    char *start;
  112.    start = str;
  113.    while (*str) {
  114.       if (isupper(*str))
  115.          *str = tolower(*str);
  116.       str++; }
  117.    return(start);
  118. }
  119.  
  120.  
  121.  
  122. char *multiand(str,c)                 /* AND string with a given value */
  123.    char *str, c;
  124. {
  125.    char *start;
  126.  
  127.    start = str;
  128.    while (*str)
  129.       *str++ &= c;
  130.    return(start);
  131. }
  132.  
  133.  
  134.  
  135. char *multior(str,c)                  /* OR string with a given value */
  136.    char *str, c;
  137. {
  138.    char *start;
  139.  
  140.    start = str;
  141.    while (*str)
  142.       *str++ |= c;
  143.    return(start);
  144. }
  145.  
  146.  
  147.  
  148. char *multixor(str,c)                 /* XOR string with a given value */
  149.    char *str, c;
  150. {
  151.    char *start;
  152.  
  153.    start = str;
  154.    while (*str)
  155.       *str++ ^= c;
  156.    return(start);
  157. }
  158.  
  159.  
  160.  
  161. char *reverse(str)                    /* reverse order of chrs in a string */
  162.    char *str;
  163. {
  164.    char *start, *end, tmp;
  165.    int length;
  166.  
  167.    end = str + (length = strlen(start = str)) -1;
  168.    length >>= 1;
  169.  
  170.    while (length--) {
  171.       tmp = *str;
  172.       *str++ = *end;
  173.       *end-- = tmp; }
  174.    return(start);
  175. }
  176.  
  177.  
  178.  
  179. char *soundex(str)                    /* get soundex code for a string */
  180.    char *str;
  181. {
  182.    static char soundexbuf[81];
  183.    char *table = "01230120022455012623010202";
  184.    char *sdx = soundexbuf, lastchr = ' ';
  185.  
  186.    while (*str) {
  187.       if (isalpha(*str) && (*str != lastchr)) {
  188.          *sdx = *(table + toupper(*str) - 'A');
  189.          if ((*sdx != '0') && (*sdx != lastchr))
  190.             lastchr = *sdx++; }
  191.       str++; }
  192.    *sdx = '\0';
  193.    return(&soundexbuf[0]);
  194. }
  195.  
  196.  
  197.  
  198. char *strip(str,c)                    /* strip a given chr from a string */
  199.    char *str, c;
  200. {
  201.    char *start, *newstr;
  202.  
  203.    start = newstr = str;
  204.    while (*str) {
  205.       if (*str != c)
  206.          *newstr++ = *str;
  207.       str++; }
  208.    *newstr = '\0';
  209.    return(start);
  210. }
  211.  
  212.  
  213.  
  214. char *striprange(str,clo,chi)         /* strip a range of chrs from a string */
  215.    char *str, clo, chi;
  216. {
  217.    char *start, *newstr;
  218.  
  219.    start = newstr = str;
  220.    while (*str) {
  221.       if ((*str > chi) | (*str < clo))
  222.          *newstr++ = *str;
  223.       str++; }
  224.    *newstr = '\0';
  225.    return(start);
  226. }
  227.  
  228.  
  229.  
  230. char *upcase(str)                     /* convert a string to uppercase */
  231.    char *str;
  232. {
  233.    char *start;
  234.    start = str;
  235.    while (*str) {
  236.       if (islower(*str))
  237.          *str = toupper(*str);
  238.       str++; }
  239.    return(start);
  240. }
  241.