home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STK_BLOB.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  81 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* =======================================================================
  4.     STK_BLOB.c      Stack of Blobs. Binary Large OBjects.
  5.  
  6.                     A.Reitsma, Delft, The Netherlands.
  7.                     v0.10  94-07-03  Public Domain.
  8.  
  9.     Stack for variable sized items. Implemented by making a stack of
  10.     'Blob Descriptors' and duplicating the item in malloc'ed memory.
  11.     Localizing the stack number storage required interface functions
  12.     to the stack module.
  13.     The Pop function has two 'modes': one just returns the size of the
  14.     blob; the other actually pops the blob.
  15.     WARNING: This version is not really tested!
  16. ----------------------------------------------------------------------- */
  17.  
  18. #include <string.h>
  19. #include "stk_defs.h"
  20. #include "stack.h"
  21. #include "stk_blob.h"
  22.  
  23. #define ERR_MEMORY   -1
  24. #define NO_PROBLEMS  0
  25. #define Error(x)     (x)
  26.  
  27. struct BlobDesc
  28. {
  29.     void * data;
  30.     int size;
  31. };
  32.  
  33. int StackBlobCreate( void )
  34. {
  35.     return StackCreate( sizeof( struct BlobDesc ));
  36. }
  37.  
  38. int PushBlob( int Stack, void * BlobSource, unsigned int BlobSize )
  39. {
  40.     struct BlobDesc Blob;
  41.  
  42.     Blob.size = BlobSize;
  43.  
  44.     /* get storage space for the blob
  45.     */
  46.     Blob.data = MALLOC( BlobSize, char );
  47.     if( NULL == Blob.data )
  48.     {
  49.         return ERR_MEMORY ;
  50.     }
  51.  
  52.     memcpy( Blob.data, BlobSource, BlobSize ); /* duplicate the blob */
  53.  
  54.     if( ERR_MEMORY == Push( Stack, & Blob )) /* push descriptor */
  55.     {
  56.         FREE( Blob.data ); /* no need to cause memory leaks ... */
  57.         return ERR_MEMORY ;
  58.     }
  59.  
  60.     return NO_PROBLEMS ;
  61. }
  62.  
  63. unsigned int PopBlob( int Stack, void * BlobDestination )
  64. {
  65.     struct BlobDesc Blob ;
  66.  
  67.     Pop( Stack, & Blob );
  68.  
  69.     if( NULL == BlobDestination )
  70.         StackUnpop( Stack );
  71.     else
  72.     {
  73.         memcpy( BlobDestination, Blob.data, Blob.size );
  74.         FREE( Blob.data );
  75.     }
  76.  
  77.     return Blob.size ;
  78. }
  79.  
  80. /* ==== STK_BLOB.c end ================================================ */
  81.