summaryrefslogtreecommitdiff
path: root/src/rl_str_proc.cc
blob: a35ff2f82230b20ead78ddf3ef88cb488be983dd (plain)
1
2
3
4
5
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Module: rl_str_proc.cc
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the 
 * Free Software Foundation.
 */
#include "rl_str_proc.hh"

using namespace std;

/**
 *
 **/
StrProc::StrProc(const string &in_str, const string &token)
{
  string tmp = in_str;
  
  //convert tabs to spaces
  size_t pos = 0;
  string tabtospace = "    ";
  string::iterator iter = tmp.begin();
  while ((pos = tmp.find("\t", pos)) != string::npos) {
    tmp.replace(pos, 1, tabtospace);
    pos += tabtospace.length();
  }
  
  // convert & to &
  string amp = "&";
  pos = 0;
  while ((pos = tmp.find("&", pos)) != string::npos) {
    tmp.replace(pos, 1, amp);
    pos += amp.length();
  }
  
  //remove the cr
  pos = tmp.find("\n");
  if (pos != string::npos) {
    tmp.replace(pos, 1, "");
  }

  //now handle the case of the multiple length token
  //note that we are using the '~' as a token internally
  size_t start = 0, end;
  while ((start = tmp.find(token, start)) != string::npos) {
    tmp.replace(start, token.length(), "~");
  }


  while ((start = tmp.find_first_not_of("~")) != string::npos) {
    tmp = tmp.substr(start, tmp.length() - start);
    end = tmp.find_first_of("~");
    _str_coll.push_back(tmp.substr(0, end));
    tmp = tmp.substr(end+1, tmp.length() - end-1);
    if (end == string::npos) {
      break;
    }
  }
}

/**
 *
 **/
string
StrProc::get(int i)
{
  if (((unsigned int) i) >= _str_coll.size()) {
    return string("");
  }
  return _str_coll[i];
}

/**
 *
 **/
string
StrProc::get(int start, int end)
{
  if (((unsigned int) start) >= _str_coll.size()) {
    return string("");
  }

  string tmp;
  for (int i = start; (i < end) && (((unsigned int) i) < _str_coll.size());
       ++i) {
    tmp += _str_coll[i] + " ";
  }
  return tmp.substr(0,tmp.length()-1);
}

/**
 *
 **/
vector<string>
StrProc::get()
{
  return _str_coll;
}