1 | // ----------------------------------------------------------------------- |
---|
2 | // string_related.cc: General utility functions for manipulating strings |
---|
3 | // and input flags/parameters |
---|
4 | // ----------------------------------------------------------------------- |
---|
5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or modify it |
---|
8 | // under the terms of the GNU General Public License as published by the |
---|
9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
10 | // option) any later version. |
---|
11 | // |
---|
12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
15 | // for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License |
---|
18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
20 | // |
---|
21 | // Correspondence concerning Duchamp may be directed to: |
---|
22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
23 | // Postal address: Dr. Matthew Whiting |
---|
24 | // Australia Telescope National Facility, CSIRO |
---|
25 | // PO Box 76 |
---|
26 | // Epping NSW 1710 |
---|
27 | // AUSTRALIA |
---|
28 | // ----------------------------------------------------------------------- |
---|
29 | #include <iostream> |
---|
30 | #include <sstream> |
---|
31 | #include <iomanip> |
---|
32 | #include <string> |
---|
33 | #include <duchamp/Utils/utils.hh> |
---|
34 | |
---|
35 | |
---|
36 | std::string makelower( std::string s ) |
---|
37 | { |
---|
38 | // "borrowed" from Matt Howlett's 'fred' |
---|
39 | std::string out = ""; |
---|
40 | for( size_t i=0; i<s.size(); ++i ) { |
---|
41 | out += tolower(s[i]); |
---|
42 | } |
---|
43 | return out; |
---|
44 | } |
---|
45 | |
---|
46 | std::string stringize(bool b) |
---|
47 | { |
---|
48 | /// Convert a bool variable to the textual equivalent. |
---|
49 | /// \return A std::string with the english equivalent of the bool. |
---|
50 | |
---|
51 | std::string output; |
---|
52 | if(b) output="true"; |
---|
53 | else output="false"; |
---|
54 | return output; |
---|
55 | } |
---|
56 | |
---|
57 | bool boolify( std::string s ) |
---|
58 | { |
---|
59 | /// Convert a std::string to a bool variable: |
---|
60 | /// "1" and "true" get converted to true; |
---|
61 | /// "0" and "false" (and anything else) get converted to false. |
---|
62 | /// \return The bool equivalent of the string. |
---|
63 | |
---|
64 | if((s=="1") || (makelower(s)=="true")) return true; |
---|
65 | else if((s=="0") || (makelower(s)=="false")) return false; |
---|
66 | else return false; |
---|
67 | } |
---|
68 | |
---|
69 | std::string readSval(std::stringstream& ss) |
---|
70 | { |
---|
71 | std::string val; |
---|
72 | ss >> val; |
---|
73 | return val; |
---|
74 | } |
---|
75 | |
---|
76 | std::string readFilename(std::stringstream& ss) |
---|
77 | { |
---|
78 | std::string val; |
---|
79 | getline(ss,val); |
---|
80 | return removeLeadingBlanks(val); |
---|
81 | } |
---|
82 | |
---|
83 | bool readFlag(std::stringstream& ss) |
---|
84 | { |
---|
85 | std::string val; |
---|
86 | ss >> val; |
---|
87 | return boolify(val); |
---|
88 | } |
---|
89 | |
---|
90 | float readFval(std::stringstream& ss) |
---|
91 | { |
---|
92 | float val; |
---|
93 | ss >> val; |
---|
94 | return val; |
---|
95 | } |
---|
96 | |
---|
97 | int readIval(std::stringstream& ss) |
---|
98 | { |
---|
99 | int val; |
---|
100 | ss >> val; |
---|
101 | return val; |
---|
102 | } |
---|
103 | |
---|
104 | std::string removeLeadingBlanks(std::string s) |
---|
105 | { |
---|
106 | /** |
---|
107 | * All blank spaces from the start of the string to the first |
---|
108 | * non-blank-space character are deleted. |
---|
109 | */ |
---|
110 | int i=0; |
---|
111 | while(isspace(s[i])){ |
---|
112 | i++; |
---|
113 | } |
---|
114 | std::string newstring=""; |
---|
115 | for(unsigned int j=i;j<s.size();j++) newstring += s[j]; |
---|
116 | return newstring; |
---|
117 | } |
---|
118 | //------------------------------------------------------------------------ |
---|
119 | |
---|
120 | std::string deblank(std::string s) |
---|
121 | { |
---|
122 | /** |
---|
123 | * All blank spaces from the start of the string to the first |
---|
124 | * non-blank-space character, and from the last non-blank-space |
---|
125 | * character to the end are deleted. |
---|
126 | */ |
---|
127 | int beg=0; |
---|
128 | while(isspace(s[beg])){ |
---|
129 | beg++; |
---|
130 | } |
---|
131 | int end=s.size()-1; |
---|
132 | while(isspace(s[end])){ |
---|
133 | end--; |
---|
134 | } |
---|
135 | std::string newstring; |
---|
136 | for(int j=beg;j<=end;j++) newstring += s[j]; |
---|
137 | return newstring; |
---|
138 | } |
---|