home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / Atmel / Asm / src / string.c < prev    next >
C/C++ Source or Header  |  2000-08-22  |  6KB  |  283 lines

  1. /**************************************************************************/
  2. /*                                                                        */
  3. /* STRING.C                                                               */
  4. /*                                                                        */
  5. /* Various string manipulating functions.                                 */
  6. /* LJS 2000                                                               */
  7. /**************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "str_p.h"
  13.  
  14. #ifndef max
  15.  #define max(a,b) ((a) > (b) ? (a):(b))
  16. #endif
  17.  
  18. /****************************************************************/
  19. /*                                                              */
  20. /* Function:@CountChar                                          */
  21. /* Args    :Pointer to string, char                             */
  22. /* Returns :Result                                              */
  23. /*                                                              */
  24. /* Description: Counts occurances of a char in a string.        */
  25. /*                                                              */
  26. /****************************************************************/
  27. int CountChar(char *S, char C)
  28. {
  29.     int Result=0;
  30.     while(*S)
  31.     {
  32.         if(*S==C)
  33.         {
  34.             Result++;
  35.         }
  36.         S++;
  37.     }
  38.     return Result;
  39. }
  40.  
  41. /****************************************************************/
  42. /*                                                              */
  43. /* Function:@StrCpyChar                                         */
  44. /* Args    :                                                    */
  45. /* Returns :Pointer to input string.                            */
  46. /*                                                              */
  47. /* Description: Copy Src to Dest until end of Src or Stop char  */
  48. /*              is reached.                                     */
  49. /*                                                              */
  50. /****************************************************************/
  51. char *StrCpyChar(char *Dest, char *Src, char Stop)
  52. {
  53.     char C;
  54.  
  55.     if(Src==NULL)
  56.     {
  57.         *Dest=0;
  58.         return NULL;
  59.     }
  60.  
  61.     do
  62.     {
  63.         C=*Src;
  64.         Src++;
  65.         if(C==0)
  66.         {
  67.             *Dest=0;
  68.             return NULL;
  69.         }
  70.         if(C==Stop)
  71.         {
  72.             C=0;
  73.         }
  74.         *Dest=C;
  75.         Dest++;
  76.     }while(C);
  77.     while(*Src==Stop)
  78.     {
  79.         Src++;
  80.     }
  81.     return Src;
  82. }
  83.  
  84. /****************************************************************/
  85. /*                                                              */
  86. /* Function:@StripCrudd                                         */
  87. /* Args    :Pointer to string                                   */
  88. /* Returns :1                                                   */
  89. /*                                                              */
  90. /* Description: Removes rubbish from a string, replace with ' ' */
  91. /*                                                              */
  92. /****************************************************************/
  93. int StripCrudd(char *S)
  94. {
  95.     while(*S)
  96.     {
  97.         if(*S<32)
  98.         {
  99.             *S=32;
  100.         }
  101.         else if(*S==':')
  102.         {
  103.             *S=' ';
  104.         }
  105.         S++;
  106.     }
  107.     return 1;
  108. }
  109.  
  110. /****************************************************************/
  111. /*                                                              */
  112. /* Function:@RemoveChar                                         */
  113. /* Args    :Pointer to string, char                             */
  114. /* Returns :1                                                   */
  115. /*                                                              */
  116. /* Description: Removes all instances of char from a string.    */
  117. /*             Squashes up string to fill spaces.               */
  118. /*                                                              */
  119. /****************************************************************/
  120. int RemoveChar(char *S, char T)
  121. {
  122.     /*Removes a character from a string*/
  123.     char *Temp;
  124.     Temp=S;
  125.     while(*S)
  126.     {
  127.         if(*S==T)
  128.         {
  129.             *S=' ';
  130.         }
  131.         S++;
  132.     }
  133.     RemoveSpace(Temp);
  134.     return 1;
  135. }
  136.  
  137. /****************************************************************/
  138. /*                                                              */
  139. /* Function:@RemoveSpace                                        */
  140. /* Args    :Pointer to string                                   */
  141. /* Returns :1                                                   */
  142. /*                                                              */
  143. /* Description: Compresses out ' ' in a string                  */
  144. /*                                                              */
  145. /****************************************************************/
  146. int RemoveSpace(char *S)
  147. {
  148.     /*Removes space from a string*/
  149.     char *T;
  150.  
  151.     while(*S)
  152.     {
  153.         if(*S==' ')
  154.         {
  155.             T=S;
  156.             while(*T)
  157.             {
  158.                 *T=*(T+1);
  159.                 T++;
  160.             }
  161.         }
  162.         else
  163.         {
  164.             S++;
  165.         }
  166.     }
  167.     return 1;
  168. }
  169.  
  170. int RemoveSpaceTSF(char *S)
  171. {
  172.   /* Remove Space Text String Friendly */
  173.     /* Removes space from a string but not if in quotes "" */
  174.     char *T;
  175.  
  176.     while(*S)
  177.     {
  178.         if(*S==' ')
  179.         {
  180.             T=S;
  181.             while(*T)
  182.             {
  183.                 *T=*(T+1);
  184.                 T++;
  185.             }
  186.         }
  187.         else if (*S=='"')
  188.     {
  189.       S++;
  190.       while( (*S) && (*S!='"') )
  191.       {
  192.         S++;
  193.       }
  194.       if(*S)
  195.       {
  196.         S++;
  197.       }
  198.     }
  199.     else
  200.         {
  201.             S++;
  202.         }
  203.     }
  204.     return 1;
  205. }
  206.  
  207. int Validate(char *Str, char *Comp)
  208. {
  209.  /*Checks a string for invalid characters*/
  210.     char *Tmp;
  211.     unsigned char Result=0;
  212.  
  213.     Tmp=Comp;
  214.     while(*Str)
  215.     {
  216.         Comp=Tmp;
  217.         Result=0;
  218.         while(*Comp)
  219.         {
  220.             if(*Str==*Comp)
  221.             {
  222.                 Result=1;
  223.             }
  224.             Comp++;
  225.         }
  226.         if(Result==0)
  227.         {
  228.             return 0;
  229.         }
  230.         Str++;
  231.     }
  232.     return 1;
  233. }
  234.  
  235. int StripDontCare(char *Str)
  236. {
  237. /*Replaces 'X' with '0' in a string*/
  238.     while(*Str)
  239.     {
  240.         if(*Str=='X')
  241.         {
  242.             *Str='0';
  243.         }
  244.         Str++;
  245.     }
  246.     return 1;
  247. }
  248.  
  249. void StrUpr(char *String)
  250. {
  251.   unsigned char TextCount=0;
  252.  
  253.   while(*String)
  254.   {
  255.     if(*String=='"') /*Dont upper constant text*/
  256.     {
  257.       TextCount=~TextCount;
  258.     }
  259.     if ( ('a' <= *String) && (*String <= 'z') && (TextCount==0) )
  260.     {
  261.           *String= *String + 'A' - 'a';
  262.     }
  263.     String++;
  264.   }
  265. }
  266.  
  267. void SwapStrings(char *StrA, char *StrB)
  268. {
  269.   char Tmp;
  270.   int Count;
  271.  
  272.   Count=max(strlen(StrA), strlen(StrB));
  273.   while(Count)
  274.   {
  275.     Tmp = *StrA;
  276.     *StrA = *StrB;
  277.     *StrB = Tmp;
  278.     StrA++;
  279.     StrB++;
  280.     Count--;
  281.   }
  282. }
  283.