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 | |
18de460b |
34 | ClassImp(AliFstream) |
35 | |
36 | //______________________________________________________________________________ |
37 | AliFstream::AliFstream(): |
38 | fFile(0x0), |
39 | fBuffer(0x0), |
e3adf4cb |
40 | fBufferSize(0) |
18de460b |
41 | { |
42 | // Default constructor |
18de460b |
43 | } |
44 | |
45 | //______________________________________________________________________________ |
46 | AliFstream::AliFstream(const char *fileName): |
47 | fFile(0x0), |
48 | fBuffer(0x0), |
e3adf4cb |
49 | fBufferSize(0) |
18de460b |
50 | { |
51 | // Constructor |
52 | // Takes the input filename and |
53 | // opens the output stream |
54 | |
55 | #ifndef __DECCXX |
56 | fFile = new fstream(fileName, ios::binary|ios::out); |
57 | #else |
58 | fFile = new fstream(fileName, ios::out); |
59 | #endif |
18de460b |
60 | } |
61 | |
62 | //______________________________________________________________________________ |
18de460b |
63 | AliFstream::~AliFstream() |
64 | { |
65 | // Destructor |
66 | // |
67 | if (fFile) { |
68 | fFile->close(); |
69 | delete fFile; |
70 | } |
71 | if (fBuffer) delete [] fBuffer; |
72 | } |
73 | |
74 | //______________________________________________________________________________ |
41008432 |
75 | void AliFstream::Seekp(UInt_t position) |
18de460b |
76 | { |
77 | // Go to a given position |
78 | // inside the output stream |
41008432 |
79 | if (fFile) fFile->seekp(position); |
18de460b |
80 | } |
81 | |
82 | //______________________________________________________________________________ |
41008432 |
83 | UInt_t AliFstream::Tellp() |
18de460b |
84 | { |
85 | // Return the current |
86 | // position inside the |
87 | // output stream |
41008432 |
88 | if (fFile) return fFile->tellp(); |
18de460b |
89 | else return 0; |
90 | } |
91 | |
92 | //______________________________________________________________________________ |
e3adf4cb |
93 | UInt_t AliFstream::Swap(UInt_t x) |
94 | { |
95 | // Swap the endianess of the integer value 'x' |
96 | |
97 | return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) | |
98 | ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24)); |
99 | } |
100 | |
101 | //______________________________________________________________________________ |
18de460b |
102 | void AliFstream::WriteBuffer(const char *buffer, UInt_t size, Bool_t force) |
103 | { |
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. |
41008432 |
111 | |
112 | // The raw data payload size is always |
113 | // 4 bytes aligned |
e3adf4cb |
114 | |
115 | if ((size % sizeof(UInt_t)) != 0) |
41008432 |
116 | AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size)); |
e3adf4cb |
117 | |
18de460b |
118 | if (force) { |
119 | fFile->write(buffer,size); |
120 | } |
121 | else { |
e3adf4cb |
122 | #ifdef R__BYTESWAP |
123 | fFile->write(buffer,size); |
124 | #else |
125 | size /= sizeof(UInt_t); |
126 | |
127 | if (size > fBufferSize) { |
128 | if (fBuffer) delete [] fBuffer; |
129 | fBuffer = new UInt_t[size]; |
130 | fBufferSize = size; |
18de460b |
131 | } |
e3adf4cb |
132 | |
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)); |
18de460b |
137 | } |
e3adf4cb |
138 | |
139 | fFile->write((const char *)fBuffer,size*sizeof(UInt_t)); |
140 | #endif |
18de460b |
141 | } |
142 | } |