Repository navigation
Expand file tree
/
Copy pathBSTR_functions.h
More file actions
762 lines (691 loc) · 16.3 KB
/
Copy pathBSTR_functions.h
File metadata and controls
762 lines (691 loc) · 16.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/* /////////////////////////////////////////////////////////////////////////
* File: comstl/string/BSTR_functions.h (originally MOBStrFn.h, ::SynesisCom)
*
* Purpose: Contains classes and functions for dealing with BSTR strings.
*
* Created: 24th June 2002
* Updated: 24th December 2024
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2019-2024, Matthew Wilson and Synesis Information Systems
* Copyright (c) 2002-2019, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Information Systems
* nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file comstl/string/BSTR_functions.h
*
* \brief [C, C++] Functions for manipulating BSTR strings
* (\ref group__library__String "String" Library).
*/
#ifndef COMSTL_INCL_COMSTL_STRING_H_BSTR_FUNCTIONS
#define COMSTL_INCL_COMSTL_STRING_H_BSTR_FUNCTIONS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define COMSTL_VER_COMSTL_STRING_H_BSTR_FUNCTIONS_MAJOR 4
# define COMSTL_VER_COMSTL_STRING_H_BSTR_FUNCTIONS_MINOR 4
# define COMSTL_VER_COMSTL_STRING_H_BSTR_FUNCTIONS_REVISION 9
# define COMSTL_VER_COMSTL_STRING_H_BSTR_FUNCTIONS_EDIT 108
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* includes
*/
#ifndef COMSTL_INCL_COMSTL_H_COMSTL
# include <comstl/comstl.h>
#endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
#ifdef STLSOFT_TRACE_INCLUDE
# pragma message(__FILE__)
#endif /* STLSOFT_TRACE_INCLUDE */
#ifndef STLSOFT_INCL_STLSOFT_QUALITY_H_CONTRACT
# include <stlsoft/quality/contract.h>
#endif /* !STLSOFT_INCL_STLSOFT_QUALITY_H_CONTRACT */
#ifndef STLSOFT_INCL_STLSOFT_QUALITY_H_COVER
# include <stlsoft/quality/cover.h>
#endif /* !STLSOFT_INCL_STLSOFT_QUALITY_H_COVER */
#ifndef WINSTL_INCL_WINSTL_API_external_h_ErrorHandling
# include <winstl/api/external/ErrorHandling.h>
#endif /* !WINSTL_INCL_WINSTL_API_external_h_ErrorHandling */
#ifndef WINSTL_INCL_WINSTL_API_external_h_UnicodeAndCharacterSet
# include <winstl/api/external/UnicodeAndCharacterSet.h>
#endif /* !WINSTL_INCL_WINSTL_API_external_h_UnicodeAndCharacterSet */
#ifndef STLSOFT_INCL_STLSOFT_API_external_h_string
# include <stlsoft/api/external/string.h>
#endif /* !STLSOFT_INCL_STLSOFT_API_external_h_string */
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#if !defined(COMSTL_NO_NAMESPACE) && \
!defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
# if defined(STLSOFT_NO_NAMESPACE)
/* There is no stlsoft namespace, so must define ::comstl */
namespace comstl
{
# else
/* Define stlsoft::comstl_project */
namespace stlsoft
{
namespace comstl_project
{
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !COMSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* C functions
*/
/** [C] Retrieves the length of a BSTR
*
* \ingroup group__library__String
*
* \param bstr The string whose length is to be determined
*
* \result The length of the BSTR
*/
STLSOFT_INLINE
cs_size_t
comstl_C_BSTR_len(BSTR bstr)
{
return STLSOFT_NS_GLOBAL(SysStringLen)(bstr);
}
/** [C] Creates a BSTR from a Unicode string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
*
* \result The created BSTR
*/
STLSOFT_INLINE
BSTR
comstl_C_BSTR_create_w(
cs_char_w_t const* s
)
{
return STLSOFT_NS_GLOBAL(SysAllocString)(s);
}
/** [C] Creates a BSTR from a (part of a) Unicode string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
* \param len The number of characters of \c s to copy into the result
*
* \result The created BSTR
*/
STLSOFT_INLINE
BSTR
comstl_C_BSTR_create_len_w(
cs_char_w_t const* s
, cs_size_t len
)
{
COMSTL_MESSAGE_ASSERT("Cannot pass size_t(-1) as length!", 0 != len + 1);
COMSTL_ASSERT(NULL == s || !STLSOFT_NS_GLOBAL(IsBadStringPtrW(s, len)));
return STLSOFT_NS_GLOBAL(SysAllocStringLen)(s, stlsoft_static_cast(UINT, len));
}
/** [C] Creates a BSTR from an ANSI string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
*
* \result The created BSTR
*/
STLSOFT_INLINE
BSTR
comstl_C_BSTR_create_a(
cs_char_a_t const* s
)
{
BSTR bstr = NULL;
if (s != NULL)
{
const cs_size_t len = STLSOFT_NS_GLOBAL(strlen)(s);
cs_char_w_t* pwsz = stlsoft_static_cast(cs_char_w_t*, STLSOFT_NS_GLOBAL(CoTaskMemAlloc)((1 + len) * sizeof(cs_char_w_t)));
if (NULL != pwsz)
{
int n = WINSTL_API_EXTERNAL_UnicodeAndCharacterSet_MultiByteToWideChar(0, 0, s, -1, pwsz, stlsoft_static_cast(int, 1 + len));
if (0 != n)
{
if (stlsoft_static_cast(cs_size_t, n) < len)
{
pwsz[n] = L'\0';
}
bstr = comstl_C_BSTR_create_w(pwsz);
}
STLSOFT_NS_GLOBAL(CoTaskMemFree)(pwsz);
}
}
return bstr;
}
/** [C] Creates a BSTR from an ANSI string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
* \param len The number of characters of \c s to copy into the result
*
* \result The created BSTR
*/
STLSOFT_INLINE
BSTR
comstl_C_BSTR_create_len_a(
cs_char_a_t const* s
, cs_size_t len
)
{
BSTR bstr = NULL;
COMSTL_MESSAGE_ASSERT("Cannot pass size_t(-1) as length!", 0 != len + 1);
if (s != NULL)
{
cs_char_w_t* pwsz = stlsoft_static_cast(cs_char_w_t*, STLSOFT_NS_GLOBAL(CoTaskMemAlloc)((1 + len) * sizeof(cs_char_w_t)));
if (NULL != pwsz)
{
int n = WINSTL_API_EXTERNAL_UnicodeAndCharacterSet_MultiByteToWideChar(0, 0, s, stlsoft_static_cast(int, len), pwsz, stlsoft_static_cast(int, 1 + len));
#ifdef STLSOFT_DEBUG
if (0 == n)
{
WINSTL_API_EXTERNAL_ErrorHandling_GetLastError();
}
#endif /* STLSOFT_DEBUG */
if (0 != n)
{
bstr = comstl_C_BSTR_create_len_w(pwsz, len);
}
STLSOFT_NS_GLOBAL(CoTaskMemFree)(pwsz);
}
}
return bstr;
}
/** [C] Destroys a BSTR
*
* \ingroup group__library__String
*
* \param bstr The BSTR to destroy
*/
STLSOFT_INLINE
void
comstl_C_BSTR_destroy(
BSTR bstr
)
{
STLSOFT_NS_GLOBAL(SysFreeString)(bstr);
}
/** [C] Duplicates a BSTR
*
* \ingroup group__library__String
*
* \param bstr The BSTR to duplicate
* \return The copied BSTR
*/
STLSOFT_INLINE
BSTR
comstl_C_BSTR_dup(
BSTR bstr
)
{
return comstl_C_BSTR_create_len_w(bstr, STLSOFT_NS_GLOBAL(SysStringLen)(bstr));
}
/** [C] Compares two BSTR strings
*
* \ingroup group__library__String
*
* \param s1 The first BSTR to compare. May be \c nullptr
* \param s2 The second BSTR to compare. May be \c nullptr
*
* \return An integer value representing the relationship between the strings
* \retval <0 s1 is lexicographically less than s2
* \retval 0 s1 is lexicographically equal than s2
* \retval >0 s1 is lexicographically greater than s2
*/
STLSOFT_INLINE
int
comstl_C_BSTR_compare(
BSTR s1
, BSTR s2
)
{
if (NULL == s1)
{
if (NULL == s2)
{
return 0; /* Two NULLs are equal */
}
else
{
return -1;
}
}
else
{
if (NULL == s2)
{
return +1;
}
else
{
return STLSOFT_NS_GLOBAL(wcscmp)(s1, s2);
}
}
}
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
namespace comstl
{
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* C++ functions
*/
#ifdef __cplusplus
/** [C++] Retrieves the length of a BSTR
*
* \ingroup group__library__String
*
* \param bstr The string whose length is to be determined
*
* \result The length of the BSTR
*/
inline
cs_size_t
BSTR_len(
BSTR bstr
)
{
return comstl_C_BSTR_len(bstr);
}
/** [C++] Creates a BSTR from a Unicode string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create_w(
cs_char_w_t const* s
)
{
return comstl_C_BSTR_create_w(s);
}
/** [C++] Creates a BSTR from a Unicode string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
* \param len The number of characters of \c s to copy into the result
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create_w(
cs_char_w_t const* s
, cs_size_t len
)
{
return comstl_C_BSTR_create_len_w(s, len);
}
/** [C++] Creates a BSTR from an ANSI string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create_a(
cs_char_a_t const* s
)
{
return comstl_C_BSTR_create_a(s);
}
/** [C++] Creates a BSTR from an ANSI string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
* \param len The number of characters of \c s to copy into the result
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create_a(
cs_char_a_t const* s
, cs_size_t len
)
{
return comstl_C_BSTR_create_len_a(s, len);
}
/** [C++] Creates a BSTR from an ANSI string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create(
cs_char_a_t const* s
)
{
return BSTR_create_a(s);
}
/** [C++] Creates a BSTR from an ANSI string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
* \param len The number of characters of \c s to copy into the result
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create(
cs_char_a_t const* s
, cs_size_t len
)
{
return BSTR_create_a(s, len);
}
/** [C++] Creates a BSTR from a Unicode string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create(
cs_char_w_t const* s
)
{
return BSTR_create_w(s);
}
/** [C++] Creates a BSTR from a Unicode string
*
* \ingroup group__library__String
*
* \param s The string from which to create the BSTR
* \param len The number of characters of \c s to copy into the result
*
* \result The created BSTR
*/
inline
BSTR
BSTR_create(
cs_char_w_t const* s
, cs_size_t len
)
{
return BSTR_create_w(s, len);
}
/** [C++] Destroys a BSTR
*
* \ingroup group__library__String
*
* \param bstr The BSTR to destroy
*/
inline
void
BSTR_destroy(
BSTR bstr
)
{
comstl_C_BSTR_destroy(bstr);
}
/** [C++] Duplicates a BSTR
*
* \ingroup group__library__String
*
* \param bstr The BSTR to duplicate
* \return The copied BSTR
*/
inline
BSTR
BSTR_dup(
BSTR bstr
)
{
return comstl_C_BSTR_dup(bstr);
}
/** [C++] Compares two BSTR strings
*
* \ingroup group__library__String
*
* \param s1 The first BSTR to compare. May be \c nullptr
* \param s2 The second BSTR to compare. May be \c nullptr
*
* \return An integer value representing the relationship between the strings
* \retval <0 s1 is lexicographically less than s2
* \retval 0 s1 is lexicographically equal than s2
* \retval >0 s1 is lexicographically greater than s2
*/
inline
int
BSTR_compare(
BSTR s1
, BSTR s2
)
{
return comstl_C_BSTR_compare(s1, s2);
}
#endif /* __cplusplus */
/* /////////////////////////////////////////////////////////////////////////
* backwards compatibility
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_create_w, comstl_C_bstr_create_w)
STLSOFT_INLINE
BSTR
comstl__bstr_create_w(
cs_char_w_t const* s
)
{
return comstl_C_BSTR_create_w(s);
}
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_create_len_w, comstl_C_bstr_create_len_w)
STLSOFT_INLINE
BSTR
comstl__bstr_create_len_w(
cs_char_w_t const* s
, cs_size_t len
)
{
return comstl_C_BSTR_create_len_w(s, len);
}
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_create_a, comstl_C_bstr_create_a)
STLSOFT_INLINE
BSTR
comstl__bstr_create_a(
cs_char_a_t const* s
)
{
return comstl_C_BSTR_create_a(s);
}
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_create_len_a, comstl_C_bstr_create_len_a)
STLSOFT_INLINE
BSTR
comstl__bstr_create_len_a(
cs_char_a_t const* s
, cs_size_t len
)
{
return comstl_C_BSTR_create_len_a(s, len);
}
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_destroy, comstl_C_bstr_destroy)
STLSOFT_INLINE
void
comstl__bstr_destroy(
BSTR bstr
)
{
comstl_C_BSTR_destroy(bstr);
}
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_dup, comstl_C_bstr_dup)
STLSOFT_INLINE
BSTR
comstl__bstr_dup(
BSTR bstr
)
{
return comstl_C_BSTR_dup(bstr);
}
STLSOFT_DECLARE_FUNCTION_DEPRECATION_IN_FAVOUR_OF(comstl__bstr_compare, comstl_C_bstr_compare)
STLSOFT_INLINE
int
comstl__bstr_compare(
BSTR s1
, BSTR s2
)
{
return comstl_C_BSTR_compare(s1, s2);
}
# ifdef __cplusplus
inline
BSTR
bstr_create_w(
cs_char_w_t const* s
)
{
return BSTR_create_w(s);
}
inline
BSTR
bstr_create_w(
cs_char_w_t const* s
, cs_size_t len
)
{
return BSTR_create_w(s, len);
}
inline
BSTR
bstr_create_a(
cs_char_a_t const* s
)
{
return BSTR_create_a(s);
}
inline
BSTR
bstr_create_a(
cs_char_a_t const* s
, cs_size_t len
)
{
return BSTR_create_a(s, len);
}
inline
BSTR
bstr_create(
cs_char_a_t const* s
)
{
return BSTR_create(s);
}
inline
BSTR
bstr_create(
cs_char_a_t const* s
, cs_size_t len
)
{
return BSTR_create(s, len);
}
inline
BSTR
bstr_create(
cs_char_w_t const* s
)
{
return BSTR_create(s);
}
inline
BSTR
bstr_create(
cs_char_w_t const* s
, cs_size_t len
)
{
return BSTR_create(s, len);
}
inline
void
bstr_destroy(
BSTR bstr
)
{
BSTR_destroy(bstr);
}
inline
BSTR
bstr_dup(
BSTR bstr
)
{
return BSTR_dup(bstr);
}
inline
int
bstr_compare(
BSTR s1
, BSTR s2
)
{
return BSTR_compare(s1, s2);
}
# endif /* __cplusplus */
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef COMSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} /* namespace comstl */
# else
} /* namespace comstl_project */
} /* namespace stlsoft */
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !COMSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* inclusion control
*/
#ifdef STLSOFT_CF_PRAGMA_ONCE_SUPPORT
# pragma once
#endif /* STLSOFT_CF_PRAGMA_ONCE_SUPPORT */
#endif /* !COMSTL_INCL_COMSTL_STRING_H_BSTR_FUNCTIONS */
/* ///////////////////////////// end of file //////////////////////////// */