uuu
uuu (Universal Update Utility), mfgtools 3.0
Toggle main menu visibility
Loading...
Searching...
No Matches
libcomm.h
Go to the documentation of this file.
1
/*
2
* Copyright 2018 NXP.
3
*
4
* Redistribution and use in source and binary forms, with or without modification,
5
* are permitted provided that the following conditions are met:
6
*
7
* Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
9
*
10
* Redistributions in binary form must reproduce the above copyright notice, this
11
* list of conditions and the following disclaimer in the documentation and/or
12
* other materials provided with the distribution.
13
*
14
* Neither the name of the NXP Semiconductor nor the names of its
15
* contributors may be used to endorse or promote products derived from this
16
* software without specific prior written permission.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
* POSSIBILITY OF SUCH DAMAGE.
29
*
30
*/
31
#include <cstdint>
32
#include <string>
33
#include <stdarg.h>
34
#include <locale>
35
#include <cctype>
36
#include <algorithm>
37
#include "
libuuu.h
"
38
39
#pragma once
40
41
using namespace
std;
42
43
void
call_notify
(
struct
uuu_notify
nf);
44
45
#define log printf
46
#define dbg printf
47
48
int
get_libusb_debug_level
() noexcept;
49
int
get_libuuu_debug_level
() noexcept;
50
51
class
string_ex
: public std::
string
52
{
53
public
:
54
55
int
format
(
const
char
*fmt, ...)
56
{
57
va_list args;
58
va_start(args, fmt);
59
size_t
len = std::vsnprintf(
nullptr
, 0, fmt, args);
60
va_end(args);
61
62
this->resize(len);
63
64
va_start(args, fmt);
65
std::vsnprintf((
char
*)c_str(), len+1, fmt, args);
66
va_end(args);
67
68
return
0;
69
}
70
void
replace
(
char
a,
char
b)
71
{
72
for
(
size_t
i = 0; i < size(); i++)
73
if
(at(i) == a)
74
(*this)[i] = b;
75
}
76
};
77
78
class
Path
:
public
string_ex
79
{
80
public
:
81
string
get_file_name
()
82
{
83
replace
(
'\\'
,
'/'
);
84
size_t
pos
;
85
pos
= rfind(
'/'
);
86
if
(
pos
== string::npos)
87
return
*
this
;
88
return
substr(
pos
+ 1);
89
}
90
};
91
92
inline
uint64_t
EndianSwap
(uint64_t x) {
93
return
(((x & 0x00000000000000ffLL) << 56) |
94
((x & 0x000000000000ff00LL) << 40) |
95
((x & 0x0000000000ff0000LL) << 24) |
96
((x & 0x00000000ff000000LL) << 8) |
97
((x & 0x000000ff00000000LL) >> 8) |
98
((x & 0x0000ff0000000000LL) >> 24) |
99
((x & 0x00ff000000000000LL) >> 40) |
100
((x & 0xff00000000000000LL) >> 56));
101
}
102
103
inline
uint32_t
EndianSwap
(uint32_t x)
104
{
105
return
(x >> 24) |
106
((x << 8) & 0x00FF0000) |
107
((x >> 8) & 0x0000FF00) |
108
(x << 24);
109
}
110
inline
uint16_t
EndianSwap
(uint16_t x)
111
{
112
return
(x >> 8) |
113
((x << 8) & 0xFF00);
114
}
115
116
inline
string
str_to_upper
(
const
string
&str)
117
{
118
std::locale loc;
119
string
s;
120
121
for
(
size_t
i = 0; i < str.size(); i++)
122
s.push_back(std::toupper(str[i], loc));
123
124
return
s;
125
}
126
127
inline
string
remove_quota
(
string
str)
128
{
129
if
(!str.empty())
130
{
131
if
(str[0] ==
'"'
)
132
{
133
str.erase(0, 1);
134
if
(!str.empty() && str[str.size() - 1] ==
'"'
)
135
str.erase(str.size() - 1, 1);
136
}
137
}
138
return
str;
139
}
140
141
inline
bool
compare_str
(
const
string
&str1,
const
string
&str2,
bool
ignore_case)
142
{
143
if
(ignore_case)
144
return
str_to_upper
(str1) ==
str_to_upper
(str2);
145
else
146
return
str1 == str2;
147
}
148
149
uint16_t
str_to_uint16
(
const
string
&str,
bool
* conversion_succeeded =
nullptr
);
150
uint32_t
str_to_uint32
(
const
string
&str,
bool
* conversion_succeeded =
nullptr
);
151
uint64_t
str_to_uint64
(
const
string
&str,
bool
* conversion_succeeded =
nullptr
);
152
153
template
<
class
T>
154
inline
T
round_up
(T x, T align)
155
{
156
return
(x + align - 1) / align * align;
157
}
158
159
template
<
class
T>
160
inline
T
div_round_up
(T x, T align)
161
{
162
return
(x + align - 1) / align;
163
}
164
165
inline
std::string
trim
(
const
std::string &s)
166
{
167
auto
wsfront = std::find_if_not(s.begin(), s.end(), [](
int
c) {return std::isspace(c); });
168
return
std::string(wsfront, std::find_if_not(s.rbegin(), std::string::const_reverse_iterator(wsfront), [](
int
c) {return std::isspace(c); }).base());
169
}
170
171
static
inline
bool
uuu_force_bmap
() {
172
return
uuu_get_bmap_mode
() ==
bmap_mode::Force
;
173
}
174
175
static
inline
bool
uuu_ignore_bmap
() {
176
return
uuu_get_bmap_mode
() ==
bmap_mode::Ignore
;
177
}
uuu_get_bmap_mode
bmap_mode uuu_get_bmap_mode()
Definition
bmap.cpp:11
Path
Definition
libcomm.h:79
Path::get_file_name
string get_file_name()
Definition
libcomm.h:81
string_ex
Definition
uuu.cpp:105
string_ex::format
int format(const char *fmt,...)
Definition
libcomm.h:55
string_ex::replace
void replace(char a, char b)
Definition
libcomm.h:70
str_to_uint32
uint32_t str_to_uint32(const string &str, bool *conversion_succeeded)
Definition
cmd.cpp:405
str_to_uint16
uint16_t str_to_uint16(const string &str, bool *conversion_succeeded)
Definition
cmd.cpp:400
str_to_uint64
uint64_t str_to_uint64(const string &str, bool *conversion_succeeded)
Definition
cmd.cpp:410
get_libuuu_debug_level
int get_libuuu_debug_level() noexcept
Definition
error.cpp:52
get_libusb_debug_level
int get_libusb_debug_level() noexcept
Definition
error.cpp:47
EndianSwap
uint64_t EndianSwap(uint64_t x)
Definition
libcomm.h:92
trim
std::string trim(const std::string &s)
Definition
libcomm.h:165
round_up
T round_up(T x, T align)
Definition
libcomm.h:154
remove_quota
string remove_quota(string str)
Definition
libcomm.h:127
uuu_ignore_bmap
static bool uuu_ignore_bmap()
Definition
libcomm.h:175
call_notify
void call_notify(struct uuu_notify nf)
Definition
notify.cpp:62
uuu_force_bmap
static bool uuu_force_bmap()
Definition
libcomm.h:171
div_round_up
T div_round_up(T x, T align)
Definition
libcomm.h:160
compare_str
bool compare_str(const string &str1, const string &str2, bool ignore_case)
Definition
libcomm.h:141
str_to_upper
string str_to_upper(const string &str)
Definition
libcomm.h:116
libuuu.h
bmap_mode::Force
@ Force
Definition
libuuu.h:170
bmap_mode::Ignore
@ Ignore
Definition
libuuu.h:171
uuu_notify
Definition
libuuu.h:77
pos
auto pos
Definition
usbhotplug.cpp:160
libuuu
libcomm.h
Generated by
1.18.0