w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ustrenum.cpp
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2002-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Author: Alan Liu
9 * Created: November 11 2002
10 * Since: ICU 2.4
11 **********************************************************************
12 */
13 #include "utypeinfo.h" // for 'typeid' to work
14 
15 #include "unicode/ustring.h"
16 #include "unicode/strenum.h"
17 #include "unicode/putil.h"
18 #include "uenumimp.h"
19 #include "ustrenum.h"
20 #include "cstring.h"
21 #include "cmemory.h"
22 #include "uassert.h"
23 
25 // StringEnumeration implementation ---------------------------------------- ***
26 
27 StringEnumeration::StringEnumeration()
28  : chars(charsBuffer), charsCapacity(sizeof(charsBuffer)) {
29 }
30 
31 StringEnumeration::~StringEnumeration() {
32  if (chars != NULL && chars != charsBuffer) {
34  }
35 }
36 
37 // StringEnumeration base class clone() default implementation, does not clone
38 StringEnumeration *
39 StringEnumeration::clone() const {
40  return NULL;
41 }
42 
43 const char *
45  const UnicodeString *s=snext(status);
46  if(U_SUCCESS(status) && s!=NULL) {
47  unistr=*s;
48  ensureCharsCapacity(unistr.length()+1, status);
49  if(U_SUCCESS(status)) {
50  if(resultLength!=NULL) {
51  *resultLength=unistr.length();
52  }
53  unistr.extract(0, INT32_MAX, chars, charsCapacity, US_INV);
54  return chars;
55  }
56  }
57 
58  return NULL;
59 }
60 
61 const UChar *
62 StringEnumeration::unext(int32_t *resultLength, UErrorCode &status) {
63  const UnicodeString *s=snext(status);
64  if(U_SUCCESS(status) && s!=NULL) {
65  unistr=*s;
66  if(resultLength!=NULL) {
67  *resultLength=unistr.length();
68  }
69  return unistr.getTerminatedBuffer();
70  }
71 
72  return NULL;
73 }
74 
75 const UnicodeString *
76 StringEnumeration::snext(UErrorCode &status) {
78  const char *s=next(&length, status);
79  return setChars(s, length, status);
80 }
81 
82 void
83 StringEnumeration::ensureCharsCapacity(int32_t capacity, UErrorCode &status) {
84  if(U_SUCCESS(status) && capacity>charsCapacity) {
85  if(capacity<(charsCapacity+charsCapacity/2)) {
86  // avoid allocation thrashing
87  capacity=charsCapacity+charsCapacity/2;
88  }
89  if(chars!=charsBuffer) {
91  }
92  chars=(char *)uprv_malloc(capacity);
93  if(chars==NULL) {
94  chars=charsBuffer;
95  charsCapacity=sizeof(charsBuffer);
97  } else {
98  charsCapacity=capacity;
99  }
100  }
101 }
102 
103 UnicodeString *
104 StringEnumeration::setChars(const char *s, int32_t length, UErrorCode &status) {
105  if(U_SUCCESS(status) && s!=NULL) {
106  if(length<0) {
108  }
109 
110  UChar *buffer=unistr.getBuffer(length+1);
111  if(buffer!=NULL) {
113  buffer[length]=0;
114  unistr.releaseBuffer(length);
115  return &unistr;
116  } else {
118  }
119  }
120 
121  return NULL;
122 }
123 UBool
124 StringEnumeration::operator==(const StringEnumeration& that)const {
125  return typeid(*this) == typeid(that);
126 }
127 
128 UBool
129 StringEnumeration::operator!=(const StringEnumeration& that)const {
130  return !operator==(that);
131 }
132 
133 // UStringEnumeration implementation --------------------------------------- ***
134 
137  UEnumeration *uenumToAdopt, UErrorCode &status) {
138  if (U_FAILURE(status)) {
139  uenum_close(uenumToAdopt);
140  return NULL;
141  }
142  UStringEnumeration *result = new UStringEnumeration(uenumToAdopt);
143  if (result == NULL) {
145  uenum_close(uenumToAdopt);
146  return NULL;
147  }
148  return result;
149 }
150 
152  uenum(_uenum) {
153  U_ASSERT(_uenum != 0);
154 }
155 
158 }
159 
161  return uenum_count(uenum, &status);
162 }
163 
164 const char *UStringEnumeration::next(int32_t *resultLength, UErrorCode &status) {
165  return uenum_next(uenum, resultLength, &status);
166 }
167 
169  int32_t length;
170  const UChar* str = uenum_unext(uenum, &length, &status);
171  if (str == 0 || U_FAILURE(status)) {
172  return 0;
173  }
174  return &unistr.setTo(str, length);
175 }
176 
179 }
180 
183 
184 // C wrapper --------------------------------------------------------------- ***
185 
186 #define THIS(en) ((icu::StringEnumeration*)(en->context))
187 
189 
190 /**
191  * Wrapper API to make StringEnumeration look like UEnumeration.
192  */
193 static void U_CALLCONV
195  delete THIS(en);
196  uprv_free(en);
197 }
198 
199 /**
200  * Wrapper API to make StringEnumeration look like UEnumeration.
201  */
202 static int32_t U_CALLCONV
204  UErrorCode* ec)
205 {
206  return THIS(en)->count(*ec);
207 }
208 
209 /**
210  * Wrapper API to make StringEnumeration look like UEnumeration.
211  */
212 static const UChar* U_CALLCONV
214  int32_t* resultLength,
215  UErrorCode* ec)
216 {
217  return THIS(en)->unext(resultLength, *ec);
218 }
219 
220 /**
221  * Wrapper API to make StringEnumeration look like UEnumeration.
222  */
223 static const char* U_CALLCONV
225  int32_t* resultLength,
226  UErrorCode* ec)
227 {
228  return THIS(en)->next(resultLength, *ec);
229 }
230 
231 /**
232  * Wrapper API to make StringEnumeration look like UEnumeration.
233  */
234 static void U_CALLCONV
236  UErrorCode* ec)
237 {
238  THIS(en)->reset(*ec);
239 }
240 
241 /**
242  * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
243  * The StringEnumeration pointer will be stored in 'context'.
244  */
245 static const UEnumeration USTRENUM_VT = {
246  NULL,
247  NULL, // store StringEnumeration pointer here
253 };
254 
256 
257 /**
258  * Given a StringEnumeration, wrap it in a UEnumeration. The
259  * StringEnumeration is adopted; after this call, the caller must not
260  * delete it (regardless of error status).
261  */
263 uenum_openFromStringEnumeration(icu::StringEnumeration* adopted, UErrorCode* ec) {
265  if (U_SUCCESS(*ec) && adopted != NULL) {
267  if (result == NULL) {
269  } else {
271  result->context = adopted;
272  }
273  }
274  if (result == NULL) {
275  delete adopted;
276  }
277  return result;
278 }
279 
280 // C wrapper --------------------------------------------------------------- ***
281 
283 
284 typedef struct UCharStringEnumeration {
288 
289 static void U_CALLCONV
291  uprv_free(en);
292 }
293 
294 static int32_t U_CALLCONV
296  UErrorCode* /*ec*/) {
297  return ((UCharStringEnumeration*)en)->count;
298 }
299 
300 static const UChar* U_CALLCONV
302  int32_t* resultLength,
303  UErrorCode* /*ec*/) {
305  if (e->index >= e->count) {
306  return NULL;
307  }
308  const UChar* result = ((const UChar**)e->uenum.context)[e->index++];
309  if (resultLength) {
310  *resultLength = (int32_t)u_strlen(result);
311  }
312  return result;
313 }
314 
315 
316 static const char* U_CALLCONV
318  int32_t* resultLength,
319  UErrorCode* /*ec*/) {
321  if (e->index >= e->count) {
322  return NULL;
323  }
324  const char* result = ((const char**)e->uenum.context)[e->index++];
325  if (resultLength) {
326  *resultLength = (int32_t)uprv_strlen(result);
327  }
328  return result;
329 }
330 
331 static void U_CALLCONV
333  UErrorCode* /*ec*/) {
334  ((UCharStringEnumeration*)en)->index = 0;
335 }
336 
338  NULL,
339  NULL, // store StringEnumeration pointer here
345 };
346 
348  NULL,
349  NULL, // store StringEnumeration pointer here
355 };
356 
358 
361  UErrorCode* ec) {
363  if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) {
365  if (result == NULL) {
367  } else {
368  U_ASSERT((char*)result==(char*)(&result->uenum));
370  result->uenum.context = (void*)strings;
371  result->index = 0;
372  result->count = count;
373  }
374  }
375  return (UEnumeration*) result;
376 }
377 
380  UErrorCode* ec) {
382  if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) {
384  if (result == NULL) {
386  } else {
387  U_ASSERT((char*)result==(char*)(&result->uenum));
389  result->uenum.context = (void*)strings;
390  result->index = 0;
391  result->count = count;
392  }
393  }
394  return (UEnumeration*) result;
395 }
396 
397 
398 // end C Wrapper
#define count(a)
Definition: aptex-macros.h:781
#define next(a)
Definition: aptex-macros.h:924
virtual int32_t count(UErrorCode &status) const
Definition: ustrenum.cpp:160
virtual const char * next(int32_t *resultLength, UErrorCode &status)
Definition: ustrenum.cpp:164
static UStringEnumeration * fromUEnumeration(UEnumeration *enumToAdopt, UErrorCode &status)
Definition: ustrenum.cpp:136
virtual const UnicodeString * snext(UErrorCode &status)
Definition: ustrenum.cpp:168
UStringEnumeration(UEnumeration *uenum)
Definition: ustrenum.cpp:151
virtual ~UStringEnumeration()
Definition: ustrenum.cpp:156
virtual void reset(UErrorCode &status)
Definition: ustrenum.cpp:177
UEnumeration * uenum
Definition: ustrenum.h:81
#define uprv_memcpy(dst, src, size)
Definition: cmemory.h:40
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CompactDecimalFormat) CompactDecimalFormat *CompactDecimalFormat
#define uprv_strlen(str)
Definition: cstring.h:37
char * strings
Definition: dvidvi.c:141
#define s
Definition: afcover.h:80
bool operator!=(const GenBuffer &s1, const GenBuffer &s2)
Definition: gensi.hpp:716
bool operator==(const GenBuffer &s1, const GenBuffer &s2)
Definition: gensi.hpp:671
#define ec
Definition: gsftopk.c:502
unsigned char UChar
Definition: bzip2.c:163
#define NULL
Definition: ftobjs.h:61
sizeof(AF_ModuleRec)
#define INT32_MAX
Definition: stdint.h:137
signed int int32_t
Definition: stdint.h:77
int capacity
Definition: pdfcolor.c:1335
#define length(c)
Definition: ctangleboot.c:65
#define U_CALLCONV
Definition: platform.h:877
#define U_EXPORT2
Definition: platform.h:844
#define status
C API: Unicode string handling functions.
C API: Platform Utilities.
#define str(s)
Definition: sh6.c:399
ShellFileEnvironment e
Definition: sh6.c:388
#define int32_t
Definition: stdint.in.h:167
bool operator==(const String &a, const String &b)
Compares two strings for equality.
Definition: string.hh:739
UEnumeration uenum
Definition: ustrenum.cpp:285
Definition: utils.c:300
#define U_ASSERT(exp)
Definition: uassert.h:37
#define U_CDECL_END
Definition: umachine.h:86
int8_t UBool
Definition: umachine.h:269
#define U_CAPI
Definition: umachine.h:110
#define U_CDECL_BEGIN
Definition: umachine.h:85
#define uenum_unext
Definition: urename.h:933
#define uenum_unextDefault
Definition: urename.h:934
#define uenum_close
Definition: urename.h:925
#define uenum_reset
Definition: urename.h:932
#define uenum_openUCharStringsEnumeration
Definition: urename.h:931
#define u_charsToUChars
Definition: urename.h:220
#define uenum_openFromStringEnumeration
Definition: urename.h:930
#define uenum_next
Definition: urename.h:927
#define uprv_malloc
Definition: urename.h:1435
#define uprv_free
Definition: urename.h:1414
#define uenum_count
Definition: urename.h:926
#define uenum_openCharStringsEnumeration
Definition: urename.h:929
#define u_strlen
Definition: urename.h:384
#define uenum_nextDefault
Definition: urename.h:928
struct UCharStringEnumeration UCharStringEnumeration
static const UChar * ustrenum_unext(UEnumeration *en, int32_t *resultLength, UErrorCode *ec)
Definition: ustrenum.cpp:213
static const UEnumeration UCHARSTRENUM_VT
Definition: ustrenum.cpp:337
static void ustrenum_reset(UEnumeration *en, UErrorCode *ec)
Definition: ustrenum.cpp:235
static int32_t ustrenum_count(UEnumeration *en, UErrorCode *ec)
Definition: ustrenum.cpp:203
static const UChar * ucharstrenum_unext(UEnumeration *en, int32_t *resultLength, UErrorCode *)
Definition: ustrenum.cpp:301
static const char * ucharstrenum_next(UEnumeration *en, int32_t *resultLength, UErrorCode *)
Definition: ustrenum.cpp:317
static void ustrenum_close(UEnumeration *en)
Definition: ustrenum.cpp:194
static const UEnumeration UCHARSTRENUM_U_VT
Definition: ustrenum.cpp:347
static void ucharstrenum_reset(UEnumeration *en, UErrorCode *)
Definition: ustrenum.cpp:332
static void ucharstrenum_close(UEnumeration *en)
Definition: ustrenum.cpp:290
#define THIS(en)
Definition: ustrenum.cpp:186
static const UEnumeration USTRENUM_VT
Definition: ustrenum.cpp:245
static int32_t ucharstrenum_count(UEnumeration *en, UErrorCode *)
Definition: ustrenum.cpp:295
static const char * ustrenum_next(UEnumeration *en, int32_t *resultLength, UErrorCode *ec)
Definition: ustrenum.cpp:224
UErrorCode
Definition: utypes.h:431
@ U_MEMORY_ALLOCATION_ERROR
Definition: utypes.h:473
#define U_FAILURE(x)
Definition: utypes.h:735
#define U_SUCCESS(x)
Definition: utypes.h:730
#define chars