2013-02-06 14:31:32 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2013-02-06 14:31:32 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2013-02-06 14:31:32 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2013-02-06 14:31:32 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-22 03:18:42 +00:00
|
|
|
#include <qse/xli/stdxli.h>
|
2013-02-06 14:31:32 +00:00
|
|
|
#include <qse/cmn/str.h>
|
|
|
|
#include <qse/cmn/mem.h>
|
|
|
|
#include <qse/cmn/chr.h>
|
|
|
|
#include <qse/cmn/opt.h>
|
|
|
|
#include <qse/cmn/xma.h>
|
|
|
|
#include <qse/cmn/path.h>
|
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
2013-10-20 14:14:10 +00:00
|
|
|
#include <qse/cmn/fmt.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/fs.h>
|
|
|
|
#include <qse/si/glob.h>
|
2016-04-28 15:29:28 +00:00
|
|
|
#include <qse/si/sio.h>
|
2013-02-06 14:31:32 +00:00
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
# include <tchar.h>
|
|
|
|
# include <process.h>
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
# define INCL_DOSPROCESS
|
|
|
|
# define INCL_DOSEXCEPTIONS
|
|
|
|
# define INCL_ERRORS
|
|
|
|
# include <os2.h>
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
# include <dos.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2015-09-02 01:27:08 +00:00
|
|
|
#define IO_FLAG_INI_INPUT (1 << 0)
|
|
|
|
#define IO_FLAG_INI_OUTPUT (1 << 1)
|
2018-01-09 10:45:43 +00:00
|
|
|
#define IO_FLAG_JSON_INPUT (1 << 2)
|
|
|
|
#define IO_FLAG_JSON_OUTPUT (1 << 3)
|
2015-09-02 01:27:08 +00:00
|
|
|
|
|
|
|
static int g_io_flags = 0;
|
2013-02-06 14:31:32 +00:00
|
|
|
static qse_char_t* g_input_file = QSE_NULL;
|
|
|
|
static qse_char_t* g_output_file = QSE_NULL;
|
2013-02-18 13:45:50 +00:00
|
|
|
static qse_char_t* g_lookup_key = QSE_NULL;
|
2015-09-02 08:43:43 +00:00
|
|
|
static qse_char_t* g_value = QSE_NULL;
|
2013-02-06 14:31:32 +00:00
|
|
|
static qse_ulong_t g_memlimit = 0;
|
2013-02-09 13:31:31 +00:00
|
|
|
static int g_trait = 0;
|
2013-02-06 14:31:32 +00:00
|
|
|
|
|
|
|
static qse_cmgr_t* g_infile_cmgr = QSE_NULL;
|
|
|
|
static qse_cmgr_t* g_outfile_cmgr = QSE_NULL;
|
|
|
|
|
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
#include <stdlib.h>
|
|
|
|
static qse_ulong_t g_failmalloc = 0;
|
|
|
|
static qse_ulong_t debug_mmgr_count = 0;
|
|
|
|
static qse_ulong_t debug_mmgr_alloc_count = 0;
|
|
|
|
static qse_ulong_t debug_mmgr_realloc_count = 0;
|
|
|
|
static qse_ulong_t debug_mmgr_free_count = 0;
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void* debug_mmgr_alloc (qse_mmgr_t* mmgr, qse_size_t size)
|
2013-02-06 14:31:32 +00:00
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
debug_mmgr_count++;
|
|
|
|
if (debug_mmgr_count % g_failmalloc == 0) return QSE_NULL;
|
|
|
|
ptr = malloc (size);
|
|
|
|
if (ptr) debug_mmgr_alloc_count++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void* debug_mmgr_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
|
2013-02-06 14:31:32 +00:00
|
|
|
{
|
|
|
|
void* rptr;
|
|
|
|
debug_mmgr_count++;
|
|
|
|
if (debug_mmgr_count % g_failmalloc == 0) return QSE_NULL;
|
|
|
|
rptr = realloc (ptr, size);
|
|
|
|
if (rptr)
|
|
|
|
{
|
|
|
|
if (ptr) debug_mmgr_realloc_count++;
|
|
|
|
else debug_mmgr_alloc_count++;
|
|
|
|
}
|
|
|
|
return rptr;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void debug_mmgr_free (qse_mmgr_t* mmgr, void* ptr)
|
2013-02-06 14:31:32 +00:00
|
|
|
{
|
|
|
|
debug_mmgr_free_count++;
|
|
|
|
free (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static qse_mmgr_t debug_mmgr =
|
|
|
|
{
|
|
|
|
debug_mmgr_alloc,
|
|
|
|
debug_mmgr_realloc,
|
|
|
|
debug_mmgr_free,
|
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void* xma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
|
|
|
|
{
|
|
|
|
return qse_xma_alloc (mmgr->ctx, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* xma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
|
|
|
|
{
|
|
|
|
return qse_xma_realloc (mmgr->ctx, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xma_free (qse_mmgr_t* mmgr, void* ptr)
|
|
|
|
{
|
|
|
|
qse_xma_free (mmgr->ctx, ptr);
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
static qse_mmgr_t xma_mmgr =
|
|
|
|
{
|
2014-07-09 15:01:16 +00:00
|
|
|
xma_alloc,
|
|
|
|
xma_realloc,
|
|
|
|
xma_free,
|
2013-02-06 14:31:32 +00:00
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void print_version (void)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_printf (QSE_T("QSEXLI version %hs\n"), QSE_PACKAGE_VERSION);
|
2013-02-06 14:31:32 +00:00
|
|
|
}
|
|
|
|
|
2013-10-20 15:22:44 +00:00
|
|
|
static void print_usage (qse_sio_t* out, int argc, qse_char_t* argv[])
|
2013-02-06 14:31:32 +00:00
|
|
|
{
|
|
|
|
const qse_char_t* b = qse_basename (argv[0]);
|
|
|
|
|
2015-09-02 08:43:43 +00:00
|
|
|
qse_fprintf (out, QSE_T("USAGE: %s [options] -i input-file [key [value]]\n"), b);
|
2013-11-03 16:48:20 +00:00
|
|
|
|
|
|
|
qse_fprintf (out, QSE_T("options as follows:\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -h/--help show this message\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" --version show version\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -i file specify an input file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -o file specify an output file\n"));
|
2015-09-02 01:27:08 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -I file specify an ini input file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -O file specify an ini output file\n"));
|
2018-01-09 10:45:43 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -j file specify a json input file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -J file specify a json output file\n"));
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -u disallow duplicate keys\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -a allow a key alias\n"));
|
2018-01-10 10:01:41 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -b allow true and false as boolean values\n"));
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -f keep file inclusion info\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -t keep comment text\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -s allow multi-segmented strings\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -d allow a leading digit in identifiers\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -n disallow nil\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -l disallow lists\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -K allow key tags\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -S allow string tags\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -v perform validation\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n"));
|
2013-02-06 14:31:32 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -X number fail the number'th memory allocation\n"));
|
2013-02-06 14:31:32 +00:00
|
|
|
#endif
|
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" --infile-encoding string specify input file encoding name\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n"));
|
2013-02-06 14:31:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_args (int argc, qse_char_t* argv[])
|
|
|
|
{
|
|
|
|
static qse_opt_lng_t lng[] =
|
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
|
|
|
{ QSE_T(":infile-encoding"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":outfile-encoding"), QSE_T('\0') },
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{ QSE_T("version"), QSE_T('\0') },
|
|
|
|
{ QSE_T("help"), QSE_T('h') },
|
|
|
|
{ QSE_NULL, QSE_T('\0') }
|
|
|
|
};
|
|
|
|
static qse_opt_t opt =
|
|
|
|
{
|
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
2018-01-10 10:01:41 +00:00
|
|
|
QSE_T("hi:o:I:O:j:J:uabftsdnlKSvm:X:"),
|
2013-02-06 14:31:32 +00:00
|
|
|
#else
|
2018-01-10 10:01:41 +00:00
|
|
|
QSE_T("hi:o:I:O:j:J:uabftsdnlKSvm:"),
|
2013-02-06 14:31:32 +00:00
|
|
|
#endif
|
|
|
|
lng
|
|
|
|
};
|
|
|
|
qse_cint_t c;
|
|
|
|
|
2018-01-09 10:45:43 +00:00
|
|
|
while ((c = qse_getopt(argc, argv, &opt)) != QSE_CHAR_EOF)
|
2013-02-06 14:31:32 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
default:
|
2013-11-03 16:48:20 +00:00
|
|
|
print_usage (QSE_STDERR, argc, argv);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
|
|
|
|
case QSE_T('?'):
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-02-06 14:31:32 +00:00
|
|
|
QSE_T("ERROR: bad option - %c\n"),
|
|
|
|
opt.opt
|
|
|
|
);
|
2013-11-03 16:48:20 +00:00
|
|
|
print_usage (QSE_STDERR, argc, argv);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
|
|
|
|
case QSE_T(':'):
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-02-06 14:31:32 +00:00
|
|
|
QSE_T("ERROR: bad parameter for %c\n"),
|
|
|
|
opt.opt
|
|
|
|
);
|
2013-11-03 16:48:20 +00:00
|
|
|
print_usage (QSE_STDERR, argc, argv);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
|
|
|
|
case QSE_T('h'):
|
2013-11-03 16:48:20 +00:00
|
|
|
print_usage (QSE_STDOUT, argc, argv);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto done;
|
|
|
|
|
2013-06-23 14:44:29 +00:00
|
|
|
case QSE_T('i'):
|
2013-02-06 14:31:32 +00:00
|
|
|
g_input_file = opt.arg;
|
2018-01-09 10:45:43 +00:00
|
|
|
g_io_flags &= ~(IO_FLAG_INI_INPUT | IO_FLAG_JSON_OUTPUT);
|
2015-09-02 01:27:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('I'):
|
|
|
|
g_input_file = opt.arg;
|
|
|
|
g_io_flags |= IO_FLAG_INI_INPUT;
|
2013-02-06 14:31:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-01-09 10:45:43 +00:00
|
|
|
case QSE_T('j'):
|
|
|
|
g_input_file = opt.arg;
|
|
|
|
g_io_flags |= IO_FLAG_JSON_INPUT;
|
|
|
|
break;
|
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
case QSE_T('o'):
|
|
|
|
g_output_file = opt.arg;
|
2018-01-09 10:45:43 +00:00
|
|
|
g_io_flags &= ~(IO_FLAG_INI_OUTPUT | IO_FLAG_JSON_OUTPUT);
|
2015-09-02 01:27:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('O'):
|
|
|
|
g_output_file = opt.arg;
|
|
|
|
g_io_flags |= IO_FLAG_INI_OUTPUT;
|
2013-02-06 14:31:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-01-09 10:45:43 +00:00
|
|
|
case QSE_T('J'):
|
|
|
|
g_output_file = opt.arg;
|
|
|
|
g_io_flags |= IO_FLAG_JSON_OUTPUT;
|
|
|
|
break;
|
|
|
|
|
2013-07-11 16:55:08 +00:00
|
|
|
case QSE_T('u'):
|
|
|
|
g_trait |= QSE_XLI_KEYNODUP;
|
|
|
|
break;
|
|
|
|
|
2013-07-01 15:16:19 +00:00
|
|
|
case QSE_T('a'):
|
|
|
|
g_trait |= QSE_XLI_KEYALIAS;
|
2013-02-09 13:31:31 +00:00
|
|
|
break;
|
|
|
|
|
2018-01-10 10:01:41 +00:00
|
|
|
case QSE_T('b'):
|
|
|
|
g_trait |= QSE_XLI_BOOLEAN;
|
|
|
|
break;
|
|
|
|
|
2013-06-23 14:44:29 +00:00
|
|
|
case QSE_T('f'):
|
|
|
|
g_trait |= QSE_XLI_KEEPFILE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('t'):
|
|
|
|
g_trait |= QSE_XLI_KEEPTEXT;
|
|
|
|
break;
|
|
|
|
|
2013-09-16 13:33:51 +00:00
|
|
|
case QSE_T('s'):
|
|
|
|
g_trait |= QSE_XLI_MULSEGSTR;
|
|
|
|
break;
|
|
|
|
|
2013-09-17 01:28:04 +00:00
|
|
|
case QSE_T('d'):
|
|
|
|
g_trait |= QSE_XLI_LEADDIGIT;
|
|
|
|
break;
|
|
|
|
|
2013-09-23 14:06:25 +00:00
|
|
|
case QSE_T('n'):
|
|
|
|
g_trait |= QSE_XLI_NONIL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('l'):
|
|
|
|
g_trait |= QSE_XLI_NOLIST;
|
|
|
|
break;
|
|
|
|
|
2013-09-25 06:40:40 +00:00
|
|
|
case QSE_T('K'):
|
|
|
|
g_trait |= QSE_XLI_KEYTAG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('S'):
|
2013-09-24 14:02:06 +00:00
|
|
|
g_trait |= QSE_XLI_STRTAG;
|
|
|
|
break;
|
|
|
|
|
2013-07-11 16:55:08 +00:00
|
|
|
case QSE_T('v'):
|
|
|
|
g_trait |= QSE_XLI_VALIDATE;
|
|
|
|
break;
|
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
case QSE_T('m'):
|
2017-12-18 07:20:02 +00:00
|
|
|
g_memlimit = qse_strtoulong (opt.arg, 10, QSE_NULL);
|
2013-02-06 14:31:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
case QSE_T('X'):
|
2017-12-18 07:20:02 +00:00
|
|
|
g_failmalloc = qse_strtoulong (opt.arg, 10, QSE_NULL);
|
2013-02-06 14:31:32 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case QSE_T('\0'):
|
|
|
|
{
|
|
|
|
if (qse_strcmp(opt.lngopt, QSE_T("version")) == 0)
|
|
|
|
{
|
|
|
|
print_version ();
|
2013-06-29 17:01:32 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2013-02-06 14:31:32 +00:00
|
|
|
else if (qse_strcmp(opt.lngopt, QSE_T("infile-encoding")) == 0)
|
|
|
|
{
|
|
|
|
g_infile_cmgr = qse_findcmgr (opt.arg);
|
|
|
|
if (g_infile_cmgr == QSE_NULL)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (qse_strcmp(opt.lngopt, QSE_T("outfile-encoding")) == 0)
|
|
|
|
{
|
|
|
|
g_outfile_cmgr = qse_findcmgr (opt.arg);
|
|
|
|
if (g_outfile_cmgr == QSE_NULL)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_input_file)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
print_usage (QSE_STDERR, argc, argv);
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2015-09-02 08:43:43 +00:00
|
|
|
if (opt.ind < argc)
|
2013-02-18 13:45:50 +00:00
|
|
|
{
|
2015-09-02 08:43:43 +00:00
|
|
|
g_lookup_key = argv[opt.ind++];
|
|
|
|
if (opt.ind < argc)
|
|
|
|
{
|
|
|
|
g_value = argv[opt.ind++];
|
|
|
|
|
|
|
|
if (opt.ind < argc)
|
|
|
|
{
|
|
|
|
print_usage (QSE_STDERR, argc, argv);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_exec_error (qse_xli_t* xli)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
const qse_xli_loc_t* errloc = qse_xli_geterrloc(xli);
|
|
|
|
if (errloc->line > 0 || errloc->colm > 0)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-02-06 14:31:32 +00:00
|
|
|
QSE_T("ERROR: cannot execute - %s at line %lu column %lu\n"),
|
|
|
|
qse_xli_geterrmsg(xli),
|
|
|
|
(unsigned long)errloc->line,
|
|
|
|
(unsigned long)errloc->colm
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-02-06 14:31:32 +00:00
|
|
|
QSE_T("ERROR: cannot execute - %s\n"),
|
|
|
|
qse_xli_geterrmsg(xli)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xli_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
|
|
|
qse_mmgr_t* mmgr = QSE_MMGR_GETDFL();
|
|
|
|
qse_xli_t* xli = QSE_NULL;
|
|
|
|
qse_xli_iostd_t in, out;
|
2015-09-02 01:27:08 +00:00
|
|
|
int ret = -1, n;
|
2013-02-06 14:31:32 +00:00
|
|
|
|
|
|
|
ret = handle_args (argc, argv);
|
|
|
|
if (ret <= -1) return -1;
|
|
|
|
if (ret == 0) return 0;
|
|
|
|
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
if (g_failmalloc > 0)
|
|
|
|
{
|
|
|
|
debug_mmgr.ctx = QSE_NULL;
|
|
|
|
mmgr = &debug_mmgr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (g_memlimit > 0)
|
|
|
|
{
|
2020-11-03 06:33:28 +00:00
|
|
|
xma_mmgr.ctx = qse_xma_open (QSE_MMGR_GETDFL(), 0, QSE_NULL, g_memlimit);
|
2013-02-06 14:31:32 +00:00
|
|
|
if (xma_mmgr.ctx == QSE_NULL)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open memory heap\n"));
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
mmgr = &xma_mmgr;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:11:50 +00:00
|
|
|
xli = qse_xli_openstdwithmmgr (mmgr, 0, 0, QSE_NULL);
|
2013-02-06 14:31:32 +00:00
|
|
|
if (xli == QSE_NULL)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n"));
|
2013-02-06 14:31:32 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2013-02-09 13:31:31 +00:00
|
|
|
{
|
|
|
|
int trait;
|
|
|
|
qse_xli_getopt (xli, QSE_XLI_TRAIT, &trait);
|
|
|
|
g_trait |= trait;
|
|
|
|
qse_xli_setopt (xli, QSE_XLI_TRAIT, &g_trait);
|
|
|
|
}
|
2013-02-06 14:31:32 +00:00
|
|
|
|
|
|
|
in.type = QSE_XLI_IOSTD_FILE;
|
|
|
|
in.u.file.path = g_input_file;
|
|
|
|
in.u.file.cmgr = g_infile_cmgr;
|
|
|
|
|
2018-01-09 10:45:43 +00:00
|
|
|
n = (g_io_flags & IO_FLAG_JSON_INPUT)? qse_xli_readjsonstd(xli, &in):
|
|
|
|
(g_io_flags & IO_FLAG_INI_INPUT)? qse_xli_readinistd(xli, &in):
|
|
|
|
qse_xli_readstd(xli, &in);
|
2015-09-02 01:27:08 +00:00
|
|
|
if (n <= -1)
|
2013-02-06 14:31:32 +00:00
|
|
|
{
|
|
|
|
const qse_xli_loc_t* errloc;
|
|
|
|
|
2017-09-13 14:04:01 +00:00
|
|
|
errloc = qse_xli_geterrloc (xli);
|
2013-02-06 14:31:32 +00:00
|
|
|
if (errloc->line > 0 || errloc->colm > 0)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-05-15 14:20:29 +00:00
|
|
|
QSE_T("ERROR: cannot read %s - %s at line %lu column %lu%s%s\n"),
|
2013-02-09 13:31:31 +00:00
|
|
|
g_input_file,
|
2013-02-06 14:31:32 +00:00
|
|
|
qse_xli_geterrmsg(xli),
|
|
|
|
(unsigned long)errloc->line,
|
2013-05-15 14:20:29 +00:00
|
|
|
(unsigned long)errloc->colm,
|
|
|
|
(errloc->file? QSE_T(" in "): QSE_T("")),
|
|
|
|
(errloc->file? errloc->file: QSE_T(""))
|
2013-02-06 14:31:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-02-09 13:31:31 +00:00
|
|
|
QSE_T("ERROR: cannot read %s - %s\n"),
|
|
|
|
g_input_file,
|
2013-02-06 14:31:32 +00:00
|
|
|
qse_xli_geterrmsg(xli)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2015-09-02 08:43:43 +00:00
|
|
|
#if 0
|
2013-07-15 06:52:06 +00:00
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
static const qse_cstr_t strs[] =
|
2013-07-15 06:52:06 +00:00
|
|
|
{
|
|
|
|
{ QSE_T("hello"), 5 },
|
|
|
|
{ QSE_T("xli"), 3 },
|
|
|
|
{ QSE_T("world"), 5 }
|
|
|
|
};
|
|
|
|
|
2013-09-25 06:40:40 +00:00
|
|
|
if (qse_xli_insertpairwithstrs (xli, qse_xli_getroot(xli), QSE_NULL, QSE_T("test-key"), QSE_NULL, QSE_NULL, strs, QSE_COUNTOF(strs)) == QSE_NULL)
|
2013-07-15 06:52:06 +00:00
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
2013-07-15 06:52:06 +00:00
|
|
|
QSE_T("ERROR: cannot insert a string pair - %s \n"),
|
|
|
|
qse_xli_geterrmsg(xli)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 08:43:43 +00:00
|
|
|
#endif
|
2013-07-15 06:52:06 +00:00
|
|
|
|
2013-02-18 13:45:50 +00:00
|
|
|
if (g_lookup_key)
|
|
|
|
{
|
2015-09-02 08:43:43 +00:00
|
|
|
if (g_value)
|
2013-02-18 13:45:50 +00:00
|
|
|
{
|
2015-09-02 08:43:43 +00:00
|
|
|
qse_cstr_t v;
|
|
|
|
|
|
|
|
v.ptr = g_value;
|
|
|
|
v.len = qse_strlen(g_value);
|
|
|
|
if (qse_xli_setpairwithstr (xli, QSE_NULL, g_lookup_key, &v, QSE_NULL) == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_fprintf (QSE_STDERR,
|
|
|
|
QSE_T("ERROR: cannot set a string pair - %s \n"),
|
|
|
|
qse_xli_geterrmsg(xli)
|
|
|
|
);
|
2015-09-02 08:45:56 +00:00
|
|
|
goto oops;
|
2015-09-02 08:43:43 +00:00
|
|
|
}
|
2013-02-18 13:45:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-02 08:43:43 +00:00
|
|
|
qse_xli_pair_t* pair;
|
|
|
|
qse_size_t count;
|
|
|
|
|
|
|
|
count = qse_xli_countpairs (xli, QSE_NULL, g_lookup_key);
|
|
|
|
qse_printf (QSE_T("COUNT: %lu\n"), (unsigned long)count);
|
|
|
|
|
|
|
|
pair = qse_xli_findpair (xli, QSE_NULL, g_lookup_key);
|
|
|
|
if (pair == QSE_NULL)
|
2013-02-18 13:45:50 +00:00
|
|
|
{
|
2015-09-02 08:43:43 +00:00
|
|
|
qse_fprintf (QSE_STDERR,
|
|
|
|
QSE_T("ERROR: cannot find %s - %s \n"),
|
|
|
|
g_lookup_key,
|
|
|
|
qse_xli_geterrmsg(xli)
|
|
|
|
);
|
|
|
|
goto oops;
|
2013-02-18 13:45:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-02 08:43:43 +00:00
|
|
|
if (pair->val->type == QSE_XLI_STR)
|
|
|
|
{
|
|
|
|
qse_xli_str_t* str = (qse_xli_str_t*)pair->val;
|
|
|
|
qse_printf (QSE_T("[%.*s]\n"), (int)str->len, str->ptr);
|
|
|
|
}
|
|
|
|
else if (pair->val->type == QSE_XLI_NIL)
|
|
|
|
{
|
2018-01-10 10:01:41 +00:00
|
|
|
qse_printf (QSE_T("nil\n"));
|
|
|
|
}
|
|
|
|
else if (pair->val->type == QSE_XLI_TRUE)
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("true\n"));
|
|
|
|
}
|
|
|
|
else if (pair->val->type == QSE_XLI_FALSE)
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("false\n"));
|
2015-09-02 08:43:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.type = QSE_XLI_IOSTD_FILE;
|
|
|
|
out.u.file.path = QSE_T("-");
|
|
|
|
out.u.file.cmgr = g_outfile_cmgr;
|
2017-09-13 14:04:01 +00:00
|
|
|
qse_xli_writestd (xli, (qse_xli_list_t*)pair->val, &out);
|
2015-09-02 08:43:43 +00:00
|
|
|
}
|
2013-02-18 13:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 17:01:32 +00:00
|
|
|
out.type = QSE_XLI_IOSTD_FILE;
|
|
|
|
out.u.file.path = g_output_file? g_output_file: QSE_T("-");
|
|
|
|
out.u.file.cmgr = g_outfile_cmgr;
|
2015-09-02 01:27:08 +00:00
|
|
|
|
2018-01-09 10:45:43 +00:00
|
|
|
ret = (g_io_flags & IO_FLAG_JSON_OUTPUT)? qse_xli_writejsonstd(xli, QSE_NULL, &out):
|
|
|
|
(g_io_flags & IO_FLAG_INI_OUTPUT)? qse_xli_writeinistd(xli, QSE_NULL, &out):
|
|
|
|
qse_xli_writestd(xli, QSE_NULL, &out);
|
2018-01-10 10:01:41 +00:00
|
|
|
if (ret <= -1)
|
|
|
|
{
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("WARNING: cannot write - %s\n"), qse_xli_geterrmsg(xli));
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
oops:
|
|
|
|
if (xli) qse_xli_close (xli);
|
|
|
|
if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx);
|
|
|
|
|
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
if (g_failmalloc > 0)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
|
2013-02-06 14:31:32 +00:00
|
|
|
(unsigned long)debug_mmgr_alloc_count,
|
|
|
|
(unsigned long)debug_mmgr_free_count,
|
|
|
|
(unsigned long)debug_mmgr_realloc_count);
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
|
2013-02-06 14:31:32 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-09-16 14:32:50 +00:00
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
2013-10-20 14:14:10 +00:00
|
|
|
int x;
|
|
|
|
|
2013-02-06 14:31:32 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
char locale[100];
|
2015-08-29 13:04:46 +00:00
|
|
|
UINT codepage = GetConsoleOutputCP();
|
2013-02-06 14:31:32 +00:00
|
|
|
if (codepage == CP_UTF8)
|
|
|
|
{
|
|
|
|
/*SetConsoleOUtputCP (CP_UTF8);*/
|
|
|
|
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-20 14:14:10 +00:00
|
|
|
/* .codepage */
|
|
|
|
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
|
|
|
|
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
|
2013-02-06 14:31:32 +00:00
|
|
|
setlocale (LC_ALL, locale);
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2013-02-06 14:31:32 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
setlocale (LC_ALL, "");
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2013-02-06 14:31:32 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2018-09-13 03:16:23 +00:00
|
|
|
x = qse_run_main(argc, argv, xli_main);
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2013-10-20 14:14:10 +00:00
|
|
|
|
|
|
|
return x;
|
2013-02-06 14:31:32 +00:00
|
|
|
}
|
|
|
|
|