[DGD] buffer maintenance

Par Winzell zell at skotos.net
Sun Sep 19 16:28:56 CEST 2004


> 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



More information about the DGD mailing list