aboutsummaryrefslogtreecommitdiff
path: root/CRT/eastl_compat.cpp
blob: cc2df45375bc708b3061cbc5c1ea72951cce713d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "eastl_compat.hpp"

#define NANOPRINTF_VISIBILITY_STATIC 1
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0
#define NANOPRINTF_IMPLEMENTATION 1
#include "nanoprintf.h"

#ifndef NATIVE
#include <wdm.h>
#endif

/*
 * eastl::to_string(...) does not work yet event if with a provided Vsnprintf/Vsnprintf8
 * The issue seems to be caused by a broken va_list/va_copy.
 */

eastl::string to_string(int value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%d", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%d", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(long value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%ld", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%ld", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(long long value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%lld", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%lld", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(unsigned int value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%u", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%u", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(unsigned long int value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%lu", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%lu", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(unsigned long long int value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%llu", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%llu", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(float value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%f", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%f", value);
        return result;
    }
    else
        return "";
}

eastl::string to_string(double value)
{
    int nbytes = npf_snprintf(nullptr, 0, "%lf", value);
    if (nbytes > 0)
    {
        char result[nbytes + 1] = {};
        npf_snprintf(result, nbytes, "%lf", value);
        return result;
    }
    else
        return "";
}

#ifndef NATIVE
eastl::string from_unicode(wchar_t * wstr, unsigned short wlen, unsigned short wmax)
{
    ANSI_STRING ansi;
    UNICODE_STRING unicode;

    unicode.Buffer = wstr;
    unicode.Length = wlen;
    unicode.MaximumLength = (wmax > 0 ? wmax : wlen);

    if (NT_SUCCESS(RtlUnicodeStringToAnsiString(&ansi, &unicode, TRUE))) {
        eastl::string result(ansi.Buffer, ansi.Length);
        RtlFreeAnsiString(&ansi);
        return result;
    }

    return "";
}
#endif