[DGD] buffer maintenance

John Ruiz jruiz at johnruiz.com
Sun Sep 19 17:56:35 CEST 2004


That's exactly what I'm looking for!

On reflection, I was rather retarded in my use of sscanf. ;-)  Ah, the
dangers of early morning code...

Thanks,
John

On Sun, 2004-09-19 at 10:28, Par Winzell wrote:
> > void trimBuffer()
> > {
> >     int i;
> > 
> >     // trim until all complete packets are removed.
> >     while(  sscanf( inBuffer, "})\000" )  !=  0  )
> >     {
> >         for( i=0; i<strlen(inBuffer)-3; i++ )
> >         {
> >             // do we have a complete packet at "i"?
> >             if( inBuffer[i..i+2] == "})\000" )
> >             {
> >                 // remove everything before and including "\000"
> >                 inBuffer = inBuffer[i+2..strlen(inBuffer)-1];
> > 
> >                 // we've messed with inBuffer!
> >                 break;
> >             }
> >         }
> >     }
> > }
> 
> 
> This is a strange use of sscanf(). Why not do simply,
> 
>    string packet, remainder;
> 
>    while (sscanf(inBuffer, "%s})\000%s", packet, remainder) == 2) {
>      inBuffer = remainder;
>      messWithPacket(packet);
>    }
> 
> That's plenty efficient. The only inefficiency would be if you are using 
> a complex parser anyway for the packet-messing itself, you could turn it 
> all into a parse_string() application, and include the packet delimiter 
> handling there.
> 
> Zell
> _________________________________________________________________
> List config page:  http://list.imaginary.com/mailman/listinfo/dgd

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list