home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / elm-2.4-pl20.tar.Z / elm-2.4-pl20.tar / lib / striparens.c < prev    next >
C/C++ Source or Header  |  1992-10-03  |  3KB  |  135 lines

  1. static char rcsid[] = "@(#)$Id: striparens.c,v 5.1 1992/10/03 22:41:36 syd Exp $";
  2.  
  3. /*******************************************************************************
  4.  *  The Elm Mail System  -  $Revision: 5.1 $   $State: Exp $
  5.  *
  6.  *            Copyright (c) 1988-1992 USENET Community Trust
  7.  *            Copyright (c) 1986,1987 Dave Taylor
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: striparens.c,v $
  16.  * Revision 5.1  1992/10/03  22:41:36  syd
  17.  * Initial checkin as of 2.4 Release at PL0
  18.  *
  19.  *
  20.  ******************************************************************************/
  21.  
  22. /** 
  23.  
  24. **/
  25.  
  26. #include "headers.h"
  27.  
  28.  
  29. char *strip_parens(string)
  30. char *string;
  31. {
  32.     /**
  33.         Remove parenthesized information from a string.  More specifically,
  34.         comments as defined in RFC822 are removed.  This procedure is
  35.         non-destructive - a pointer to static data is returned.
  36.     **/
  37.     static char  buffer[VERY_LONG_STRING];
  38.     register char *bufp;
  39.     register int depth, l;
  40.  
  41.     bufp = buffer;
  42.     depth = 0;
  43.     while (*string != '\0') {
  44.       l = len_next_part(string);
  45.       if (l == 1) {
  46.         switch ( *string ) {
  47.         case '(':            /* begin comment on '('        */
  48.           ++depth;
  49.           break;
  50.         case ')':            /* decr nesting level on ')'    */
  51.           --depth;
  52.           break;
  53.         case '\\':            /* treat next char literally    */
  54.           if ( *++string == '\0' ) {        /* gracefully handle    */
  55.         *bufp++ = '\\';            /* '\' at end of string    */
  56.         --string;                /* even tho it's wrong    */
  57.           } else if ( depth == 0 ) {
  58.         *bufp++ = '\\';
  59.         *bufp++ = *string;
  60.           }
  61.           break;
  62.         default:            /* a regular char        */
  63.           if ( depth == 0 )
  64.         *bufp++ = *string;
  65.           break;
  66.         }
  67.         string++;
  68.       } else {
  69.         if (depth == 0) {
  70.           while (--l >= 0)
  71.         *bufp++ = *string++;
  72.         } else
  73.           string += l;
  74.       }
  75.     }
  76.     *bufp = '\0';
  77.     return( (char *) buffer);
  78. }
  79.  
  80. /*
  81.  * Added by RLH.  This could be combined w/ above if willing to pass
  82.  * a TRUE/FALSE for whether we are stripping or getting...
  83.  */
  84. char *get_parens(string)
  85. char *string;
  86. {
  87.     /**
  88.         Find and return parenthesized information in a string.  More
  89.         specifically, comments as defined in RFC822 are retrieved.
  90.         This procedure is non-destructive - a pointer to static data
  91.         is returned.
  92.     **/
  93.     static char  buffer[VERY_LONG_STRING];
  94.     register char *bufp;
  95.     register int depth, l;
  96.  
  97.     bufp = buffer;
  98.     depth = 0;
  99.     while (*string != '\0') {
  100.       l = len_next_part(string);
  101.       if (l == 1) {
  102.         switch ( *string ) {
  103.         case '(':            /* begin comment on '('        */
  104.           ++depth;
  105.           break;
  106.         case ')':            /* decr nesting level on ')'    */
  107.           --depth;
  108.           break;
  109.         case '\\':            /* treat next char literally    */
  110.           if ( *++string == '\0' ) {        /* gracefully handle    */
  111.         *bufp++ = '\\';            /* '\' at end of string    */
  112.         --string;                /* even tho it's wrong    */
  113.           } else if ( depth > 0 ) {
  114.         *bufp++ = '\\';
  115.         *bufp++ = *string;
  116.           }
  117.           break;
  118.         default:            /* a regular char        */
  119.           if ( depth > 0 )
  120.         *bufp++ = *string;
  121.           break;
  122.         }
  123.         string++;
  124.       } else {
  125.         if (depth > 0) {
  126.           while (--l >= 0)
  127.         *bufp++ = *string++;
  128.         } else
  129.           string += l;
  130.       }
  131.     }
  132.     *bufp = '\0';
  133.     return( (char *) buffer);
  134. }
  135.