Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | /* |
2 | * This file is copyright by Rebirth contributors and licensed as |
||
3 | * described in COPYING.txt. |
||
4 | * See COPYING.txt for license details. |
||
5 | */ |
||
6 | |||
7 | #pragma once |
||
8 | |||
9 | #include <algorithm> |
||
10 | #include <cassert> |
||
11 | #include <cstdint> |
||
12 | #include <cstdlib> |
||
13 | #include <cstring> |
||
14 | #include <iterator> |
||
15 | #include <type_traits> |
||
16 | #include "dxxsconf.h" |
||
17 | #include "ntstring.h" |
||
18 | |||
19 | template <std::size_t N> |
||
20 | static inline bool cmp(const char *token, const char *equal, const char (&name)[N]) |
||
21 | { |
||
22 | return &token[N - 1] == equal && !strncmp(token, name, N - 1); |
||
23 | } |
||
24 | |||
25 | template <typename F, typename T> |
||
26 | static inline bool convert_integer(F *f, T &t, const char *value, int base) |
||
27 | { |
||
28 | char *e; |
||
29 | auto r = (*f)(value, &e, base); |
||
30 | if (*e) |
||
31 | /* Trailing garbage found */ |
||
32 | return false; |
||
33 | T tr = static_cast<T>(r); |
||
34 | if (r != tr) |
||
35 | /* Result truncated */ |
||
36 | return false; |
||
37 | t = tr; |
||
38 | return true; |
||
39 | } |
||
40 | |||
41 | template <typename T> |
||
42 | static inline typename std::enable_if<std::is_signed<T>::value, bool>::type convert_integer(T &t, const char *value, int base = 10) |
||
43 | { |
||
44 | return convert_integer(strtol, t, value, base); |
||
45 | } |
||
46 | |||
47 | template <typename T> |
||
48 | static inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type convert_integer(T &t, const char *value, int base = 10) |
||
49 | { |
||
50 | return convert_integer(strtoul, t, value, base); |
||
51 | } |
||
52 | |||
53 | template <std::size_t N> |
||
54 | static inline void convert_string(ntstring<N> &out, const char *const value, const char *eol) |
||
55 | { |
||
56 | assert(*eol == 0); |
||
57 | const std::size_t i = std::distance(value, ++ eol); |
||
58 | if (i > out.size()) |
||
59 | /* Only if not truncated */ |
||
60 | return; |
||
61 | std::copy(value, eol, out.begin()); |
||
62 | } |