home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / STRINGS.H < prev    next >
C/C++ Source or Header  |  1993-03-16  |  19KB  |  727 lines

  1. /*
  2.  *  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)
  3.  *
  4.  * File         : strings.h
  5.  *
  6.  * Description  : strings management
  7.  *
  8.  */                       
  9.  
  10.  
  11. #ifndef __Strings_H
  12. #define __Strings_H
  13.  
  14.  
  15.                 /*  INCLUDE FILES  */
  16.  
  17. #include "c2cpp.h"
  18. #include <string.h>
  19. #include <stddef.h>
  20.  
  21.  
  22.  
  23. /***
  24.  *  Function    :   foreach
  25.  *
  26.  *  Description :   Loop on each token of a string
  27.  *
  28.  *  Parameters  :   out    char *word    pointer that will contain word (token)
  29.  *                  in/out char *string  string to tokenize
  30.  *
  31.  *  Side-effects:   Macro, so be careful.
  32.  *                  '\0' will be inserted into string.
  33.  *
  34.  *  Return code :   none
  35.  ***/
  36.  
  37. #define foreach( word , string ) \
  38.     for ( word = strtok(string," ") ; word ; word = strtok(NULL," ") )
  39.  
  40.  
  41.  
  42.  
  43. /***
  44.  *  Function    :   STRMIN/STRMAX
  45.  *
  46.  *  Description :   return min/max of two strings
  47.  *
  48.  *  Parameters  :   in   char *str1
  49.  *                  in   char *str2
  50.  *
  51.  *  Side-effects:   Macro, so be careful.
  52.  *
  53.  *  Return      :   pointer to min/max of the two strings
  54.  ***/
  55.  
  56. #define STRMIN( str1 , str2 )        ( (strcmp(str1 , str2) < 0) ? str1 : str2 )
  57. #define STRMAX( str1 , str2 )        ( (strcmp(str1 , str2) > 0) ? str1 : str2   )
  58.  
  59. /***
  60.  *  Function    :   strisequal
  61.  *
  62.  *  Description :   Test equality of two strings
  63.  *
  64.  *  Parameters  :   in   char *str1
  65.  *                  in   char *str2
  66.  *
  67.  *  Return      :   0 or 1
  68.  ***/
  69.  
  70. #define strisequal( string1 , string2 )    ( ! strcmp(string1 , string2) )
  71.  
  72.  
  73.     /***   For switch/case-like string manipulation
  74.  
  75.                usage:   STRWITCH( string )
  76.                                 {
  77.                                   STRCASE "string1": ... ;
  78.                                   STRCASE "string2": ... ; break ;
  79.                                   ...
  80.                                   STRDEFAULT    : ... ;
  81.                                 } STRENDSWITCH
  82.      ***/
  83.  
  84. #define STRSWITCH( _str )     { char *_tmp_s = _str ; \
  85.                                 int _tmp_flag = 0 ; \
  86.                                 do { if ( _tmp_flag )
  87.  
  88. #define STRCASE( _str )       ( _tmp_flag = 1 ) ; } \
  89.                               if ( _tmp_flag || ! strcmp(_tmp_s, _str) ) { 0 ? 0
  90.  
  91. #define STRDEFAULT            0 ; } { 0 ? 0
  92.  
  93. #define STRENDSWITCH          } while (0) ; }
  94.  
  95.  
  96.  
  97.  
  98.          /*  TYPES DEFINITIONS  */
  99.  
  100.  
  101. typedef enum { align_left , align_right , align_center } align_type ;
  102.  
  103. typedef enum { LOWER = 5 , UPPER } casetype ;
  104.  
  105.  
  106.  
  107.         /*  FUNCTIONS DEFINITIONS  */
  108.  
  109.  
  110. /***
  111.  *  Function    :  strleft
  112.  *
  113.  *  Description :  Copy the first ... characters of a string.
  114.  *           Like strncpy but add a '\0' at the end of the output string.
  115.  *
  116.  *  Decisions   :  If given length > string length : normal strcpy
  117.  *           If given length <= 0 returns an empty string.
  118.  *
  119.  *  Parameters  :  out  char  *out_str     result
  120.  *                 in   char  *in_str      in string
  121.  *                 in   int   length       length to be copied
  122.  *
  123.  *  Return code :   pointer to result.
  124.  *
  125.  *  OS/Compiler :   All
  126.  */
  127.  
  128. EXTERN char *strleft( char* , const char* , int ) ;
  129.  
  130.  
  131.  
  132.  
  133. /***
  134.  *  Function    :  strright
  135.  *
  136.  *  Description :  Copy the last ... characters of a string.
  137.  *
  138.  *  Decisions   :  If given length > string length : normal strcpy
  139.  *           If given length <= 0 returns an empty string.
  140.  *
  141.  *  Parameters  :  out  char  *out_str     result
  142.  *                 in   char  *in_str      in string
  143.  *                 in   int   length       length to be copied
  144.  *
  145.  *  Return code :   pointer to result.
  146.  *
  147.  *  OS/Compiler :   All
  148.  */
  149.  
  150. EXTERN char *strright( char* , const char* , int ) ;
  151.  
  152.  
  153.  
  154. /***
  155.  *  Function    :  strmid
  156.  *
  157.  *  Description :  Copy n characters of a string, begining at a given position
  158.  *           ( form 1 to ... )
  159.  *
  160.  *  Decisions   :  Stops at the end of input string if given length
  161.  *           is too big or length = 0.
  162.  *           If given length or position < 0 returns an empty string.
  163.  *
  164.  *  Parameters  :  out  char  *out_str     result
  165.  *                 in   char  *in_str      in string
  166.  *                 in   int   pos          position where begin to copy
  167.  *                 in   int   length       length to be copied
  168.  *
  169.  *  Return code :   pointer to result.
  170.  *
  171.  *  OS/Compiler :   All
  172.  */
  173.  
  174. EXTERN char *strmid( char* , const char* , int , int ) ;
  175.  
  176.  
  177.  
  178. /***
  179.  *  Function    :  stralign
  180.  *
  181.  *  Description :  Copy an input string in an output string
  182.  *           with specified alignement (blank padding).
  183.  *
  184.  *  Decisions   :  If given length < 0 returns an empty string.
  185.  *
  186.  *  Parameters  :  out  char        *out_str     result
  187.  *                 in   char        *in_str      in string
  188.  *                 in   align_type  type,     type
  189.  *                 in   int         length       length to be copied
  190.  *
  191.  *  Value       :  type = { align_left, align_center, align_right }
  192.  *
  193.  *  Return code :   pointer to result.
  194.  *
  195.  *  OS/Compiler :   All
  196.  */
  197.  
  198. EXTERN char *stralign( char* , const char* , align_type , int ) ;
  199.  
  200.  
  201.  
  202. /***
  203.  *  Function    :  strmvstr
  204.  *
  205.  *  Description :  Copy an input string in an output string
  206.  *           with replacing all occurences of a target string.
  207.  *
  208.  *  Parameters  :  out      char  *out_str    result
  209.  *                 in       char  *in_str     in string
  210.  *                 in       char  *target     target string to replace
  211.  *                 in       char  *new_str    string to put in place of target
  212.  *
  213.  *  Return code :  pointer to result.
  214.  *
  215.  *  OS/Compiler :  All
  216.  ***/
  217.  
  218. EXTERN char *strmvstr( char* , const char* , const char* , const char* ) ;
  219.  
  220.  
  221.  
  222. /***
  223.  *  Function    :  strmvchr
  224.  *
  225.  *  Description :  Replace all occurences of a target character
  226.  *           by a new character.
  227.  *
  228.  *  Parameters  :  in/out   char  *string
  229.  *                 in       char  target     target char to replace
  230.  *                 in       char  new_char   char to put in place of target
  231.  *
  232.  *  Return code :  pointer to result.
  233.  *
  234.  *  OS/Compiler :  All
  235.  ***/
  236.  
  237. EXTERN char *strmvchr( char* , char , char ) ;
  238.                      
  239.  
  240.  
  241. /***
  242.  *  Function    :  strrmstr
  243.  *
  244.  *  Description :  Removing all occurences of a target string.
  245.  *
  246.  *  Parameters  :  in/out   char  *string
  247.  *                 in       char  *target     target string to remove
  248.  *
  249.  *  Decisions   :  Same implementation as strmvstr without copying
  250.  *           a replacement string.
  251.  *           Could also be implemented as
  252.  *                        strmvstr( ptr, ptr, target, "" )
  253.  *           but should be less efficient.
  254.  *
  255.  *  Return code :  pointer to result.
  256.  *
  257.  *  OS/Compiler :  All
  258.  ***/
  259.  
  260. EXTERN char *strrmstr( char* , const char* ) ;
  261.  
  262.  
  263.  
  264. /***
  265.  *  Function    :  strrmchr
  266.  *
  267.  *  Description :  Removing all occurences of a target character.
  268.  *
  269.  *  Parameters  :  in/out   char  *string
  270.  *                 in       char  target     target char to remove
  271.  *
  272.  *  Return code :  pointer to result.
  273.  *
  274.  *  OS/Compiler :  All
  275.  ***/
  276.  
  277. EXTERN char *strrmchr( char* , char ) ;
  278.  
  279.  
  280.  
  281. /***
  282.  *  Function    :  strinsert
  283.  *
  284.  *  Description :  Insert a string in another.
  285.  *
  286.  *  Parameters  :  out  char   *out_str    out string
  287.  *                 in   char   *in_str     in string
  288.  *                 in   char   *to_insert  in string to insert into the other
  289.  *                 in   int    place       place to insert string
  290.  *
  291.  *  Decisions   :  Does nothing if place specified out of range.
  292.  *
  293.  *  Return      :  pointer to result
  294.  *
  295.  *  OS/Compiler :  All
  296.  ***/
  297.  
  298. EXTERN char *strinsert( char* , const char* , const char* , int ) ;
  299.  
  300.  
  301.  
  302. /***
  303.  *  Function    :  strend
  304.  *
  305.  *  Description :  Returns a pointer to the '\0' ending a string
  306.  *
  307.  *  Parameters  :  in   char        *in_str      in string
  308.  *
  309.  *  Return code :   pointer to the '\0' ending the string..
  310.  *
  311.  *  OS/Compiler :   All
  312.  ***/
  313.     
  314. EXTERN char *strend( const char* ) ;
  315.  
  316.  
  317.  
  318. /***
  319.  *  Function    :   strskip
  320.  *
  321.  *  Description :   Skip 'n' words from a string.
  322.  *
  323.  *  Decisions   :
  324.  *
  325.  *  Parameters  :   in    char    *string     string to be matched
  326.  *                  in    int     word_nb     number of words to skip
  327.  *
  328.  *  Return code :   pointer to word.
  329.  *
  330.  *  OS/Compiler :   All
  331.  ***/
  332.  
  333. EXTERN char *strskip( const char* , int ) ;
  334.  
  335.  
  336.  
  337. /***
  338.  *  Function    :  strisalpha
  339.  *
  340.  *  Description :  Tests if a string contains only alphabetical character
  341.  *
  342.  *  Parameters  :  in   char   *string
  343.  *
  344.  *  Return      :  1 if string contains only alphabetical characters.
  345.  *                 0 otherwise
  346.  *
  347.  *  OS/Compiler :  All
  348.  ***/
  349.  
  350. EXTERN int strisalpha( const char* ) ;
  351.  
  352.  
  353.  
  354. /***
  355.  *  Function    :  strisalnum
  356.  *
  357.  *  Description :  Tests if a string contains only alphanumerical character
  358.  *
  359.  *  Parameters  :  in   char   *string
  360.  *
  361.  *  Return      :  1 if string contains only alphanumerical characters.
  362.  *                 0 otherwise
  363.  *
  364.  *  OS/Compiler :  All
  365.  ***/
  366.  
  367. EXTERN int strisalnum( char* ) ;
  368.  
  369.  
  370.  
  371. /***
  372.  *  Function    :  stris_alnum
  373.  *
  374.  *  Description :  Tests if a string contains only alphanumerical characters or '_'.
  375.  *
  376.  *  Parameters  :  in   char   *string
  377.  *
  378.  *  Return      :  1 if string contains only alphanumerical characters or '_'.
  379.  *                 0 otherwise
  380.  *
  381.  *  OS/Compiler :  All
  382.  ***/
  383.  
  384. EXTERN int stris_alnum( const char* ) ;
  385.  
  386.  
  387.  
  388. /***
  389.  *  Function    :   strelt
  390.  *
  391.  *  Description :   Return the n-th element (token) in a string.
  392.  *            The tokens are separated by several input character.
  393.  *
  394.  *  Decisions   :   If tokens are separated by ' ', '\t' or '\n',
  395.  *            the input characters will be " ".
  396.  *            If n-th token does not exist, an empty string is returned.
  397.  *
  398.  *  Parameters  :   out    char  *out_str      out string
  399.  *            in     char  *in_str       in string
  400.  *            out    char  *token        string containing token
  401.  *            in     int   element       element number
  402.  *
  403.  *  Return      :   pointer to the n-th token.
  404.  *
  405.  *  OS/Compiler :   All
  406.  */
  407.  
  408. EXTERN char *strelt( char* , const char* , char* , int ) ;
  409.  
  410.  
  411.  
  412. /***
  413.  *  Function    :  strreduce
  414.  *
  415.  *
  416.  *  Description :  Parse a string to suppress blanks, tabs and newlines
  417.  *                 and transform it in uppercase.
  418.  *
  419.  *
  420.  *  Decisions   :  - Portions of string between double quotes are unmodified
  421.  *                   (quotes are removed).
  422.  *
  423.  *                 - '\' is used to quote the next character.
  424.  *
  425.  *                 - Characters with accent are also transformed to uppercase.
  426.  *
  427.  *  Parameters  :  in/out   char *string
  428.  *
  429.  *  Return      :  pointer to end of string ( '\0' ).
  430.  *
  431.  *  OS/Compiler :  All
  432.  ***/
  433.  
  434. EXTERN char *strreduce( char* ) ;
  435.  
  436.  
  437.  
  438. /***
  439.  *  Function    :   strtoken
  440.  *
  441.  *  Description :   Parse a string to extract the next token.
  442.  *
  443.  *  Decisions   :   The tokens are separated by ' ', '\t' or '\n'.
  444.  *                Tokens may contain these separators by being quoted with
  445.  *            one of the following characters : '"`.
  446.  *            The function returns a pointer to the character following the token.
  447.  *                  Special escape sequences accepted: 
  448.  *                     \b, \f, \n, \r, \t, \e (Esc), \x..., \0..., \...
  449.  *
  450.  *  Parameters  :   in    char *string    string to be tokenized
  451.  *            out   char *token     string containing token
  452.  *
  453.  *  Return code :   pointer to character following the token.
  454.  *
  455.  *  OS/Compiler :   All
  456.  ***/
  457.  
  458. EXTERN char *strtoken( const char* , char* ) ;
  459.  
  460.  
  461.  
  462. /***
  463.  *  Function    :  strcase
  464.  *
  465.  *  Description :  change case of a string.
  466.  *              All special characters are translated (éèà...)
  467.  *
  468.  *  Parameters  :  in   char      *string  string to translate
  469.  *                 in   casetype  type     UPPER/LOWER
  470.  *                                        
  471.  *  Value       :  type = { UPPER, LOWER }
  472.  *
  473.  *  Decisions   :  If character > 255, no change made.
  474.  *
  475.  *  Return      :  pointer to result
  476.  *
  477.  *  OS/Compiler :  All
  478.  ***/
  479.  
  480. EXTERN char *strcase( char* , casetype ) ;
  481.  
  482.  
  483.  
  484. /***
  485.  *  Function    :  chcase
  486.  *
  487.  *  Description :  change case of a 2-byte character.
  488.  *              All special characters are translated (éèà...)
  489.  *
  490.  *  Parameters  :  in   char      car      char to translate
  491.  *                 in   casetype  type     UPPER/LOWER
  492.  *                                        
  493.  *  Value       :  type = { UPPER, LOWER }
  494.  *
  495.  *  Decisions   :  If character > 255, no change made.
  496.  *
  497.  *  Return      :  code of char translated
  498.  *
  499.  *  OS/Compiler :  All
  500.  ***/
  501.  
  502. EXTERN int chcase( int , casetype ) ;
  503.  
  504. /***
  505.  *  Function    :  strcomp
  506.  *
  507.  *  Description :  Compare two strings case-insensitive
  508.  *           All special characters are translated (éèà...)
  509.  *
  510.  *  Parameters  :  in   char  *str1
  511.  *                 in   char  *str2
  512.  *                 
  513.  *  Return      :  -1 if str1 <  str2
  514.  *            1 if str1 >  str2
  515.  *            0 if str1 == str2
  516.  *
  517.  *  OS/Compiler :  All
  518.  ***/
  519.  
  520. EXTERN int strcomp( const char *str1, const char *str2 );
  521.  
  522.  
  523.  
  524. /***
  525.  *  Function    :  strncomp
  526.  *
  527.  *  Description :  Compare n characters of two strings case-insensitive
  528.  *           All special characters are translated (éèà...)
  529.  *
  530.  *  Parameters  :  in   char  *str1
  531.  *                 in   char  *str2
  532.  *
  533.  *  Return      :  -1 if str1 <  str2
  534.  *            1 if str1 >  str2
  535.  *            0 if str1 == str2
  536.  *
  537.  *  OS/Compiler :  All
  538.  ***/
  539.  
  540. EXTERN int strncomp( const char *str1, const char *str2, int length );
  541.  
  542.  
  543.  
  544. /***
  545.  *  Function    :  matchset (internal)
  546.  *
  547.  *  Description :  Test if a character matches a set expression.
  548.  *
  549.  *  Parameters  :  in    char c          character to be matched
  550.  *                 in    char *pattern   regular expression to match
  551.  *
  552.  *  Parameters  :  in   char   *string
  553.  *
  554.  *  Decisions   :  The following symbols are treated specially:
  555.  *
  556.  *              \  quote next character      -  range of values
  557.  *              ^  non-inclusion (if first character)
  558.  *
  559.  *              ex: aeiou0-9   match a, e, i, o, u, and 0 thru 9
  560.  *                  ^aeiou0-9  match anything but a, e, i, o, u, and 0 thru 9
  561.  *
  562.  *  Side-effects:  pattern contents will be destroyed.
  563.  *
  564.  *  Return      :  1 or 0
  565.  *
  566.  *  OS/Compiler :  All
  567.  ***/
  568.  
  569. EXTERN int matchset( char c, char *pattern );
  570.  
  571.  
  572.  
  573. /***
  574.  *  Function    :  recursexp
  575.  *
  576.  *  Description :  Returns the number of character of a string matched
  577.  *                 by a regular expression.
  578.  *
  579.  *  Decisions   :  The following symbols are treated specially:
  580.  *
  581.  *                 .  any character             \  quote next character
  582.  *                 *  match zero or more        +  match one or more
  583.  *                 [] set of characters
  584.  *
  585.  *
  586.  *  Parameters  :  in   char *string    input string to be matched
  587.  *                 in   char *pattern   regular expression to match
  588.  *
  589.  *  Return      :  number of character matched by regular expression
  590.  *                 -1 if not matched
  591.  *
  592.  *  OS/Compiler :  All
  593.  ***/
  594.  
  595. EXTERN int recursexp( const char *string, char *pattern );
  596.  
  597.  
  598.  
  599. /***
  600.  *  Function    :  regexp
  601.  *
  602.  *  Description :  Returns the string matched by a regular expression
  603.  *                 into a string.
  604.  *
  605.  *  Decisions   :  The following symbols are treated specially:
  606.  *
  607.  *                 ^  start of line             $  end of line
  608.  *                 ?  any character             \  quote next character
  609.  *                 *  match zero or more        [] set of characters
  610.  *
  611.  *              ex: [aeiou0-9]   match a, e, i, o, u, and 0 thru 9
  612.  *                  [^aeiou0-9]  match anything but a, e, i, o, u, and 0 thru 9
  613.  *
  614.  *  Parameters  :  out   char *outstr    resulting string
  615.  *                 in    char *string    input string in which we search
  616.  *                 in    char *pattern   regular expression to match
  617.  *
  618.  *  Return      :  - pointer to resulting string
  619.  *                 - if ( outstr == NULL ) returns pointer to matched string
  620.  *                   inside 'string'.
  621.  *
  622.  *  OS/Compiler :  All
  623.  ***/
  624.  
  625. EXTERN char *regexp( char *outstr, const char *string, const char *pattern );
  626.  
  627.  
  628.  
  629. /***
  630.  *  Function    :   strtohex
  631.  *
  632.  *  Description :   convert a string containing an hexadecimal
  633.  *                  representation into an hexadecimal flow of bytes.
  634.  *
  635.  *  Parameters  :   char *string   in/out
  636.  *
  637.  *  Return      :   length of hexadecimal flow of bytes.
  638.  *                  -1 on error
  639.  *
  640.  *  OS/Compiler :   All
  641.  ***/
  642.  
  643. EXTERN int strtohex( char *string );
  644.  
  645.  
  646.  
  647. /***
  648.  *  Function    :  hextostr
  649.  *
  650.  *  Description :  Convert an hexadecimal flow of bytes into
  651.  *                 a string containing an hexadecimal representation.
  652.  *
  653.  *  Parameters  :  out   char *string
  654.  *                 in    char *hexa
  655.  *                 in    int  length        number of hexadecimal bytes
  656.  *
  657.  *  Return      :   0 if OK
  658.  *                 -1 on error
  659.  *
  660.  *  OS/Compiler :  All
  661.  ***/              
  662.     
  663. EXTERN int hextostr( char *string, const char *hexa, int length );
  664.  
  665.  
  666.  
  667. /***
  668.  *  Function    :  strcpyb
  669.  *
  670.  *  Description :  Like strcpy but truncates trailing blanks and \n
  671.  *                 and skip leading blanks.
  672.  *
  673.  *  Parameters  :  out   char * target
  674.  *           in    char * source
  675.  *
  676.  *  Return code :  like strcpy
  677.  *
  678.  *  OS/Compiler :  All
  679.  ***/
  680.  
  681. EXTERN char *strcpyb( char *target, const char *source );
  682.  
  683.  
  684.  
  685. /***
  686.  *  Function    : strncpyb
  687.  *
  688.  *  Description :  Like strncpy but truncates trailing blanks and \n,
  689.  *                 skip leading blanks and adds a '\0'.
  690.  *
  691.  *  Decisions   :
  692.  *
  693.  *  Parameters  :  out   char * target
  694.  *               in    char * source
  695.  *                 in    int    length
  696.  *
  697.  *  Return code :  like strncpy
  698.  *
  699.  *  Side-effects:  Pad target string to length with '\0'.
  700.  *
  701.  *  OS/Compiler :  All
  702.  ***/
  703.  
  704. EXTERN char *strncpyb( char *target, const char *source, int length );
  705.  
  706.  
  707. /***
  708.  *  Function    :  strcalc
  709.  *
  710.  *  Description :  Mathematical expression evaluation 
  711.  *
  712.  *  Parameters  :  in/out   char *   calcul      expression to calculate
  713.  *
  714.  *  Values      :  Allowed operators : + - * / () = < > <= >= <>  on floats
  715.  *                                                             %  on ints
  716.  *
  717.  *  Return      :  result   if OK
  718.  *                 HUGE_VAL if error
  719.  *
  720.  *  OS/Compiler :  All
  721.  ***/
  722.                    
  723.                 
  724. EXTERN long double strcalc( char *calcul );
  725.  
  726. #endif
  727.