1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 //-----------------------------------------------------------------
17 // This is the class which is to be used during the writing of
18 // simulated raw data (DDL files format).
19 // It is using the root functionality in order to deal correctly
20 // with little/big endian issue. By convention the detector raw
21 // data payload is stored always with little endian (this corresponds
22 // to the real life situation when the detector data is coming from
23 // the hardware). The implementation of this class is based on Root
24 // tobuf() method defined in Bytes.h
25 //-------------------------------------------------------------------------
28 #include <Riostream.h>
30 #include "AliFstream.h"
36 //______________________________________________________________________________
37 AliFstream::AliFstream():
42 // Default constructor
45 //______________________________________________________________________________
46 AliFstream::AliFstream(const char *fileName):
52 // Takes the input filename and
53 // opens the output stream
56 fFile = new fstream(fileName, ios::binary|ios::out);
58 fFile = new fstream(fileName, ios::out);
62 //______________________________________________________________________________
63 AliFstream::~AliFstream()
71 if (fBuffer) delete [] fBuffer;
74 //______________________________________________________________________________
75 void AliFstream::Seekp(UInt_t position)
77 // Go to a given position
78 // inside the output stream
79 if (fFile) fFile->seekp(position);
82 //______________________________________________________________________________
83 UInt_t AliFstream::Tellp()
86 // position inside the
88 if (fFile) return fFile->tellp();
92 //______________________________________________________________________________
93 UInt_t AliFstream::Swap(UInt_t x)
95 // Swap the endianess of the integer value 'x'
97 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
98 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
101 //______________________________________________________________________________
102 void AliFstream::WriteBuffer(const char *buffer, UInt_t size, Bool_t force)
104 // Write the buffer to a file
105 // In case the user gives a 'force'
106 // flag then the buffer is written
107 // as it is. Otherwise, we check the
108 // endianess and swap the buffer data
109 // so that it is always stored using
110 // little endian format.
112 // The raw data payload size is always
115 if ((size % sizeof(UInt_t)) != 0)
116 AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size));
119 fFile->write(buffer,size);
123 fFile->write(buffer,size);
125 size /= sizeof(UInt_t);
127 if (size > fBufferSize) {
128 if (fBuffer) delete [] fBuffer;
129 fBuffer = new UInt_t[size];
133 UInt_t *buf = (UInt_t *)buffer;
134 for (UInt_t i = 0; i < size; i++, buf++) {
135 UInt_t value = Swap(*buf);
136 memcpy(fBuffer+i, &value, sizeof(UInt_t));
139 fFile->write((const char *)fBuffer,size*sizeof(UInt_t));