uuu
uuu (Universal Update Utility), mfgtools 3.0
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
41using namespace std;
42
43void call_notify(struct uuu_notify nf);
44
45#define log printf
46#define dbg printf
47
48int get_libusb_debug_level() noexcept;
49int get_libuuu_debug_level() noexcept;
50
51class string_ex : public std::string
52{
53public:
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
78class Path : public string_ex
79{
80public:
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
92inline 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
103inline uint32_t EndianSwap(uint32_t x)
104{
105 return (x >> 24) |
106 ((x << 8) & 0x00FF0000) |
107 ((x >> 8) & 0x0000FF00) |
108 (x << 24);
109}
110inline uint16_t EndianSwap(uint16_t x)
111{
112 return (x >> 8) |
113 ((x << 8) & 0xFF00);
114}
115
116inline 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
127inline 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
141inline 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
149uint16_t str_to_uint16(const string &str, bool * conversion_succeeded = nullptr);
150uint32_t str_to_uint32(const string &str, bool * conversion_succeeded = nullptr);
151uint64_t str_to_uint64(const string &str, bool * conversion_succeeded = nullptr);
152
153template <class T>
154inline T round_up(T x, T align)
155{
156 return (x + align - 1) / align * align;
157}
158
159template <class T>
160inline T div_round_up(T x, T align)
161{
162 return (x + align - 1) / align;
163}
164
165inline 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
171static inline bool uuu_force_bmap() {
173}
174
175static inline bool uuu_ignore_bmap() {
177}
bmap_mode uuu_get_bmap_mode()
Definition bmap.cpp:11
Definition libcomm.h:79
string get_file_name()
Definition libcomm.h:81
Definition uuu.cpp:107
int format(const char *fmt,...)
Definition libcomm.h:55
void replace(char a, char b)
Definition libcomm.h:70
int get_libuuu_debug_level() noexcept
Definition error.cpp:52
uint16_t str_to_uint16(const string &str, bool *conversion_succeeded=nullptr)
Definition cmd.cpp:400
uint64_t EndianSwap(uint64_t x)
Definition libcomm.h:92
std::string trim(const std::string &s)
Definition libcomm.h:165
T round_up(T x, T align)
Definition libcomm.h:154
string remove_quota(string str)
Definition libcomm.h:127
static bool uuu_ignore_bmap()
Definition libcomm.h:175
void call_notify(struct uuu_notify nf)
Definition notify.cpp:62
uint64_t str_to_uint64(const string &str, bool *conversion_succeeded=nullptr)
Definition cmd.cpp:410
static bool uuu_force_bmap()
Definition libcomm.h:171
uint32_t str_to_uint32(const string &str, bool *conversion_succeeded=nullptr)
Definition cmd.cpp:405
T div_round_up(T x, T align)
Definition libcomm.h:160
int get_libusb_debug_level() noexcept
Definition error.cpp:47
bool compare_str(const string &str1, const string &str2, bool ignore_case)
Definition libcomm.h:141
string str_to_upper(const string &str)
Definition libcomm.h:116
@ Force
Definition libuuu.h:170
@ Ignore
Definition libuuu.h:171
Definition libuuu.h:77
auto pos
Definition usbhotplug.cpp:159