-
Notifications
You must be signed in to change notification settings - Fork 4
/
string_additions.cc
95 lines (83 loc) · 2.01 KB
/
string_additions.cc
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
//#include "network.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
#include "string_additions.h"
using namespace std;
/** Compares string with a vector of strings. Returns
the index if it finds a match, otherwise -1 **/
int strCmpList(vector<string> list,string str){
for (unsigned int ndx=0;ndx<list.size();ndx++){
if(list[ndx].compare(str) == 0){
return ndx;
}
}
return -1;
}
/** Function that will split a string based on the
delimiters used if there are two delimeters in a
row it will skip them **/
vector<string> split(string line, string delimeter){
vector<string> token;
string temp=line;
int ndx;
while (!temp.empty()){
ndx = temp.find_first_of(delimeter);
if (ndx>0){
token.push_back(temp.substr(0,ndx));
}
else if (ndx ==-1){
token.push_back(temp);
return token;
}
temp=temp.substr(ndx+1);
}
return token;
}
/** While this is messy it was messy to convert an array
strings to a vector of strings **/
vector<string> strAry2StrVec(string list[]){
vector<string> veclist;
int ndx=0;
while (list[ndx]!="NULL"){
veclist.push_back(list[ndx]);
ndx++;
}
return veclist;
}
/** Function takes in a string and returns a double **/
double convertToDouble(string const& str){
istringstream i(str);
double x;
if (!(i >> x)){
cout << "Bad string to double conversion" << endl;
exit(0);
}
return x;
}
/** Function takes in a string and returns an int **/
int convertToInt(string const& str){
istringstream i(str);
int x;
if (!(i >> x)){
cout << "Bad string to int conversion" << endl;
exit(0);
}
return x;
}
/** Function takes in a double and returns a string **/
string doubleToString(float const& dbl){
ostringstream buffer;
if (!(buffer << dbl)){
cout << "Bad double to string conversion" << endl;
exit(0);
}
return buffer.str();
}
//convert int to string
string intAsString(int number) {
std::ostringstream sin;
sin << number;
std::string val = sin.str();
return val;
}