]> git.saurik.com Git - apple/icu.git/blob - icuSources/io/sscanf.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / io / sscanf.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 * Copyright (C) 2000-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * File sscanf.c
12 *
13 * Modification History:
14 *
15 * Date Name Description
16 * 02/08/00 george Creation. Copied from uscanf.c
17 ******************************************************************************
18 */
19
20 #include "unicode/utypes.h"
21
22 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
23
24 #include "unicode/putil.h"
25 #include "unicode/ustdio.h"
26 #include "unicode/ustring.h"
27 #include "uscanf.h"
28 #include "ufile.h"
29 #include "ufmt_cmn.h"
30
31 #include "cmemory.h"
32 #include "cstring.h"
33
34
35 U_CAPI int32_t U_EXPORT2
36 u_sscanf(const UChar *buffer,
37 const char *patternSpecification,
38 ... )
39 {
40 va_list ap;
41 int32_t converted;
42
43 va_start(ap, patternSpecification);
44 converted = u_vsscanf(buffer, patternSpecification, ap);
45 va_end(ap);
46
47 return converted;
48 }
49
50 U_CAPI int32_t U_EXPORT2
51 u_sscanf_u(const UChar *buffer,
52 const UChar *patternSpecification,
53 ... )
54 {
55 va_list ap;
56 int32_t converted;
57
58 va_start(ap, patternSpecification);
59 converted = u_vsscanf_u(buffer, patternSpecification, ap);
60 va_end(ap);
61
62 return converted;
63 }
64
65 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
66 u_vsscanf(const UChar *buffer,
67 const char *patternSpecification,
68 va_list ap)
69 {
70 int32_t converted;
71 UChar *pattern;
72 UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE];
73 int32_t size = (int32_t)uprv_strlen(patternSpecification) + 1;
74
75 /* convert from the default codepage to Unicode */
76 if (size >= (int32_t)MAX_UCHAR_BUFFER_SIZE(patBuffer)) {
77 pattern = (UChar *)uprv_malloc(size * sizeof(UChar));
78 if(pattern == 0) {
79 return 0;
80 }
81 }
82 else {
83 pattern = patBuffer;
84 }
85 u_charsToUChars(patternSpecification, pattern, size);
86
87 /* do the work */
88 converted = u_vsscanf_u(buffer, pattern, ap);
89
90 /* clean up */
91 if (pattern != patBuffer) {
92 uprv_free(pattern);
93 }
94
95 return converted;
96 }
97
98 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
99 u_vsscanf_u(const UChar *buffer,
100 const UChar *patternSpecification,
101 va_list ap)
102 {
103 int32_t converted;
104 UFILE inStr;
105
106 inStr.fConverter = NULL;
107 inStr.fFile = NULL;
108 inStr.fOwnFile = FALSE;
109 #if !UCONFIG_NO_TRANSLITERATION
110 inStr.fTranslit = NULL;
111 #endif
112 inStr.fUCBuffer[0] = 0;
113 inStr.str.fBuffer = (UChar *)buffer;
114 inStr.str.fPos = (UChar *)buffer;
115 inStr.str.fLimit = buffer + u_strlen(buffer);
116
117 if(u_locbund_init(&inStr.str.fBundle, "en_US_POSIX") == 0) {
118 return 0;
119 }
120
121 converted = u_scanf_parse(&inStr, patternSpecification, ap);
122
123 u_locbund_close(&inStr.str.fBundle);
124
125 /* return # of items converted */
126 return converted;
127 }
128
129 #endif /* #if !UCONFIG_NO_FORMATTING */
130