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