home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / ST_ALPH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-10  |  4.9 KB  |  308 lines

  1. /*********************
  2.  *
  3.  *  st_alph.c - string alpha functions.
  4.  *
  5.  *  Purpose: This file contains string functions dealing with
  6.  *           alpha conversion.
  7.  *
  8.  *  Blackstar C Function Library
  9.  *  (c) Copyright 1985,1989 Sterling Castle Software
  10.  *
  11.  *******/
  12.  
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "blackstr.h"
  16. #include "kb_head.h"
  17.  
  18.  
  19. /********
  20.  *
  21.  *   st_squ(str,c) - squeeze c characters out of string
  22.  *
  23.  **/
  24.  
  25. char *st_squ(char *str, char c)
  26. {
  27.     int i,j;
  28.  
  29.     for(i=j=0; str[i]!= '\0'; i++)
  30.     if(str[i] != c)
  31.         str[j++] = str[i];
  32.     str[j] = NUL;
  33.     return(str);
  34. }
  35.  
  36.  
  37. /********
  38.  *
  39.  *   st_ccmpu(s1,s2,n) - compare two strings for n bytes in upper case
  40.  *                       NOTE: s1 and s2 must be longer than n bytes
  41.  **/
  42.  
  43. int st_ccmpu(char *s1, char *s2, int n)
  44. {
  45.     while(*s1&&*s2&&n) {
  46.     if(toupper(*s1)<toupper(*s2))
  47.         return(-1);
  48.     else if(toupper(*s1)>toupper(*s2))
  49.         return(TRUE);
  50.     else
  51.         --n,++s1,++s2;
  52.     }
  53.     return(FALSE);
  54. }
  55.  
  56.  
  57. /********
  58.  *
  59.  *   st_ncmpi(s1,s2,n) - compare strings for n bytes, ignore case
  60.  *
  61.  **/
  62.  
  63. int st_ncmpi(char *s1, char *s2, int n)
  64. {
  65.     while ((*s1!=NUL) && (*s2!=NUL) &&(n)) {
  66.     if(toupper(*s1)>toupper(*s2))
  67.         return(-1);
  68.     else if(toupper(*s1)<toupper(*s2))
  69.         return(TRUE);
  70.     else {
  71.         ++s1;
  72.         ++s2;
  73.         --n;
  74.     }
  75.     }
  76.     return(FALSE);  /* strings are identical for n bytes */
  77. }
  78.  
  79.  
  80. /********
  81.  *
  82.  *   st_ccat (s1,s2,n) - append n bytes of s2 onto s1
  83.  *
  84.  **/
  85.  
  86. void st_ccat(char *s1, char *s2, int n)
  87. {
  88.     while(*s1++);           /* go to end of s1 */
  89.     s1--;                   /* last character */
  90.     while ((*s2!=NUL) && (n)) {
  91.     *s1++ = *s2++;
  92.     --n;
  93.     }
  94.     *s1 = NUL;              /* null terminate the result */
  95. }
  96.  
  97.  
  98. /********
  99.  *
  100.  *   st_bcpy(buff,str) - copy string to buffer
  101.  *
  102.  **/
  103.  
  104. void st_bcpy(char *buff, char *str)
  105. {
  106.     while(*str!=NUL)
  107.     *buff++ = *str++;
  108. }
  109.  
  110.  
  111. /********
  112.  *
  113.  *   st_instr(str2,str1) - see if str1 is in str2
  114.  *
  115.  **/
  116.  
  117. char *st_instr(char *str2, char *str1)
  118. {
  119.     while(*str2) {
  120.     if((st_patma(str1,str2)))
  121.         return(str2);
  122.     else
  123.         str2++;
  124.     }
  125.     return(FALSE);
  126. }
  127.  
  128.  
  129. /********
  130.  *
  131.  *   st_instri(str2,str1) - see if str1 is in str2 ignore case
  132.  *
  133.  **/
  134.  
  135. char *st_instri(char *str2, char *str1)
  136. {
  137.     while(*str2) {
  138.     if((st_patmai(str1, str2)))
  139.         return(str2);
  140.     else
  141.         str2++;
  142.     }
  143.     return(NULL);
  144. }
  145.  
  146.  
  147. /********
  148.  *
  149.  *   st_inbuf(str,buff,n) - see if string is in buffer
  150.  *
  151.  **/
  152.  
  153. char *st_inbuf(char *str, char *buff, int n)
  154. {
  155.     char *ans,*ptr;
  156.  
  157.     ptr = malloc(n+1);
  158.     strcpy(ptr,buff);
  159.     ptr[n] = NUL;               /* null terminate buffer */
  160.     ans=st_instr(ptr,str);
  161.     free(ptr);
  162.     return(ans);
  163. }
  164.  
  165.  
  166. /********
  167.  *
  168.  *   st_patmai(str1,str2) - pattern match anchored - ignore case
  169.  *
  170.  **/
  171.  
  172. int st_patmai(char *str1, char *str2)
  173. {
  174.     while(*str1 && *str2) {
  175.     if(*str1 != '?')
  176.         if(toupper(*str1) != toupper(*str2))
  177.         return(FALSE);
  178.     ++str1,++str2;
  179.     }
  180.     return(TRUE);
  181. }
  182.  
  183.  
  184. /********
  185.  *
  186.  *   st_patma(str1,str2) - do anchored pattern match of str1 in str2
  187.  *
  188.  **/
  189.  
  190. int st_patma(char *str1, char *str2)
  191. {
  192.     while(*str1&&*str2) {
  193.     if(*str1 != '?')
  194.         if(*str1 != *str2)
  195.         return(FALSE);
  196.     ++str1,++str2;
  197.     }
  198.     return(TRUE);
  199. }
  200.  
  201.  
  202. /********
  203.  *
  204.  *   st_bllz(str) - blank leading zeroes
  205.  *
  206.  **/
  207.  
  208. char *st_bllz(char *str)
  209. {
  210.     char *ptr;
  211.  
  212.     ptr = str;
  213.     while(!isdigit(*str++)) ;   /* first find a digit */
  214.     while(*str=='0') {
  215.     *str++ = ' ';           /* convert to blank */
  216.     }
  217.     return(ptr);
  218. }
  219.  
  220.  
  221. /********
  222.  *
  223.  *   st_zlbl(str) - zero leading blanks
  224.  *
  225.  **/
  226.  
  227. char *st_zlbl(char *str)
  228. {
  229.     char *ptr;
  230.  
  231.     ptr = str;
  232.     while(*str==BLANK)
  233.     *str++='0';
  234.     return(ptr);
  235. }
  236.     
  237.  
  238. /********
  239.  *
  240.  *   st_delc(str,c) - find delimiter c in string
  241.  *
  242.  **/
  243.  
  244. char *st_delc(char *str, char c)
  245. {
  246.     while((*str!=NUL) && (*str!=c)) ++str;
  247.     if(*str)
  248.     return(str);            /* return pointer to delimeter */
  249.     else
  250.     return(NULL);           /* or NUL if not found */
  251. }    
  252.  
  253.  
  254. /********
  255.  *
  256.  *   st_wild(str) - see if string has wildcard chars
  257.  *
  258.  **/
  259.  
  260. int st_wild(char *str)
  261. {
  262.     while(*str)
  263.     if((*str=='?')||(*str++=='*'))
  264.         return(TRUE);
  265.     return(FALSE);
  266. }
  267.  
  268.  
  269. /********
  270.  *
  271.  *   st_tabr(str) - expand tabs in string
  272.  *
  273.  **/
  274.  
  275. void st_tabr(char *str)
  276. {
  277.     char *ostr,*temp,buff[256];
  278.     int i;
  279.  
  280.     ostr=str;
  281.     temp = buff;
  282.     while(*str!=NUL) {
  283.     if((*temp=*str++)=='\t')
  284.         for(i=0; i<8; ++i)
  285.         *temp++=' ';
  286.     else ++temp;
  287.     }
  288.     *temp=NUL;
  289.     strcpy(ostr,buff);          /* copy back to string */
  290. }
  291.  
  292.  
  293. /********
  294.  *
  295.  *   st_ptrs(buff,ptrs,cnt) - get pointers to strings in buffer
  296.  *
  297.  **/
  298.  
  299. void st_ptrs(char *buff, char *ptrs[], int cnt)
  300. {
  301.     int i;
  302.  
  303.     for(i=0; i<cnt; ++i) {
  304.     ptrs[i] = buff;
  305.     while(*buff++);         /* find next null*/
  306.     }
  307. }
  308.