]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliFstream.cxx
CMake: STEER, adding AliConst.h to the installed headers
[u/mrichter/AliRoot.git] / STEER / STEER / AliFstream.cxx
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>
29 #include <stdio.h>
30 #include "AliFstream.h"
31 #include "AliLog.h"
32
33
34 using std::ios;
35 ClassImp(AliFstream)
36
37 //______________________________________________________________________________
38 AliFstream::AliFstream():
39   fFile(0x0),
40   fBuffer(0x0),
41   fBufferSize(0)
42 {
43   // Default constructor
44 }
45
46 //______________________________________________________________________________
47 AliFstream::AliFstream(const char *fileName):
48   fFile(0x0),
49   fBuffer(0x0),
50   fBufferSize(0)
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
61 }
62
63 //______________________________________________________________________________
64 AliFstream::~AliFstream()
65 {
66   // Destructor
67   //
68   if (fFile) {
69     fFile->close();
70     delete fFile;
71   }
72   if (fBuffer) delete [] fBuffer;
73 }
74
75 //______________________________________________________________________________
76 void AliFstream::Seekp(UInt_t position)
77 {
78   // Go to a given position
79   // inside the output stream
80   if (fFile) fFile->seekp(position);
81 }
82
83 //______________________________________________________________________________
84 UInt_t AliFstream::Tellp()
85 {
86   // Return the current
87   // position inside the
88   // output stream
89   if (fFile) return fFile->tellp();
90   else return 0;
91 }
92
93 //______________________________________________________________________________
94 UInt_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 //______________________________________________________________________________
103 void 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.
112
113   // The raw data payload size is always
114   // 4 bytes aligned
115   
116   if ((size % sizeof(UInt_t)) != 0)
117     AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size));
118   
119   if (force) {
120     fFile->write(buffer,size);
121   }
122   else {
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;
132     }
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));
138     }
139
140     fFile->write((const char *)fBuffer,size*sizeof(UInt_t));
141 #endif
142   }
143 }