JDFTx  1.2.0
string.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2012 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_ISTRING_H
21 #define JDFTX_CORE_ISTRING_H
22 
24 
25 #include <string>
26 #include <cstring>
27 #include <iostream>
28 #include <sstream>
29 #include <fstream>
30 
32 struct ichar_traits : public std::char_traits<char>
33 { static bool eq( char c1, char c2 ) { return toupper(c1) == toupper(c2); }
34  static bool ne( char c1, char c2 ) { return toupper(c1) != toupper(c2); }
35  static bool lt( char c1, char c2 ) { return toupper(c1) < toupper(c2); }
36  static int compare( const char* s1, const char* s2, size_t n ) { return strncasecmp( s1, s2, n ); }
37  static const char* find( const char* s, int n, char a )
38  { for(int i=0; i<n; i++) if(toupper(s[i])==toupper(a)) return s+i;
39  return 0;
40  }
41 };
42 typedef std::basic_string<char, ichar_traits> string;
43 
45 inline void trim(string& s)
46 { //Trim trailing whitespace:
47  size_t endNonWS = s.find_last_not_of(" \t\n\r");
48  if(endNonWS != string::npos)
49  s.erase(endNonWS+1);
50  //Trim leading whitespace:
51  s.erase(0, s.find_first_not_of(" \t\n\r")); //deletes entire line if all whitespace
52 }
53 
54 using std::istream;
55 using std::ostream;
56 
57 inline istream& operator>>(istream& is, string& str) { std::string tmp; is >> tmp; str.assign(tmp.c_str()); return is; }
58 inline ostream& operator<<(ostream& os, const string& str) { os << str.c_str(); return os; }
59 inline istream& getline (istream& is, string& str, char delim='\n')
60 { std::string tmp;
61  getline(is, tmp, delim);
62  str.assign(tmp.c_str());
63  return is;
64 }
65 
66 struct ifstream : public std::ifstream
67 { ifstream() {}
68  explicit ifstream(const string& fname) : std::ifstream(fname.c_str()) {}
69  void open(const string& fname) { std::ifstream::open(fname.c_str()); }
70 };
71 struct ofstream : public std::ofstream
72 { ofstream() {}
73  explicit ofstream(const string& fname) : std::ofstream(fname.c_str()) {}
74  void open(const string& fname) { std::ofstream::open(fname.c_str()); }
75 };
76 
77 struct istringstream : public std::istringstream
78 { istringstream() {}
79  explicit istringstream(const string& s) { std::istringstream::str(std::string(s.c_str())); }
80  void str(const string& s) { std::istringstream::str(std::string(s.c_str())); }
81 };
82 struct ostringstream : public std::ostringstream
83 { string str() const { return string(std::ostringstream::str().c_str()); }
84 };
85 
86 #endif
Definition: string.h:66
Definition: string.h:77
Definition: string.h:71
Definition: string.h:82
void trim(string &s)
Remove leading and trailing spaces from a string.
Definition: string.h:45
std::basic_string< char, ichar_traits > string
Case-insensitive string.
Definition: string.h:42
Case insensitive character trait.
Definition: string.h:32