Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:46

0001 
0002 #include "olzoBuffer.h"
0003 #include "BufferConstants.h"
0004 
0005 #include <lzo/lzoutil.h>
0006 #include <cstring>
0007 #include <stdlib.h>
0008 #include <unistd.h>
0009 
0010 int olzoBuffer::lzo_initialized = 0;
0011 
0012 
0013 // the constructor first ----------------
0014 #ifndef WIN32
0015 olzoBuffer::olzoBuffer (int fdin, PHDWORD * where, 
0016               const int length, 
0017               const int irun, 
0018               const int iseq): 
0019   ophBuffer(fdin,where,length,irun,iseq)
0020 #else
0021 olzoBuffer::olzoBuffer (const char *fpp, PHDWORD * where, 
0022               const int length, 
0023                       int &status,
0024               const int irun, 
0025               const int iseq): 
0026   ophBuffer(fpp,where,length,status,irun,iseq)
0027 #endif
0028 {
0029   // get a buffer for zlib
0030 
0031   _broken = 0;
0032 
0033   if ( !  lzo_initialized )
0034     {
0035       if (lzo_init() != LZO_E_OK)
0036     {
0037       COUT << "Could not initialize LZO" << std::endl;
0038       _broken = 1;
0039     }
0040       
0041       lzo_initialized = 1;
0042     }
0043 
0044 
0045   wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_12_MEM_COMPRESS);
0046   if (wrkmem)
0047     {
0048       memset(wrkmem, 0, LZO1X_1_12_MEM_COMPRESS);
0049     }
0050   outputarraylength = (int)(length *1.1) + 2048;
0051   outputarray = new PHDWORD[outputarraylength];
0052 
0053 }
0054 
0055 // ----------------------------------------------------------
0056 // returns the number of bytes written, including record wasted space.
0057 //
0058 int olzoBuffer::writeout()
0059 {
0060 
0061 
0062   if (! dirty) return 0;
0063 
0064   if (! has_end) addEoB();
0065 
0066   lzo_uint outputlength_in_bytes = outputarraylength*4-16;
0067   lzo_uint in_len = bptr->Length; 
0068 
0069   lzo1x_1_12_compress( (lzo_byte *) bptr,
0070             in_len,  
0071                (lzo_byte *)&outputarray[4],
0072             &outputlength_in_bytes,wrkmem);
0073 
0074 
0075   outputarray[0] = outputlength_in_bytes +4*BUFFERHEADERLENGTH;
0076   outputarray[1] =  LZO1XBUFFERMARKER;
0077   outputarray[2] = bptr->Bufseq;
0078   outputarray[3] = bptr->Length;
0079 
0080   unsigned int ip =0;
0081   char *cp = (char *) outputarray;
0082 
0083   while (ip<outputarray[0])
0084     {
0085       int n = write ( fd, cp, BUFFERBLOCKSIZE);
0086       if ( n != BUFFERBLOCKSIZE)
0087         {
0088           std::cout << " could not write output, bytes written: " << n << std::endl;
0089           return 0;
0090         }
0091 
0092       cp += BUFFERBLOCKSIZE;
0093       ip += BUFFERBLOCKSIZE;
0094     }
0095   dirty = 0;
0096   byteswritten += ip;
0097   return 0;
0098 }
0099 
0100 
0101 // ----------------------------------------------------------
0102 olzoBuffer::~olzoBuffer()
0103 {
0104   writeout();
0105   delete [] outputarray;
0106   lzo_free(wrkmem);
0107 
0108 }
0109