home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / str.1.2.0 / strparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  4.5 KB  |  220 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: strparse.c,v 1.4 1992/11/24 20:45:33 panos Exp $" ;
  8. static char version[] = VERSION ;
  9.  
  10. #include "str.h"
  11. #include "strparse.h"
  12.  
  13. char *strcpy() ;
  14. char *strncpy() ;
  15. char *strpbrk() ;
  16.  
  17. char *malloc() ;
  18.  
  19. int str_errno ;
  20.  
  21.  
  22. PRIVATE char *new_string( s )
  23.     register char *s ;
  24. {
  25.     register char *p = malloc( strlen( s ) + 1 ) ;
  26.  
  27.     return( p ? strcpy( p, s ) : p ) ;
  28. }
  29.  
  30.  
  31. str_h str_parse( str, separ, flags, errnop )
  32.     register char *str ;
  33.     char *separ ;
  34.     int flags ;
  35.     int *errnop ;
  36. {
  37.     register struct str_handle *hp ;
  38.     int *errp = ( errnop == NULL ) ? &str_errno : errnop ;
  39.     char *malloc() ;
  40.  
  41.     if ( separ == NULL )
  42.         HANDLE_ERROR( flags, NULL, errp, STR_ENULLSEPAR,
  43.                                 "STR str_parse: NULL separator\n" ) ;
  44.  
  45.     hp = (struct str_handle *) malloc( sizeof( struct str_handle ) ) ;
  46.     if ( hp == NULL )
  47.         HANDLE_ERROR( flags, NULL, errp, STR_ENOMEM,
  48.                                 "STR str_parse: malloc failed\n" ) ;
  49.  
  50.     hp->string = str ;
  51.     hp->pos = str ;
  52.     hp->separator = new_string( separ ) ;
  53.     if ( hp->separator == NULL )
  54.         if ( flags & STR_RETURN_ERROR )
  55.         {
  56.             free( (char *) hp ) ;
  57.             *errp = STR_ENOMEM ;
  58.             return( NULL ) ;
  59.         }
  60.         else
  61.             TERMINATE( "STR str_parse: malloc failed\n" ) ;
  62.     
  63.     hp->flags = flags ;
  64.     hp->errnop = errp ;
  65.     hp->no_more = ( str == NULL ) ;
  66.     return( (str_h) hp ) ;
  67. }
  68.  
  69.  
  70. void str_endparse( handle )
  71.     str_h handle ;
  72. {
  73.     register struct str_handle *hp = (struct str_handle *) handle ;
  74.  
  75.     free( hp->separator ) ;
  76.     free( (char *) handle ) ;
  77. }
  78.  
  79.  
  80. /*
  81.  * Change the string
  82.  */
  83. int str_setstr( handle, newstr )
  84.     str_h handle ;
  85.     char *newstr ;
  86. {
  87.     register struct str_handle *hp = (struct str_handle *) handle ;
  88.     
  89.     if ( newstr == NULL )
  90.         HANDLE_ERROR( hp->flags, STR_ERR, hp->errnop, STR_ENULLSTRING,
  91.                                 "STR str_setstr: NULL string\n" ) ;
  92.     
  93.     hp->string = newstr ;
  94.     hp->pos = newstr ;
  95.     hp->no_more = FALSE ;
  96.     return( STR_OK ) ;
  97. }
  98.  
  99.  
  100.  
  101. /*
  102.  * Change the separator
  103.  */
  104. int str_separator( handle, separator )
  105.     str_h handle ;
  106.     char *separator ;
  107. {
  108.     register struct str_handle *hp = (struct str_handle *) handle ;
  109.     char *new_separator ;
  110.  
  111.     if ( separator == NULL )
  112.         HANDLE_ERROR( hp->flags, STR_ERR, hp->errnop, STR_ENULLSEPAR,
  113.                                 "STR str_separator: NULL separator\n" ) ;
  114.     new_separator = new_string( separator ) ;
  115.     if ( new_separator == NULL )
  116.         HANDLE_ERROR( hp->flags, STR_ERR, hp->errnop, STR_ENOMEM,
  117.             "STR str_separator: malloc failed\n" ) ;
  118.  
  119.     free( hp->separator ) ;
  120.     hp->separator = new_separator ;
  121.     return( STR_OK ) ;
  122. }
  123.  
  124.  
  125. char *str_nextpos( handle )
  126.     str_h handle ;
  127. {
  128.     struct str_handle *hp = (struct str_handle *) handle ;
  129.  
  130.     if ( hp->no_more || *hp->pos == '\0' )
  131.         return( NULL ) ;
  132.     else
  133.         return( hp->pos ) ;
  134. }
  135.  
  136.  
  137. char *str_component( handle )
  138.     str_h handle ;
  139. {
  140.     register struct str_handle *hp = (struct str_handle *) handle ;
  141.     register char *start, *last ;
  142.     register int first_call = ( hp->pos == hp->string ) ;
  143.     register int sep_count ;
  144.     char *retval ;
  145.     int last_char ;
  146.  
  147.     if ( hp->no_more )
  148.         return( NULL ) ;
  149.  
  150.     /*
  151.      * Get number of separator characters.
  152.      * Find beginning of component.
  153.      */
  154.     sep_count = strspn( hp->pos, hp->separator ) ;
  155.  
  156.     /*
  157.      * If this is the first call, and there are separator characters
  158.      * at the beginning of the string and the STR_NULL_START flag is set
  159.      * we return a 0-length string.
  160.      */
  161.     if ( first_call && sep_count > 0 && ( hp->flags & STR_NULL_START ) )
  162.     {
  163.         start = hp->pos ;
  164.         last = hp->pos ;
  165.     }
  166.     else
  167.     {
  168.         start = hp->pos + sep_count ;
  169.  
  170.         if ( *start == '\0' )
  171.         {
  172.             last = start ;
  173.             hp->no_more = TRUE ;
  174.             if ( ! ( hp->flags & STR_NULL_END ) )
  175.                 return( NULL ) ;
  176.         }
  177.         else
  178.         {
  179.             last = strpbrk( start, hp->separator ) ;
  180.             if ( last == NULL )
  181.                 last = start + strlen( start ) ;
  182.         }
  183.     }
  184.  
  185.     /*
  186.      * At this point, the following variables must be set:
  187.      *        start:    beginning of component
  188.      *        last:       end of component + 1
  189.      *
  190.      * If STR_MALLOC is set, allocate space for the new string.
  191.      *
  192.      * NOTE: If STR_MALLOC is not set, the processed string is trashed.
  193.      */
  194.     last_char = *last ;
  195.     if ( hp->flags & STR_MALLOC )
  196.     {
  197.         int len = last - start ;
  198.  
  199.         retval = malloc( len + 1 ) ;
  200.         if ( retval == NULL )
  201.             HANDLE_ERROR( hp->flags, NULL, hp->errnop, STR_ENOMEM,
  202.                                             "STR str_component: malloc failed\n" ) ;
  203.         strncpy( retval, start, len )[ len ] = '\0' ;
  204.     }
  205.     else
  206.     {
  207.         retval = start ;
  208.         *last = '\0' ;
  209.     }
  210.  
  211.     /*
  212.      * Check if last_char is NUL to avoid setting hp->pos past the
  213.      * end of the string
  214.      */
  215.     hp->pos = ( last_char == '\0' ) ? last : last+1 ;
  216.     return( retval ) ;
  217. }
  218.  
  219.  
  220.