Showing
9 changed files
with
451 additions
and
0 deletions
vnr/cpputil/cppcstring.h
0 → 100644
1 | +#ifndef CPPCSTRING_H | ||
2 | +#define CPPCSTRING_H | ||
3 | + | ||
4 | +// cppcstring.h | ||
5 | +// 10/12/2014 jichi | ||
6 | + | ||
7 | +#include <cstddef> // for size_t | ||
8 | +#include <cstring> | ||
9 | +//#include <algorithm> // for std::min | ||
10 | +#include "ccutil/ccmacro.h" | ||
11 | + | ||
12 | +// strlen | ||
13 | + | ||
14 | +template <typename charT> | ||
15 | +inline size_t cpp_basic_strlen(const charT *s) | ||
16 | +{ | ||
17 | + const charT *p = s; | ||
18 | + while (*p) p++; | ||
19 | + return p - s; | ||
20 | +} | ||
21 | + | ||
22 | +inline size_t cpp_strlen(const char *s) { return cpp_basic_strlen(s); } | ||
23 | +inline size_t cpp_wstrlen(const wchar_t *s) { return cpp_basic_strlen(s); } | ||
24 | + | ||
25 | +template <typename charT> | ||
26 | +inline size_t cpp_basic_strnlen(const charT *s, size_t n) | ||
27 | +{ | ||
28 | + const charT *p = s; | ||
29 | + while (*p && n) p++, n--; | ||
30 | + return p - s; | ||
31 | +} | ||
32 | + | ||
33 | +inline size_t cpp_strnlen(const char *s, size_t n) { return cpp_basic_strnlen(s, n); } | ||
34 | +inline size_t cpp_wstrnlen(const wchar_t *s, size_t n) { return cpp_basic_strnlen(s, n); } | ||
35 | + | ||
36 | +// strnchr | ||
37 | + | ||
38 | +#define cpp_basic_strnchr_(s, c, n) \ | ||
39 | + { \ | ||
40 | + while (*s && n) { \ | ||
41 | + if (*s == c) \ | ||
42 | + return s; \ | ||
43 | + s++, n--; \ | ||
44 | + } \ | ||
45 | + return nullptr; \ | ||
46 | + } | ||
47 | +template <typename charT> | ||
48 | +inline charT *cpp_basic_strnchr(charT *s, int c, size_t n) | ||
49 | +cpp_basic_strnchr_(s, c, n) | ||
50 | + | ||
51 | +template <typename charT> | ||
52 | +inline const charT *cpp_basic_strnchr(const charT *s, int c, size_t n) | ||
53 | +cpp_basic_strnchr_(s, c, n) | ||
54 | + | ||
55 | +// The same as memchr | ||
56 | +inline char *cpp_strnchr(char *s, int c, size_t n) { return cpp_basic_strnchr(s, c, n); } | ||
57 | +inline const char *cpp_strnchr(const char *s, int c, size_t n) { return cpp_basic_strnchr(s, c, n); } | ||
58 | + | ||
59 | +inline wchar_t *cpp_wcsnchr(wchar_t *s, int c, size_t n) { return cpp_basic_strnchr(s, c, n); } | ||
60 | +inline const wchar_t *cpp_wcsnchr(const wchar_t *s, int c, size_t n) { return cpp_basic_strnchr(s, c, n); } | ||
61 | + | ||
62 | +// strnstr | ||
63 | + | ||
64 | +#define cpp_basic_strnstr_(s, slen, r, rlen, ncmp) \ | ||
65 | + { \ | ||
66 | + while (*s && slen >= rlen) { \ | ||
67 | + if (ncmp(s, r, CC_MIN(slen, rlen)) == 0) \ | ||
68 | + return s; \ | ||
69 | + s++, slen--; \ | ||
70 | + } \ | ||
71 | + return nullptr; \ | ||
72 | + } | ||
73 | + | ||
74 | +template <typename charT> | ||
75 | +inline charT *cpp_basic_strnstr(charT *s, const charT *r, size_t n) | ||
76 | +cpp_basic_strnstr_(s, n, r, ::strlen(r), ::strncmp) | ||
77 | + | ||
78 | +template <typename charT> | ||
79 | +inline const charT *cpp_basic_strnstr(const charT *s, const charT *r, size_t n) | ||
80 | +cpp_basic_strnstr_(s, n, r, ::strlen(r), ::strncmp) | ||
81 | + | ||
82 | +template <> | ||
83 | +inline wchar_t *cpp_basic_strnstr<wchar_t>(wchar_t *s, const wchar_t *r, size_t n) | ||
84 | +cpp_basic_strnstr_(s, n, r, ::wcslen(r), ::wcsncmp) | ||
85 | + | ||
86 | +template <> | ||
87 | +inline const wchar_t *cpp_basic_strnstr<wchar_t>(const wchar_t *s, const wchar_t *r, size_t n) | ||
88 | +cpp_basic_strnstr_(s, n, r, ::wcslen(r), ::wcsncmp) | ||
89 | + | ||
90 | +inline char *cpp_strnstr(char *s, const char *r, size_t n) { return cpp_basic_strnstr(s, r, n); } | ||
91 | +inline const char *cpp_strnstr(const char *s, const char *r, size_t n) { return cpp_basic_strnstr(s, r, n); } | ||
92 | +inline wchar_t *cpp_wcsnstr(wchar_t *s, const wchar_t *r, size_t n) { return cpp_basic_strnstr(s, r, n); } | ||
93 | +inline const wchar_t *cpp_wcsnstr(const wchar_t *s, const wchar_t *r, size_t n) { return cpp_basic_strnstr(s, r, n); } | ||
94 | + | ||
95 | +// strnpbrk | ||
96 | + | ||
97 | +// it might be faster to use strchr functions, which is not portable though | ||
98 | +#define cpp_basic_strnpbrk_(s, sep, n) \ | ||
99 | + { \ | ||
100 | + while (*s && n) { \ | ||
101 | + for (auto p = sep; *p; p++) \ | ||
102 | + if (*s == *p) \ | ||
103 | + return s; \ | ||
104 | + s++, n--; \ | ||
105 | + } \ | ||
106 | + return nullptr; \ | ||
107 | + } | ||
108 | + | ||
109 | +template <typename charT, typename char2T> | ||
110 | +inline charT *cpp_basic_strnpbrk(charT *dest, const char2T *breakset, size_t n) | ||
111 | +cpp_basic_strnpbrk_(dest, breakset, n) | ||
112 | + | ||
113 | +template <typename charT, typename char2T> | ||
114 | +inline const charT *cpp_basic_strnpbrk(const charT *dest, const char2T *breakset, size_t n) | ||
115 | +cpp_basic_strnpbrk_(dest, breakset, n) | ||
116 | + | ||
117 | +inline char *cpp_strnpbrk(char *dest, const char *breakset, size_t n) { return cpp_basic_strnpbrk(dest, breakset, n); } | ||
118 | +inline const char *cpp_strnpbrk(const char *dest, const char *breakset, size_t n) { return cpp_basic_strnpbrk(dest, breakset, n); } | ||
119 | +inline wchar_t *cpp_wcsnpbrk(wchar_t *dest, const wchar_t *breakset, size_t n) { return cpp_basic_strnpbrk(dest, breakset, n); } | ||
120 | +inline const wchar_t *cpp_wcsnpbrk(const wchar_t *dest, const wchar_t *breakset, size_t n) { return cpp_basic_strnpbrk(dest, breakset, n); } | ||
121 | + | ||
122 | +#endif // CPPCSTRING_H |
vnr/cpputil/cpplocale.h
0 → 100644
1 | +#ifndef CPPLOCALE_H | ||
2 | +#define CPPLOCALE_H | ||
3 | + | ||
4 | +// cpplocale.h | ||
5 | +// 9/26/2014 jichi | ||
6 | + | ||
7 | +#include <codecvt> | ||
8 | +#include <locale> | ||
9 | + | ||
10 | +//#include <boost/locale.hpp> | ||
11 | + | ||
12 | +// See: http://stackoverflow.com/questions/20195262/how-to-read-an-utf-8-encoded-file-containing-chinese-characters-and-output-them | ||
13 | +// The same as boost::locale::generator().generate("UTF-8"), which require linking | ||
14 | +// See: http://en.cppreference.com/w/cpp/locale/codecvt_utf8 | ||
15 | +// - 0x10ffff is the default maximum value. | ||
16 | +// - std::consume_header will skip the leading encoding byte from the input. | ||
17 | +template <class charT> | ||
18 | +inline std::locale cpp_utf8_locale(std::locale init = std::locale()) | ||
19 | +{ return std::locale(init, new std::codecvt_utf8<charT, 0x10ffff, std::consume_header>()); } | ||
20 | + | ||
21 | +#endif // CPPLOCALE_H |
vnr/cpputil/cppmarshal.h
0 → 100644
1 | +#ifndef CPPMARSHAL_H | ||
2 | +#define CPPMARSHAL_H | ||
3 | + | ||
4 | +// cppmarshal.h | ||
5 | +// 10/12/2014 jichi | ||
6 | +// | ||
7 | +// Functions are by default big-endian, the same as memory layout. | ||
8 | +#include "cpputil/cppcstring.h" | ||
9 | +#include "cpputil/cpptype.h" | ||
10 | +#include <cstring> | ||
11 | + | ||
12 | +/* Read */ | ||
13 | + | ||
14 | +// Read number | ||
15 | + | ||
16 | +template <typename valT, typename byteT> | ||
17 | +inline const byteT *cpp_marshal_getval(const byteT *p, valT *v) | ||
18 | +{ *v = *reinterpret_cast<const valT *>(p); return p + sizeof(valT); } | ||
19 | + | ||
20 | +// Read pointer | ||
21 | + | ||
22 | +template <typename ptrT, typename byteT> \ | ||
23 | +inline const byteT *cpp_marshal_getptr(const byteT *p, ptrT v) | ||
24 | +{ return cpp_marshal_getval<unsigned long>(p, reinterpret_cast<unsigned long *>(v)); } | ||
25 | + | ||
26 | +// Read string | ||
27 | + | ||
28 | +template <typename charT, typename byteT> | ||
29 | +inline const byteT *cpp_marshal_getstr(const byteT *p, charT *s) | ||
30 | +{ | ||
31 | + size_t n = cpp_basic_strlen(p); | ||
32 | + ::memcpy(s, p, n + 1); // including '\0' | ||
33 | + return p + n + 1; | ||
34 | +} | ||
35 | + | ||
36 | +template <typename charT, typename byteT> | ||
37 | +inline const byteT *cpp_marshal_getnstr(const byteT *p, charT *s, size_t n) | ||
38 | +{ | ||
39 | + if (n = cpp_basic_strnlen(p, n)) | ||
40 | + ::memcpy(s, p, n); // including '\0' | ||
41 | + s[n] = 0; | ||
42 | + return p + n + 1; | ||
43 | +} | ||
44 | + | ||
45 | +/* Write */ | ||
46 | + | ||
47 | +// Write number | ||
48 | + | ||
49 | +template <typename valT, typename byteT> | ||
50 | +inline byteT *cpp_marshal_putval(byteT *p, valT v) | ||
51 | +{ *reinterpret_cast<valT *>(p) = v; return p + sizeof(valT); } | ||
52 | + | ||
53 | +// Write pointer | ||
54 | + | ||
55 | +template <typename ptrT, typename byteT> \ | ||
56 | +inline byteT *cpp_marshal_putptr(byteT *p, ptrT v) | ||
57 | +{ return cpp_marshal_putval<unsigned long>(p, reinterpret_cast<unsigned long>(v)); } | ||
58 | + | ||
59 | +// Write string | ||
60 | + | ||
61 | +template <typename charT, typename byteT> | ||
62 | +inline byteT *cpp_marshal_putstr(byteT *p, charT *s) | ||
63 | +{ | ||
64 | + size_t n = cpp_basic_strlen(s); | ||
65 | + ::memcpy(p, s, n + 1); // including '\0' | ||
66 | + return p + n + 1; | ||
67 | +} | ||
68 | + | ||
69 | +template <typename charT, typename byteT> | ||
70 | +inline byteT *cpp_marshal_putstr(byteT *p, charT *s, size_t n) | ||
71 | +{ | ||
72 | + if (n = cpp_basic_strnlen(s, n)) | ||
73 | + ::memcpy(p, s, n); // including '\0' | ||
74 | + s[n] = 0; | ||
75 | + return p + n + 1; | ||
76 | +} | ||
77 | + | ||
78 | +/* Expansion */ | ||
79 | + | ||
80 | +#define CPP_DECLARE_MARSHAL_GETVAL(type) \ | ||
81 | + template <typename byteT> \ | ||
82 | + inline const byteT *cpp_marshal_get##type(const byteT *p, cpp_##type *v) { return cpp_marshal_getval(p, v); } | ||
83 | + | ||
84 | +#define CPP_DECLARE_MARSHAL_PUTVAL(type) \ | ||
85 | + template <typename byteT> \ | ||
86 | + inline byteT *cpp_marshal_put##type(byteT *p, cpp_##type v) { return cpp_marshal_putval(p, v); } | ||
87 | + | ||
88 | +CPP_DECLARE_MARSHAL_PUTVAL(float) | ||
89 | +CPP_DECLARE_MARSHAL_PUTVAL(double) | ||
90 | +CPP_DECLARE_MARSHAL_GETVAL(float) | ||
91 | +CPP_DECLARE_MARSHAL_GETVAL(double) | ||
92 | +CPP_DECLARE_MARSHAL_GETVAL(int) | ||
93 | +CPP_DECLARE_MARSHAL_GETVAL(int8) | ||
94 | +CPP_DECLARE_MARSHAL_GETVAL(int32) | ||
95 | +CPP_DECLARE_MARSHAL_GETVAL(int64) | ||
96 | +CPP_DECLARE_MARSHAL_GETVAL(uint) | ||
97 | +CPP_DECLARE_MARSHAL_GETVAL(uint8) | ||
98 | +CPP_DECLARE_MARSHAL_GETVAL(uint32) | ||
99 | +CPP_DECLARE_MARSHAL_GETVAL(uint64) | ||
100 | + | ||
101 | +CPP_DECLARE_MARSHAL_PUTVAL(int) | ||
102 | +CPP_DECLARE_MARSHAL_PUTVAL(int8) | ||
103 | +CPP_DECLARE_MARSHAL_PUTVAL(int32) | ||
104 | +CPP_DECLARE_MARSHAL_PUTVAL(int64) | ||
105 | +CPP_DECLARE_MARSHAL_PUTVAL(uint) | ||
106 | +CPP_DECLARE_MARSHAL_PUTVAL(uint8) | ||
107 | +CPP_DECLARE_MARSHAL_PUTVAL(uint32) | ||
108 | +CPP_DECLARE_MARSHAL_PUTVAL(uint64) | ||
109 | + | ||
110 | +#endif // CPPMARSHAL_H |
vnr/cpputil/cppmath.h
0 → 100644
1 | +#ifndef CPPMATH_H | ||
2 | +#define CPPMATH_H | ||
3 | + | ||
4 | +// cppmacro.h | ||
5 | +// 10/12/2014 jichi | ||
6 | +#include <cmath> | ||
7 | + | ||
8 | +// The same as qMin | ||
9 | +template <typename T> | ||
10 | +inline const T &cpp_min(const T &a, const T &b) { return (a < b) ? a : b; } | ||
11 | + | ||
12 | +// The same as qMax | ||
13 | +template <typename T> | ||
14 | +inline const T &cpp_max(const T &a, const T &b) { return (a < b) ? b : a; } | ||
15 | + | ||
16 | +// The same as qBound | ||
17 | +template <typename T> | ||
18 | +inline const T &cpp_bound(const T &min, const T &val, const T &max) | ||
19 | +{ return cpp_max(min, cpp_min(max, val)); } | ||
20 | + | ||
21 | +// The same as qFuzzyCompare | ||
22 | +inline bool cpp_fuzzy_compare(float p1, float p2) | ||
23 | +{ return (abs(p1 - p2) <= 0.00001f * cpp_min(abs(p1), abs(p2))); } | ||
24 | + | ||
25 | +#endif // CPPMATH_H |
vnr/cpputil/cpppath.h
0 → 100644
1 | +#ifndef CPPPATH_H | ||
2 | +#define CPPPATH_H | ||
3 | + | ||
4 | +// cpppath.h | ||
5 | +// 5/7/2014 jichi | ||
6 | + | ||
7 | +#include <cstddef> // for size_t | ||
8 | + | ||
9 | +enum : char { cpp_pathsep_unix = '/' , cpp_pathsep_win = '\\' }; | ||
10 | + | ||
11 | +// basename | ||
12 | + | ||
13 | +template <class charT> | ||
14 | +inline const charT *cpp_basic_basename(const charT *s) | ||
15 | +{ | ||
16 | + const charT *p = s; | ||
17 | + //if (s) // not checked | ||
18 | + for (; *s; s++) | ||
19 | + if (*s == cpp_pathsep_unix || *s == cpp_pathsep_win) | ||
20 | + p = s + 1; | ||
21 | + return p; | ||
22 | +} | ||
23 | + | ||
24 | +//if (const char *r = ::strrchr(s, pathsep)) | ||
25 | +// return r + 1; // skip the path seperator | ||
26 | +//else | ||
27 | +// return s; | ||
28 | +inline const char *cpp_basename(const char *s) { return cpp_basic_basename<char>(s); } | ||
29 | + | ||
30 | +//if (const wchar_t *r = ::wcsrchr(s, pathsep)) | ||
31 | +// return r + 1; // skip the path seperator | ||
32 | +//else | ||
33 | +// return s; | ||
34 | +inline const wchar_t *cpp_wbasename(const wchar_t *s) { return cpp_basic_basename<wchar_t>(s); } | ||
35 | + | ||
36 | +// dirmame | ||
37 | + | ||
38 | +/// Return the length so that s[len] == pathsep | ||
39 | +template <class charT> | ||
40 | +inline size_t cpp_basic_dirlen(const charT *s) | ||
41 | +{ | ||
42 | + const charT *p = s, | ||
43 | + *t = s; | ||
44 | + //if (s) // not checked | ||
45 | + for (; *s; s++) | ||
46 | + if (*s == cpp_pathsep_unix || *s == cpp_pathsep_win) | ||
47 | + p = s + 1; | ||
48 | + return p - t; | ||
49 | +} | ||
50 | + | ||
51 | +inline size_t cpp_wdirlen(const char *s) { return cpp_basic_dirlen<char>(s); } | ||
52 | +inline size_t cpp_wdirlen(const wchar_t *s) { return cpp_basic_dirlen<wchar_t>(s); } | ||
53 | + | ||
54 | +#endif // CPPPATH_H |
vnr/cpputil/cppstring.h
0 → 100644
1 | +#ifndef CPPSTRING_H | ||
2 | +#define CPPSTRING_H | ||
3 | + | ||
4 | +// cppstring.h | ||
5 | +// 10/12/2014 jichi | ||
6 | + | ||
7 | +#include <cstring> | ||
8 | +#include <string> | ||
9 | + | ||
10 | +// Initializers | ||
11 | + | ||
12 | +template <typename charT, typename stringT> | ||
13 | +inline std::basic_string<charT> cpp_basic_string_of(const stringT &s) | ||
14 | +{ return std::basic_string<charT>(s.cbegin(), s.cend()); } | ||
15 | + | ||
16 | +template <typename stringT> | ||
17 | +inline std::string cpp_string_of(const stringT &s) | ||
18 | +{ return std::string(s.cbegin(), s.cend()); } | ||
19 | + | ||
20 | +inline std::string cpp_string_of(const char *s) | ||
21 | +{ return s; } | ||
22 | + | ||
23 | +inline std::string cpp_string_of(const wchar_t *s) | ||
24 | +{ return std::string(s, s + ::wcslen(s)); } | ||
25 | + | ||
26 | +template <typename stringT> | ||
27 | +inline std::wstring cpp_wstring_of(const stringT &s) | ||
28 | +{ return std::wstring(s.cbegin(), s.cend()); } | ||
29 | + | ||
30 | +inline std::wstring cpp_wstring_of(const wchar_t *s) | ||
31 | +{ return s; } | ||
32 | + | ||
33 | +inline std::wstring cpp_wstring_of(const char *s) | ||
34 | +{ return std::wstring(s, s + ::strlen(s)); } | ||
35 | + | ||
36 | +#endif // CPPSTRING_H |
vnr/cpputil/cpptype.h
0 → 100644
1 | +#ifndef CPPTYPE_H | ||
2 | +#define CPPTYPE_H | ||
3 | + | ||
4 | +// cpptype.h | ||
5 | +// 10/12/2014 jichi | ||
6 | +#include <cstdint> | ||
7 | + | ||
8 | +// Platform-dependent | ||
9 | + | ||
10 | +typedef char cpp_char; | ||
11 | +typedef unsigned char cpp_uchar; | ||
12 | + | ||
13 | +typedef short cpp_short; | ||
14 | +typedef unsigned short cpp_ushort; | ||
15 | + | ||
16 | +typedef int cpp_int; | ||
17 | +typedef unsigned int cpp_uint; | ||
18 | + | ||
19 | +typedef long cpp_long; | ||
20 | +typedef unsigned long cpp_ulong; | ||
21 | + | ||
22 | +typedef long long cpp_llong; | ||
23 | +typedef unsigned long long cpp_ullong; | ||
24 | + | ||
25 | +typedef float cpp_float; | ||
26 | +typedef double cpp_double; | ||
27 | + | ||
28 | +// Platform-independent | ||
29 | + | ||
30 | +typedef int8_t cpp_int8; | ||
31 | +typedef uint8_t cpp_uint8; | ||
32 | + | ||
33 | +typedef cpp_int8 cpp_byte; | ||
34 | +typedef cpp_uint8 cpp_ubyte; | ||
35 | + | ||
36 | +typedef int32_t cpp_int32; | ||
37 | +typedef uint32_t cpp_uint32; | ||
38 | + | ||
39 | +typedef int64_t cpp_int64; | ||
40 | +typedef uint64_t cpp_uint64; | ||
41 | + | ||
42 | +#endif // CPPTYPE_H |
vnr/cpputil/cppunicode.h
0 → 100644
1 | +#ifndef CPPUNICODE_H | ||
2 | +#define CPPUNICODE_H | ||
3 | + | ||
4 | +#include <string> | ||
5 | +typedef std::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t> > cpp_u16string; | ||
6 | +typedef std::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > cpp_u32string; | ||
7 | + | ||
8 | +// <fstream> | ||
9 | +#if defined(_FSTREAM_) || defined(_LIBCPP_FSTREAM) || defined(_GLIBCXX_FSTREAM) | ||
10 | +typedef std::basic_ifstream<char16_t, std::char_traits<char16_t> > cpp_u16ifstream; | ||
11 | +typedef std::basic_ifstream<char32_t, std::char_traits<char32_t> > cpp_u32ifstream; | ||
12 | + | ||
13 | +typedef std::basic_ofstream<char16_t, std::char_traits<char16_t> > cpp_u16ofstream; | ||
14 | +typedef std::basic_ofstream<char32_t, std::char_traits<char32_t> > cpp_u32ofstream; | ||
15 | + | ||
16 | +typedef std::basic_fstream<char16_t, std::char_traits<char16_t> > cpp_u16fstream; | ||
17 | +typedef std::basic_fstream<char32_t, std::char_traits<char32_t> > cpp_u32fstream; | ||
18 | +#endif // <fstream> | ||
19 | + | ||
20 | +inline char16_t cpp_u32low(char32_t c) { return c; } | ||
21 | +inline char16_t cpp_u32high(char32_t c) { return c >> 16; } | ||
22 | + | ||
23 | +#endif // CPPUNICODE_H |
vnr/cpputil/cpputil.pri
0 → 100644
1 | +# cpputil.pri | ||
2 | +# 9/26/2012 jichi | ||
3 | + | ||
4 | +DEFINES += WITH_LIB_CPPUTIL | ||
5 | + | ||
6 | +DEPENDPATH += $$PWD | ||
7 | + | ||
8 | +HEADERS += \ | ||
9 | + $$PWD/cppcstring.h \ | ||
10 | + $$PWD/cpplocale.h \ | ||
11 | + $$PWD/cppmarshal.h \ | ||
12 | + $$PWD/cpppath.h \ | ||
13 | + $$PWD/cppregex.h \ | ||
14 | + $$PWD/cppstring.h \ | ||
15 | + $$PWD/cpptype.h \ | ||
16 | + $$PWD/cppunicode.h | ||
17 | + | ||
18 | +# EOF |
-
Please register or login to post a comment