VGM Version 5.3
Loading...
Searching...
No Matches
utilities.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The XmlVGM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// XmlVGM utilities
14// --------------
15// Utility functions
16//
17// Author: I. Hrivnacova, 27.07.2000
18
19#include "XmlVGM/utilities.h"
20
22
23#include <ClhepVGM/transform.h>
24
25#include <iomanip>
26#include <iostream>
27#include <sstream>
28#include <time.h>
29
30//_____________________________________________________________________________
32 const std::string& name, const std::string& extension)
33{
37
38 std::string newName(name);
39
40 // Remove spaces after the name
41 int i = newName.length();
42 while (newName[--i] == ' ') newName = std::string(newName, 0, i);
43
44 // Replace not allowed characters
45 //
46 for (i = 0; i < int(newName.length()); i++) {
47
48 if ((i == 0 &&
49 fgkNotAllowedChars1.find(newName[i]) < fgkNotAllowedChars1.size()) ||
50 fgkNotAllowedChars.find(newName[i]) < fgkNotAllowedChars.size())
51
52 newName[i] = fgkCharReplacement;
53 }
54
55 // Append extension
56 newName.append(extension);
57
58 return newName;
59}
60
61//_____________________________________________________________________________
63 const std::string& name, const std::string& extension)
64{
66
67 return name.substr(0, name.find(extension));
68}
69
70//_____________________________________________________________________________
71std::string XmlVGM::AppendName(const std::string& name, int size)
72{
74
75 std::string newName(name);
76
77 for (int i = 0; i < size - int(name.size()); i++) {
78 // std::cout << string << "appending " << i << std::endl;
79 newName.append(" ");
80 }
81
82 return newName;
83}
84
85//_____________________________________________________________________________
86std::string XmlVGM::IsotopeName(const VGM::IIsotope* isotope)
87{
89
90 std::string name = UpdateName(isotope->Name());
91 std::ostringstream nStream;
92 nStream << isotope->N();
93 std::string n(nStream.str());
94
95 // Cut N from name if present
96 if (name.find(n) < name.size())
97 name.insert(name.find(n), 1, fgkCharReplacement);
98 else {
99 name += fgkCharReplacement;
100 name += n;
101 }
102
103 return name;
104}
105
106//_____________________________________________________________________________
107void XmlVGM::CutName(std::string& name)
108{
110
111 int i = name.length();
112 while (name[--i] == ' ') name = std::string(name, 0, i);
113}
114
115//_____________________________________________________________________________
116void XmlVGM::CutName(std::string& name, int size)
117{
118 // Cut name to given size
119
120 if (int(name.length()) > size) name = std::string(name, 0, size);
121}
122
123//_____________________________________________________________________________
124std::ostream& XmlVGM::SmartPut(std::ostream& out, int size, int precision,
125 double tolerance, double number, const std::string& separator)
126{
129
130 if (fabs(number) < tolerance) number = 0.0;
131
132 if (number != 0.0 &&
133 ClhepVGM::Round(number * pow(10., precision)) / pow(10., precision) ==
134 0.0) {
135 out << std::scientific << std::setw(size) << std::setprecision(precision)
136 << number << std::fixed << separator;
137 }
138 else {
139 out << std::setw(size) << std::setprecision(precision) << number
140 << separator;
141 }
142
143 return out;
144}
145
146//_____________________________________________________________________________
147std::ostream& XmlVGM::SmartPut(std::ostream& out, int size, int precision,
148 double tolerance, const std::string& separator1, double number,
149 const std::string& separator2)
150{
153
154 out << separator1;
155
156 if (fabs(number) < tolerance) number = 0.0;
157
158 if (number != 0.0 &&
159 ClhepVGM::Round(number * pow(10., precision)) / pow(10., precision) ==
160 0.0) {
161 out << std::scientific << std::setw(size) << std::setprecision(precision)
162 << number << std::fixed;
163 }
164 else {
165 out << std::setw(size) << std::setprecision(precision) << number;
166 }
167
168 out << separator2;
169
170 return out;
171}
172
173//_____________________________________________________________________________
174std::string XmlVGM::Date()
175{
177
178 time_t t = time(0);
179 tm time = *localtime(&t);
180
181 std::ostringstream tmpStream;
182 tmpStream << time.tm_year + 1900 << "-" << std::setw(2) << std::setfill('0')
183 << time.tm_mon + 1 << "-" << std::setw(2) << std::setfill('0')
184 << time.tm_mday;
185
186 return tmpStream.str();
187}
The VGM interface to elements.
Definition IIsotope.h:28
virtual int N() const =0
Return the effective number of nucleons.
virtual std::string Name() const =0
Return the name of this element.
double Round(double x)
std::string IsotopeName(const VGM::IIsotope *isotope)
Definition utilities.cxx:86
std::string AppendName(const std::string &name, int size)
Definition utilities.cxx:71
const std::string fgkNotAllowedChars
Definition utilities.h:53
std::string Date()
const std::string fgkNotAllowedChars1
Definition utilities.h:55
std::string StripName(const std::string &name, const std::string &extension="")
Definition utilities.cxx:62
const char fgkCharReplacement
Definition utilities.h:52
void CutName(std::string &name)
std::ostream & SmartPut(std::ostream &out, int size, int precision, double tolerance, double number, const std::string &separator)
std::string UpdateName(const std::string &name, const std::string &extension="")
Definition utilities.cxx:31