1 /* Public Domain Curses */
5 RCSID("$Id: scanw.c,v 1.42 2008/07/14 12:22:13 wmcbrine Exp $")
7 /*man-start**************************************************************
12 int scanw(const char *fmt, ...);
13 int wscanw(WINDOW *win, const char *fmt, ...);
14 int mvscanw(int y, int x, const char *fmt, ...);
15 int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...);
16 int vwscanw(WINDOW *win, const char *fmt, va_list varglist);
17 int vw_scanw(WINDOW *win, const char *fmt, va_list varglist);
20 These routines correspond to the standard C library's scanf()
21 family. Each gets a string from the window via wgetnstr(), and
22 uses the resulting line as input for the scan.
25 On successful completion, these functions return the number of
26 items successfully matched. Otherwise they return ERR.
28 Portability X/Open BSD SYS V
36 **man-end****************************************************************/
45 static int _pdc_vsscanf(const char *, const char *, va_list);
47 # define vsscanf _pdc_vsscanf
50 int vwscanw(WINDOW
*win
, const char *fmt
, va_list varglist
)
54 PDC_LOG(("vwscanw() - called\n"));
56 if (wgetnstr(win
, scanbuf
, 255) == ERR
)
59 return vsscanf(scanbuf
, fmt
, varglist
);
62 int scanw(const char *fmt
, ...)
67 PDC_LOG(("scanw() - called\n"));
70 retval
= vwscanw(stdscr
, fmt
, args
);
76 int wscanw(WINDOW
*win
, const char *fmt
, ...)
81 PDC_LOG(("wscanw() - called\n"));
84 retval
= vwscanw(win
, fmt
, args
);
90 int mvscanw(int y
, int x
, const char *fmt
, ...)
95 PDC_LOG(("mvscanw() - called\n"));
97 if (move(y
, x
) == ERR
)
101 retval
= vwscanw(stdscr
, fmt
, args
);
107 int mvwscanw(WINDOW
*win
, int y
, int x
, const char *fmt
, ...)
112 PDC_LOG(("mvscanw() - called\n"));
114 if (wmove(win
, y
, x
) == ERR
)
118 retval
= vwscanw(win
, fmt
, args
);
124 int vw_scanw(WINDOW
*win
, const char *fmt
, va_list varglist
)
126 PDC_LOG(("vw_scanw() - called\n"));
128 return vwscanw(win
, fmt
, varglist
);
133 /* _pdc_vsscanf() - Internal routine to parse and format an input
134 buffer. It scans a series of input fields; each field is formatted
135 according to a supplied format string and the formatted input is
136 stored in the variable number of addresses passed. Returns the number
137 of input fields or EOF on error.
139 Don't compile this unless required. Some compilers (at least Borland
140 C++ 3.0) have to link with math libraries due to the use of floats.
142 Based on vsscanf.c and input.c from emx 0.8f library source,
143 Copyright (c) 1990-1992 by Eberhard Mattes, who has kindly agreed to
144 its inclusion in PDCurses. */
146 #define WHITE(x) ((x) == ' ' || (x) == '\t' || (x) == '\n')
152 return (count ? count : EOF); \
161 static int _pdc_vsscanf(const char *buf
, const char *fmt
, va_list arg_ptr
)
163 int count
, chars
, c
, width
, radix
, d
, i
;
169 char neg
, assign
, ok
, size
;
172 double dx
, dd
, *dbl_ptr
;
180 while ((f
= *fmt
) != 0)
224 while (isdigit(*fmt
))
225 width
= width
* 10 + (*fmt
++ - '0');
230 if (*fmt
== 'h' || *fmt
== 'l')
236 if (width
== INT_MAX
)
239 char_ptr
= va_arg(arg_ptr
, char *);
246 *char_ptr
++ = (char) c
;
263 f
= (unsigned char) *fmt
;
267 /* avoid skipping past 0 */
279 if (fmt
[1] == '-' && fmt
[2]
280 && f
< (unsigned char)fmt
[2])
282 memset(map
+ f
, 1, (unsigned char)fmt
[2] - f
);
306 char_ptr
= va_arg(arg_ptr
, char *);
307 while (width
> 0 && map
[(unsigned char) c
] != end
)
311 *char_ptr
++ = (char) c
;
350 while (width
> 0 && isdigit(c
))
353 dx
= dx
* 10.0 + (double) (c
- '0');
361 if (width
> 0 && c
== '.')
366 while (width
> 0 && isdigit(c
))
369 dx
+= (double) (c
- '0') / dd
;
381 if (width
> 0 && (c
== 'e' || c
== 'E'))
387 if (width
> 0 && c
== '+')
391 } else if (width
> 0 && c
== '-')
397 if (!(width
> 0 && isdigit(c
)))
402 while (width
> 0 && isdigit(c
))
405 exp
= exp
* 10 + (c
- '0');
431 dbl_ptr
= va_arg(arg_ptr
, double *);
436 flt_ptr
= va_arg(arg_ptr
, float *);
437 *flt_ptr
= (float)dx
;
454 if (!(width
> 0 && c
== '0'))
455 goto scan_complete_number
;
458 if (width
> 0 && (c
== 'x' || c
== 'X'))
464 else if (width
> 0 && (c
>= '0' && c
<= '7'))
466 goto scan_unsigned_number
;
490 scan_complete_number
:
492 if (width
> 0 && c
== '+')
497 else if (width
> 0 && c
== '-' && radix
== 10)
503 scan_unsigned_number
:
517 if (d
< 0 || d
>= radix
)
536 short_ptr
= va_arg(arg_ptr
, short *);
537 *short_ptr
= (short) n
;
540 long_ptr
= va_arg(arg_ptr
, long *);
541 *long_ptr
= (long) n
;
544 int_ptr
= va_arg(arg_ptr
, int *);
557 int_ptr
= va_arg(arg_ptr
, int *);
563 if (!f
) /* % at end of string */
575 #endif /* HAVE_VSSCANF */