Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * general implementation of scanf used by scanf, sscanf, fscanf, |
| 3 | * _cscanf, wscanf, swscanf and fwscanf |
| 4 | * |
| 5 | * Copyright 1996,1998 Marcus Meissner |
| 6 | * Copyright 1996 Jukka Iivonen |
| 7 | * Copyright 1997,2000 Uwe Bonnes |
| 8 | * Copyright 2000 Jon Griffiths |
| 9 | * Copyright 2002 Daniel Gudbjartsson |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2.1 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 26 | #include <stdarg.h> |
| 27 | |
| 28 | #include "windef.h" |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 29 | #include "winbase.h" |
György 'Nog' Jeney | e022026 | 2002-10-15 02:20:07 +0000 | [diff] [blame] | 30 | #include "winternl.h" |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 31 | #include "msvcrt.h" |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 32 | #include "wine/debug.h" |
| 33 | |
| 34 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); |
| 35 | |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 36 | extern MSVCRT_FILE MSVCRT__iob[]; |
| 37 | |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 38 | /* helper function for *scanf. Returns the value of character c in the |
| 39 | * given base, or -1 if the given character is not a digit of the base. |
| 40 | */ |
| 41 | static int char2digit(char c, int base) { |
| 42 | if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0'); |
| 43 | if (base<=10) return -1; |
| 44 | if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10); |
| 45 | if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | /* helper function for *wscanf. Returns the value of character c in the |
| 50 | * given base, or -1 if the given character is not a digit of the base. |
| 51 | */ |
Alexandre Julliard | 5f31b32 | 2002-12-19 04:21:30 +0000 | [diff] [blame] | 52 | static int wchar2digit(MSVCRT_wchar_t c, int base) { |
Michael Stefaniuc | ec4936a | 2007-06-22 23:39:31 +0200 | [diff] [blame] | 53 | if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0'); |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 54 | if (base<=10) return -1; |
Michael Stefaniuc | ec4936a | 2007-06-22 23:39:31 +0200 | [diff] [blame] | 55 | if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10); |
| 56 | if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10); |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 60 | /* vfscanf */ |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 61 | #undef WIDE_SCANF |
| 62 | #undef CONSOLE |
| 63 | #undef STRING |
| 64 | #include "scanf.h" |
| 65 | |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 66 | /* vfwscanf */ |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 67 | #define WIDE_SCANF 1 |
| 68 | #undef CONSOLE |
| 69 | #undef STRING |
| 70 | #include "scanf.h" |
| 71 | |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 72 | /* vsscanf */ |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 73 | #undef WIDE_SCANF |
| 74 | #undef CONSOLE |
| 75 | #define STRING 1 |
| 76 | #include "scanf.h" |
| 77 | |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 78 | /* vswscanf */ |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 79 | #define WIDE_SCANF 1 |
| 80 | #undef CONSOLE |
| 81 | #define STRING 1 |
| 82 | #include "scanf.h" |
| 83 | |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 84 | /* vcscanf */ |
Daniel Gudbjartsson | 42c74d6 | 2002-08-17 01:22:00 +0000 | [diff] [blame] | 85 | #undef WIDE_SCANF |
| 86 | #define CONSOLE 1 |
| 87 | #undef STRING |
| 88 | #include "scanf.h" |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | /********************************************************************* |
| 92 | * fscanf (MSVCRT.@) |
| 93 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 94 | int CDECL MSVCRT_fscanf(MSVCRT_FILE *file, const char *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 95 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 96 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 97 | int res; |
| 98 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 99 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 100 | res = MSVCRT_vfscanf(file, format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 101 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 102 | return res; |
| 103 | } |
| 104 | |
| 105 | /********************************************************************* |
| 106 | * scanf (MSVCRT.@) |
| 107 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 108 | int CDECL MSVCRT_scanf(const char *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 109 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 110 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 111 | int res; |
| 112 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 113 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 114 | res = MSVCRT_vfscanf(MSVCRT_stdin, format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 115 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 116 | return res; |
| 117 | } |
| 118 | |
| 119 | /********************************************************************* |
| 120 | * fwscanf (MSVCRT.@) |
| 121 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 122 | int CDECL MSVCRT_fwscanf(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 123 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 124 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 125 | int res; |
| 126 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 127 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 128 | res = MSVCRT_vfwscanf(file, format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 129 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 130 | return res; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /********************************************************************* |
| 135 | * wscanf (MSVCRT.@) |
| 136 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 137 | int CDECL MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 138 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 139 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 140 | int res; |
| 141 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 142 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 143 | res = MSVCRT_vfwscanf(MSVCRT_stdin, format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 144 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 145 | return res; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /********************************************************************* |
| 150 | * sscanf (MSVCRT.@) |
| 151 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 152 | int CDECL MSVCRT_sscanf(const char *str, const char *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 153 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 154 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 155 | int res; |
| 156 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 157 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 158 | res = MSVCRT_vsscanf(str, format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 159 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 160 | return res; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /********************************************************************* |
| 165 | * swscanf (MSVCRT.@) |
| 166 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 167 | int CDECL MSVCRT_swscanf(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 168 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 169 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 170 | int res; |
| 171 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 172 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 173 | res = MSVCRT_vswscanf(str, format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 174 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 175 | return res; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /********************************************************************* |
| 180 | * _cscanf (MSVCRT.@) |
| 181 | */ |
Alexandre Julliard | 24beabf | 2006-06-13 11:21:19 +0200 | [diff] [blame] | 182 | int CDECL _cscanf(const char *format, ...) |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 183 | { |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 184 | __ms_va_list valist; |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 185 | int res; |
| 186 | |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 187 | __ms_va_start(valist, format); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 188 | res = MSVCRT_vcscanf(format, valist); |
Alexandre Julliard | f8de2eb | 2009-01-02 21:44:40 +0100 | [diff] [blame^] | 189 | __ms_va_end(valist); |
Alexandre Julliard | c042e13 | 2004-02-19 01:13:12 +0000 | [diff] [blame] | 190 | return res; |
| 191 | } |