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"
37 //______________________________________________________________________________
38 AliFstream::AliFstream():
43 // Default constructor
46 //______________________________________________________________________________
47 AliFstream::AliFstream(const char *fileName):
53 // Takes the input filename and
54 // opens the output stream
57 fFile = new fstream(fileName, ios::binary|ios::out);
59 fFile = new fstream(fileName, ios::out);
63 //______________________________________________________________________________
64 AliFstream::~AliFstream()
72 if (fBuffer) delete [] fBuffer;
75 //______________________________________________________________________________
76 void AliFstream::Seekp(UInt_t position)
78 // Go to a given position
79 // inside the output stream
80 if (fFile) fFile->seekp(position);
83 //______________________________________________________________________________
84 UInt_t AliFstream::Tellp()
87 // position inside the
89 if (fFile) return fFile->tellp();
93 //______________________________________________________________________________
94 UInt_t AliFstream::Swap(UInt_t x)
96 // Swap the endianess of the integer value 'x'
98 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
99 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
102 //______________________________________________________________________________
103 void AliFstream::WriteBuffer(const char *buffer, UInt_t size, Bool_t force)
105 // Write the buffer to a file
106 // In case the user gives a 'force'
107 // flag then the buffer is written
108 // as it is. Otherwise, we check the
109 // endianess and swap the buffer data
110 // so that it is always stored using
111 // little endian format.
113 // The raw data payload size is always
116 if ((size % sizeof(UInt_t)) != 0)
117 AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size));
120 fFile->write(buffer,size);
124 fFile->write(buffer,size);
126 size /= sizeof(UInt_t);
128 if (size > fBufferSize) {
129 if (fBuffer) delete [] fBuffer;
130 fBuffer = new UInt_t[size];
134 UInt_t *buf = (UInt_t *)buffer;
135 for (UInt_t i = 0; i < size; i++, buf++) {
136 UInt_t value = Swap(*buf);
137 memcpy(fBuffer+i, &value, sizeof(UInt_t));
140 fFile->write((const char *)fBuffer,size*sizeof(UInt_t));