ROOT logo
/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

//-----------------------------------------------------------------
// This is the class which is to be used during the writing of
// simulated raw data (DDL files format).
// It is using the root functionality in order to deal correctly
// with little/big endian issue. By convention the detector raw
// data payload is stored always with little endian (this corresponds
// to the real life situation when the detector data is coming from
// the hardware). The implementation of this class is based on Root
// tobuf() method defined in Bytes.h
//-------------------------------------------------------------------------

#include <unistd.h>
#include <Riostream.h>
#include <stdio.h>
#include "AliFstream.h"
#include "AliLog.h"


using std::ios;
ClassImp(AliFstream)

//______________________________________________________________________________
AliFstream::AliFstream():
  fFile(0x0),
  fBuffer(0x0),
  fBufferSize(0)
{
  // Default constructor
}

//______________________________________________________________________________
AliFstream::AliFstream(const char *fileName):
  fFile(0x0),
  fBuffer(0x0),
  fBufferSize(0)
{
  // Constructor
  // Takes the input filename and
  // opens the output stream

#ifndef __DECCXX
  fFile = new fstream(fileName, ios::binary|ios::out);
#else
  fFile = new fstream(fileName, ios::out);
#endif
}

//______________________________________________________________________________
AliFstream::~AliFstream()
{
  // Destructor
  //
  if (fFile) {
    fFile->close();
    delete fFile;
  }
  if (fBuffer) delete [] fBuffer;
}

//______________________________________________________________________________
void AliFstream::Seekp(UInt_t position)
{
  // Go to a given position
  // inside the output stream
  if (fFile) fFile->seekp(position);
}

//______________________________________________________________________________
UInt_t AliFstream::Tellp()
{
  // Return the current
  // position inside the
  // output stream
  if (fFile) return fFile->tellp();
  else return 0;
}

//______________________________________________________________________________
UInt_t AliFstream::Swap(UInt_t x)
{
   // Swap the endianess of the integer value 'x'

   return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
           ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24));
}

//______________________________________________________________________________
void AliFstream::WriteBuffer(const char *buffer, UInt_t size, Bool_t force)
{
  // Write the buffer to a file
  // In case the user gives a 'force'
  // flag then the buffer is written
  // as it is. Otherwise, we check the
  // endianess and swap the buffer data
  // so that it is always stored using
  // little endian format.

  // The raw data payload size is always
  // 4 bytes aligned
  
  if ((size % sizeof(UInt_t)) != 0)
    AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size));
  
  if (force) {
    fFile->write(buffer,size);
  }
  else {
#ifdef R__BYTESWAP
    fFile->write(buffer,size);
#else
    size /= sizeof(UInt_t);

    if (size > fBufferSize) {
      if (fBuffer) delete [] fBuffer;
      fBuffer = new UInt_t[size];
      fBufferSize = size;
    }

    UInt_t *buf = (UInt_t *)buffer;
    for (UInt_t i = 0; i < size; i++, buf++) {
      UInt_t value = Swap(*buf);
      memcpy(fBuffer+i, &value, sizeof(UInt_t));
    }

    fFile->write((const char *)fBuffer,size*sizeof(UInt_t));
#endif
  }
}
 AliFstream.cxx:1
 AliFstream.cxx:2
 AliFstream.cxx:3
 AliFstream.cxx:4
 AliFstream.cxx:5
 AliFstream.cxx:6
 AliFstream.cxx:7
 AliFstream.cxx:8
 AliFstream.cxx:9
 AliFstream.cxx:10
 AliFstream.cxx:11
 AliFstream.cxx:12
 AliFstream.cxx:13
 AliFstream.cxx:14
 AliFstream.cxx:15
 AliFstream.cxx:16
 AliFstream.cxx:17
 AliFstream.cxx:18
 AliFstream.cxx:19
 AliFstream.cxx:20
 AliFstream.cxx:21
 AliFstream.cxx:22
 AliFstream.cxx:23
 AliFstream.cxx:24
 AliFstream.cxx:25
 AliFstream.cxx:26
 AliFstream.cxx:27
 AliFstream.cxx:28
 AliFstream.cxx:29
 AliFstream.cxx:30
 AliFstream.cxx:31
 AliFstream.cxx:32
 AliFstream.cxx:33
 AliFstream.cxx:34
 AliFstream.cxx:35
 AliFstream.cxx:36
 AliFstream.cxx:37
 AliFstream.cxx:38
 AliFstream.cxx:39
 AliFstream.cxx:40
 AliFstream.cxx:41
 AliFstream.cxx:42
 AliFstream.cxx:43
 AliFstream.cxx:44
 AliFstream.cxx:45
 AliFstream.cxx:46
 AliFstream.cxx:47
 AliFstream.cxx:48
 AliFstream.cxx:49
 AliFstream.cxx:50
 AliFstream.cxx:51
 AliFstream.cxx:52
 AliFstream.cxx:53
 AliFstream.cxx:54
 AliFstream.cxx:55
 AliFstream.cxx:56
 AliFstream.cxx:57
 AliFstream.cxx:58
 AliFstream.cxx:59
 AliFstream.cxx:60
 AliFstream.cxx:61
 AliFstream.cxx:62
 AliFstream.cxx:63
 AliFstream.cxx:64
 AliFstream.cxx:65
 AliFstream.cxx:66
 AliFstream.cxx:67
 AliFstream.cxx:68
 AliFstream.cxx:69
 AliFstream.cxx:70
 AliFstream.cxx:71
 AliFstream.cxx:72
 AliFstream.cxx:73
 AliFstream.cxx:74
 AliFstream.cxx:75
 AliFstream.cxx:76
 AliFstream.cxx:77
 AliFstream.cxx:78
 AliFstream.cxx:79
 AliFstream.cxx:80
 AliFstream.cxx:81
 AliFstream.cxx:82
 AliFstream.cxx:83
 AliFstream.cxx:84
 AliFstream.cxx:85
 AliFstream.cxx:86
 AliFstream.cxx:87
 AliFstream.cxx:88
 AliFstream.cxx:89
 AliFstream.cxx:90
 AliFstream.cxx:91
 AliFstream.cxx:92
 AliFstream.cxx:93
 AliFstream.cxx:94
 AliFstream.cxx:95
 AliFstream.cxx:96
 AliFstream.cxx:97
 AliFstream.cxx:98
 AliFstream.cxx:99
 AliFstream.cxx:100
 AliFstream.cxx:101
 AliFstream.cxx:102
 AliFstream.cxx:103
 AliFstream.cxx:104
 AliFstream.cxx:105
 AliFstream.cxx:106
 AliFstream.cxx:107
 AliFstream.cxx:108
 AliFstream.cxx:109
 AliFstream.cxx:110
 AliFstream.cxx:111
 AliFstream.cxx:112
 AliFstream.cxx:113
 AliFstream.cxx:114
 AliFstream.cxx:115
 AliFstream.cxx:116
 AliFstream.cxx:117
 AliFstream.cxx:118
 AliFstream.cxx:119
 AliFstream.cxx:120
 AliFstream.cxx:121
 AliFstream.cxx:122
 AliFstream.cxx:123
 AliFstream.cxx:124
 AliFstream.cxx:125
 AliFstream.cxx:126
 AliFstream.cxx:127
 AliFstream.cxx:128
 AliFstream.cxx:129
 AliFstream.cxx:130
 AliFstream.cxx:131
 AliFstream.cxx:132
 AliFstream.cxx:133
 AliFstream.cxx:134
 AliFstream.cxx:135
 AliFstream.cxx:136
 AliFstream.cxx:137
 AliFstream.cxx:138
 AliFstream.cxx:139
 AliFstream.cxx:140
 AliFstream.cxx:141
 AliFstream.cxx:142
 AliFstream.cxx:143