Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
191 lines (154 loc) · 4.73 KB
/
Copy pathmain.cpp
File metadata and controls
191 lines (154 loc) · 4.73 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* /////////////////////////////////////////////////////////////////////////
* File: examples/cpp/cstring.dynload/main.cpp
*
* Purpose: Windows-only example loading the cstring DLL dynamically via
* `LoadLibrary` / `GetProcAddress`.
*
* Created: 19th August 2005
* Updated: 5th October 2026
*
* ////////////////////////////////////////////////////////////////////// */
/* cstring header files */
#include <cstring/cstring.h>
#include "cstring.helpers.h"
/* STLSoft header files */
#include <stlsoft/stlsoft.h>
#if _STLSOFT_VER >= 0x010a01ff
#include <stlsoft/conversion/integer_to_string/integer_to_decimal_string.hpp>
#else /* ? _STLSOFT_VER */
#include <stlsoft/conversion/integer_to_string.hpp>
#endif /* _STLSOFT_VER */
#include <winstl/winstl.h>
/* Standard C++ header files */
#include <exception>
#include <new>
#include <stdexcept>
#ifdef __WATCOMC__
namespace std
{
using ::exception;
}
#endif /* __WATCOMC__ */
/* Standard C header files */
#include <stdio.h>
/* /////////////////////////////////////////////////////////////////////////
* forward declarations
*/
namespace cstring
{
static void setCapacity(struct cstring_t* pcs, size_t capacity)
{
struct _
{
static int onAllocFail(void* /* pv */, size_t /* cb */, cstring_flags_t /* flags */, void* param)
{
*static_cast<bool*>(param) = true;
return 0;
}
};
bool bThrow = false;
if (CSTRING_RC_SUCCESS != cstring_setCapacityFn(pcs, capacity, _::onAllocFail, &bThrow))
{
if (bThrow)
{
throw std::bad_alloc();
}
}
}
} // namespace cstring
/* /////////////////////////////////////////////////////////////////////////
* main()
*/
static char const* cstring_error_(CSTRING_RC rc)
{
static char s_sz[21];
int const i = static_cast<int>(rc);
#if _STLSOFT_VER >= 0x010a01ff
return stlsoft::integer_to_decimal_string(
#else /* ? _STLSOFT_VER */
return stlsoft::integer_to_string(
#endif /* _STLSOFT_VER */
&s_sz[0]
, STLSOFT_NUM_ELEMENTS(s_sz)
, i
);
}
#define cstring_error cstring_error_
static int main_(int /* argc */, char ** /*argv*/)
{
/* . */
HINSTANCE hinst = ::LoadLibrary("cstring.3.debug.dll");
GetLastError();
CSTRING_RC (*_create)(cstring_t*, cstring_char_t const*);
CSTRING_RC (*_yield)(cstring_t* pcs, cstring_char_t**);
CSTRING_RC (*_createEx)(cstring_t*, cstring_char_t const*, size_t, void*, size_t);
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wcast-function-type"
#endif /* __GNUC__ */
_create = reinterpret_cast<CSTRING_RC (*)(cstring_t*, cstring_char_t const*)>(::GetProcAddress(hinst, "cstring_create"));
_createEx = reinterpret_cast<CSTRING_RC (*)(cstring_t*, cstring_char_t const*, size_t, void*, size_t)>(::GetProcAddress(hinst, "cstring_createEx"));
_yield = reinterpret_cast<CSTRING_RC (*)(cstring_t*, cstring_char_t**)>(::GetProcAddress(hinst, "cstring_yield"));
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif /* __GNUC__ */
if (NULL != _create &&
NULL != _yield)
{
cstring_t cs;
#if 0
CSTRING_RC rc = _create(&cs, CSTRING_T_("Hello"));
#else /* ? 0 */
CSTRING_RC rc = _createEx(&cs, CSTRING_T_("Hello"), CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY, NULL, 100);
#endif /* 0 */
cstring_char_t* p;
try
{
cstring::setCapacity(&cs, 0x7feeffff);
}
catch(std::exception &x)
{
fprintf(stdout, "caught expected exception: %s\n", x.what());
}
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stderr, "create failed: %s\n", cstring_error(rc));
}
else
{
fprintf(stdout, "string=");
cstring_write(stdout, &cs, NULL);
fprintf(stdout, "\n");
rc = _yield(&cs, &p);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stderr, "yield failed: %s\n", cstring_error(rc));
}
}
}
return 0;
}
int main(int argc, char** argv)
{
#if 0
::Sleep(100000);
#endif /* 0 */
try
{
#if defined(_DEBUG) || \
defined(__SYNSOFT_DBS_DEBUG)
puts("example.cpp.cstring.dynload: " STLSOFT_COMPILER_LABEL_STRING);
#endif /* debug */
return main_(argc, argv);
}
catch(std::exception &x)
{
fprintf(stderr, "Unhandled error: %s\n", x.what());
}
catch(...)
{
fprintf(stderr, "Unhandled unknown error\n");
}
return EXIT_FAILURE;
}
/* ///////////////////////////// end of file //////////////////////////// */