-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
162 lines (139 loc) · 4.38 KB
/
util.h
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <ctime>
#include <algorithm>
#include <fstream>
#include <cstdlib>
#ifdef DEBUG
#define LOG(x) std::cout << x << std::endl
#else
#define LOG(x)
#endif
// split string with delimeter
std::vector<std::string> split(std::string strToSplit, char delimeter) {
std::stringstream ss(strToSplit);
std::string item;
std::vector<std::string> splittedStrings;
while (std::getline(ss, item, delimeter)) {
splittedStrings.push_back(item);
}
return splittedStrings;
}
// this function turns timestamps into DD/MM/YYYY format e.g.17/05/2021, 03/05/2020
std::string timestampToString(const time_t rawtime) {
tm *ltm = localtime(&rawtime);
std::string date_string = (std::to_string(ltm->tm_mday).size()<2 ? "0":"") + std::to_string(ltm->tm_mday) + "/";
date_string += (std::to_string(1 + ltm->tm_mon).size()<2 ? "0":"") + std::to_string(1 + ltm->tm_mon) + "/";
date_string += std::to_string(1900 + ltm->tm_year);
return date_string;
}
// this function turns "DD/MM/YYYY" into timestamp
int dateStringToTimestamp(std::string& d) {
std::vector<std::string> a = split(d, '/');
int day, month, year;
day = std::stoi(a.at(0));
month = std::stoi(a.at(1));
year = std::stoi(a.at(2));
struct tm t = {0};
t.tm_mday = day;
t.tm_mon = month - 1;
t.tm_year = year - 1900;
time_t when = mktime(&t); // the when variable gives the timestamp
const struct tm *norm = localtime(&when);
return when;
}
// gives the current timestamp
int timestampNow() {
return std::time(nullptr);
}
// this function returns number of dots (given in as parameter) in a string
// cash A/c ............... Dr. (this kind of dots)
std::string dots(int x) {
std::string s;
for (int i = 0; i < x; i++) s += ".";
return s;
}
// ledger name validator (ledger names cannot contain comma)
Ledger* ledgerExists(std::string& s, std::vector<Ledger>* ledgerList) {
for(size_t i = 0; i<ledgerList->size(); i++) {
if(ledgerList->at(i).getName() == s) return &(ledgerList->at(i));
}
return NULL;
}
// check if date is valid or not (DD/MM/YYYY format)
bool isValidDateString(std::string& d) {
try {
std::vector<std::string> a = split(d, '/');
int day, month, year;
day = std::stoi(a.at(0));
month = std::stoi(a.at(1));
year = std::stoi(a.at(2));
struct tm t = {0};
t.tm_mday = day;
t.tm_mon = month - 1;
t.tm_year = year - 1900;
time_t when = mktime(&t); // the when variable gives the timestamp
const struct tm *norm = localtime(&when);
return (norm->tm_mday == day &&
norm->tm_mon == month - 1 &&
norm->tm_year == year - 1900);
}
catch(std::exception e) {return false;}
}
// credit printing at the start of the application
void printCredit() {
std::cout << "######################################" << std::endl;
std::cout << "# #" << std::endl;
std::cout << "# WELCOME TO ACCOUNTLITE #" << std::endl;
std::cout << "# - Ayan Chakraborty #" << std::endl;
std::cout << "# - @captainAyan (Github) #" << std::endl;
std::cout << "# #" << std::endl;
std::cout << "######################################" << std::endl;
}
std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first) return str;
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
std::string getInput() {
std::string s;
std::getline(std::cin, s);
return trim(s);
}
std::string formatCurrency(int amount, std::string type) {
std::string a = std::to_string(amount);
if(type == "ind") {
// do the Indian 🇮🇳 formatting
if(a.size() > 3) a.insert(a.size()-3, 1, ',');
for(int i=a.length()-6; i>0; i-=2) {
a.insert(i, 1, ',');
}
return a;
}
else if(type == "int") {
// do the international formatting
for(int i=a.length()-3; i>0; i-=3) {
a.insert(i, 1, ',');
}
return a;
}
else return a;
}
std::string toLowerCase(std::string my_str) {
std::transform(my_str.begin(), my_str.end(), my_str.begin(), ::tolower);
return my_str;
}
void fileSave(std::string data) {
std::ofstream f;
f.open (FILE_NAME);
f << data;
f.close();
}
void clearScreen() {
if (getenv ("SHELL")!=NULL) std::system("clear");
else std::system("cls");
}