misc.cpp 6.52 KB
Newer Older
dino's avatar
 
dino committed
1
#include "misc.h"
2 3

#include <iostream>
dino's avatar
 
dino committed
4
#include <fstream>
5
#include <algorithm>
dino's avatar
 
dino committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

using namespace std;

void stringTrim(std::string &str)
{
  size_t ll = str.length();

  // get read of empty  strings
  if(ll <= 0)
    return;

  // get read of the CR character
  // (e.g. files written on Windows (\r\n) and read on Unix(\n) )
  if(str[ll-1] == '\r') {
    str = str.substr(0, ll-1);
    ll--;
  }

  // discard if now empty
  if(ll <= 0)
    return;

  size_t ii = str.find_first_not_of(" \t");

  // contains only white space
  if(ii == string::npos) {
    str.clear();
    return;
  }

  // remove white space at the beginning
  if(ii > 0) {
    str = str.substr(ii);
    ll = str.length();
  }

  ii = str.find_last_not_of(" \t");

  // remove white space at the end
francesco's avatar
francesco committed
45 46
//  if(ii >= 0 && ii < ll-1)
  if(ii < ll-1)
dino's avatar
 
dino committed
47 48 49
    str = str.substr(0, ii+1);
}

dino's avatar
dino committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
void  stringRemove(std::string &str, const std::string rem, bool by_char) {
  if(by_char) {
    size_t nn = str.find_first_of(rem);
    while(nn != string::npos) {
      str = str.substr(0, nn) + str.substr(nn+1);
      nn = str.find_first_of(rem);
    }
  }
  else {
    size_t nn = str.find(rem);
    while(nn != string::npos) {
      str = str.substr(0, nn) + str.substr(nn+rem.size());
      nn = str.find(rem);
    }
  }
}

67
bool stringSplit(std::string &line, const std::string sep, std::string &keyw)
dino's avatar
 
dino committed
68 69 70 71 72 73
{
  stringTrim(line);
  if(line.size() == 0)
    return false;
  if(line[0] == '#' )
    return false;
74 75
  size_t npos1 = line.find_first_not_of(sep, 0);
  size_t npos2 = line.find_first_of(sep, npos1);
dino's avatar
 
dino committed
76 77 78 79 80 81 82
  keyw = line.substr(npos1, npos2);
  line = line.substr(npos2+1);
  stringTrim(keyw);
  stringTrim(line);
  return true;
}

83
bool stringSplit(std::string &line, const std::string sep, std::string &keyw, std::string &data)
dino's avatar
 
dino committed
84 85 86 87 88 89
{
  stringTrim(line);
  if(line.size() == 0)
    return false;
  if(line[0] == '#' )
    return false;
90 91
  size_t npos1 = line.find_first_not_of(sep, 0);
  size_t npos2 = line.find_first_of(sep, npos1);
dino's avatar
 
dino committed
92 93 94 95 96 97 98 99 100 101
  keyw = line.substr(npos1, npos2);
  if(npos2 != string::npos)
    data = line.substr(npos2+1);
  else
    data.clear();
  stringTrim(keyw);
  stringTrim(data);
  return true;
}

102
bool stringIncrement(std::string &str, int count)
dino's avatar
 
dino committed
103 104 105 106
{
  if(count == 0)
    return false;

107
  size_t lf = str.size();
dino's avatar
 
dino committed
108 109 110 111 112 113 114
  if(lf == string::npos || lf < 1)
    return false;

  if(count > 0) {
    // increment
    for(int nn = 0; nn < count; nn++) {
      for(size_t ll = lf-1; ll > 0; ll--) {
115 116
        if(str[ll] >= '0' && str[ll] < '9') {
          str[ll]++;
dino's avatar
 
dino committed
117 118
          break;
        }
119 120
        else if(str[ll] == '9') {
          str[ll] = '0';
dino's avatar
 
dino committed
121
        }
122 123
        else if(str[ll] >= 'a' && str[ll] < 'z') {
          str[ll]++;
dino's avatar
 
dino committed
124 125
          break;
        }
126 127
        else if(str[ll] == 'z') {
          str[ll] = 'a';
dino's avatar
 
dino committed
128
        }
129 130
        else if(str[ll] >= 'A' && str[ll] < 'Z') {
          str[ll]++;
dino's avatar
 
dino committed
131 132
          break;
        }
133 134
        else if(str[ll] == 'Z') {
          str[ll] = 'A';
dino's avatar
 
dino committed
135 136 137 138 139 140 141 142 143 144 145
        }
        else {
          return false;
        }
      }
    }
  }
  else {
    // decrement
    for(int nn = 0; nn < -count; nn++) {
      for(size_t ll = lf-1; ll > 0; ll--) {
146 147
        if(str[ll] >= '0' && str[ll] < '9') {
          str[ll]--;
dino's avatar
 
dino committed
148 149
          break;
        }
150 151
        else if(str[ll] == '0') {
          str[ll] = '9';
dino's avatar
 
dino committed
152
        }
153 154
        else if(str[ll] >= 'a' && str[ll] < 'z') {
          str[ll]++;
dino's avatar
 
dino committed
155 156
          break;
        }
157 158
        else if(str[ll] == 'a') {
          str[ll] = 'z';
dino's avatar
 
dino committed
159
        }
160 161
        else if(str[ll] >= 'A' && str[ll] < 'Z') {
          str[ll]++;
dino's avatar
 
dino committed
162 163
          break;
        }
164 165
        else if(str[ll] == 'A') {
          str[ll] = 'Z';
dino's avatar
 
dino committed
166 167 168 169 170 171 172 173 174
        }
        else 
          return false;
      }
    }
  }
  return true;
}

175
bool stringIncrement(std::string &str, const std::string sep, int count)
dino's avatar
 
dino committed
176 177 178 179
{
  if(count == 0)
    return false;

180
  size_t lf = str.size();
dino's avatar
 
dino committed
181 182 183
  if(lf == string::npos || lf < 1)
    return false;

184
  size_t lff = (int)str.find_last_of(sep);
dino's avatar
 
dino committed
185 186 187
  if(lff == string::npos)
    return false;

188
  string fnn = str.substr(0, lff);
dino's avatar
 
dino committed
189
  if(stringIncrement(fnn, count)) {
190
    str = fnn + str.substr(lff);
dino's avatar
 
dino committed
191 192 193 194
    return true;
  }

  return false;
195
}
dino's avatar
 
dino committed
196

197
bool stringEq(const std::string &s1, const std::string &s2, bool ignorecase) {
198
  string sLow1(s1), sLow2(s2);
199 200 201 202
  if(ignorecase) {
    transform(sLow1.begin(), sLow1.end(), sLow1.begin(), ::tolower);
    transform(sLow2.begin(), sLow2.end(), sLow2.begin(), ::tolower);
  }
203 204 205 206 207 208 209 210
  return (sLow1 == sLow2);
}

bool stringHas(const std::string &s1, const std::string &s2, bool ignorecase) {
  string sLow1(s1), sLow2(s2);
  if(ignorecase) {
    transform(sLow1.begin(), sLow1.end(), sLow1.begin(), ::tolower);
    transform(sLow2.begin(), sLow2.end(), sLow2.begin(), ::tolower);
211
  }
212 213
  size_t ind = sLow1.find(sLow2);
  return (ind != string::npos);
dino's avatar
 
dino committed
214 215
}

216
int getKeyFromFile(const std::string filename, const std::string theKey, std::string &theValue, bool verbose)
217 218 219
{
  ifstream configfile( filename.c_str() );
  if(!configfile.good()) {
dino's avatar
 
dino committed
220
    cout << "Error opening " << filename << endl;
221 222 223
    return 2;   // error
  }
  if(verbose)
dino's avatar
dino committed
224
    cout << "getKeyFromFile() reading from " << filename << endl;
225 226 227 228

  string line, keyw, data;
  while(getline(configfile, line)) {
    if(!stringSplit(line, " \t", keyw, data))
229
      continue;           // empty or comment lines
230 231 232 233 234 235 236 237 238
    if(verbose)
      cout << line << endl;
    if(keyw == theKey) {
      theValue = data;    // data can be null
      return 0;           // found
    }
  }
  return 1;   // not found
}
239

dino's avatar
 
dino committed
240
void  forceTailSlash(std::string &dr) {
241 242 243
  if( dr.size()>0 && dr.at(dr.size()-1)!= '/' ) dr += '/';
} 

244 245 246 247 248 249 250 251 252
bool fileExists(std::string fname)
{
  FILE *fp = fopen(fname.c_str(), "r");
  if(fp) {
    fclose(fp);
    return true;
  }
  return false;
}
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

void printInfo(const std::string &s1) {
  cout << s1 << endl;
}

void printInfo(const std::string &s1, const std::string &s2) {
  const int p2 = 23;

  cout << s1;
  int n1 = p2-int(s1.size());
  for(int ii = 0; ii < n1; ii++) 
    cout << " ";

  cout << s2 << endl;
}

void printInfo(const std::string &s1, const std::string &s2, const std::string &s3) {
  const int p2 = 23;
  const int p3 = 45;

  cout << s1;
  int n1 = p2 - int(s1.size());
  if(n1 < 0) n1 = 0;
  for(int ii = 0; ii < n1; ii++) 
    std::cout << " ";

  cout << s2;
  int n2 = p3 - (int(s1.size()) + n1 + int(s2.size()));
  if(n2 < 0) n2 = 0;
  for(int ii = 0; ii < n2; ii++) 
    cout << " ";

  cout << s3 << endl;
}