Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #105317

Re: solved Re: writing a c program to substitute text

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: solved Re: writing a c program to substitute text
Date 2017-03-02 10:54 +0000
Organization A noiseless patient Spider
Message-ID <87fuiwlzjd.fsf@bsb.me.uk> (permalink)
References <ehfv4oFu00sU1@mid.individual.net> <o8ul21$ip3$1@dont-email.me> <ehiij4Ffgn1U1@mid.individual.net> <aa13d6d7-7cdf-4001-84fa-782ae8764b83@googlegroups.com> <ehq6ohF195iU1@mid.individual.net>

Show all headers | View raw


fred flintstone <fred@server.invalid> writes:
<snip>

There's a bug in the code, so I'll mention a couple of other things that
are not very important since I posting about something that does matter.

<snip>
> /**
>  * Read the next token from the input stream.  In this program,
>  * tokens are either sequences of alphanumeric characters, or individual
>  * non-alphanumeric characters.  Both are saved as strings to buf.
>  *
>  * For example, the input ">>text3.txt" would be tokenized as ">", ">",
>  * "text3", ".", "txt".
>  */
> int getNextSym( FILE *stream, char **buf, size_t *bufsize )

I would consider returning the number of characters read here.  It's
always a shame to return 0/1 when you have a potentially useful
alternative.  You might not need it now, but getNextSym is a potentially
reusable function.

> {
>   size_t charsRead = 0;
>   int c;
>
>   while ( ( c = fgetc( stream ) ) != EOF )
>   {
>     if ( !isalpha( c ) && !isdigit( c ) )

You can use isalnum here.

>     {
>       if ( charsRead > 0 )
>       {
>         /**
>          * If we've read a sequence of alphanumeric characters,
>          * we *don't* want this non-alphanumeric character to be
>          * part of the string (IOW, we treat all non-alphanumerics
>          * as string delimiters).  Push the current character back
>          * on to the input stream, terminate the buffer, and return.
>          */
>         ungetc( c, stream );
>         (*buf)[charsRead] = 0;
>         return 1;
>       }
>       else
>       {
>         /**
>          * All non-alphanumeric characters are returned in their own
>          * 1-character strings.
>          */
>         (*buf)[0] = c;
>         (*buf)[1] = 0;
>         return 1;
>       }
>     }
>     else
>     {
>       /**
>        * Current character is alphanumeric, so we want to
>        * append it to the buffer, extending the buffer if
>        * necessary.
>        */
>       if ( charsRead < *bufsize || expand( buf, bufsize ) )

I think you need <= here or you can overflow the buffer when it gets
zero terminated.

>       {
>         (*buf)[charsRead++] = c;
>       }
>       else if ( charsRead == *bufsize )
>       {
>         /**
>          * Buffer expansion failed; push the latest character back on
>          * to the input stream, terminate the buffer, and return a failure
>          * code.  It's up to the caller to decide what to do next.
>          */
>         ungetc( c, stream );
>         (*buf)[charsRead-1] = 0;
>         return 0;
>       }
>     }
>   }
>   if ( charsRead > 0 )
>   {
>     /**
>      * We hit EOF while reading an alphanumeric sequence; terminate
>      * the buffer.
>      */
>     (*buf)[charsRead] = 0;
>     return 1;
>   }
>
>   return 0;
> }
>
> /**
>  * Parse the sequence number out of the file name.  This assumes the
>  * file name is of the form "toolchain<N>.sh".
>  */
> int getOrdinal( const char *filename )
> {
>   int ord;
>   if ( sscanf( filename, "toolchain%d.sh", &ord ) == 1 )

I think I'd relax the requirement that it must be "toolchain" but that's
a small matter.  You could make the output file have the same pattern as
the input file but with the number incremented.

>     return ord;
>
>   return -1;

I like to use a conditional expression in such cases

  return sscanf(filename, "toolchain%d.sh", &ord) == 1 ? ord : -1;

> }

<snip>
>   size_t bufsize = 80; // initial buffer size
>   char *buffer = malloc( bufsize );

You should really check the buffer != 0 here.

What I do in these cases in to start by setting buffer = 0 and
bufsize = 0 so the first character triggers and expand operation.  You
need to make the expand work correctly with a zero size so you can do

  new_size = size * 2 + 80;

or

  new_size = size ? 80 : size * 2;

but that avoids duplicating allocation code and checks.

>   /**
>    * Meat of the program - read the next symbol from the input file.
>    * If it matches our search symbol, write the replacement symbol
>    * to the output file - otherwise, copy the input symbol to the
>    * output file.  **/
>   while ( getNextSym( input, &buffer, &bufsize ) )
>   {
>     if ( strcmp( buffer, sym1 ) == 0 )
>       fprintf( output, "%s", sym2 );
>     else
>       fprintf( output, "%s", buffer );
>   }

Again I favour conditional expressions for cases like this:

  fputs(strcmp( buffer, sym1 ) == 0 ? sym2 : buffer, output);

>   free( buffer );
>   fclose( input );
>   fclose( output );
>
>   return 0;
> }

<snip>
Only the buffer overrun is really significant.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-26 04:14 -0800
  Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-26 04:55 -0800
  Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-26 14:28 +0100
    Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 03:58 -0800
      Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:11 +0000
        Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 13:33 +0000
          Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 17:12 +0000
            Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-27 17:59 +0000
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:05 +0100
              Re: writing a c program to substitute text scott@slp53.sl.home (Scott Lurndal) - 2017-02-27 18:37 +0000
                Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 11:40 -0800
        Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:03 +0100
        Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 05:33 -0800
          Re: writing a c program to substitute text "D. Aaron Sawyer" <aaron@110.net> - 2017-03-01 10:32 -0400
            Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 06:59 -0800
              Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 15:24 +0000
                Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:47 -0800
                Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 11:05 +0000
      Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 13:33 +0000
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:10 -0800
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:07 +0100
            Re: writing a c program to substitute text jt@toerring.de (Jens Thoms Toerring) - 2017-02-27 18:23 +0000
      Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 14:46 +0100
        Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:27 -0500
          Re: writing a c program to substitute text GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-27 15:28 +0100
            Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 09:38 -0500
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 15:44 +0100
            Re: writing a c program to substitute text James Kuyper <jameskuyper@verizon.net> - 2017-02-27 10:36 -0500
            Re: writing a c program to substitute text Anglican <h.toms@qmul.ac.uk> - 2017-02-27 15:50 +0000
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 17:13 +0100
              Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 16:30 -0800
          Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:08 -0800
            Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:20 -0800
              Re: writing a c program to substitute text fir <profesor.fir@gmail.com> - 2017-02-27 07:24 -0800
        Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-02-27 16:00 +0100
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:22 -0800
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 19:11 +0100
      Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-02-28 15:41 -0800
        solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-02 01:25 -0800
          Re: solved Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-03-02 11:28 +0100
          Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 10:54 +0000
            Re: solved Re: writing a c program to substitute text Manfred <noname@invalid.add> - 2017-03-02 15:28 +0100
              Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 14:51 +0000
            Re: solved Re: writing a c program to substitute text John Bode <jfbode1029@gmail.com> - 2017-03-02 14:18 -0800
              Re: solved Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-02 23:55 +0000
                Re: solved Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-14 18:32 -0700
        Re: writing a c program to substitute text Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-17 07:04 -0700
    Re: writing a c program to substitute text jladasky@itu.edu - 2017-02-27 13:48 -0800
      Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 14:16 -0800
        Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 08:47 +0100
          Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 01:59 -0800
            Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 11:39 +0100
            Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-28 12:03 +0000
              Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:33 +0000
                Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:43 +0100
                Re: writing a c program to substitute text BartC <bc@freeuk.com> - 2017-02-28 13:59 +0000
                Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-03-01 04:26 -0800
      Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 23:29 +0100
      Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 15:10 -0800
  Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 06:42 -0800
    Re: writing a c program to substitute text Richard Heathfield <rjh@cpax.org.uk> - 2017-02-26 16:54 +0000
      [OT] "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 00:56 +0800
    Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 13:36 -0800
      Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 14:53 -0800
        Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 00:53 +0100
          Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-26 17:30 -0800
            Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-26 17:44 -0800
            Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 01:46 +0000
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 09:29 +0100
                Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 02:12 -0800
                Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-27 13:11 +0100
    Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-27 04:41 -0800
      Re: writing a c program to substitute text Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-27 05:04 -0800
        Re: writing a c program to substitute text Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-27 13:25 +0000
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-27 09:56 -0800
  Re: writing a c program to substitute text "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-02-27 01:00 +0800
  Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-26 18:55 -0500
    Re: writing a c program to substitute text fred flintstone <fred@server.invalid> - 2017-02-28 04:20 -0800
      Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 14:38 +0100
        Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:33 -0800
          Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 19:38 +0100
            Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 11:40 -0800
              Re: writing a c program to substitute text David Brown <david.brown@hesbynett.no> - 2017-02-28 21:03 +0100
            Re: gcc optimization levels Noob <root@127.0.0.1> - 2017-02-28 22:55 +0100
      Re: writing a c program to substitute text "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-28 11:43 -0500
      Re: writing a c program to substitute text Keith Thompson <kst-u@mib.org> - 2017-02-28 08:46 -0800

csiph-web