Event server (Mihai)
[u/mrichter/AliRoot.git] / STEER / STEER / AliFstream.cxx
CommitLineData
18de460b 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
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//-------------------------------------------------------------------------
26
27#include <unistd.h>
28#include <Riostream.h>
e3adf4cb 29#include <stdio.h>
18de460b 30#include "AliFstream.h"
31#include "AliLog.h"
32
e3adf4cb 33
66b0310c 34using std::ios;
18de460b 35ClassImp(AliFstream)
36
37//______________________________________________________________________________
38AliFstream::AliFstream():
39 fFile(0x0),
40 fBuffer(0x0),
e3adf4cb 41 fBufferSize(0)
18de460b 42{
43 // Default constructor
18de460b 44}
45
46//______________________________________________________________________________
47AliFstream::AliFstream(const char *fileName):
48 fFile(0x0),
49 fBuffer(0x0),
e3adf4cb 50 fBufferSize(0)
18de460b 51{
52 // Constructor
53 // Takes the input filename and
54 // opens the output stream
55
56#ifndef __DECCXX
57 fFile = new fstream(fileName, ios::binary|ios::out);
58#else
59 fFile = new fstream(fileName, ios::out);
60#endif
18de460b 61}
62
63//______________________________________________________________________________
18de460b 64AliFstream::~AliFstream()
65{
66 // Destructor
67 //
68 if (fFile) {
69 fFile->close();
70 delete fFile;
71 }
72 if (fBuffer) delete [] fBuffer;
73}
74
75//______________________________________________________________________________
41008432 76void AliFstream::Seekp(UInt_t position)
18de460b 77{
78 // Go to a given position
79 // inside the output stream
41008432 80 if (fFile) fFile->seekp(position);
18de460b 81}
82
83//______________________________________________________________________________
41008432 84UInt_t AliFstream::Tellp()
18de460b 85{
86 // Return the current
87 // position inside the
88 // output stream
41008432 89 if (fFile) return fFile->tellp();
18de460b 90 else return 0;
91}
92
93//______________________________________________________________________________
e3adf4cb 94UInt_t AliFstream::Swap(UInt_t x)
95{
96 // Swap the endianess of the integer value 'x'
97
98 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
99 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
100}
101
102//______________________________________________________________________________
18de460b 103void AliFstream::WriteBuffer(const char *buffer, UInt_t size, Bool_t force)
104{
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.
41008432 112
113 // The raw data payload size is always
114 // 4 bytes aligned
e3adf4cb 115
116 if ((size % sizeof(UInt_t)) != 0)
41008432 117 AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size));
e3adf4cb 118
18de460b 119 if (force) {
120 fFile->write(buffer,size);
121 }
122 else {
e3adf4cb 123#ifdef R__BYTESWAP
124 fFile->write(buffer,size);
125#else
126 size /= sizeof(UInt_t);
127
128 if (size > fBufferSize) {
129 if (fBuffer) delete [] fBuffer;
130 fBuffer = new UInt_t[size];
131 fBufferSize = size;
18de460b 132 }
e3adf4cb 133
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));
18de460b 138 }
e3adf4cb 139
140 fFile->write((const char *)fBuffer,size*sizeof(UInt_t));
141#endif
18de460b 142 }
143}