Imported Upstream version 58.1
[platform/upstream/icu.git] / source / io / uscanf.c
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 *   Copyright (C) 1998-2014, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * File uscanf.c
12 *
13 * Modification History:
14 *
15 *   Date        Name        Description
16 *   12/02/98    stephen        Creation.
17 *   03/13/99    stephen     Modified for new C API.
18 ******************************************************************************
19 */
20
21 #include "unicode/utypes.h"
22
23 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
24
25 #include "unicode/putil.h"
26 #include "unicode/ustdio.h"
27 #include "unicode/ustring.h"
28 #include "uscanf.h"
29 #include "ufile.h"
30 #include "ufmt_cmn.h"
31
32 #include "cmemory.h"
33 #include "cstring.h"
34
35
36 U_CAPI int32_t U_EXPORT2
37 u_fscanf(UFILE        *f,
38          const char    *patternSpecification,
39          ... )
40 {
41     va_list ap;
42     int32_t converted;
43
44     va_start(ap, patternSpecification);
45     converted = u_vfscanf(f, patternSpecification, ap);
46     va_end(ap);
47
48     return converted;
49 }
50
51 U_CAPI int32_t U_EXPORT2
52 u_fscanf_u(UFILE        *f,
53            const UChar    *patternSpecification,
54            ... )
55 {
56     va_list ap;
57     int32_t converted;
58
59     va_start(ap, patternSpecification);
60     converted = u_vfscanf_u(f, patternSpecification, ap);
61     va_end(ap);
62
63     return converted;
64 }
65
66 U_CAPI int32_t  U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
67 u_vfscanf(UFILE        *f,
68           const char    *patternSpecification,
69           va_list        ap)
70 {
71     int32_t converted;
72     UChar *pattern;
73     UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE];
74     int32_t size = (int32_t)uprv_strlen(patternSpecification) + 1;
75
76     /* convert from the default codepage to Unicode */
77     if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) {
78         pattern = (UChar *)uprv_malloc(size * sizeof(UChar));
79         if(pattern == 0) {
80             return 0;
81         }
82     }
83     else {
84         pattern = patBuffer;
85     }
86     u_charsToUChars(patternSpecification, pattern, size);
87
88     /* do the work */
89     converted = u_vfscanf_u(f, pattern, ap);
90
91     /* clean up */
92     if (pattern != patBuffer) {
93         uprv_free(pattern);
94     }
95
96     return converted;
97 }
98
99 U_CAPI int32_t  U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
100 u_vfscanf_u(UFILE       *f,
101             const UChar *patternSpecification,
102             va_list     ap)
103 {
104     return u_scanf_parse(f, patternSpecification, ap);
105 }
106
107 #endif /* #if !UCONFIG_NO_FORMATTING */
108