Rev 8687 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8687 | Rev 8718 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 1 | #include |
|
1 | //#include "format_scan.h" |
2 | #include |
2 | #include |
3 | #include |
3 | #include |
4 | #include |
- | 5 | char *__scanf_buffer = 0; |
|
- | 6 | ||
- | 7 | int virtual_getc_con(void *sp, const void *obj) |
|
- | 8 | // get next chat from string obj, save point is ptr to string char ptr |
|
- | 9 | { |
|
- | 10 | int ch; |
|
- | 11 | const char**spc= (const char**)sp; |
|
- | 12 | if (!spc) return EOF; // error |
|
- | 13 | ||
- | 14 | if (!*spc) *spc = __scanf_buffer; // first call, init savepoint |
|
- | 15 | ||
- | 16 | while (!**spc) // need to read more |
|
- | 17 | { |
|
- | 18 | if(!gets(__scanf_buffer)) return EOF; |
|
- | 19 | *spc = __scanf_buffer; |
|
- | 20 | strcat(__scanf_buffer,"\n"); // imitate delimiter |
|
- | 21 | } |
|
- | 22 | if (**spc == 26 || **spc == 3) // ^C ^Z end of scan, clear buffer |
|
- | 23 | { |
|
- | 24 | *spc = __scanf_buffer; |
|
- | 25 | *__scanf_buffer = 0; |
|
- | 26 | return EOF; // ^C ^Z |
|
- | 27 | } |
|
- | 28 | ||
- | 29 | ch = **spc; (*spc)++ ; |
|
- | 30 | ||
- | 31 | //printf("getc '%c'[%d];", ch, ch); |
|
- | 32 | return ch; |
|
- | 33 | } |
|
- | 34 | ||
- | 35 | void virtual_ungetc_con(void *sp, int c, const void *obj) |
|
- | 36 | // if can, one step back savepoint in s |
|
- | 37 | { |
|
- | 38 | const char**spc= (const char**)sp; |
|
- | 39 | ||
- | 40 | if (spc && *spc > __scanf_buffer) (*spc)--; |
|
- | 41 | //printf("Ungetc '%c'[%d];", c, c); |
|
- | 42 | } |
|
- | 43 | ||
- | 44 | int vscanf ( const char * format, va_list arg ) |
|
- | 45 | { |
|
- | 46 | return format_scan(NULL, format, arg, &virtual_getc_con, &virtual_ungetc_con); |
|
- | 47 | }; |
|
Line 4... | Line 48... | ||
4 | 48 | ||
5 | int scanf ( const char * format, ...) |
49 | int scanf ( const char * format, ...) |
6 | { |
50 | { |
7 | va_list arg; |
51 | va_list arg; |
8 | int n; |
52 | int n; |
Line 9... | Line 53... | ||
9 | va_start(arg, format); |
53 | va_start(arg, format); |
10 | 54 | ||
Line 11... | Line 55... | ||
11 | if(__scanf_buffer == NULL) __scanf_buffer = malloc(STDIO_MAX_MEM); |
55 | if(__scanf_buffer == NULL) __scanf_buffer = malloc(4096); |
12 | if(__scanf_buffer == NULL) errno = ENOMEM; return ENOMEM; |
56 | if(__scanf_buffer == NULL) return -3; |
Line 13... | Line 57... | ||
13 | 57 |