DATRW++ library: seismic data I/O with multiple formats
util.cc
Go to the documentation of this file.
1 /*! \file util.cc
2  * \brief utilities used by more than one type of data reader (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 22/12/2004
8  *
9  * utilities used by more than one type of data reader (implementation)
10  *
11  * ----
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25  * ----
26  *
27  * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach)
28  *
29  * REVISIONS and CHANGES
30  * - 22/12/2004 V1.0 Thomas Forbriger
31  * - 29/07/2011 V1.1 added clipstring
32  * - 06/09/2011 V1.2 moved string manipulation functions to this file:
33  * commatospace and trimws
34  *
35  * ============================================================================
36  */
37 #define DATRW_UTIL_CC_VERSION \
38  "DATRW_UTIL_CC V1.2"
39 
40 #include <cmath>
41 #include <algorithm>
42 #include <datrwxx/util.h>
43 
44 namespace datrw {
45 
46  namespace util {
47 
48  //! return number of significant digits
49  int nsignificantdigits(double v, const bool& debug)
50  {
51  const double epsilon=1.e-30;
52  if (debug)
53  {
54  std::cerr << "DEBUG (nsignificantdigits): "
55  << "entered function for value:" << v << std::endl;
56  }
57  if (v<0) { v = -v; }
58  int n=0;
59  if (v<epsilon)
60  {
61  n=2;
62  }
63  else
64  {
65  double basefactor=pow(10.,-floor(log10(v)));
66  double v1,v2;
67  if (debug)
68  {
69  std::cerr << "DEBUG (nsignificantdigits): "
70  << "basefactor: " << basefactor << " "
71  << "v: " << v << " "
72  << std::endl;
73  }
74  do
75  {
76  double factor=basefactor*pow(10.,n);
77  v1=v*factor;
78  v2=floor(v1);
79  n++;
80  if (debug)
81  {
82  std::cerr << "DEBUG (nsignificantdigits): "
83  << "factor: " << factor << " "
84  << "n: " << n << " "
85  << "v1: " << v1 << " "
86  << "v2: " << v2 << " "
87  << std::endl;
88  }
89  }
90  while (v1 != v2);
91  }
92  return (n);
93  } // int nsignificantdigits(const double& v)
94 
95  /*----------------------------------------------------------------------*/
96 
97  int ntrailingdigits(double v, const bool& debug)
98  {
99  int retval=nsignificantdigits(v, debug)-floor(log10(v))-1;
100  return(retval);
101  } // int ntrailingdigits(double v, const bool& debug=false)
102 
103  /*----------------------------------------------------------------------*/
104 
105  std::string clipstring(std::string& s, const std::string& delim)
106  {
107  std::string::size_type i=s.find(delim);
108  std::string result;
109  if ((i>=0) && (i<s.length())) {
110  result=s.substr(0,i);
111  s.erase(0,i+delim.length());
112  } else {
113  result=s;
114  s.erase();
115  }
116  return(result);
117  } // std::string clipstring
118 
119  /*----------------------------------------------------------------------*/
120 
121  std::string commatospace(std::string s)
122  {
123  std::replace(s.begin(), s.end(), ',', ' ');
124  return(s);
125  } // std::string commatospace(const std::string& s)
126 
127  /*----------------------------------------------------------------------*/
128 
129  std::string trimws(std::string s)
130  {
131  if (s.length()>0)
132  {
133  std::string::size_type ib=s.find_first_not_of(" ", 0);
134  if (ib==std::string::npos)
135  {
136  s="";
137  }
138  else
139  {
140  std::string::size_type il=s.find_last_not_of(" \r", s.length());
141  std::string::size_type n=il>=ib ? il-ib+1 : 0;
142  if (n==0) { ib = 0; }
143  if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
144  }
145  }
146  return(s);
147  } // std::string trimws(std::string s)
148 
149  } // namespace util
150 
151 } // namespace datrw
152 
153 /* ----- END OF util.cc ----- */
std::string commatospace(std::string s)
Definition: util.cc:121
std::string trimws(std::string s)
remove leading and trailing whitespace
Definition: util.cc:129
Root namespace of library.
Definition: aalibdatrwxx.cc:16
utilities used by more than one type of data reader (prototypes)
int ntrailingdigits(double v, const bool &debug)
return number of trailing digits (after decimal point)
Definition: util.cc:97
int nsignificantdigits(double v, const bool &debug)
return number of significant digits
Definition: util.cc:49
std::string clipstring(std::string &s, const std::string &delim)
strip substringStrips off first substring up to given delimiter. The string is passed as a reference ...
Definition: util.cc:105