ATLAS Offline Software
Loading...
Searching...
No Matches
utils.cxx
Go to the documentation of this file.
1
19
20
21
22#include <string>
23#include <vector>
24#include <iostream>
25//#include <math.h>
26#include <cmath>
27
28#include <stdio.h>
29
30#include "utils.h"
31#include <algorithm>
32// chop tokens off the end of a string, leave string unchanged
33// return choped string
34std::string choptoken(std::string& s1, const std::string& s2)
35{
36 std::string s3 = "";
37 std::string::size_type pos = s1.find(s2);
38 if ( pos != std::string::npos ) {
39 s3 = s1.substr(0, pos+1);
40 s1.erase(0, pos+1);
41 }
42 return s3;
43}
44
45// chop tokens off the end of a string, leave string unchanged
46// return choped string
47std::string chomptoken(std::string& s1, const std::string& s2)
48{
49 std::string s3 = "";
50 std::string::size_type pos = s1.find_last_of(s2);
51 if ( pos != std::string::npos ) {
52 s3 = s1.substr(pos, s1.size());
53 s1.erase(pos, s1.size());
54 }
55 return s3;
56}
57
58
59
60std::string chopfirst(std::string& s1, const std::string& s2)
61{
62 std::string s3;
63 std::string::size_type pos = s1.find_first_not_of(s2);
64 if ( pos != std::string::npos ) {
65 s3 = s1.substr(0, pos);
66 s1.erase(0, pos);
67 }
68 else {
69 s3 = s1;
70 s1.clear();
71 }
72 return s3;
73}
74
75// chop tokens off the front of a string
76std::string chopends(std::string& s1, const std::string& s2)
77{
78 chopfirst(s1, s2);
79 choplast(s1, s2);
80 return s1;
81}
82
83
84
85std::string choplast(std::string& s1, const std::string& s2)
86{
87 std::string s3 = "";
88 std::string::size_type pos = s1.find_last_not_of(s2);
89 if ( pos != std::string::npos ) {
90 s3 = s1.substr(pos+1, s1.size());
91 s1.erase(pos+1, s1.size());
92 }
93 return s3;
94}
95
96
97// chop tokens off the front of a string
98std::string chop(std::string& s1, const std::string& s2)
99{
100 std::string::size_type pos = s1.find(s2);
101 std::string s3;
102 if ( pos == std::string::npos ) {
103 s3 = std::move(s1);
104 s1.clear();
105 }
106 else {
107 s3 = s1.substr(0, pos);
108 s1.erase(0, pos+s2.size());
109 }
110 return s3;
111}
112
113
114// chomp them off the end
115std::string chomp(std::string& s1, const std::string& s2)
116{
117 std::string::size_type pos = s1.find(s2);
118 std::string s3;
119 if ( pos == std::string::npos ) {
120 s3 = std::move(s1);
121 s1.clear();
122 }
123 else {
124 s3 = s1.substr(pos+s2.size(),s1.size());
125 s1.erase(pos,s1.size());
126 }
127 return s3;
128}
129
130
131// remove strings from a string
132void removespace(std::string& s, const std::string& s2)
133{
134 std::string::size_type pos;
135 while ( (pos = s.find(s2))!=std::string::npos ) {
136 s.erase(pos, 1);
137 }
138}
139
140
141// replace from a string
142void replace(std::string& s, const std::string& s2, const std::string& s3)
143{
144 std::string::size_type pos;
145 // while ( (pos = s.find(" "))!=std::string::npos ) {
146 // s.replace(pos, 1, "-");
147 while ( (pos = s.find(s2))!=std::string::npos ) {
148 s.replace(pos, 1, s3);
149 }
150}
151
152void replace(std::string& s, char c1, char c2) noexcept {
153 std::replace(s.begin(), s.end(), c1, c2);
154}
155
156// remove colons from a string
157void depunctuate(std::string& s)
158{
159 std::string::size_type pos;
160 while ( (pos = s.find(':'))!=std::string::npos ) {
161 s.erase(pos, 1);
162 }
163}
164
165
166
167
168// helper method to get difference in phi
169double deltaPhi(double phi1, double phi2) {
170 double delta = fabs(phi1-phi2);
171 delta = (delta > M_PI) ? (2.0*M_PI - delta) : delta;
172 return delta;
173}
174
175
176
177// checks to see if a particular file can be opened
178bool canopen(const std::string& s) {
179 FILE* f = fopen(s.c_str(), "r");
180 if ( f ) { fclose(f); return true; }
181 return false;
182}
183
184
185// converts number into integer string ...
186std::string number(const double& d, const std::string& s) {
187 char tmp[512];
188 sprintf(tmp, s.c_str(), d);
189 return tmp;
190}
191
192std::string number(const int& i, const std::string& s) {
193 char tmp[512];
194 sprintf(tmp, s.c_str(), i);
195 return tmp;
196}
197
198
199
200std::string dirname( std::string name ) {
201 std::string::size_type pos = name.find_last_of( '/' );
202 if ( pos!=std::string::npos ) name.resize( pos );
203 return name;
204}
205
206
207std::string basename( std::string name ) {
208 std::string::size_type pos = name.find( '/' );
209 while ( pos!=std::string::npos ) {
210 name = name.substr( pos+1, name.size()-pos-1 );
211 pos = name.find( '/' );
212 }
213 return name;
214}
#define M_PI
std::string dirname(std::string name)
Definition utils.cxx:200
void replace(std::string &s, const std::string &s2, const std::string &s3)
Definition utils.cxx:142
std::string choptoken(std::string &s1, const std::string &s2)
Definition utils.cxx:34
std::string chopends(std::string &s1, const std::string &s2)
Definition utils.cxx:76
std::string chomp(std::string &s1, const std::string &s2)
Definition utils.cxx:115
void removespace(std::string &s, const std::string &s2)
Definition utils.cxx:132
bool canopen(const std::string &s)
Definition utils.cxx:178
std::string chopfirst(std::string &s1, const std::string &s2)
Definition utils.cxx:60
std::string basename(std::string name)
Definition utils.cxx:207
std::string choplast(std::string &s1, const std::string &s2)
Definition utils.cxx:85
double deltaPhi(double phi1, double phi2)
Definition utils.cxx:169
std::string number(const double &d, const std::string &s)
Definition utils.cxx:186
std::string chomptoken(std::string &s1, const std::string &s2)
Definition utils.cxx:47
std::string chop(std::string &s1, const std::string &s2)
Definition utils.cxx:98
void depunctuate(std::string &s)
Definition utils.cxx:157