Rosetta 3.5
 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 // AUTO-REMOVED #include <algorithm>
21 #include <utility/exit.hh>
22 
23 #include <stdio.h>
24 
25 #include <utility/vector0.hh>
26 
27 
28 namespace protocols {
29 namespace sparta {
30 
31 using namespace std;
33 {
34  if ( str.empty() ) // nothing to do
35  return str;
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  if ( outc > 0 && to[outc-1] == ' ' )
54  outc--;
55  to[outc]='\0';
56  std::string result(to);
57  return result;
58 }
59 
60 
61 int contains( const std::string &str, const std::string &c )
62 {
63  int count = 0;
64 
65  int n = str.length();
66  int len = c.length();
67 
68  std::string temp = str.substr(0,len);
69  while ( n-- ) { // counts overlapping stringbs
70  if ( temp == c )
71  count++;
72  temp = str.substr(str.length()-n,len);
73  }
74 
75  return count;
76 }
77 
78 
79 int contains( const string &str, const char &c )
80 {
81  int count = 0;
82 
83  int n = str.length();
84  while ( n-- ) {
85  if ( str[n] == c )
86  count++;
87  }
88  return count;
89 }
90 
91 
92 bool isDigit( const char &c )
93 {
94  if( c >= '0' && c <= '9') return true;
95  return false;
96 }
97 
98 
99 bool isSpace( const char &c )
100 {
101  if( (c >= 9 && c <= 13) || c == ' ' ) return true;
102  return false;
103 }
104 
105 
106 StringList split(const char sep, const string &str)
107 {
108  string sp = " "; sp[0] = sep;
109  return split( sp, str );
110 }
111 
112 
113 StringList split(const string &sep, const string &str)
114 {
115  StringList lst;
116 
117  int j = 0;
118  int i = str.find( sep, j );
119 
120  while ( i != -1 ) {
121  if ( str.substr(j, i - j ).length() > 0 )
122  lst.push_back( str.substr( j, i - j ) );
123  j = i + sep.length();
124  i = str.find( sep, j );
125  }
126 
127  int l = str.length() - 1;
128  if ( str.substr( j, l - j + 1 ).length() > 0 )
129  lst.push_back( str.substr( j, l - j + 1 ) );
130 
131  return lst;
132 }
133 
134 
136 {
137  StringList lst;
138  std::stringstream stream( str );
139  copy( istream_iterator< std::string >(stream), istream_iterator< std::string >(), back_inserter( lst ) );
140 // if ( str.empty() ) // nothing to do
141 // {
142 // lst.push_back( str );
143 // return lst;
144 // }
145 
146 // string result;
147 // result.resize(str.length()); //.setLength( length() );
148 // const char *from = str.c_str();
149 // const char *fromend = from+str.length();
150 // int outc=0;
151 // char *to = &(result[0]);
152 
153 // while ( true ) {
154 // //while ( from!=fromend && isSpace(*from) ) //(c >= 9 && c <= 13) || c == ' '
155 // while ( from!=fromend && ((*from >= 9 && *from <= 13) || *from == ' ') )
156 // from++;
157 // while ( from!=fromend && !isSpace(*from) )
158 // //while ( from!=fromend && !((*from >= 9 && *from <= 13) || *from == ' ') )
159 // to[outc++] = *from++;
160 
161 // if ( from!=fromend )
162 // {
163 // to[outc++] = '\0';
164 // lst.push_back( to );
165 // to = &(result[0]);
166 // outc=0;
167 // }
168 // else
169 // break;
170 // }
171 // if ( outc > 0 )
172 // {
173 // to[outc++] = '\0';
174 // lst.push_back( to );
175 // }
176 
177 // // if ( outc > 0 && to[outc-1] == ' ' )
178 // // outc--;
179 // // result.resize( outc );
180 // // return result;
181  return lst;
182 }
183 
184 
185 //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)
186 char *section( const string &str, const char &sep, char *buff, int start, int end ) {
187  StringList fields = split(sep, str);
188 
189  string temp = "";
190  if( start < (int) fields.size() )
191  for( int i = start; i <= end; i++) {
192  if( i >= (int) fields.size()) break;
193  temp += fields[i];
194  if(i!=end) temp+=sep;
195  }
196 
197  sprintf( buff, "%s", temp.c_str() );
198  return buff;
199 }
200 
201 }
202 }