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


Groups > comp.lang.c > #106110

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

From fred flintstone <fred@server.invalid>
Newsgroups comp.lang.c
Subject Re: solved Re: writing a c program to substitute text
Date 2017-03-14 18:32 -0700
Message-ID <eirjt4Fm1vhU1@mid.individual.net> (permalink)
References (3 earlier) <aa13d6d7-7cdf-4001-84fa-782ae8764b83@googlegroups.com> <ehq6ohF195iU1@mid.individual.net> <87fuiwlzjd.fsf@bsb.me.uk> <549192cf-828c-43a7-8a32-f6b9e93dd794@googlegroups.com> <87r32fkze2.fsf@bsb.me.uk>

Show all headers | View raw


On 03/02/2017 03:55 PM, Ben Bacarisse wrote:
> John Bode <jfbode1029@gmail.com> writes:
>
>> On Thursday, March 2, 2017 at 4:54:26 AM UTC-6, Ben Bacarisse wrote:
>>> fred flintstone <fred@server.invalid> writes:


[OP is leaving this quoted material because he didn't read it until 
right now and want to leave it as an editing decision]

>>>
>>> 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.
>>>
>>
>> In my defense, I tossed this off in about 15 minutes without putting too
>> much thought into it, so I'm not surprised there are problems.  Like I
>> said, this ain't professional-quality code.
>
> It's always grim posting code to comp.lang.c since so many eyes see it,
> but it's good for the soul and good for the group so thanks.
>

It's OP who is grateful for a decent result when he started into the 
question muttering about strings. It follows the development in 
_Intermediate Perl_ to do some small thing on github to try to figure it 
out. What's more, I'm also looking at _Programmieren in C_ chp 8, which 
I've never done because I worked through K&R2 whilst on MS.

https://github.com/TBlazer66/gcc

I have found that if I don't have C, then I don't really have perl, so 
it is that I wanted to have a program written in standard C99 to do the 
nuts and bolts things that C has to do to be the lower-level syntax of 
choice.


>>>> /**
>>>>  * 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.
>>
>> Wouldn't that be covered by calling strlen on the target buffer?
>
> Oh sure, but since this function calculates the length of the token as
> it goes, I'd be tempted to return it.  0 still means no token and != 0
> still means OK, but the caller can immediately look at tok[len-1] when
> that's useful.  Not a big deal but I dislike throwing information away.
>
> <snip>
>>>>       if ( charsRead < *bufsize || expand( buf, bufsize ) )
>>>
>>> I think you need <= here or you can overflow the buffer when it gets
>>> zero terminated.
>>
>> Hmm. Acutally, I think it needs to be "charsRead < *bufsize - 1" (or
>> "charsRead + 1 < *bufsize").  Oh well.
>
> Yes, I didn't think enough about the correction.  I think I had a
> similar bug recently where the fix was to include equality and I just
> trotted that out.
>
> <snip>
>>> 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.
>>
>> OTOH, setting the initial buffer size to something that handles most input
>> cases means you don't have to worry about reallocating most of the
>> time.
>
> I don't get that.  You have to allocate at least once no matter how you
> do it.  My preference is to keep all the allocation in one place.  The
> cost is either a test or an addition that is not strictly needed on
> every allocation.
>
> <snip>
>>>>     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);
>>
>> Doesn't fputs automatically append a newline?
>
> No, that's just puts.  I suspect a lot of people are put off using fputs
> because of this historical hang-over.
>
> <snip>
>

My best effort to edit the source from this thread together is sub5.c . 
I'll see how this looks on my newly-minted mint partition. That would be 
its first port.

--
fred

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