Krotos Modules 3
Loading...
Searching...
No Matches
helpers.cpp
Go to the documentation of this file.
1namespace krotos
2{
3 namespace utils
4 {
5 std::string HttpDateToISO8601(const std::string& httpDate)
6 {
7 // HTTP date format:
8 // Weekday, DD Mon YYYY HH:MM:SS GMT
9
10 if (httpDate.empty())
11 {
12 return std::string("");
13 }
14
15 const std::string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
16 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
17
18 std::tm t = {};
19
20 // Parse DD
21 std::string day = httpDate.substr(5, 2);
22 t.tm_mday = std::stoi(day);
23
24 // Parse Mon
25 std::string monthStr = httpDate.substr(8, 3);
26 int monthIndex = -1;
27 for (int i = 0; i < 12; i++)
28 {
29 if (months[i] == monthStr)
30 {
31 monthIndex = i;
32 break;
33 }
34 }
35
36 if (monthIndex >= 0 && monthIndex <= 11)
37 {
38 t.tm_mon = monthIndex;
39 }
40
41 // Parse YYYY
42 std::string year = httpDate.substr(12, 4);
43 t.tm_year = std::stoi(year) - 1900;
44
45 // Parse HH:MM:SS
46 std::string time = httpDate.substr(17, 8);
47 std::string hour = time.substr(0, 2);
48 std::string min = time.substr(3, 2);
49 std::string sec = time.substr(6, 2);
50
51 t.tm_hour = std::stoi(hour);
52 t.tm_min = std::stoi(min);
53 t.tm_sec = std::stoi(sec);
54
55 // Convert to ISO 8601 format
56 std::stringstream ss;
57 ss << std::put_time(&t, "%FT%TZ");
58
59 return ss.str();
60 }
61
62 StringArray getVersionSegments(StringRef p)
63 {
64 auto segments = StringArray::fromTokens(p, ",.", "");
65 segments.trim();
66 segments.removeEmptyStrings();
67 return segments;
68 }
69
70 int getVersionAsHexIntegerFromParts(const StringArray& segments)
71 {
72 int value;
73 if (segments.size() == 2)
74 {
75 value = (segments[0].getIntValue() << 8) + segments[1].getIntValue();
76 }
77 else
78 {
79 value = (segments[0].getIntValue() << 16) + (segments[1].getIntValue() << 8) +
80 segments[2].getIntValue();
81 }
82
83 return value;
84 }
85
86 int getVersionAsHexInteger(StringRef versionString)
87 {
89 }
90
91 static bool compareFloats(const float& a, const float& b) { return a < b; }
92
93 String ellipsizeStringToWidth(const Font& font, const String& input, const int maxWidth)
94 {
95 // If the input width is zero or less, we return an empty string
96 if (maxWidth <= 0)
97 {
98 return String();
99 }
100 float widthMax = static_cast<float>(maxWidth);
101 Array<int> glyphs;
102 Array<float> xOffsets;
103 font.getGlyphPositions(input, glyphs, xOffsets);
104 float inputStringWidth = xOffsets.getLast();
105 // If the input string fits within the supplied max width, we can return it unchanged
106 if (inputStringWidth <= widthMax)
107 {
108 return input;
109 }
110 const String ellipsis = "..."; // Define the ellipsis which we will use to join the halves
111 const float ellipsisWidth = font.getStringWidthFloat(ellipsis);
112 const float halfStringWidth = (widthMax - ellipsisWidth) * 0.5f;
113 const float startOfSecondHalf = inputStringWidth - halfStringWidth;
114 // Find the index of the first array member to exceed halfStringWidth
115 // Couldn't use Array::indexOfSorted because it requires an exact match
116 // std::lower_bound uses a binary chop, so should be efficient
117 const int indexEndOfFirstHalf = static_cast<int>(
118 std::lower_bound(xOffsets.begin(), xOffsets.end(), halfStringWidth, &compareFloats) -
119 xOffsets.begin());
120 // Find the index of the first array member to exceed startOfSecondHalf
121 const int indexStartOfSecondHalf = static_cast<int>(
122 std::lower_bound(xOffsets.begin(), xOffsets.end(), startOfSecondHalf, &compareFloats) -
123 xOffsets.begin());
124 // Join the fist and second halves together with the ellipsis in the middle
125 return input.substring(0, indexEndOfFirstHalf - 1) + ellipsis +
126 input.substring(indexStartOfSecondHalf, input.length());
127 }
128
129 String convertFilePathString(const String& inputPath)
130 {
131 String result = inputPath;
132#if JUCE_WINDOWS
133 if (inputPath.containsChar('/'))
134 {
135 // replace / with \\ for windows
136 result = inputPath.replaceCharacter('/', '\\');
137 }
138#elif JUCE_MAC
139 if (inputPath.containsChar('\\'))
140 {
141 // replace \\ with / for mac
142 result = inputPath.replaceCharacter('\\', '/');
143 }
144#endif
145 return result;
146 }
147
148 void hexStringToByteArray(const std::string& input, std::vector<unsigned char>& byteArray)
149 {
150 // validate input string contains hex characters only
151 if (!std::all_of(input.begin(), input.end(), [](unsigned char c){ return std::isxdigit(c); }))
152 {
153 throw std::invalid_argument("Input string contains invalid hexadecimal characters.");
154 }
155
156 // Resize the byte array to half the size of the input string, adding an extra byte for odd-length input
157 // strings
158 byteArray.resize((input.length() % 2 != 0 ? input.length() + 1 : input.length()) / 2);
159
160 for (size_t i = 0, j = 0; i < input.length(); i += 2, j++)
161 {
162 // Add padding for odd-length input strings
163 std::string byteStr = i + 1 < input.length() ? input.substr(i, 2) : input.substr(i, 1) + "0";
164 unsigned int byteValue;
165 std::istringstream(byteStr) >> std::hex >> byteValue;
166 byteArray[j] = static_cast<unsigned char>(byteValue);
167 }
168 }
169
170 } // namespace utils
171} // namespace krotos
int getVersionAsHexInteger(StringRef versionString)
Definition helpers.cpp:86
void hexStringToByteArray(const std::string &input, std::vector< unsigned char > &byteArray)
Converts a hex string to a byte array.
Definition helpers.cpp:148
StringArray getVersionSegments(StringRef p)
Definition helpers.cpp:62
std::string HttpDateToISO8601(const std::string &httpDate)
Converts a date from HTTP-Date to ISO8601 format.
Definition helpers.cpp:5
static bool compareFloats(const float &a, const float &b)
Definition helpers.cpp:91
int getVersionAsHexIntegerFromParts(const StringArray &segments)
Definition helpers.cpp:70
String convertFilePathString(const String &inputPath)
Converts a file path passed as a raw string, to the current platform. Has no effect if the path is al...
Definition helpers.cpp:129
String ellipsizeStringToWidth(const Font &font, const String &input, const int maxWidth)
Cuts the center out of a String to make it fit a given pixel width.
Definition helpers.cpp:93
Definition AirAbsorptionFilter.cpp:2