ctype+scanf fix

git-svn-id: svn://kolibrios.org@6432 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
siemargl 2016-05-16 11:03:59 +00:00
parent 9eb1268c4d
commit cd8030cee3
6 changed files with 51 additions and 18 deletions

View File

@ -16,19 +16,19 @@
#define __UPPER 512
#define __XDIGIT 1024
extern char __is[128];
extern unsigned short __is[128];
#define isalnum(c)(__is[c] & __ALNUM ) /* 'a'-'z', 'A'-'Z', '0'-'9' */
#define isalpha(c)(__is[c] & __ALPHA ) /* 'a'-'z', 'A'-'Z' */
#define iscntrl(c)(__is[c] & __CNTRL ) /* 0-31, 127 */
#define isdigit(c)(__is[c] & __DIGIT ) /* '0'-'9' */
#define isgraph(c)(__is[c] & __GRAPH ) /* '!'-'~' */
#define islower(c)(__is[c] & __LOWER ) /* 'a'-'z' */
#define isprint(c)(__is[c] & __PRINT ) /* ' '-'~' */
#define ispunct(c)(__is[c] & __PUNCT ) /* !alnum && !cntrl && !space */
#define isspace(c)(__is[c] & __BLANK ) /* HT, LF, VT, FF, CR, ' ' */
#define isupper(c)(__is[c] & __UPPER ) /* 'A'-'Z' */
#define isxdigit(c)(__is[c] & __XDIGIT) /* '0'-'9', 'a'-'f', 'A'-'F' */
#define isalnum(c)(__is[c+1] & __ALNUM ) /* 'a'-'z', 'A'-'Z', '0'-'9' */
#define isalpha(c)(__is[c+1] & __ALPHA ) /* 'a'-'z', 'A'-'Z' */
#define iscntrl(c)(__is[c+1] & __CNTRL ) /* 0-31, 127 */
#define isdigit(c)(__is[c+1] & __DIGIT ) /* '0'-'9' */
#define isgraph(c)(__is[c+1] & __GRAPH ) /* '!'-'~' */
#define islower(c)(__is[c+1] & __LOWER ) /* 'a'-'z' */
#define isprint(c)(__is[c+1] & __PRINT ) /* ' '-'~' */
#define ispunct(c)(__is[c+1] & __PUNCT ) /* !alnum && !cntrl && !space */
#define isspace(c)(__is[c+1] & __BLANK ) /* HT, LF, VT, FF, CR, ' ' */
#define isupper(c)(__is[c+1] & __UPPER ) /* 'A'-'Z' */
#define isxdigit(c)(__is[c+1] & __XDIGIT) /* '0'-'9', 'a'-'f', 'A'-'F' */
#define isascii(c) (!((c)&(~0x7f)))
#define toascii(c) ((c)&0x7f)

View File

@ -71,5 +71,12 @@ char * gets (char * str);
typedef int (*virtual_getc)(void *sp, const void *obj);
typedef void (*virtual_ungetc)(void *sp, int c, const void *obj);
int format_scan(const void *src, const char *fmt, va_list argp, virtual_getc vgetc, virtual_ungetc vungetc);
int vscanf ( const char * format, va_list arg );
int scanf ( const char * format, ...);
int vsscanf ( const char * s, const char * format, va_list arg );
int sscanf ( const char * s, const char * format, ...);
int vfscanf ( FILE * stream, const char * format, va_list arg );
#endif

View File

@ -18,6 +18,8 @@ todo:
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
typedef int (*virtual_getc)(void *sp, const void *obj);
typedef void (*virtual_ungetc)(void *sp, int c, const void *obj);
enum flags_t
{
@ -170,7 +172,9 @@ int try_parse_int(long long *digit, int ch, const void *src, void *save, vir
{
base = 8;
ch = vgetc(save, src);
if (ch == EOF) return EOF;
if (ch == EOF || isspace(ch))
have_digits++;
else
if (ch == 'x' || ch == 'X')
{
base = 16;
@ -201,6 +205,8 @@ int try_parse_int(long long *digit, int ch, const void *src, void *save, vir
ch = vgetc(save, src);
if (ch == EOF || isspace(ch)) break; // ok, just finish num
}
else if (ch == EOF || isspace(ch))
break;
else
{
vungetc(save, ch, src);
@ -365,9 +371,9 @@ int format_scan(const void *src, const char *fmt, va_list argp, virtual_getc vge
arg_str = va_arg(argp, char*);
if (fmt1 == 0) length = 1;
else length = fmt1;
for (i = 0; i < length; i++)
for (i = 0; i < length;)
{
*arg_str++ = ch;
*arg_str++ = ch; i++;
ch = vgetc(&save, src);
if (ch == EOF) break;
}

View File

@ -6,8 +6,11 @@ int virtual_getc_file(void *sp, const void *obj)
// get next chat from file obj, save point is ptr to string char ptr
{
FILE *f = (FILE *)obj;
int ch = fgetc(f);
return fgetc(f);
//printf("getc '%c'[%d];", ch, ch);
return ch;
}
void virtual_ungetc_file(void *sp, int c, const void *obj)

View File

@ -0,0 +1,16 @@
#include <stdio.h>
// non standard realization - no support for virtually change char
int ungetc(int c,FILE* file)
{
dword res;
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) return EOF;
if (file->filepos>file->filesize || file->filepos==0)
{
return EOF;
}
file->filepos--;
return c;
}

View File

@ -1,5 +1,6 @@
#include "ctype.h"
char __is[128] = {
#include <ctype.h>
unsigned short __is[129] = {
0, /* EOF */
0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
0x004, 0x104, 0x104, 0x104, 0x104, 0x104, 0x004, 0x004,
0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,