Debian Codegraph @

Utils/Utils.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <chrono>
24 #include <deque>
25 #include <future>
26 #include <map>
27 #include <string>
28 #include <sys/stat.h>
29 #include <thread>
30 #include <vector>
31
32 #include "DataTypes.h"
33
34 namespace Logger
35 {
36 class Logger;
37 }
38
39 namespace Utils
40 {
41 class Utils
42 {
43 public:
44 static void ReplaceString(std::string& str, const std::string& from, const std::string& to);
45 static void SplitString(const std::string& str, const std::string& delimiter, std::deque<std::string>& list);
46 static void SplitString(const std::string& input, const std::string& delimiter, std::string& first, std::string& second);
47 static std::string StringBeforeDelimiter(const std::string& input, const std::string& delimiter);
48 static std::string UrlDecode(const std::string& value);
49 static std::string UrlEncode(const std::string& value);
50 static std::string HtmlEncode(const std::string& value);
51
52 static inline bool IsMapEntrySet(const std::map<std::string,std::string>& map, const std::string& key)
53 {
54 return map.count(key) != 0;
55 }
56
57 static const std::string& GetStringMapEntry(const std::map<std::string,std::string>& map, const std::string& key, const std::string& defaultValue = "");
58 static int GetIntegerMapEntry(const std::map<std::string,std::string>& map, const std::string& key, const int defaultValue = 0);
59 static bool GetBoolMapEntry(const std::map<std::string,std::string>& map, const std::string& key, const bool defaultValue = false);
60 static std::string ToStringWithLeadingZeros(const unsigned int number, const unsigned char chars);
61
62 static bool StringToBool(const std::string& value, const bool defaultValue = false);
63 static std::string StringToLower(const std::string& input);
64
65 static inline bool FileExists(const std::string& name)
66 {
67 struct stat buffer;
68 return (stat(name.c_str(), &buffer) == 0);
69 }
70
71 static void CopyFile(Logger::Logger* logger, const std::string& from, const std::string& to);
72 static void RenameFile(Logger::Logger* logger, const std::string& from, const std::string& to);
73
74 static void RemoveOldBackupFiles (Logger::Logger *logger,
75 const std::string &filename,
76 unsigned int keepBackups);
77
78 static inline void SetThreadName(const std::string& name)
79 {
80 SetThreadName(name.c_str());
81 }
82
83 static inline void SetThreadName(__attribute__((unused)) const char* name)
84 {
85 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12)
86 pthread_setname_np(pthread_self(), name);
87 #endif
88 }
89 static void SetMinThreadPriority();
90 static std::string TimestampToDate(const time_t timestamp);
91
92 static inline void SleepForSeconds(unsigned int seconds)
93 {
94 std::this_thread::sleep_for(std::chrono::seconds(seconds));
95 }
96
97 static inline void SleepForMilliseconds(unsigned int milliSeconds)
98 {
99 std::this_thread::sleep_for(std::chrono::milliseconds(milliSeconds));
100 }
101
102 #ifdef __CYGWIN__
103 private:
104 static bool GetFilesInDir(std::vector<std::string>& filesFound, const std::string& path, const std::string& prefix);
105 public:
106 static bool GetComPorts(std::vector<unsigned char>& comPorts);
107 #endif
108
109 static inline void Copy8Bytes(const void* const from, void* const to)
110 {
111 *(reinterpret_cast<uint64_t*>(to)) = *(reinterpret_cast<const uint64_t*>(from));
112 }
113
114 static inline void Copy4Bytes(const void* const from, void* const to)
115 {
116 *(reinterpret_cast<uint32_t*>(to)) = *(reinterpret_cast<const uint32_t*>(from));
117 }
118
119 static uint8_t CalcXORCheckSum(const uint8_t* const buffer, size_t length);
120
121 static inline std::string ProtocolToString(const Protocol protocol)
122 {
123 return ProtocolSymbols[protocol];
124 }
125 }; // class Utils
126 } // namespace Utils