From 37c1ee648727da783987a0526124a8c7f8ec16ba Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 20 Feb 2007 14:04:22 +0000 Subject: [PATCH] *** empty log message *** --- ase/utl/ctype.h | 47 ++++++ ase/utl/main.c | 20 +-- ase/utl/main.h | 26 +++ ase/utl/makefile.msw.bcc | 32 ++++ ase/utl/stdio.c | 350 +++++++++++++++++++++++++++++++++++++++ ase/utl/stdio.h | 53 ++++++ 6 files changed, 512 insertions(+), 16 deletions(-) create mode 100644 ase/utl/ctype.h create mode 100644 ase/utl/main.h create mode 100644 ase/utl/makefile.msw.bcc create mode 100644 ase/utl/stdio.c create mode 100644 ase/utl/stdio.h diff --git a/ase/utl/ctype.h b/ase/utl/ctype.h new file mode 100644 index 00000000..9001859a --- /dev/null +++ b/ase/utl/ctype.h @@ -0,0 +1,47 @@ +/* + * $id$ + */ + +#ifndef _ASE_CTYPE_H_ +#define _ASE_CTYPE_H_ + +#include +#include + +#if defined(ASE_CHAR_IS_MCHAR) + #include + + #define ase_isupper(x) isupper(x) + #define ase_islower(x) islower(x) + #define ase_isalpha(x) isalpha(x) + #define ase_isdigit(x) isdigit(x) + #define ase_isxdigit(x) isxdigit(x) + #define ase_isalnum(x) isalnum(x) + #define ase_isspace(x) isspace(x) + #define ase_isprint(x) isprint(x) + #define ase_isgraph(x) isgraph(x) + #define ase_iscntrl(x) iscntrl(x) + #define ase_ispunct(x) ispunct(x) + + #define ase_toupper(x) tolower(x) + #define ase_tolower(x) tolower(x) +#else + #include + + #define ase_isupper(x) iswupper(x) + #define ase_islower(x) iswlower(x) + #define ase_isalpha(x) iswalpha(x) + #define ase_isdigit(x) iswdigit(x) + #define ase_isxdigit(x) iswxdigit(x) + #define ase_isalnum(x) iswalnum(x) + #define ase_isspace(x) iswspace(x) + #define ase_isprint(x) iswprint(x) + #define ase_isgraph(x) iswgraph(x) + #define ase_iscntrl(x) iswcntrl(x) + #define ase_ispunct(x) iswpunct(x) + + #define ase_toupper(x) towlower(x) + #define ase_tolower(x) towlower(x) +#endif + +#endif diff --git a/ase/utl/main.c b/ase/utl/main.c index 88ef7e98..ba2dd2f1 100644 --- a/ase/utl/main.c +++ b/ase/utl/main.c @@ -1,29 +1,17 @@ /* - * $Id: main.c,v 1.9 2007-02-19 06:13:03 bacon Exp $ + * $Id: main.c,v 1.10 2007-02-20 14:04:21 bacon Exp $ * * {License} */ -#include -#include +#include #include #include #include #include #include -#if defined(_WIN32) - -#include -#define ase_main _tmain - -#elif defined(ASE_CHAR_IS_MCHAR) - -#ifdef __cplusplus -extern "C" { int ase_main (...); } -#else -extern int ase_main (); -#endif +#if !defined(_WIN32) && defined(ASE_CHAR_IS_MCHAR) int main (int argc, char* argv[], char** envp) { @@ -31,7 +19,7 @@ int main (int argc, char* argv[], char** envp) return ase_main (argc, argv, envp); } -#else /* ASE_CHAR_IS_WCHAR */ +#elif !defined(_WIN32) && defined(ASE_CHAR_IS_WCHAR) #ifdef __cplusplus extern "C" { int ase_main (...); } diff --git a/ase/utl/main.h b/ase/utl/main.h new file mode 100644 index 00000000..9cff1fc1 --- /dev/null +++ b/ase/utl/main.h @@ -0,0 +1,26 @@ +/* + * $Id: main.h,v 1.1 2007-02-20 14:04:21 bacon Exp $ + */ + +#ifndef _ASE_MAIN_H_ +#define _ASE_MAIN_H_ + +#include +#include + +#if defined(_WIN32) + + #include + #define ase_main _tmain + +#else + + #ifdef __cplusplus + extern "C" { int ase_main (...); } + #else + extern int ase_main (); + #endif + +#endif + +#endif diff --git a/ase/utl/makefile.msw.bcc b/ase/utl/makefile.msw.bcc new file mode 100644 index 00000000..8d6a5c5b --- /dev/null +++ b/ase/utl/makefile.msw.bcc @@ -0,0 +1,32 @@ +OUT = aseetc + +C_SRCS = main.c stdio.c +C_OBJS = $(C_SRCS:.c=.obj) + +CC = bcc32 +LD = ilink32 +AR = tlib + +CFLAGS_COMMON = -O2 -WM -WU -RT- -w -q -I../.. +CFLAGS = $(CFLAGS_COMMON) -DNDEBUG + +LDFLAGS = -Tpd -ap -Gn -c -q +STARTUP = c0d32w.obj +LIBS = import32.lib cw32mt.lib +JNI_LIBS = $(LIBS) $(OUT).lib + +all: lib + +lib: $(C_OBJS) + $(AR) $(OUT).lib @&&! ++-$(**: = &^ ++-) +! + +clean: + -del $(OBJS) $(OUT).lib *.obj + +.SUFFIXES: .c .obj +.c.obj: + $(CC) $(CFLAGS) -c $< + diff --git a/ase/utl/stdio.c b/ase/utl/stdio.c new file mode 100644 index 00000000..5c6439fd --- /dev/null +++ b/ase/utl/stdio.c @@ -0,0 +1,350 @@ +/* + * $Id: stdio.c,v 1.1 2007-02-20 14:04:22 bacon Exp $ + * + * {License} + */ + +#include +#include + +#include +#include + +#if defined(_WIN32) + +int ase_vsprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, va_list ap) +{ + int n; + + n = _vsntprintf (buf, size, fmt, ap); + if (n < 0 || (size_t)n >= size) + { + if (size > 0) buf[size-1] = ASE_T('\0'); + n = -1; + } + + return n; +} + +int ase_sprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, ...) +{ + int n; + va_list ap; + + va_start (ap, fmt); + n = ase_vsprintf (buf, size, fmt, ap); + va_end (ap); + return n; +} + +#else + +static ase_char_t* __adjust_format (const ase_char_t* format); + +int ase_vfprintf (FILE *stream, const ase_char_t* fmt, va_list ap) +{ + int n; + ase_char_t* nf = __adjust_format (fmt); + if (nf == NULL) return -1; + +#ifdef ASE_CHAR_IS_MCHAR + n = vfprintf (stream, nf, ap); +#else + n = vfwprintf (stream, nf, ap); +#endif + free (nf); + return n; +} + +int ase_vprintf (const ase_char_t* fmt, va_list ap) +{ + return ase_vfprintf (stdout, fmt, ap); +} + +int ase_fprintf (FILE* file, const ase_char_t* fmt, ...) +{ + int n; + va_list ap; + + va_start (ap, fmt); + n = ase_vfprintf (file, fmt, ap); + va_end (ap); + return n; +} + +int ase_printf (const ase_char_t* fmt, ...) +{ + int n; + va_list ap; + + va_start (ap, fmt); + n = ase_vprintf (fmt, ap); + va_end (ap); + return n; +} + +int ase_vsprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, va_list ap) +{ + int n; + ase_char_t* nf = __adjust_format (fmt); + if (nf == NULL) return -1; + +#if defined(ASE_CHAR_IS_MCHAR) + n = vsnprintf (buf, size, nf, ap); +#elif defined(_WIN32) + n = _vsnwprintf (buf, size, nf, ap); +#else + n = vswprintf (buf, size, nf, ap); +#endif + if (n < 0 || (size_t)n >= size) + { + if (size > 0) buf[size-1] = ASE_T('\0'); + n = -1; + } + + free (nf); + return n; +} + +int ase_sprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, ...) +{ + int n; + va_list ap; + + va_start (ap, fmt); + n = ase_vsprintf (buf, size, fmt, ap); + va_end (ap); + return n; +} + +#define MOD_SHORT 1 +#define MOD_LONG 2 +#define MOD_LONGLONG 3 + +#define ADDC(buf,c) \ + do { \ + if (buf.len >= buf.cap) \ + { \ + ase_char_t* tmp; \ + tmp = (ase_char_t*)realloc ( \ + buf.ptr, sizeof(ase_char_t)*(buf.cap+256+1)); \ + if (tmp == NULL) \ + { \ + free (buf.ptr); \ + return NULL; \ + } \ + buf.ptr = tmp; \ + buf.cap = buf.cap + 256; \ + } \ + buf.ptr[buf.len++] = c; \ + } while (0) + +static ase_char_t* __adjust_format (const ase_char_t* format) +{ + const ase_char_t* fp = format; + int modifier; + ase_char_t ch; + + struct + { + ase_char_t* ptr; + ase_size_t len; + ase_size_t cap; + } buf; + + buf.len = 0; + buf.cap = 256; +#if (defined(vms) || defined(__vms)) && (ASE_SIZEOF_VOID_P >= 8) + buf.ptr = (ase_char_t*) _malloc32 (sizeof(ase_char_t)*(buf.cap+1)); +#else + buf.ptr = (ase_char_t*) malloc (sizeof(ase_char_t)*(buf.cap+1)); +#endif + if (buf.ptr == NULL) return NULL; + + while (*fp != ASE_T('\0')) + { + while (*fp != ASE_T('\0') && *fp != ASE_T('%')) + { + ADDC (buf, *fp++); + } + + if (*fp == ASE_T('\0')) break; + + ch = *fp++; + ADDC (buf, ch); /* add % */ + + ch = *fp++; + + /* flags */ + while (1) + { + if (ch == ASE_T(' ') || ch == ASE_T('+') || + ch == ASE_T('-') || ch == ASE_T('#')) + { + ADDC (buf, ch); + ch = *fp++; + } + else + { + if (ch == ASE_T('0')) + { + ADDC (buf, ch); + ch = *fp++; + } + + break; + } + } + + /* check the width */ + if (ch == ASE_T('*')) + { + ADDC (buf, ch); + ch = *fp++; + } + else + { + while (ase_isdigit(ch)) + { + ADDC (buf, ch); + ch = *fp++; + } + } + + /* precision */ + if (ch == ASE_T('.')) + { + ADDC (buf, ch); + ch = *fp++; + + if (ch == ASE_T('*')) + { + ADDC (buf, ch); + ch = *fp++; + } + else + { + while (ase_isdigit(ch)) + { + ADDC (buf, ch); + ch = *fp++; + } + } + } + + /* modifier */ + for (modifier = 0;;) + { + if (ch == ASE_T('h')) modifier = MOD_SHORT; + else if (ch == ASE_T('l')) + { + modifier = (modifier == MOD_LONG)? MOD_LONGLONG: MOD_LONG; + } + else break; + ch = *fp++; + } + + + /* type */ + if (ch == ASE_T('%')) ADDC (buf, ch); + else if (ch == ASE_T('c') || ch == ASE_T('s')) + { +#if !defined(ASE_CHAR_IS_MCHAR) && !defined(_WIN32) + ADDC (buf, 'l'); +#endif + ADDC (buf, ch); + } + else if (ch == ASE_T('C') || ch == ASE_T('S')) + { +#ifdef _WIN32 + ADDC (buf, ch); +#else + #ifdef ASE_CHAR_IS_MCHAR + ADDC (buf, 'l'); + #endif + ADDC (buf, ase_tolower(ch)); +#endif + } + else if (ch == ASE_T('d') || ch == ASE_T('i') || + ch == ASE_T('o') || ch == ASE_T('u') || + ch == ASE_T('x') || ch == ASE_T('X')) + { + if (modifier == MOD_SHORT) + { + ADDC (buf, 'h'); + } + else if (modifier == MOD_LONG) + { + ADDC (buf, 'l'); + } + else if (modifier == MOD_LONGLONG) + { +#if defined(_WIN32) && !defined(__LCC__) + ADDC (buf, 'I'); + ADDC (buf, '6'); + ADDC (buf, '4'); +#else + ADDC (buf, 'l'); + ADDC (buf, 'l'); +#endif + } + ADDC (buf, ch); + } + else if (ch == ASE_T('\0')) break; + else ADDC (buf, ch); + } + + buf.ptr[buf.len] = ASE_T('\0'); + + return buf.ptr; +} + +#endif + +FILE* ase_fopen (const ase_char_t* path, const ase_char_t* mode) +{ +#if defined(_WIN32) + return _tfopen (path, mode); +#elif defined(ASE_CHAR_IS_MCHAR) + return fopen (path, mode); +#else + + char path_mb[PATH_MAX + 1]; + char mode_mb[32]; + size_t n; + + n = wcstombs (path_mb, path, ASE_COUNTOF(path_mb)); + if (n == (size_t)-1) return NULL; + if (n == ASE_COUNTOF(path_mb)) path_mb[ASE_COUNTOF(path_mb)-1] = '\0'; + + n = wcstombs (mode_mb, mode, ASE_COUNTOF(mode_mb)); + if (n == (size_t)-1) return NULL; + if (n == ASE_COUNTOF(mode_mb)) path_mb[ASE_COUNTOF(mode_mb)-1] = '\0'; + + return fopen (path_mb, mode_mb); +#endif +} + +FILE* ase_popen (const ase_char_t* cmd, const ase_char_t* mode) +{ +#if defined(_WIN32) + return _tpopen (cmd, mode); +#elif defined(ASE_CHAR_IS_MCHAR) + return popen (cmd, mode); +#else + char cmd_mb[PATH_MAX + 1]; + char mode_mb[32]; + size_t n; + + n = wcstombs (cmd_mb, cmd, ASE_COUNTOF(cmd_mb)); + if (n == (size_t)-1) return NULL; + if (n == ASE_COUNTOF(cmd_mb)) cmd_mb[ASE_COUNTOF(cmd_mb)-1] = '\0'; + + n = wcstombs (mode_mb, mode, ASE_COUNTOF(mode_mb)); + if (n == (size_t)-1) return NULL; + if (n == ASE_COUNTOF(mode_mb)) cmd_mb[ASE_COUNTOF(mode_mb)-1] = '\0'; + + return popen (cmd_mb, mode_mb); +#endif +} + diff --git a/ase/utl/stdio.h b/ase/utl/stdio.h new file mode 100644 index 00000000..f6a8ed69 --- /dev/null +++ b/ase/utl/stdio.h @@ -0,0 +1,53 @@ +/* + * $Id: stdio.h,v 1.1 2007-02-20 14:04:22 bacon Exp $ + */ + +#ifndef _ASE_STDIO_H_ +#define _ASE_STDIO_H_ + +#include +#include + +#include +#include + +#if defined(_WIN32) + #include + + #define ase_printf _tprintf + #define ase_vprintf _vtprintf + #define ase_fprintf _ftprintf + #define ase_vfprintf _vftprintf + + #define awk_fgets _fgetts + #define awk_fgetc _fgettc +#elif defined(ASE_CHAR_IS_MCHAR) + #define awk_fgets fgets + #define awk_fgetc fgetc +#else + #define awk_fgets fgetws + #define awk_fgetc fgetwc +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +int ase_vsprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, va_list ap); +int ase_sprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, ...); + +#if !defined(WIN32) +int ase_vfprintf (FILE *stream, const ase_char_t* fmt, va_list ap); +int ase_vprintf (const ase_char_t* fmt, va_list ap); +int ase_fprintf (FILE* file, const ase_char_t* fmt, ...); +int ase_printf (const ase_char_t* fmt, ...); +#endif + +FILE* ase_fopen (const ase_char_t* path, const ase_char_t* mode); +FILE* ase_popen (const ase_char_t* cmd, const ase_char_t* mode); + +#ifdef __cplusplus +} +#endif + +#endif