Skip to content

Commit dce687b

Browse files
committed
Added function for convert bin-data to hex-string (and inversely) into Utils
Changed realization of function Utils::getUniqueName()
1 parent 927ea1a commit dce687b

File tree

3 files changed

+134
-11
lines changed

3 files changed

+134
-11
lines changed

projects/qt-creator/httpserver.pro.user

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 3.5.1, 2015-11-16T00:54:33. -->
3+
<!-- Written by QtCreator 3.5.1, 2015-12-17T18:30:09. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -61,7 +61,7 @@
6161
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
6262
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
6363
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{c77ee4a2-1c2a-4bac-9185-8378ec4ebf5d}</value>
64-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
64+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
6565
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
6666
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
6767
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
@@ -241,11 +241,11 @@
241241
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">httpserver</value>
242242
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">httpserver2</value>
243243
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/media/projects/httpserver/projects/qt-creator/httpserver.pro</value>
244-
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
244+
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments">--start</value>
245245
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">httpserver.pro</value>
246246
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
247247
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal">true</value>
248-
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
248+
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory">/media/projects/sites/servertest/httpserver/Debug</value>
249249
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
250250
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
251251
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>

src/Utils.cpp

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,126 @@ namespace Utils
4343
return values;
4444
}
4545

46+
std::string encodeHtmlSymbols(const std::string &str)
47+
{
48+
std::string buf;
49+
buf.reserve(str.length() );
50+
51+
for (size_t pos = 0; pos < str.length(); ++pos)
52+
{
53+
switch (str[pos])
54+
{
55+
case '&': buf.append("&amp;"); break;
56+
case '\"': buf.append("&quot;"); break;
57+
case '\'': buf.append("&apos;"); break;
58+
case '<': buf.append("&lt;"); break;
59+
case '>': buf.append("&gt;"); break;
60+
default: buf.push_back(str[pos]); break;
61+
}
62+
}
63+
64+
return buf;
65+
}
66+
67+
std::string binToHexString(const char *binData, const size_t dataSize)
68+
{
69+
std::string str(dataSize * 2, 0);
70+
71+
const uint8_t *bin = reinterpret_cast<const uint8_t *>(binData);
72+
73+
const char hexDigits[] = { "0123456789abcdef" };
74+
75+
for (size_t i = dataSize - 1; std::numeric_limits<size_t>::max() != i; --i)
76+
{
77+
str[(i << 1) + 0] = hexDigits[bin[i] >> 4];
78+
str[(i << 1) + 1] = hexDigits[bin[i] & 0x0F];
79+
}
80+
81+
return str;
82+
}
83+
84+
static unsigned char hexStringToBinEncodeSymbol(const char c)
85+
{
86+
if (c >= '0' && c <= '9')
87+
{
88+
return c - 0x30;
89+
}
90+
else if (c >= 'a' && c <= 'f')
91+
{
92+
return c - 0x57;
93+
}
94+
else if (c >= 'A' && c <= 'F')
95+
{
96+
return c - 0x37;
97+
}
98+
99+
return 0;
100+
}
101+
102+
std::string hexStringToBin(const std::string &hexStr)
103+
{
104+
std::string bin(hexStr.length() / 2, 0);
105+
106+
for (size_t i = 0; i < bin.length(); ++i)
107+
{
108+
char a = hexStr[(i << 1) + 0];
109+
char b = hexStr[(i << 1) + 1];
110+
111+
bin[i] = (
112+
(hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b)
113+
);
114+
}
115+
116+
return bin;
117+
}
118+
119+
unsigned long long htonll(const unsigned long long src)
120+
{
121+
enum Endianness {
122+
INIT = 0,
123+
LITE = 1,
124+
BIGE = 2
125+
};
126+
127+
static int endian = Endianness::INIT;
128+
129+
union {
130+
unsigned long long ull;
131+
unsigned char c[8];
132+
} x;
133+
134+
if (endian == Endianness::INIT)
135+
{
136+
x.ull = 0x01;
137+
endian = (x.c[7] == 0x01ULL) ? Endianness::BIGE : Endianness::LITE;
138+
}
139+
140+
if (endian == Endianness::BIGE)
141+
{
142+
return src;
143+
}
144+
145+
x.ull = src;
146+
147+
unsigned char c;
148+
149+
c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c;
150+
c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c;
151+
c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c;
152+
c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c;
153+
154+
return x.ull;
155+
}
156+
157+
std::string getUniqueName()
158+
{
159+
size_t time = std::chrono::high_resolution_clock::now().time_since_epoch().count();
160+
161+
time = htonll(time);
162+
163+
return binToHexString(reinterpret_cast<const char *>(&time), sizeof(time) );
164+
}
165+
46166
char *stlStringToPChar(const std::string &str)
47167
{
48168
const size_t length = str.length();
@@ -146,7 +266,7 @@ namespace Utils
146266
const size_t str_mon_length = 64;
147267
std::vector<char> s_mon(str_mon_length);
148268

149-
struct ::tm tc = {0};
269+
struct ::tm tc = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
150270

151271
// Parse RFC 822
152272
#ifdef WIN32

src/Utils.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ namespace Utils
2727

2828
std::vector<std::string> explode(const std::string &str, const char sep);
2929

30-
inline std::string getUniqueName()
31-
{
32-
std::stringstream s;
33-
s << std::hex << std::chrono::high_resolution_clock::now().time_since_epoch().count();
34-
return s.str();
35-
}
30+
std::string encodeHtmlSymbols(const std::string &str);
31+
32+
std::string binToHexString(const char *bin, const size_t size);
33+
34+
std::string hexStringToBin(const std::string &hexStr);
35+
36+
unsigned long long htonll(const unsigned long long src);
37+
38+
std::string getUniqueName();
3639

3740
char *stlStringToPChar(const std::string &str);
3841

0 commit comments

Comments
 (0)