-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctutilities.cpp
More file actions
120 lines (104 loc) · 3.23 KB
/
ctutilities.cpp
File metadata and controls
120 lines (104 loc) · 3.23 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Name: Phillip Tette
Class: CIS170L
Professor: Penn Wu
Project Name: Tower of Hanoi
Date: 20200810
Date Modified: 20200827
*/
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include "ctutilities.h"
using namespace std;
vector<string> parseCSV(const string& line) {
/* Will read a CSV file line by line converting each line into a stringstream.
* With a comma as the delimiter, the strings will be entered one at a time into
* the string variable "item", and will be recorded into the vector results as
* elements, and results will be returned.
*/
vector<string> result;
stringstream record(line);
string item;
while (getline(record, item, ',')) {
result.push_back(item);
}
return result;
}
string writeCSV(const vector<string>& data) {
/* Will read each string in a vector
* converting each element into
*/
stringstream record;
record << data[0];
for (unsigned int i = 1; i < data.size(); i++) {
record << ',' << data[i];
}
return record.str();
}
bool fileExists(const string& file) {
fstream f(file.c_str());
return f.good();
}
string input(const string& query, const char delim) {
string result;
cout << query;
getline(cin, result, delim);
return result;
}
string capsMe(const string& lcString) {
string ucString;
for (unsigned int i = 0; i < lcString.size(); i++) {
ucString.push_back(toupper(lcString.at(i)));
}
return ucString;
}
bool isValidOption(const string& choice, const vector<string>& values) {
// Will validate all input assuming it's a string.
return !choice.empty() && find(values.begin(), values.end(), choice) != values.end();
}
string padMe(const string& txt, const size_t len, const char dir, const char pad) {
switch (dir) {
case 'l':
return string(len - txt.length(), pad) + txt;
case 'c':
return string((len - txt.length()) / 2, pad) + txt +
string((len - txt.length()) / 2 + (len - txt.length()) % 2, pad);
case 'r':
default:
return txt + string(len - txt.length(), pad);
}
}
string lPadMe(const string& txt, const size_t len, const char pad) {
return padMe(txt, len, 'l', pad);
}
string cPadMe(const string& txt, const size_t len, const char pad) {
return padMe(txt, len, 'c', pad);
}
string rPadMe(const string& txt, const size_t len, const char pad) {
return padMe(txt, len, 'r', pad);
}
string addMargin(const string& txt, const size_t len, const char dir, const char pad) {
switch (dir) {
case 'l':
return string(len, pad) + txt;
case 'c':
return string(len / 2, pad) + txt +
string(len / 2 + len % 2, pad);
case 'r':
default:
return txt + string(len, pad);
}
}
string lMargin(const string& txt, const size_t len, const char pad) {
return addMargin(txt, len, 'l', pad);
}
string cMargin(const string& txt, const size_t len, const char pad) {
return addMargin(txt, len, 'c', pad);
}
string rMargin(const string& txt, const size_t len, const char pad) {
return addMargin(txt, len, 'r', pad);
}