/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
*
* MAUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MAUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MAUS. If not, see .
*
*/
// MAUS copyright message inserted to prevent style errors. Original copyright below
//--------------------------------------------------------------------------
// File and Version Information:
// $Id: HepRepXMLWriter.cc,v 1.5 2002/02/05 12:04:25 perl Exp $
//
// Description:
// Utility for the creation of HepRep XML Files (HepRep version 1).
//
// For details, see:
// http://www.slac.stanford.edu/~perl/HepRepXMLWriter.html
//
// Environment:
// Software developed for the general High Energy Physics community.
//
// Author :
// Joseph Perl Original Author
//
// Copyright Information:
// Copyright (C) 2002 Stanford Linear Accelerator Center
//------------------------------------------------------------------------
#include
#include
#include
#include "src/reduce/ReduceCppEVExport/HepRepXMLWriter.hh"
HepRepXMLWriter::HepRepXMLWriter() {
isOpen = false;
init();
}
void HepRepXMLWriter::init() {
typeDepth = -1;
int i = -1;
while (i++ < 49) {
prevTypeName[i] = new char[1];
// strcpy(prevTypeName[i], "");
snprintf(prevTypeName[i], sizeof(prevTypeName[i]), "");
inType[i] = false;
inInstance[i] = false;
}
inPrimitive = false;
inPoint = false;
}
void HepRepXMLWriter::addType(const char* name, int newTypeDepth) {
if (fout.good()) {
// Flatten structure if it exceeds maximum allowed typeDepth of 49.
if (newTypeDepth > 49)
newTypeDepth = 49;
if (newTypeDepth < 0)
newTypeDepth = 0;
// Insert any layers that are missing from the hierarchy (protects against
// callers that skip from, say, layer 1 to layer 3 with no layer 2).
while (typeDepth < (newTypeDepth-1)) {
addType("Layer Inserted by HepRepXMLWriter", typeDepth + 1);
addInstance();
}
// If moving closer to the root, close previously open types.
while (newTypeDepth < typeDepth)
endType();
// Close any remaining primitives of the current instance.
endPrimitive();
// If this is a new type name for the current depth, declare the
// new Type. Otherwise, it is just another Instance of the current Type.
if (strcmp(name, prevTypeName[newTypeDepth]) != 0) {
if (inType[newTypeDepth])
endType();
prevTypeName[newTypeDepth] = new char[strlen(name)+1];
// strcpy(prevTypeName[newTypeDepth], name);
snprintf(prevTypeName[newTypeDepth], sizeof(prevTypeName[newTypeDepth]), "%s", name);
inType[newTypeDepth] = true;
indent();
fout << ""
<< std::endl;
typeDepth = newTypeDepth;
}
} else {
std::cout << "HepRepXMLWriter:addType No file is currently open." << std::endl;
}
}
void HepRepXMLWriter::addInstance() {
if (fout.good()) {
if (inType[typeDepth]) {
endInstance();
inInstance[typeDepth] = true;
indent();
fout << "" << std::endl;
} else {
std::cout <<
"HepRepXMLWriter:addInstance No HepRep Type is currently open"
<< std::endl;
}
} else {
std::cout << "HepRepXMLWriter:addInstance No file is currently open"
<< std::endl;
}
}
void HepRepXMLWriter::addPrimitive() {
if (fout.good()) {
if (inInstance[typeDepth]) {
endPrimitive();
inPrimitive = true;
indent();
fout << "" << std::endl;
} else {
std::cout <<
"HepRepXMLWriter:addPrimitive No HepRep Instance is currently open"
<< std::endl;
}
} else {
std::cout << "HepRepXMLWriter:addPrimitive No file is currently open"
<< std::endl;
}
}
void HepRepXMLWriter::addPoint(double x, double y, double z) {
if (fout.good()) {
if (inPrimitive) {
endPoint();
inPoint = true;
indent();
fout << "" << std::endl;
} else {
std::cout <<
"HepRepXMLWriter:addPoint No HepRep Primitive is currently open"
<< std::endl;
}
} else {
std::cout << "HepRepXMLWriter:addPoint No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::addAttDef(const char* name,
const char* desc,
const char* type,
const char* extra) {
if (fout.good()) {
indent();
fout << "" << std::endl;
} else {
std::cout << "HepRepXMLWriter:addAttDef No file is currently open" << std::endl;
}
}
// Four methods to fill attValues
void HepRepXMLWriter::addAttValue(const char* name,
const char* value) {
if (fout.good()) {
indent();
fout << " " << std::endl;
} else {
std::cout << "HepRepXMLWriter:addAttValue No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::addAttValue(const char* name,
double value) {
if (fout.good()) {
indent();
fout << " " << std::endl;
} else {
std::cout << "HepRepXMLWriter:addAttValue No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::addAttValue(const char* name,
int value) {
if (fout.good()) {
indent();
fout << " " << std::endl;
} else {
std::cout << "HepRepXMLWriter:addAttValue No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::addAttValue(const char* name,
bool value) {
if (fout.good()) {
indent();
fout << " " << std::endl;
else
fout << " value=\"False\"/>" << std::endl;
} else {
std::cout << "HepRepXMLWriter:addAttValue No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::addAttValue(const char* name,
double value1,
double value2,
double value3) {
if (fout.good()) {
int redness = static_cast(value1*255.);
int greenness = static_cast(value2*255.);
int blueness = static_cast(value3*255.);
indent();
fout << " " << std::endl;
} else {
std::cout << "HepRepXMLWriter:addAttValue No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::open(const char* fileSpec) {
if (isOpen)
close();
fout.open(fileSpec);
if (fout.good()) {
fout << "" << std::endl;
fout << "" << std::endl;
isOpen = true;
init();
} else {
std::cout << "HepRepXMLWriter:open Unable to write to file " << fileSpec << std::endl;
}
}
void HepRepXMLWriter::close() {
// Close any remaining open Types
endTypes();
if (fout.good()) {
fout << "" << std::endl;
fout.close();
isOpen = false;
} else {
std::cout << "HepRepXMLWriter:close No file is currently open" << std::endl;
}
}
void HepRepXMLWriter::endTypes() {
// Close any remaining open Types
while (typeDepth > -1)
endType();
}
void HepRepXMLWriter::endType() {
endInstance();
indent();
fout << "" << std::endl;
inType[typeDepth] = false;
delete [] prevTypeName[typeDepth];
prevTypeName[typeDepth] = new char[1];
// strcpy(prevTypeName[typeDepth], "");
snprintf(prevTypeName[typeDepth], sizeof(prevTypeName[typeDepth]), "");
typeDepth--;
}
void HepRepXMLWriter::endInstance() {
if (inInstance[typeDepth]) {
endPrimitive();
indent();
fout << "" << std::endl;
inInstance[typeDepth] = false;
}
}
void HepRepXMLWriter::endPrimitive() {
if (inPrimitive) {
endPoint();
indent();
fout << "" << std::endl;
inPrimitive = false;
}
}
void HepRepXMLWriter::endPoint() {
if (inPoint) {
indent();
fout << "" << std::endl;
inPoint = false;
}
}
void HepRepXMLWriter::indent() {
if (fout.good()) {
int i = 0;
while (inType[i] && i < 12) {
fout << " ";
if (inInstance[i])
fout << " ";
i++;
}
if (inPrimitive)
fout << " ";
if (inPoint)
fout << " ";
}
}