reorganized files

This commit is contained in:
2009-06-04 15:50:32 +00:00
parent d50419267f
commit 551e6f7c09
55 changed files with 204 additions and 129 deletions

View File

@ -11,7 +11,10 @@ libqsecmn_la_SOURCES = \
tio.c tio_get.c tio_put.c \
fio.c pio.c sio.c \
time.c \
misc.c
misc.c \
assert.c \
main.c \
stdio.c
libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
if WIN32

View File

@ -54,7 +54,7 @@ libqsecmn_la_DEPENDENCIES =
am_libqsecmn_la_OBJECTS = mem.lo chr.lo chr_cnv.lo rex.lo str_bas.lo \
str_cnv.lo str_dyn.lo str_utl.lo lda.lo map.lo sll.lo dll.lo \
opt.lo tio.lo tio_get.lo tio_put.lo fio.lo pio.lo sio.lo \
time.lo misc.lo
time.lo misc.lo assert.lo main.lo stdio.lo
libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS)
libqsecmn_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
@ -219,7 +219,10 @@ libqsecmn_la_SOURCES = \
tio.c tio_get.c tio_put.c \
fio.c pio.c sio.c \
time.c \
misc.c
misc.c \
assert.c \
main.c \
stdio.c
libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
@WIN32_TRUE@libqsecmn_la_LIBADD = -lpsapi
@ -292,11 +295,13 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/assert.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr_cnv.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dll.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fio.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lda.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/map.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mem.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/misc.Plo@am__quote@
@ -305,6 +310,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sll.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stdio.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_bas.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_cnv.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_dyn.Plo@am__quote@

55
qse/lib/cmn/assert.c Normal file
View File

@ -0,0 +1,55 @@
/*
* $Id: assert.c 223 2008-06-26 06:44:41Z baconevi $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <qse/types.h>
#include <qse/macros.h>
#ifndef NDEBUG
#include <qse/cmn/sio.h>
#include <stdlib.h>
void qse_assert_failed (
const qse_char_t* expr, const qse_char_t* desc,
const qse_char_t* file, qse_size_t line)
{
qse_sio_puts (QSE_SIO_ERR, QSE_T("=[ASSERTION FAILURE]============================================================"));
qse_sio_puts (QSE_SIO_ERR, QSE_T("FILE "));
qse_sio_puts (QSE_SIO_ERR, file);
qse_sio_puts (QSE_SIO_ERR, QSE_T("LINE "));
/*qse_sio_puts the number */
qse_sio_puts (QSE_SIO_ERR, QSE_T(": "));
qse_sio_puts (QSE_SIO_ERR, expr);
qse_sio_puts (QSE_SIO_ERR, QSE_T("\n"));
if (desc != QSE_NULL)
{
qse_sio_puts (QSE_SIO_ERR, QSE_T("DESCRIPTION: "));
qse_sio_puts (QSE_SIO_ERR, desc);
qse_sio_puts (QSE_SIO_ERR, QSE_T("\n"));
}
qse_sio_puts (QSE_SIO_ERR, QSE_T("================================================================================"));
abort ();
}
#endif

105
qse/lib/cmn/main.c Normal file
View File

@ -0,0 +1,105 @@
/*
* $Id: main.c 463 2008-12-09 06:52:03Z baconevi $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <qse/cmn/main.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#if defined(_WIN32) && !defined(__MINGW32__)
int qse_runmain (int argc, qse_achar_t* argv[], int(*mf) (int,qse_char_t*[]))
{
return mf (argc, argv);
}
#elif defined(QSE_CHAR_IS_WCHAR)
int qse_runmain (int argc, qse_achar_t* argv[], int(*mf) (int,qse_char_t*[]))
{
int i, ret;
qse_char_t** v;
setlocale (LC_ALL, "");
v = (qse_char_t**) malloc (argc * QSE_SIZEOF(qse_char_t*));
if (v == NULL) return -1;
for (i = 0; i < argc; i++) v[i] = NULL;
for (i = 0; i < argc; i++)
{
qse_size_t n, len, rem;
char* p = argv[i];
len = 0; rem = strlen (p);
while (*p != '\0')
{
int x = mblen (p, rem);
if (x == -1)
{
ret = -1;
goto exit_main;
}
if (x == 0) break;
p += x; rem -= x; len++;
}
#if (defined(vms) || defined(__vms)) && (QSE_SIZEOF_VOID_P >= 8)
v[i] = (qse_char_t*) _malloc32 ((len+1)*QSE_SIZEOF(qse_char_t));
#else
v[i] = (qse_char_t*) malloc ((len+1)*QSE_SIZEOF(qse_char_t));
#endif
if (v[i] == NULL)
{
ret = -1;
goto exit_main;
}
n = mbstowcs (v[i], argv[i], len);
if (n == (size_t)-1)
{
/* error */
return -1;
}
if (n == len) v[i][len] = QSE_T('\0');
}
/* TODO: envp... */
//ret = mf (argc, v, NULL);
ret = mf (argc, v);
exit_main:
for (i = 0; i < argc; i++)
{
if (v[i] != NULL) free (v[i]);
}
free (v);
return ret;
}
#else
int qse_runmain (int argc, qse_achar_t* argv[], int(*mf) (int,qse_char_t*[]))
{
return mf (argc, argv);
}
#endif

478
qse/lib/cmn/stdio.c Normal file
View File

@ -0,0 +1,478 @@
/*
* $Id: stdio.c 463 2008-12-09 06:52:03Z baconevi $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <qse/cmn/stdio.h>
#include <qse/cmn/chr.h>
#include <wchar.h>
#include <stdlib.h>
#include <limits.h>
#ifndef PATH_MAX
#define PATH_MAX 2048
#endif
#if defined(_WIN32)
int qse_vsprintf (qse_char_t* buf, size_t size, const qse_char_t* fmt, va_list ap)
{
int n;
#ifdef QSE_CHAR_IS_MCHAR
n = _vsnprintf (buf, size, fmt, ap);
#else
n = _vsnwprintf (buf, size, fmt, ap);
#endif
if (n < 0 || (size_t)n >= size)
{
if (size > 0) buf[size-1] = QSE_T('\0');
n = -1;
}
return n;
}
int qse_sprintf (qse_char_t* buf, size_t size, const qse_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = qse_vsprintf (buf, size, fmt, ap);
va_end (ap);
return n;
}
#else
static qse_char_t* __adjust_format (const qse_char_t* format);
int qse_vfprintf (QSE_FILE *stream, const qse_char_t* fmt, va_list ap)
{
int n;
qse_char_t* nf = __adjust_format (fmt);
if (nf == NULL) return -1;
#ifdef QSE_CHAR_IS_MCHAR
n = vfprintf (stream, nf, ap);
#else
n = vfwprintf (stream, nf, ap);
#endif
free (nf);
return n;
}
int qse_vprintf (const qse_char_t* fmt, va_list ap)
{
return qse_vfprintf (stdout, fmt, ap);
}
int qse_fprintf (QSE_FILE* file, const qse_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = qse_vfprintf (file, fmt, ap);
va_end (ap);
return n;
}
int qse_printf (const qse_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = qse_vprintf (fmt, ap);
va_end (ap);
return n;
}
int qse_vsprintf (qse_char_t* buf, size_t size, const qse_char_t* fmt, va_list ap)
{
int n;
qse_char_t* nf = __adjust_format (fmt);
if (nf == NULL) return -1;
#if defined(QSE_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] = QSE_T('\0');
n = -1;
}
free (nf);
return n;
}
int qse_sprintf (qse_char_t* buf, size_t size, const qse_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = qse_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) \
{ \
qse_char_t* tmp; \
tmp = (qse_char_t*)realloc ( \
buf.ptr, sizeof(qse_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 qse_char_t* __adjust_format (const qse_char_t* format)
{
const qse_char_t* fp = format;
int modifier;
qse_char_t ch;
struct
{
qse_char_t* ptr;
qse_size_t len;
qse_size_t cap;
} buf;
buf.len = 0;
buf.cap = 256;
#if (defined(vms) || defined(__vms)) && (QSE_SIZEOF_VOID_P >= 8)
buf.ptr = (qse_char_t*) _malloc32 (sizeof(qse_char_t)*(buf.cap+1));
#else
buf.ptr = (qse_char_t*) malloc (sizeof(qse_char_t)*(buf.cap+1));
#endif
if (buf.ptr == NULL) return NULL;
while (*fp != QSE_T('\0'))
{
while (*fp != QSE_T('\0') && *fp != QSE_T('%'))
{
ADDC (buf, *fp++);
}
if (*fp == QSE_T('\0')) break;
ch = *fp++;
ADDC (buf, ch); /* add % */
ch = *fp++;
/* flags */
while (1)
{
if (ch == QSE_T(' ') || ch == QSE_T('+') ||
ch == QSE_T('-') || ch == QSE_T('#'))
{
ADDC (buf, ch);
ch = *fp++;
}
else
{
if (ch == QSE_T('0'))
{
ADDC (buf, ch);
ch = *fp++;
}
break;
}
}
/* check the width */
if (ch == QSE_T('*'))
{
ADDC (buf, ch);
ch = *fp++;
}
else
{
while (QSE_ISDIGIT(ch))
{
ADDC (buf, ch);
ch = *fp++;
}
}
/* precision */
if (ch == QSE_T('.'))
{
ADDC (buf, ch);
ch = *fp++;
if (ch == QSE_T('*'))
{
ADDC (buf, ch);
ch = *fp++;
}
else
{
while (QSE_ISDIGIT(ch))
{
ADDC (buf, ch);
ch = *fp++;
}
}
}
/* modifier */
for (modifier = 0;;)
{
if (ch == QSE_T('h')) modifier = MOD_SHORT;
else if (ch == QSE_T('l'))
{
modifier = (modifier == MOD_LONG)? MOD_LONGLONG: MOD_LONG;
}
else break;
ch = *fp++;
}
/* type */
if (ch == QSE_T('%')) ADDC (buf, ch);
else if (ch == QSE_T('c') || ch == QSE_T('s'))
{
#if !defined(QSE_CHAR_IS_MCHAR) && !defined(_WIN32)
ADDC (buf, 'l');
#endif
ADDC (buf, ch);
}
else if (ch == QSE_T('C') || ch == QSE_T('S'))
{
#if defined(_WIN32)
ADDC (buf, ch);
#else
#ifdef QSE_CHAR_IS_MCHAR
ADDC (buf, 'l');
#endif
ADDC (buf, QSE_TOLOWER(ch));
#endif
}
else if (ch == QSE_T('d') || ch == QSE_T('i') ||
ch == QSE_T('o') || ch == QSE_T('u') ||
ch == QSE_T('x') || ch == QSE_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 == QSE_T('\0')) break;
else ADDC (buf, ch);
}
buf.ptr[buf.len] = QSE_T('\0');
return buf.ptr;
}
#endif
int qse_dprintf (const qse_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = qse_vfprintf (stderr, fmt, ap);
va_end (ap);
return n;
}
QSE_FILE* qse_fopen (const qse_char_t* path, const qse_char_t* mode)
{
#if defined(QSE_CHAR_IS_MCHAR)
return fopen (path, mode);
#elif defined(_WIN32)
return _wfopen (path, mode);
#else
char path_mb[PATH_MAX + 1];
char mode_mb[32];
size_t n;
n = wcstombs (path_mb, path, QSE_COUNTOF(path_mb));
if (n == (size_t)-1) return NULL;
if (n == QSE_COUNTOF(path_mb)) path_mb[QSE_COUNTOF(path_mb)-1] = '\0';
n = wcstombs (mode_mb, mode, QSE_COUNTOF(mode_mb));
if (n == (size_t)-1) return NULL;
if (n == QSE_COUNTOF(mode_mb)) path_mb[QSE_COUNTOF(mode_mb)-1] = '\0';
return fopen (path_mb, mode_mb);
#endif
}
QSE_FILE* qse_popen (const qse_char_t* cmd, const qse_char_t* mode)
{
#if defined(QSE_CHAR_IS_MCHAR)
return popen (cmd, mode);
#elif defined(_WIN32)
return _wpopen (cmd, mode);
#else
char cmd_mb[PATH_MAX + 1];
char mode_mb[32];
size_t n;
n = wcstombs (cmd_mb, cmd, QSE_COUNTOF(cmd_mb));
if (n == (size_t)-1) return NULL;
if (n == QSE_COUNTOF(cmd_mb)) cmd_mb[QSE_COUNTOF(cmd_mb)-1] = '\0';
n = wcstombs (mode_mb, mode, QSE_COUNTOF(mode_mb));
if (n == (size_t)-1) return NULL;
if (n == QSE_COUNTOF(mode_mb)) cmd_mb[QSE_COUNTOF(mode_mb)-1] = '\0';
return popen (cmd_mb, mode_mb);
#endif
}
static int isnl (const qse_char_t* ptr, qse_size_t len, void* delim)
{
return (ptr[len-1] == *(qse_char_t*)delim)? 1: 0;
}
qse_ssize_t qse_getline (qse_char_t **buf, qse_size_t *n, QSE_FILE *fp)
{
qse_char_t nl = QSE_T('\n');
return qse_getdelim (buf, n, isnl, &nl, fp);
}
qse_ssize_t qse_getdelim (
qse_char_t **buf, qse_size_t *n,
qse_getdelim_t fn, void* fnarg, QSE_FILE *fp)
{
qse_char_t* b;
qse_size_t capa;
qse_size_t len = 0;
int x;
QSE_ASSERT (buf != QSE_NULL);
QSE_ASSERT (n != QSE_NULL);
b = *buf;
capa = *n;
if (b == QSE_NULL)
{
capa = 256;
#if (defined(vms) || defined(__vms)) && (QSE_SIZEOF_VOID_P >= 8)
b = (qse_char_t*) _malloc32 (sizeof(qse_char_t)*(capa+1));
#else
b = (qse_char_t*) malloc (sizeof(qse_char_t)*(capa+1));
#endif
if (b == QSE_NULL) return -2;
}
if (qse_feof(fp))
{
len = (qse_size_t)-1;
goto exit_task;
}
while (1)
{
qse_cint_t c = qse_fgetc(fp);
if (c == QSE_CHAR_EOF)
{
if (qse_ferror(fp))
{
len = (qse_size_t)-2;
goto exit_task;
}
if (len == 0)
{
len = (qse_size_t)-1;
goto exit_task;
}
break;
}
if (len+1 >= capa)
{
qse_size_t ncapa = capa + 256;
qse_char_t* nb;
nb = realloc (b, ncapa*sizeof(qse_char_t));
if (nb == QSE_NULL)
{
len = (qse_size_t)-2;
goto exit_task;
}
b = nb;
capa = ncapa;
}
b[len++] = c;
x = fn (b, len, fnarg);
if (x < 0)
{
len = (qse_size_t)-3;
goto exit_task;
}
if (x > 0) break;
}
b[len] = QSE_T('\0');
exit_task:
*buf = b;
*n = capa;
return (qse_ssize_t)len;
}