forked from ParadoxGameConverters/commonItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonFunctions.cpp
102 lines (88 loc) · 2.26 KB
/
CommonFunctions.cpp
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
#include "CommonFunctions.h"
#include "OSCompatibilityLayer.h"
#include <algorithm>
#include <array>
std::string trimPath(const std::string& fileName)
{
auto lastSlash = fileName.find_last_of('\\');
auto trimmedFileName = fileName.substr(lastSlash + 1, fileName.length());
lastSlash = trimmedFileName.find_last_of('/');
trimmedFileName = trimmedFileName.substr(lastSlash + 1, trimmedFileName.length());
return trimmedFileName;
}
std::string getPath(const std::string& fileName)
{
const auto rawFile = trimPath(fileName);
const auto filePos = fileName.find(rawFile);
return fileName.substr(0, filePos);
}
std::string trimExtension(const std::string& fileName)
{
const auto rawFile = trimPath(fileName);
const auto dotPos = rawFile.find_last_of('.');
if (dotPos == std::string::npos)
{
return fileName;
}
return fileName.substr(0, fileName.find(rawFile) + dotPos);
}
std::string getExtension(const std::string& fileName)
{
const auto rawFile = trimPath(fileName);
const auto dotPos = rawFile.find_last_of('.');
if (dotPos == std::string::npos)
{
return {};
}
return rawFile.substr(dotPos + 1);
}
std::string replaceCharacter(std::string fileName, const char character)
{
std::ranges::replace(fileName, character, '_');
return fileName;
}
std::string cardinalToOrdinal(const int cardinal)
{
const auto hundredRemainder = cardinal % 100;
const auto tenRemainder = cardinal % 10;
if (hundredRemainder - tenRemainder == 10)
{
return "th";
}
switch (tenRemainder)
{
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
std::string cardinalToRoman(int number)
{
const std::array numbers{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
const std::array symbols{"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
int i = static_cast<int>(numbers.size()) - 1;
std::string toReturn;
while (number > 0)
{
auto div = number / numbers[i];
number = number % numbers[i];
while (div-- > 0)
{
toReturn += symbols[i];
}
i--;
}
return toReturn;
}
std::string normalizeStringPath(const std::string& stringPath)
{
auto toReturn = commonItems::normalizeUTF8Path(stringPath);
toReturn = replaceCharacter(toReturn, '-');
toReturn = replaceCharacter(toReturn, ' ');
return toReturn;
}