Develop and Download Open Source Software

Browse Subversion Repository

Contents of /Conograph/trunk/src/utility_func/zstring.hh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download) (as text)
Fri Feb 22 04:51:31 2013 UTC (11 years, 1 month ago) by rtomiyasu
File MIME type: text/x-c++hdr
File size: 3589 byte(s)


1 /*
2 * The MIT License
3
4 Conograph (powder auto-indexing program)
5
6 Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *
26 */
27 #ifndef _ZSTRING_HH_
28 #define _ZSTRING_HH_
29
30 #include <sstream>
31 #include "../RietveldAnalysisTypes.hh"
32 #include "../zerror_type/error_mes.hh"
33
34 // Change the argument type from T to string.
35 template<class T>
36 inline bool str2num(const string& str, T& ans)
37 {
38 istringstream iss(str);
39 iss >> ans;
40 if( iss.fail() ) return false;
41 return true;
42 }
43
44
45 template<class T>
46 inline string num2str(const T& num)
47 {
48 stringstream strstream;
49 strstream << num;
50 return strstream.str();
51 }
52
53
54 template<class T>
55 inline string num2str(const T& num, const Int4& precision)
56 {
57 stringstream strstream;
58 strstream.precision(precision);
59 strstream << num;
60 return strstream.str();
61 }
62
63 template<class T>
64 inline string vec2str(const vector<T>& num)
65 {
66 stringstream strstream;
67 if( num.empty() ) return "";
68 if( num.size() < 2 ) return num2str(num[0]);
69
70 strstream << "(" << *num.begin();
71 for(typename vector<T>::const_iterator it=num.begin()+1; it<num.end(); it++)
72 {
73 strstream << "," << *it;
74 }
75 strstream << ")";
76 return strstream.str();
77 }
78
79 // Check if the string is blank.
80 bool is_blank(const string&);
81
82 // Split the argument string by the delimiter.
83 void split(const string&, vector<string>&, const char&);
84
85 bool split_term(istream&, vector<string>&);
86
87 string getFileExtension (const string& inputFilePathString);
88
89 void removeFileExtension(const string&, string&);
90
91 inline string remove_blank(const string& str)
92 {
93 string ans, str2;
94 istringstream iss(str);
95
96 iss >> str2;
97 while( !iss.fail() )
98 {
99 ans += str2;
100 iss >> str2;
101 }
102 return ans;
103 }
104
105
106 inline ZErrorType getnewline(istream& ifs, string& s)
107 {
108 s.clear();
109 char c;
110 while( ifs.get(c) )
111 {
112 if( c == '\n' ) return ZErrorNoError;
113 if( c == '\r' )
114 {
115 if( ifs.eof() ) return ZErrorNoError;
116 c = ifs.peek();
117 if( c == '\n' ) ifs.get(c);
118 return ZErrorNoError;
119 }
120 s += c;
121 }
122 return ZErrorDelimiterNotFound;
123 }
124
125
126 inline void getfirstword(istream& is, string& label)
127 {
128 label="";
129 string str2;
130 ZErrorType zerr = ZErrorNoError;
131 while( zerr == ZErrorNoError )
132 {
133 zerr = getnewline(is, str2);
134 if( !is_blank(str2) )
135 {
136 istringstream iss2(str2);
137 iss2 >> label;
138 break;
139 }
140 }
141 }
142
143
144 // Read the sentence before delim and put it in ans.
145 ZErrorMessage getdelim(istream& ifs, string& ans, const string& delim);
146
147 #endif /*STRING_HH_*/

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26