Update README.md
[mfgtools.git] / libuuu / libcomm.h
blob918ab1e69aba9d4a4a2546a4ed0670cd27f40c0f
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
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.
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.
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.
31 #include <cstdint>
32 #include <string>
33 #include <stdarg.h>
34 #include <locale>
35 #include <cctype>
36 #include <algorithm>
37 #pragma once
39 using namespace std;
41 void call_notify(struct uuu_notify nf);
43 #define log printf
44 #define dbg printf
46 int get_libusb_debug_level() noexcept;
48 class string_ex : public std::string
50 public:
52 int format(const char *fmt, ...)
54 va_list args;
55 va_start(args, fmt);
56 size_t len = std::vsnprintf(nullptr, 0, fmt, args);
57 va_end(args);
59 this->resize(len);
61 va_start(args, fmt);
62 std::vsnprintf((char*)c_str(), len+1, fmt, args);
63 va_end(args);
65 return 0;
67 void replace(char a, char b)
69 for (size_t i = 0; i < size(); i++)
70 if (at(i) == a)
71 (*this)[i] = b;
75 class Path : public string_ex
77 public:
78 string get_file_name()
80 replace('\\', '/');
81 size_t pos;
82 pos = rfind('/');
83 if (pos == string::npos)
84 return *this;
85 return substr(pos + 1);
89 inline uint64_t EndianSwap(uint64_t x) {
90 return (((x & 0x00000000000000ffLL) << 56) |
91 ((x & 0x000000000000ff00LL) << 40) |
92 ((x & 0x0000000000ff0000LL) << 24) |
93 ((x & 0x00000000ff000000LL) << 8) |
94 ((x & 0x000000ff00000000LL) >> 8) |
95 ((x & 0x0000ff0000000000LL) >> 24) |
96 ((x & 0x00ff000000000000LL) >> 40) |
97 ((x & 0xff00000000000000LL) >> 56));
100 inline uint32_t EndianSwap(uint32_t x)
102 return (x >> 24) |
103 ((x << 8) & 0x00FF0000) |
104 ((x >> 8) & 0x0000FF00) |
105 (x << 24);
107 inline uint16_t EndianSwap(uint16_t x)
109 return (x >> 8) |
110 ((x << 8) & 0xFF00);
113 inline string str_to_upper(const string &str)
115 std::locale loc;
116 string s;
118 for (size_t i = 0; i < str.size(); i++)
119 s.push_back(std::toupper(str[i], loc));
121 return s;
124 inline string remove_quota(string str)
126 if (!str.empty())
128 if (str[0] == '"')
130 str.erase(0, 1);
131 if (!str.empty() && str[str.size() - 1] == '"')
132 str.erase(str.size() - 1, 1);
135 return str;
138 inline bool compare_str(const string &str1, const string &str2, bool ignore_case)
140 if (ignore_case)
141 return str_to_upper(str1) == str_to_upper(str2);
142 else
143 return str1 == str2;
146 uint16_t str_to_uint16(const string &str, bool * conversion_succeeded = nullptr);
147 uint32_t str_to_uint32(const string &str, bool * conversion_succeeded = nullptr);
148 uint64_t str_to_uint64(const string &str, bool * conversion_succeeded = nullptr);
150 template <class T>
151 inline T round_up(T x, T align)
153 return (x + align - 1) / align * align;
156 template <class T>
157 inline T div_round_up(T x, T align)
159 return (x + align - 1) / align;
162 inline std::string trim(const std::string &s)
164 auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) {return std::isspace(c); });
165 return std::string(wsfront, std::find_if_not(s.rbegin(), std::string::const_reverse_iterator(wsfront), [](int c) {return std::isspace(c); }).base());