Skip to content

Commit 4810c59

Browse files
committed
Add time related functions
1 parent fdbf058 commit 4810c59

7 files changed

+443
-160
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2018 Juraj Smiesko
1+
Copyright 2019 Juraj Smiesko
22

33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

Test.cxx

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* \file Test.cxx
3-
* \brief Simple test of the library.
4-
*/
5-
61
// std
72
#include <iostream>
83
// Throw

Throw.cxx

-151
Original file line numberDiff line numberDiff line change
@@ -5,163 +5,12 @@
55

66

77
// std
8-
#include <sstream>
98
#include <string>
109
#include <vector>
1110
// Throw
1211
#include "Throw.h"
1312

1413

15-
/**
16-
* \ingroup StringsManip
17-
* \brief Returns random string of length 6.
18-
*/
19-
std::string Throw::RandomString() {
20-
std::string str = RandomString(6);
21-
22-
return str;
23-
}
24-
25-
/**
26-
* \ingroup StringsManip
27-
* \brief Returns random string of variable length.
28-
*
29-
* Solution found
30-
* <a href="https://stackoverflow.com/questions/440133">here</a>.
31-
*/
32-
std::string Throw::RandomString(size_t length) {
33-
auto randchar = []() -> char {
34-
const char charset[] =
35-
"0123456789"
36-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37-
"abcdefghijklmnopqrstuvwxyz";
38-
const size_t max_index = (sizeof(charset) - 1);
39-
return charset[ rand() % max_index ];
40-
};
41-
std::string str(length, 0);
42-
std::generate_n(str.begin(), length, randchar);
43-
44-
return str;
45-
}
46-
47-
/**
48-
* \ingroup StringsManip
49-
* \brief Splits string at delimiters and returns vector of strings.
50-
*
51-
* Solution found
52-
* <a href="https://www.fluentcpp.com/2017/04/21/">here</a>.
53-
*/
54-
std::vector<std::string> Throw::SplitString(const std::string& s,
55-
char delimiter) {
56-
std::vector<std::string> tokens;
57-
std::string token;
58-
std::istringstream tokenStream(s);
59-
while (std::getline(tokenStream, token, delimiter)) {
60-
tokens.emplace_back(token);
61-
}
62-
63-
return tokens;
64-
}
65-
66-
/**
67-
* \ingroup StringsManip
68-
* \brief Replace sub-string in first string.
69-
*
70-
* Solution found
71-
* <a href="https://stackoverflow.com/questions/3418231">here</a>.
72-
*/
73-
bool Throw::ReplaceString(std::string& firstString,
74-
const std::string& fromString,
75-
const std::string& toString) {
76-
size_t startPos = firstString.find(fromString, 0);
77-
if (startPos == std::string::npos) {
78-
return false;
79-
}
80-
81-
firstString.replace(startPos, fromString.length(), toString);
82-
return true;
83-
}
84-
85-
/**
86-
* \ingroup StringsManip
87-
* \brief Remove last character from the first string if the first string
88-
* contains it.
89-
*/
90-
bool Throw::RemoveLastCharacter(std::string& firstString,
91-
const std::string& c) {
92-
if (firstString.empty()) {
93-
return false;
94-
}
95-
96-
if (c.size() != 1) {
97-
return false;
98-
}
99-
100-
if (c.compare(&firstString.back()) != 0) {
101-
return false;
102-
}
103-
104-
firstString.replace(firstString.size() - 1, firstString.size(), "");
105-
106-
return true;
107-
}
108-
109-
/**
110-
* \ingroup StringsManip
111-
* \brief Returns true if second string is found inside of the first one.
112-
*/
113-
bool Throw::FindString(const std::string& firstString,
114-
const std::string& secondString) {
115-
return firstString.find(secondString, 0) != std::string::npos;
116-
}
117-
118-
/**
119-
* \ingroup StringsManip
120-
* \brief Returns true if strings match exactly.
121-
*/
122-
bool Throw::StringsMatch(const std::string& firstString,
123-
const std::string& secondString) {
124-
125-
return firstString.compare(secondString) == 0;
126-
}
127-
128-
/**
129-
* \brief Default constructor of InputParser.
130-
* \param argc number of program parameters.
131-
* \param argv array of program parameters.
132-
*/
133-
Throw::InputParser::InputParser (int& argc, char** argv) {
134-
for (size_t i = 1; i < argc; ++i) {
135-
this->tokens.emplace_back(std::string(argv[i]));
136-
}
137-
}
138-
139-
/**
140-
* \brief Get parameter value.
141-
* \param option parameter name.
142-
*/
143-
const std::string& Throw::InputParser::getCmdOption(
144-
const std::string& option) const {
145-
std::vector<std::string>::const_iterator itr;
146-
itr = find(this->tokens.begin(), this->tokens.end(), option);
147-
if (itr != this->tokens.end() && ++itr != this->tokens.end()) {
148-
return *itr;
149-
}
150-
static const std::string empty_string;
151-
152-
return empty_string;
153-
}
154-
155-
/**
156-
* \brief Test whether the parameter exists.
157-
* \param option parameter name.
158-
*/
159-
bool Throw::InputParser::cmdOptionExists(const std::string& option) const {
160-
return find(this->tokens.begin(), this->tokens.end(), option)
161-
!= this->tokens.end();
162-
}
163-
164-
16514
// Graph point
16615
double Throw::GetPointX(TGraph* graph, size_t i) {
16716
double x, y;

Throw.h

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
namespace Throw {
2323
/**
24-
* \defgroup StringManip Strings manipulation
25-
* \brief Functions for manipulation with strings
24+
* \defgroup StringManipulation
25+
* \brief Functions for manipulation with strings.
2626
* @{
2727
*/
2828
std::string RandomString();
@@ -37,7 +37,7 @@ namespace Throw {
3737

3838
/**
3939
* \class InputParser
40-
* \brief Input parser
40+
* \brief Input parser.
4141
*
4242
* Solution found
4343
* <a href="https://stackoverflow.com/questions/865668">here</a>.
@@ -52,6 +52,24 @@ namespace Throw {
5252
};
5353

5454

55+
/**
56+
* \defgroup Time
57+
* \brief Time related fuctions.
58+
* @{
59+
*/
60+
tm GetCurrentTime();
61+
std::string MonthNumToName(int);
62+
std::string GetCurrentYear();
63+
std::string GetCurrentMonth();
64+
std::string GetCurrentDay();
65+
std::string GetCurrentHour();
66+
std::string GetCurrentMinute();
67+
std::string GetCurrentSecond();
68+
std::string Now();
69+
std::string Today();
70+
std::string Yesterday();
71+
/** @} */
72+
5573
// Graph point
5674
double GetPointX(TGraph*, size_t);
5775
double GetPointX(TGraphAsymmErrors*, size_t);

ThrowInputParser.cxx

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* \file ThrowInputParser.cxx
3+
* \brief Implementation of InputParser.
4+
*/
5+
6+
7+
// std
8+
#include <string>
9+
#include <vector>
10+
// Throw
11+
#include "Throw.h"
12+
13+
14+
/**
15+
* \brief Default constructor of InputParser.
16+
* \param argc number of program parameters.
17+
* \param argv array of program parameters.
18+
*/
19+
Throw::InputParser::InputParser (int& argc, char** argv) {
20+
for (size_t i = 1; i < argc; ++i) {
21+
this->tokens.emplace_back(std::string(argv[i]));
22+
}
23+
}
24+
25+
/**
26+
* \brief Get parameter value.
27+
* \param option parameter name.
28+
*/
29+
const std::string& Throw::InputParser::getCmdOption(
30+
const std::string& option) const {
31+
std::vector<std::string>::const_iterator itr;
32+
itr = find(this->tokens.begin(), this->tokens.end(), option);
33+
if (itr != this->tokens.end() && ++itr != this->tokens.end()) {
34+
return *itr;
35+
}
36+
static const std::string empty_string;
37+
38+
return empty_string;
39+
}
40+
41+
/**
42+
* \brief Test whether the parameter exists.
43+
* \param option parameter name.
44+
*/
45+
bool Throw::InputParser::cmdOptionExists(const std::string& option) const {
46+
return find(this->tokens.begin(), this->tokens.end(), option)
47+
!= this->tokens.end();
48+
}

ThrowStringManipulation.cxx

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* \file ThrowStringManipulation.cxx
3+
* \brief Implementation of functions for string manipulation.
4+
*/
5+
6+
7+
// std
8+
#include <sstream>
9+
#include <string>
10+
#include <vector>
11+
// Throw
12+
#include "Throw.h"
13+
14+
15+
/**
16+
* \ingroup StringsManipulation
17+
* \brief Returns random string of length 6.
18+
*/
19+
std::string Throw::RandomString() {
20+
std::string str = RandomString(6);
21+
22+
return str;
23+
}
24+
25+
/**
26+
* \ingroup StringsManipulation
27+
* \brief Returns random string of variable length.
28+
*
29+
* Solution found
30+
* <a href="https://stackoverflow.com/questions/440133">here</a>.
31+
*/
32+
std::string Throw::RandomString(size_t length) {
33+
auto randchar = []() -> char {
34+
const char charset[] =
35+
"0123456789"
36+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37+
"abcdefghijklmnopqrstuvwxyz";
38+
const size_t max_index = (sizeof(charset) - 1);
39+
return charset[ rand() % max_index ];
40+
};
41+
std::string str(length, 0);
42+
std::generate_n(str.begin(), length, randchar);
43+
44+
return str;
45+
}
46+
47+
/**
48+
* \ingroup StringsManipulation
49+
* \brief Splits string at delimiters and returns vector of strings.
50+
*
51+
* Solution found
52+
* <a href="https://www.fluentcpp.com/2017/04/21/">here</a>.
53+
*/
54+
std::vector<std::string> Throw::SplitString(const std::string& s,
55+
char delimiter) {
56+
std::vector<std::string> tokens;
57+
std::string token;
58+
std::istringstream tokenStream(s);
59+
while (std::getline(tokenStream, token, delimiter)) {
60+
tokens.emplace_back(token);
61+
}
62+
63+
return tokens;
64+
}
65+
66+
/**
67+
* \ingroup StringsManipulation
68+
* \brief Replace sub-string in first string.
69+
*
70+
* Solution found
71+
* <a href="https://stackoverflow.com/questions/3418231">here</a>.
72+
*/
73+
bool Throw::ReplaceString(std::string& firstString,
74+
const std::string& fromString,
75+
const std::string& toString) {
76+
size_t startPos = firstString.find(fromString, 0);
77+
if (startPos == std::string::npos) {
78+
return false;
79+
}
80+
81+
firstString.replace(startPos, fromString.length(), toString);
82+
return true;
83+
}
84+
85+
/**
86+
* \ingroup StringsManipulation
87+
* \brief Remove last character from the first string if the first string
88+
* contains it.
89+
*/
90+
bool Throw::RemoveLastCharacter(std::string& firstString,
91+
const std::string& c) {
92+
if (firstString.empty()) {
93+
return false;
94+
}
95+
96+
if (c.size() != 1) {
97+
return false;
98+
}
99+
100+
if (c.compare(&firstString.back()) != 0) {
101+
return false;
102+
}
103+
104+
firstString.replace(firstString.size() - 1, firstString.size(), "");
105+
106+
return true;
107+
}
108+
109+
/**
110+
* \ingroup StringsManipulation
111+
* \brief Returns true if second string is found inside of the first one.
112+
*/
113+
bool Throw::FindString(const std::string& firstString,
114+
const std::string& secondString) {
115+
return firstString.find(secondString, 0) != std::string::npos;
116+
}
117+
118+
/**
119+
* \ingroup StringsManipulation
120+
* \brief Returns true if strings match exactly.
121+
*/
122+
bool Throw::StringsMatch(const std::string& firstString,
123+
const std::string& secondString) {
124+
125+
return firstString.compare(secondString) == 0;
126+
}

0 commit comments

Comments
 (0)