evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
evio::Util Class Reference

Class containing static methods of general purpose. More...

#include <Util.h>

Static Public Member Functions

static uint32_t getWords (uint32_t length)
 Returns length padded to 4-byte boundary for given length in bytes. More...
 
static uint32_t getPadding (uint32_t length)
 Returns number of bytes needed to pad to 4-byte boundary for the given length. More...
 
static bool iStrEquals (const std::string &a, const std::string &b)
 Case insensitive compare for 2 strings. More...
 
static const DataTypegetDataType (const std::string &type)
 This method returns an XML element name given an evio data type. More...
 
static void toIntArray (char const *data, uint32_t dataLen, const ByteOrder &byteOrder, uint32_t *dest)
 Turn byte array into an int array. More...
 
static uint32_t toInt (char b1, char b2, char b3, char b4, const ByteOrder &byteOrder)
 Turn 4 bytes into an unsigned 32 bit int. More...
 
static uint32_t toInt (uint8_t const *data, ByteOrder const &byteOrder)
 Turn 4 bytes into an unsigned 32 bit int. More...
 
static uint64_t toLong (uint8_t const *data, ByteOrder const &byteOrder)
 Turn 4 bytes into an unsigned 32 bit int. More...
 
static void toBytes (uint32_t data, const ByteOrder &byteOrder, uint8_t *dest)
 Write int into byte array. More...
 
static void toBytes (uint64_t data, const ByteOrder &byteOrder, uint8_t *dest)
 Turn long into byte array. More...
 
static void toBytes (uint32_t data, const ByteOrder &byteOrder, std::vector< uint8_t > &dest, size_t off)
 Write int into byte vector. More...
 
static void toBytes (uint16_t data, const ByteOrder &byteOrder, uint8_t *dest)
 Write short into byte array. More...
 
static void toBytes (uint16_t data, const ByteOrder &byteOrder, std::vector< uint8_t > &dest, size_t off)
 Write short into byte vector. More...
 
static const std::string & NO_NAME_STRING ()
 Get a string used to indicate that no name can be determined. More...
 
static uint32_t findEvioVersion (ByteBuffer &bb, size_t initialPos)
 Reads a couple things in a block/record header in order to determine the evio version and endianness of a buffer/file. More...
 
static void printBytes (const std::shared_ptr< ByteBuffer > buf, uint32_t position, uint32_t bytes, const std::string &label)
 This method takes a byte buffer and prints out the desired number of bytes from the given position, in hex. More...
 
static void printBytes (const ByteBuffer &buf, uint32_t position, uint32_t bytes, const std::string &label)
 This method takes a byte buffer and prints out the desired number of bytes from the given position, in hex. More...
 
static void printBytes (uint8_t const *data, uint32_t bytes, const std::string &label)
 This method takes a pointer and prints out the desired number of bytes from the given position, in hex. More...
 
static void printBytes (const std::string &fileName, uint64_t offset, uint32_t bytes, const std::string &label)
 This method takes a file and prints out the desired number of bytes from the given offset, in hex. More...
 
static void writeBytes (const std::string &fileName, ByteBuffer &buf)
 This method takes a ByteBuffer and writes its data to a file. More...
 
static void readBytes (const std::string &fileName, ByteBuffer &buf)
 This method reads part of a file into a ByteBuffer. More...
 
static int powerOfTwo (int x, bool roundUp)
 Return the power of 2 closest to the given argument. More...
 
static void stringToASCII (const std::string &input, std::vector< uint8_t > &array)
 Return an input string as ASCII in which each character is one byte. More...
 
static void stringToASCII (const std::string &input, ByteBuffer &buf)
 Return an input string as ASCII in which each character is one byte. More...
 
static uint32_t stringsToRawSize (std::vector< std::string > const &strings)
 This method returns the number of bytes in a raw evio format of the given string array, not including header. More...
 
static uint32_t stringToRawSize (const std::string &str)
 This method returns the number of bytes in a raw evio format of the given string array (with a single string), not including header. More...
 
static void stringsToRawBytes (std::vector< std::string > &strings, std::vector< uint8_t > &bytes)
 This method transforms an array/vector of strings into raw evio format data, not including header. More...
 
static void unpackRawBytesToStrings (std::vector< uint8_t > &bytes, size_t offset, std::vector< std::string > &strData)
 This method extracts an array of strings from byte array of raw evio string data. More...
 
static void unpackRawBytesToStrings (std::vector< uint8_t > &bytes, size_t offset, size_t maxLength, std::vector< std::string > &strData)
 This method extracts an array of strings from byte array of raw evio string data. More...
 
static void unpackRawBytesToStrings (uint8_t *bytes, size_t length, std::vector< std::string > &strData)
 This method extracts an array of strings from byte array of raw evio string data. More...
 
static void unpackRawBytesToStrings (ByteBuffer &buffer, size_t pos, size_t length, std::vector< std::string > &strData)
 This method extracts an array of strings from buffer containing raw evio string data. More...
 
static void stringBuilderToStrings (std::string const &strData, bool onlyGoodChars, std::vector< std::string > &strings)
 This method extracts an array of strings from a string containing evio string data. More...
 
static void expandEnvironmentalVariables (std::string &text)
 Substitute environmental variables in a given string when they come in the form, . More...
 
static uint32_t countAndFixIntSpecifiers (std::string &text)
 Count the number of integer specifiers (e.g. More...
 
static int generateBaseFileName (const std::string &baseName, const std::string &runType, std::string &modifiedBaseName)
 This method generates part of a file name given a base file name as an argument. More...
 
static std::string generateFileName (std::string fileName, uint32_t specifierCount, uint32_t runNumber, uint64_t split, uint32_t splitNumber, uint32_t streamId, uint32_t streamCount)
 This method does NOT work on its own. More...
 

Detailed Description

Class containing static methods of general purpose.

Version
6.0
Since
6.0 11/2/19
Author
timmer

Member Function Documentation

static uint32_t evio::Util::countAndFixIntSpecifiers ( std::string &  text)
inlinestatic

Count the number of integer specifiers (e.g.

%34d, %07x) in the given string, making sure any number preceding "x" or "d" starts with a 0. This is done so there will be no empty spaces in the resulting string (i.e. file name) when final substitutions are made for these specifiers.

Parameters
textstring to analyze.
Returns
number of integer specifiers.

Referenced by generateBaseFileName().

static void evio::Util::expandEnvironmentalVariables ( std::string &  text)
inlinestatic

Substitute environmental variables in a given string when they come in the form, .

Parameters
textstring to analyze.

Referenced by generateBaseFileName().

static uint32_t evio::Util::findEvioVersion ( ByteBuffer bb,
size_t  initialPos 
)
inlinestatic

Reads a couple things in a block/record header in order to determine the evio version and endianness of a buffer/file.

The endianness can be read from the given ByteBuffer by calling, bb.order(). This does not change any parameters of the given buffer.

Parameters
bbByteBuffer to read from.
initialPosposition in bb to start reading.
Returns
evio version.
Exceptions
underflow_errorif not enough data in buffer.
EvioExceptionbad magic number in header.

References evio::IBlockHeader::BIT_INFO_OFFSET, evio::ByteOrder::ENDIAN_BIG, evio::ByteOrder::ENDIAN_LITTLE, evio::ByteBuffer::getInt(), evio::ByteBuffer::getUInt(), evio::ByteBuffer::limit(), evio::IBlockHeader::MAGIC_NUMBER, evio::IBlockHeader::MAGIC_OFFSET, evio::ByteBuffer::order(), and evio::IBlockHeader::VERSION_MASK.

Referenced by evio::EvioCompactReader::EvioCompactReader(), and evio::EvioReader::EvioReader().

static int evio::Util::generateBaseFileName ( const std::string &  baseName,
const std::string &  runType,
std::string &  modifiedBaseName 
)
inlinestatic

This method generates part of a file name given a base file name as an argument.

The base file name may contain up to 3, C-style integer format specifiers (such as %03d, or x). If more than 3 are found, an exception will be thrown. If no "0" precedes any integer between the "%" and the "d" or "x" of the format specifier, it will be added automatically in order to avoid spaces in the returned string. In the generateFileName(std::string, uint32_t, uint32_t, uint64_t, uint32_t, uint32_t, uint32_t) method, the first occurrence will be substituted with the given runNumber value. If the file is being split, the second will be substituted with the split number. If there are multiple streams, the third will be substituted with the stream id.

The base file name may contain characters of the form which will be substituted with the value of the associated environmental variable or a blank string if none is found.

Finally, the base file name may contain occurrences of the string "%s" which will be substituted with the value of the runType arg or nothing if the runType is null.

Parameters
baseNamefile name to start with
runTyperun type/configuration name
modifiedBaseNamefinal generated base file name
Returns
number of C-style int format specifiers found in baseName arg.
Exceptions
EvioExceptionif baseName arg is improperly formatted or blank

References countAndFixIntSpecifiers(), and expandEnvironmentalVariables().

Referenced by evio::EventWriter::EventWriter().

static std::string evio::Util::generateFileName ( std::string  fileName,
uint32_t  specifierCount,
uint32_t  runNumber,
uint64_t  split,
uint32_t  splitNumber,
uint32_t  streamId,
uint32_t  streamCount 
)
inlinestatic

This method does NOT work on its own.

It generates a complete file name from the previously determined baseFileName obtained from calling generateBaseFileName. If evio data is to be split up into multiple files (split > 0), numbers are used to distinguish between the split files with splitNumber. If baseFileName contains C-style int format specifiers (specifierCount > 0), then the first occurrence will be substituted with the given runNumber value. If the file is being split, the second will be substituted with the splitNumber. If there are multiple streams, the third will be substituted with the stream id.

If no specifier for the splitNumber exists, it is tacked onto the end of the file name. If no specifier for the stream id exists, it is tacked onto the end of the file name, after the splitNumber. No run numbers are ever tacked on without a specifier.

For splitting: if there is only 1 stream, no stream ids are used and any third specifier is removed. For non-splitting: if there is only 1 stream, no stream ids are used and any second and third specifiers are removed. For multiple streams, the second specifier is removed and the 3rd substituted with the stream id. For all cases: if there are more than 3 specifiers, NO SUBSTITUTIONS ARE DONE.

Parameters
fileNamefile name to use as a basis for the generated file name
specifierCountnumber of C-style int format specifiers in baseFileName arg
runNumberCODA run number
splitnumber of bytes at which to split off evio file
splitNumbernumber of the split file
streamIdnumber of the stream id
streamCounttotal number of streams
Returns
generated file name
Exceptions
EvioExceptionif the baseFileName arg contains printing format specifiers which are not compatible with integers and interfere with formatting.

Referenced by evio::EventWriter::EventWriter().

static uint32_t evio::Util::getPadding ( uint32_t  length)
inlinestatic

Returns number of bytes needed to pad to 4-byte boundary for the given length.

Parameters
lengthlength in bytes.
Returns
number of bytes needed to pad to 4-byte boundary.

Array to help find number of bytes to pad data.

Referenced by getWords(), and evio::FileHeader::setUserHeaderLength().

static uint32_t evio::Util::getWords ( uint32_t  length)
inlinestatic

Returns length padded to 4-byte boundary for given length in bytes.

Parameters
lengthlength in bytes.
Returns
length in bytes padded to 4-byte boundary.

References getPadding().

Referenced by evio::FileHeader::setUserHeaderLength().

static bool evio::Util::iStrEquals ( const std::string &  a,
const std::string &  b 
)
inlinestatic

Case insensitive compare for 2 strings.

Parameters
afirst string.
bsecond string.
Returns
true if equal, else false.

Referenced by getDataType(), and evio::EvioXMLDictionary::parseXML().

static const std::string& evio::Util::NO_NAME_STRING ( )
inlinestatic

Get a string used to indicate that no name can be determined.

Returns
string used to indicate that no name can be determined.

Referenced by evio::EvioXMLDictionary::NO_NAME_STRING().

static int evio::Util::powerOfTwo ( int  x,
bool  roundUp 
)
inlinestatic

Return the power of 2 closest to the given argument.

Parameters
xvalue to get the power of 2 closest to.
roundUpif true, round up, else down
Returns
-1 if x is negative or the closest power of 2 to value

Referenced by evio::EventWriter::EventWriter().

static void evio::Util::printBytes ( const std::shared_ptr< ByteBuffer buf,
uint32_t  position,
uint32_t  bytes,
const std::string &  label 
)
inlinestatic

This method takes a byte buffer and prints out the desired number of bytes from the given position, in hex.

Prints all bytes.

Parameters
bufbuffer to print out
positionposition of data (bytes) in buffer to start printing
bytesnumber of bytes to print in hex
labela label to print as header

Referenced by printBytes().

static void evio::Util::printBytes ( const ByteBuffer buf,
uint32_t  position,
uint32_t  bytes,
const std::string &  label 
)
inlinestatic

This method takes a byte buffer and prints out the desired number of bytes from the given position, in hex.

Prints all bytes.

Parameters
bufbuffer to print out
positionposition of data (bytes) in buffer to start printing
bytesnumber of bytes to print in hex
labela label to print as header

References evio::ByteBuffer::capacity().

static void evio::Util::printBytes ( uint8_t const *  data,
uint32_t  bytes,
const std::string &  label 
)
inlinestatic

This method takes a pointer and prints out the desired number of bytes from the given position, in hex.

Parameters
datadata to print out
bytesnumber of bytes to print in hex
labela label to print as header
static void evio::Util::printBytes ( const std::string &  fileName,
uint64_t  offset,
uint32_t  bytes,
const std::string &  label 
)
inlinestatic

This method takes a file and prints out the desired number of bytes from the given offset, in hex.

Parameters
fileNamefile to print out
offsetoffset into file to start printing
bytesnumber of bytes to print in hex
labela label to print as header

References evio::ByteBuffer::array(), and printBytes().

static void evio::Util::readBytes ( const std::string &  fileName,
ByteBuffer buf 
)
inlinestatic

This method reads part of a file into a ByteBuffer.

The buffer's position will be at the end of the data that is read. The caller will have to flip the buffer to read it.

Parameters
fileNamefile to read from.
bufbuffer to write into.
Exceptions
EvioExceptionbad file name or unable to do I/O.

References evio::ByteBuffer::array(), evio::ByteBuffer::arrayOffset(), evio::ByteBuffer::limit(), evio::ByteBuffer::position(), and evio::ByteBuffer::remaining().

static void evio::Util::stringBuilderToStrings ( std::string const &  strData,
bool  onlyGoodChars,
std::vector< std::string > &  strings 
)
inlinestatic

This method extracts an array of strings from a string containing evio string data.

If non-printable chars are found (besides those used to terminate strings), then 1 string with all characters will be returned. However, if the "onlyGoodChars" flag is true, 1 string is returned in truncated form without the bad characters at the end.

The name of this method is taken from the java and has little to do with C++. That's done for ease of code maintenance.

Parameters
strDatacontaining string data
onlyGoodCharsif true and non-printable chars found, only 1 string with printable ASCII chars will be returned.
stringsvector in which to place extracted strings.

Referenced by unpackRawBytesToStrings().

static void evio::Util::stringsToRawBytes ( std::vector< std::string > &  strings,
std::vector< uint8_t > &  bytes 
)
inlinestatic

This method transforms an array/vector of strings into raw evio format data, not including header.

Parameters
stringsvector of strings to transform.
bytesvector of bytes to contain evio formatted strings.

References stringsToRawSize().

Referenced by evio::CompactEventBuilder::addStringData(), evio::CompositeData::dataToRawBytes(), and evio::CompositeData::stringsToFormat().

static uint32_t evio::Util::stringsToRawSize ( std::vector< std::string > const &  strings)
inlinestatic

This method returns the number of bytes in a raw evio format of the given string array, not including header.

Parameters
stringsvector of strings to size
Returns
the number of bytes in a raw evio format of the given strings
0 if vector empty.

Referenced by evio::CompositeData::Data::addString(), evio::BaseStructure::stringsToRawBytes(), and stringsToRawBytes().

static void evio::Util::stringToASCII ( const std::string &  input,
std::vector< uint8_t > &  array 
)
inlinestatic

Return an input string as ASCII in which each character is one byte.

Parameters
inputinput string.
arrayvector in which to place ASCII.
static void evio::Util::stringToASCII ( const std::string &  input,
ByteBuffer buf 
)
inlinestatic

Return an input string as ASCII in which each character is one byte.

Parameters
inputinput string.
bufByteBuffer in which to place ASCII. Clears existing data and may expand internal storage.

References evio::ByteBuffer::clear(), evio::ByteBuffer::expand(), and evio::ByteBuffer::put().

static uint32_t evio::Util::stringToRawSize ( const std::string &  str)
inlinestatic

This method returns the number of bytes in a raw evio format of the given string array (with a single string), not including header.

Parameters
strsingle string to size
Returns
the number of bytes in a raw evio format of the given strings or 0 if vector empty.

Referenced by evio::TagSegmentHeader::TagSegmentHeader().

static void evio::Util::toBytes ( uint32_t  data,
const ByteOrder byteOrder,
uint8_t *  dest 
)
inlinestatic

Write int into byte array.

Parameters
dataint to convert.
byteOrderbyte order of written bytes.
destarray in which to write int.
Exceptions
EvioExceptionif dest is null or too small.

References evio::ByteOrder::ENDIAN_BIG.

Referenced by evio::SegmentHeader::write(), evio::TagSegmentHeader::write(), and evio::BankHeader::write().

static void evio::Util::toBytes ( uint64_t  data,
const ByteOrder byteOrder,
uint8_t *  dest 
)
inlinestatic

Turn long into byte array.

Parameters
datalong to convert.
byteOrderbyte order of written bytes.
destarray in which to write long.
Exceptions
EvioExceptionif dest is null or too small

References evio::ByteOrder::ENDIAN_BIG.

static void evio::Util::toBytes ( uint32_t  data,
const ByteOrder byteOrder,
std::vector< uint8_t > &  dest,
size_t  off 
)
inlinestatic

Write int into byte vector.

Parameters
dataint to convert.
byteOrderbyte order of vector.
destvector in which to write int.
offoffset into vector where int is to be written.

References evio::ByteOrder::ENDIAN_BIG.

static void evio::Util::toBytes ( uint16_t  data,
const ByteOrder byteOrder,
uint8_t *  dest 
)
inlinestatic

Write short into byte array.

Parameters
datashort to convert.
byteOrderbyte order of array.
destarray in which to write short.
Exceptions
EvioExceptionif dest is null or too small.

References evio::ByteOrder::ENDIAN_BIG.

static void evio::Util::toBytes ( uint16_t  data,
const ByteOrder byteOrder,
std::vector< uint8_t > &  dest,
size_t  off 
)
inlinestatic

Write short into byte vector.

Parameters
datashort to convert.
byteOrderbyte order of vector.
destvector in which to write short.
offoffset into vector where short is to be written.

References evio::ByteOrder::ENDIAN_BIG.

static uint32_t evio::Util::toInt ( char  b1,
char  b2,
char  b3,
char  b4,
const ByteOrder byteOrder 
)
inlinestatic

Turn 4 bytes into an unsigned 32 bit int.

Parameters
b11st byte
b22nd byte
b33rd byte
b44th byte
byteOrderif big endian, 1st byte is most significant & 4th is least
Returns
int converted from byte array

References evio::ByteOrder::ENDIAN_BIG.

Referenced by evio::EvioReader::getEvent(), and toIntArray().

static uint32_t evio::Util::toInt ( uint8_t const *  data,
ByteOrder const &  byteOrder 
)
inlinestatic

Turn 4 bytes into an unsigned 32 bit int.

Parameters
datapointer to bytes to convert.
byteOrderbyte order of bytes.
Returns
int converted from byte array.
Exceptions
EvioExceptionif data is null.

References evio::ByteOrder::ENDIAN_BIG.

static void evio::Util::toIntArray ( char const *  data,
uint32_t  dataLen,
const ByteOrder byteOrder,
uint32_t *  dest 
)
inlinestatic

Turn byte array into an int array.

Number of int array elements = number of bytes / 4.

Parameters
datachar array to convert.
dataLennumber of bytes to convert.
byteOrderbyte order of supplied bytes.
destarray in which to write converted bytes.
Exceptions
EvioExceptionif data or dest is null

References toInt().

Referenced by evio::EventHeaderParser::createBankHeader(), evio::EventHeaderParser::createSegmentHeader(), evio::EventHeaderParser::createTagSegmentHeader(), and evio::Reader::scanFile().

static uint64_t evio::Util::toLong ( uint8_t const *  data,
ByteOrder const &  byteOrder 
)
inlinestatic

Turn 4 bytes into an unsigned 32 bit int.

Parameters
datapointer to bytes to convert
byteOrderbyte order of bytes.
Returns
int converted from byte array.
Exceptions
EvioExceptionif data is null.

References evio::ByteOrder::ENDIAN_BIG.

static void evio::Util::unpackRawBytesToStrings ( std::vector< uint8_t > &  bytes,
size_t  offset,
std::vector< std::string > &  strData 
)
inlinestatic

This method extracts an array of strings from byte array of raw evio string data.

Parameters
bytesraw evio string data.
offsetoffset into raw data array.
strDatavector in which to place extracted strings.

Referenced by evio::CompositeData::parse(), evio::CompositeData::process(), evio::CompositeData::swapAll(), and evio::CompositeData::toString().

static void evio::Util::unpackRawBytesToStrings ( std::vector< uint8_t > &  bytes,
size_t  offset,
size_t  maxLength,
std::vector< std::string > &  strData 
)
inlinestatic

This method extracts an array of strings from byte array of raw evio string data.

Don't go beyond the specified max character limit and stop at the first non-character value.

Parameters
bytesraw evio string data
offsetoffset into raw data vector
maxLengthmax length in bytes of valid data in bytes vector
strDatavector in which to place extracted strings.

References stringBuilderToStrings().

static void evio::Util::unpackRawBytesToStrings ( uint8_t *  bytes,
size_t  length,
std::vector< std::string > &  strData 
)
inlinestatic

This method extracts an array of strings from byte array of raw evio string data.

Don't go beyond the specified max character limit and stop at the first non-character value.

Parameters
bytesraw evio string data
lengthlength in bytes of valid data in bytes vector
strDatavector in which to place extracted strings.

References stringBuilderToStrings().

static void evio::Util::unpackRawBytesToStrings ( ByteBuffer buffer,
size_t  pos,
size_t  length,
std::vector< std::string > &  strData 
)
inlinestatic

This method extracts an array of strings from buffer containing raw evio string data.

Parameters
bufferbuffer containing evio string data
posposition of string data in buffer
lengthlength of string data in buffer in bytes
strDatavector in which to place extracted strings.

References evio::ByteBuffer::array(), evio::ByteBuffer::arrayOffset(), and stringBuilderToStrings().

static void evio::Util::writeBytes ( const std::string &  fileName,
ByteBuffer buf 
)
inlinestatic

This method takes a ByteBuffer and writes its data to a file.

This will overwrite any existing file of the same name.

Parameters
fileNamefile to write to.
bufbuffer to write into the file.
Exceptions
EvioExceptionbad file name or unable to write.

References evio::ByteBuffer::array(), evio::ByteBuffer::arrayOffset(), evio::ByteBuffer::position(), and evio::ByteBuffer::remaining().


The documentation for this class was generated from the following file: