Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 /* */
3 /* ---- SPARTA ---- */
4 /* Shifts Prediction from Analogue of Residue type and Torsion Angle */
5 /* Yang Shen and Ad Bax */
6 /* J. Biomol. NMR, 38, 289-302 (2007) */
7 /* NIH, NIDDK, Laboratory of Chemical Physics */
8 /* version, 1.01 (build 2009.0928.17) */
9 /* */
10 /* for any problem, please contact */
11 /* shenyang@niddk.nih.gov */
12 /* */
13 /******************************************************************************/
14 #include <protocols/sparta/util.hh>
15 
16 #include <string>
17 #include <vector>
18 #include <sstream>
19 #include <iterator>
20 #include <utility/exit.hh>
21 
22 #include <stdio.h>
23 
24 #include <utility/vector0.hh>
25 
26 
27 namespace protocols {
28 namespace sparta {
29 
30 using namespace std;
32 {
33  if ( str.empty() ) { // nothing to do
34  return str;
35  }
36 
37  char to[ 20000 ];
38  runtime_assert( 20000 > str.size() );
39  const char *from = str.c_str();
40  const char *fromend = from+str.length();
41  int outc=0;
42  // char *to = &(result[0]);
43 
44  while ( true ) {
45  while ( from!=fromend && isSpace(*from) ) from++;
46  while ( from!=fromend && !isSpace(*from) ) to[outc++] = *(from++);
47  if ( from!=fromend ) {
48  to[outc++] = ' ';
49  } else {
50  break;
51  }
52  }
53 
54  if ( outc > 0 && to[outc-1] == ' ' ) {
55  outc--;
56  }
57  to[outc]='\0';
58  std::string result(to);
59  return result;
60 }
61 
62 
63 int contains( const std::string &str, const std::string &c )
64 {
65  int count = 0;
66 
67  int n = str.length();
68  int len = c.length();
69 
70  std::string temp = str.substr(0,len);
71  while ( n-- ) { // counts overlapping stringbs
72  if ( temp == c ) {
73  count++;
74  }
75  temp = str.substr(str.length()-n,len);
76  }
77 
78  return count;
79 }
80 
81 
82 int contains( const string &str, const char &c )
83 {
84  int count = 0;
85 
86  int n = str.length();
87  while ( n-- ) {
88  if ( str[n] == c ) {
89  count++;
90  }
91  }
92  return count;
93 }
94 
95 
96 bool isDigit( const char &c )
97 {
98  if ( c >= '0' && c <= '9' ) return true;
99  return false;
100 }
101 
102 
103 bool isSpace( const char &c )
104 {
105  if ( (c >= 9 && c <= 13) || c == ' ' ) return true;
106  return false;
107 }
108 
109 
110 StringList split(const char sep, const string &str)
111 {
112  string sp = " "; sp[0] = sep;
113  return split( sp, str );
114 }
115 
116 
117 StringList split(const string &sep, const string &str)
118 {
119  StringList lst;
120 
121  int j = 0;
122  int i = str.find( sep, j );
123 
124  while ( i != -1 ) {
125  if ( str.substr(j, i - j ).length() > 0 ) {
126  lst.push_back( str.substr( j, i - j ) );
127  }
128  j = i + sep.length();
129  i = str.find( sep, j );
130  }
131 
132  int l = str.length() - 1;
133  if ( str.substr( j, l - j + 1 ).length() > 0 ) {
134  lst.push_back( str.substr( j, l - j + 1 ) );
135  }
136 
137  return lst;
138 }
139 
140 
142 {
143  StringList lst;
144  std::stringstream stream( str );
145  copy( istream_iterator< std::string >(stream), istream_iterator< std::string >(), back_inserter( lst ) );
146  // if ( str.empty() ) // nothing to do
147  // {
148  // lst.push_back( str );
149  // return lst;
150  // }
151 
152  // string result;
153  // result.resize(str.length()); //.setLength( length() );
154  // const char *from = str.c_str();
155  // const char *fromend = from+str.length();
156  // int outc=0;
157  // char *to = &(result[0]);
158 
159  // while ( true ) {
160  // //while ( from!=fromend && isSpace(*from) ) //(c >= 9 && c <= 13) || c == ' '
161  // while ( from!=fromend && ((*from >= 9 && *from <= 13) || *from == ' ') )
162  // from++;
163  // while ( from!=fromend && !isSpace(*from) )
164  // //while ( from!=fromend && !((*from >= 9 && *from <= 13) || *from == ' ') )
165  // to[outc++] = *from++;
166 
167  // if ( from!=fromend )
168  // {
169  // to[outc++] = '\0';
170  // lst.push_back( to );
171  // to = &(result[0]);
172  // outc=0;
173  // }
174  // else
175  // break;
176  // }
177  // if ( outc > 0 )
178  // {
179  // to[outc++] = '\0';
180  // lst.push_back( to );
181  // }
182 
183  // // if ( outc > 0 && to[outc-1] == ' ' )
184  // // outc--;
185  // // result.resize( outc );
186  // // return result;
187  return lst;
188 }
189 
190 
191 //returns a section of the string, each section is defined by char 'sep', numbers of start and end are the index number (begin with 0)
192 char *section( const string &str, const char &sep, char *buff, int start, int end ) {
193  StringList fields = split(sep, str);
194 
195  string temp = "";
196  if ( start < (int) fields.size() ) {
197  for ( int i = start; i <= end; i++ ) {
198  if ( i >= (int) fields.size() ) break;
199  temp += fields[i];
200  if ( i!=end ) temp+=sep;
201  }
202  }
203 
204  sprintf( buff, "%s", temp.c_str() );
205  return buff;
206 }
207 
208 }
209 }
std::string simplifyWhiteSpace(const std::string &str)
Definition: util.cc:31
bool isSpace(const char &c)
Definition: util.cc:103
c
DEPRECATED convert between the real-valued chi dihedrals and the rotamer well indices.
std::string str(T const &t)
Definition: FunGroupTK.cc:77
bool isDigit(const char &c)
Definition: util.cc:96
int contains(const std::string &str, const std::string &c)
Definition: util.cc:63
utility::vector0< std::string > StringList
Definition: Sparta.hh:32
StringList split(const char sep, const string &str)
Definition: util.cc:110
StringList split_WhiteSpace(const string &str)
Definition: util.cc:141
char * section(const string &str, const char &sep, char *buff, int start, int end)
Definition: util.cc:192