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