/* vim: set syntax=objc ts=4 sw=4 expandtab : */

/* main.m
 *
 * Demonstration entry point.
 */

#include <stdlib.h>

#import "CBuffer.h"
#import "SimpleString.h"

void put( CBuffer *buf, char *cstr )
{
   SimpleString *string = [SimpleString new: cstr];
   if ( [buf put: string] )
   { 
      fprintf( stdout, "Put '%s' successfully!\n", cstr );
   } 
   else
   {
      fprintf( stdout, "Failed to put '%s'!\n", cstr );
      [string free];
   }
}

void pull( CBuffer *buf ) {
   SimpleString *string = [buf get];
   if ( string == nil )
   {
      fprintf( stdout, "Get failed!\n" );
   }
   else
   {
      fprintf( stdout, "Got '%s'\n", [string cstring] );
      [string free];
   }
}

int main( int argc, char **argv )
{
   /* demonstration code here */
   /* NOTE: need to work in allocation pools etc. */

   CBuffer *cbuffer;
   int i;

   cbuffer = [CBuffer new: 8];
   fprintf( stdout, "Can take %d puts.\n", [cbuffer available] );

   for ( i = 0; i < argc; i++ )
   {
      switch (argv[i][0])
      {
          case '+':
             put( cbuffer, &argv[i][1] );
             break;
          case '-':
             pull( cbuffer );
             break;
          case '?':
             fprintf( stdout, "Have %d available.\n", [cbuffer available] );
             break;
          case '!':
             [cbuffer dump];
             break;
      }
   } 

   [cbuffer dump];

   [cbuffer free];

   return EXIT_FAILURE;
}
