cleaned up source code a little more

This commit is contained in:
hyung-hwan 2009-01-19 08:32:51 +00:00
parent 524f2e34c9
commit 2220eda162
5 changed files with 188 additions and 68 deletions

View File

@ -11,21 +11,30 @@
#include <math.h> #include <math.h>
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> # include <windows.h>
#else #else
#include <unistd.h> # include <unistd.h>
#include <signal.h> # include <signal.h>
# include <errno.h>
#endif #endif
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC # define _CRTDBG_MAP_ALLOC
#include <crtdbg.h> # include <crtdbg.h>
#endif #endif
#if defined(__linux) && defined(_DEBUG) #if defined(__linux) && defined(_DEBUG)
#include <mcheck.h> # include <mcheck.h>
#endif #endif
class TestAwk;
#ifdef _WIN32
static BOOL WINAPI stop_run (DWORD ctrl_type);
#else
static void stop_run (int sig);
#endif
TestAwk* app_awk = QSE_NULL;
static bool verbose = false; static bool verbose = false;
class TestAwk: public QSE::StdAwk class TestAwk: public QSE::StdAwk
@ -226,6 +235,17 @@ protected:
void onRunStart (Run& run) void onRunStart (Run& run)
{ {
if (verbose) qse_printf (QSE_T("*** awk run started ***\n")); if (verbose) qse_printf (QSE_T("*** awk run started ***\n"));
app_awk = this;
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, TRUE);
#else
struct sigaction sa_int;
sa_int.sa_handler = stop_run;
sigemptyset (&sa_int.sa_mask);
sa_int.sa_flags = 0;
sigaction (SIGINT, &sa_int, NULL);
#endif
} }
void onRunEnd (Run& run) void onRunEnd (Run& run)
@ -238,6 +258,16 @@ protected:
run.getErrorLine(), run.getErrorMessage()); run.getErrorLine(), run.getErrorMessage());
} }
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, FALSE);
#else
struct sigaction sa_int;
sa_int.sa_handler = SIG_DFL;
sigemptyset (&sa_int.sa_mask);
sa_int.sa_flags = 0;
sigaction (SIGINT, &sa_int, NULL);
#endif
app_awk = QSE_NULL;
if (verbose) qse_printf (QSE_T("*** awk run ended ***\n")); if (verbose) qse_printf (QSE_T("*** awk run ended ***\n"));
} }
@ -597,6 +627,28 @@ private:
#endif #endif
}; };
#ifdef _WIN32
static BOOL WINAPI stop_run (DWORD ctrl_type)
{
if (ctrl_type == CTRL_C_EVENT ||
ctrl_type == CTRL_CLOSE_EVENT)
{
if (app_awk) app_awk->stop ();
return TRUE;
}
return FALSE;
}
#else
static void stop_run (int sig)
{
int e = errno;
if (app_awk) app_awk->stop ();
errno = e;
}
#endif
#ifndef NDEBUG #ifndef NDEBUG
void qse_assert_abort (void) void qse_assert_abort (void)
{ {
@ -690,49 +742,6 @@ static void print_usage (const qse_char_t* argv0)
} }
} }
TestAwk* app_awk = QSE_NULL;
#ifdef _WIN32
static BOOL WINAPI stop_run (DWORD ctrl_type)
{
if (ctrl_type == CTRL_C_EVENT ||
ctrl_type == CTRL_CLOSE_EVENT)
{
if (app_awk) app_awk->stop ();
return TRUE;
}
return FALSE;
}
#else
static void stop_run (int sig)
{
signal (SIGINT, SIG_IGN);
if (app_awk) app_awk->stop ();
signal (SIGINT, stop_run);
}
#endif
static void set_intr_run (TestAwk* awk)
{
app_awk = awk;
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, TRUE);
#else
signal (SIGINT, stop_run);
#endif
}
static void unset_intr_run ()
{
app_awk = QSE_NULL;
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, FALSE);
#else
signal (SIGINT, SIG_DFL);
#endif
}
static int awk_main (int argc, qse_char_t* argv[]) static int awk_main (int argc, qse_char_t* argv[])
{ {
TestAwk awk; TestAwk awk;
@ -918,8 +927,6 @@ static int awk_main (int argc, qse_char_t* argv[])
awk.enableRunCallback (); awk.enableRunCallback ();
set_intr_run (&awk);
if (awk.run (mainfn, args, nargs) == -1) if (awk.run (mainfn, args, nargs) == -1)
{ {
qse_fprintf (stderr, QSE_T("cannot run: LINE[%d] %s\n"), qse_fprintf (stderr, QSE_T("cannot run: LINE[%d] %s\n"),
@ -927,7 +934,6 @@ static int awk_main (int argc, qse_char_t* argv[])
awk.close (); awk.close ();
return -1; return -1;
} }
unset_intr_run ();
awk.close (); awk.close ();

View File

@ -19,18 +19,17 @@
#define ABORT(label) goto label #define ABORT(label) goto label
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> # include <windows.h>
#include <tchar.h> # include <tchar.h>
#include <process.h> # include <process.h>
#pragma warning (disable: 4996)
#pragma warning (disable: 4296)
#if defined(_MSC_VER) && defined(_DEBUG) # if defined(_MSC_VER) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC # define _CRTDBG_MAP_ALLOC
#include <crtdbg.h> # include <crtdbg.h>
#endif # endif
#else #else
#include <unistd.h> # include <unistd.h>
# include <errno.h>
#endif #endif
static qse_awk_t* app_awk = NULL; static qse_awk_t* app_awk = NULL;
@ -48,7 +47,6 @@ static void dprint (const qse_char_t* fmt, ...)
} }
} }
#ifdef _WIN32 #ifdef _WIN32
static BOOL WINAPI stop_run (DWORD ctrl_type) static BOOL WINAPI stop_run (DWORD ctrl_type)
{ {
@ -64,15 +62,48 @@ static BOOL WINAPI stop_run (DWORD ctrl_type)
#else #else
static void stop_run (int sig) static void stop_run (int sig)
{ {
signal (SIGINT, SIG_IGN); int e = errno;
qse_awk_stop (app_run); qse_awk_stop (app_run);
signal (SIGINT, stop_run); errno = e;
} }
#endif #endif
static void set_intr_run (void)
{
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, TRUE);
#else
{
struct sigaction sa_int;
sa_int.sa_handler = stop_run;
sigemptyset (&sa_int.sa_mask);
sa_int.sa_flags = 0;
sigaction (SIGINT, &sa_int, NULL);
}
#endif
}
static void unset_intr_run (void)
{
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, FALSE);
#else
{
struct sigaction sa_int;
sa_int.sa_handler = SIG_DFL;
sigemptyset (&sa_int.sa_mask);
sa_int.sa_flags = 0;
sigaction (SIGINT, &sa_int, NULL);
}
#endif
}
static void on_run_start (qse_awk_run_t* run, void* custom) static void on_run_start (qse_awk_run_t* run, void* custom)
{ {
app_run = run; app_run = run;
set_intr_run ();
dprint (QSE_T("[AWK ABOUT TO START]\n")); dprint (QSE_T("[AWK ABOUT TO START]\n"));
} }
@ -146,6 +177,7 @@ static void on_run_end (qse_awk_run_t* run, int errnum, void* data)
} }
else dprint (QSE_T("[AWK ENDED SUCCESSFULLY]\n")); else dprint (QSE_T("[AWK ENDED SUCCESSFULLY]\n"));
unset_intr_run ();
app_run = NULL; app_run = NULL;
} }
@ -515,8 +547,8 @@ static qse_awk_t* open_awk (void)
static int awk_main (int argc, qse_char_t* argv[]) static int awk_main (int argc, qse_char_t* argv[])
{ {
qse_awk_t* awk; qse_awk_t* awk;
qse_awk_runcbs_t runcbs; qse_awk_runcbs_t runcbs;
struct sigaction sa_int;
int i, file_count = 0; int i, file_count = 0;
const qse_char_t* mfn = QSE_NULL; const qse_char_t* mfn = QSE_NULL;
@ -561,7 +593,10 @@ static int awk_main (int argc, qse_char_t* argv[])
#ifdef _WIN32 #ifdef _WIN32
SetConsoleCtrlHandler (stop_run, TRUE); SetConsoleCtrlHandler (stop_run, TRUE);
#else #else
signal (SIGINT, stop_run); sa_int.sa_handler = stop_run;
sigemptyset (&sa_int.sa_mask);
sa_int.sa_flags = 0;
sigaction (SIGINT, &sa_int, NULL);
#endif #endif
runcbs.on_start = on_run_start; runcbs.on_start = on_run_start;

View File

@ -79,6 +79,7 @@ typedef qse_int64_t qse_fio_off_t;
typedef enum qse_fio_seek_origin_t qse_fio_ori_t; typedef enum qse_fio_seek_origin_t qse_fio_ori_t;
typedef struct qse_fio_t qse_fio_t; typedef struct qse_fio_t qse_fio_t;
typedef struct qse_fio_lck_t qse_fio_lck_t;
struct qse_fio_t struct qse_fio_t
{ {
@ -86,6 +87,14 @@ struct qse_fio_t
qse_fio_hnd_t handle; qse_fio_hnd_t handle;
}; };
struct qse_fio_lck_t
{
int type; /* READ, WRITE */
qse_fio_off_t offset; /* starting offset */
qse_fio_off_t length; /* length */
qse_fio_ori_t origin; /* origin */
};
#define QSE_FIO_MMGR(fio) ((fio)->mmgr) #define QSE_FIO_MMGR(fio) ((fio)->mmgr)
#define QSE_FIO_HANDLE(fio) ((fio)->handle) #define QSE_FIO_HANDLE(fio) ((fio)->handle)
@ -179,13 +188,41 @@ qse_ssize_t qse_fio_write (
/****f* qse.cmn.fio/qse_fio_chmod /****f* qse.cmn.fio/qse_fio_chmod
* NAME * NAME
* ase_fio_chmod - change the file mode * qse_fio_chmod - change the file mode
* SYNOPSIS * SYNOPSIS
*/ */
int qse_fio_chmod ( int qse_fio_chmod (
qse_fio_t* fio, qse_fio_t* fio,
int mode int mode
); );
/******/
/****f* qse.cmn.fio/qse_fio_sync
* NAME
* qse_fio_sync - synchronize file contents into storage media
* DESCRIPTION
* The qse_fio_sync() function is useful in determining the media error,
* without which qse_fio_close() may succeed despite such an error.
* SYNOPSIS
*/
int qse_fio_sync (
qse_fio_t* fio
);
/******/
/* TODO: qse_fio_lock, qse_fio_unlock */
int qse_fio_lock (
qse_fio_t* fio,
qse_fio_lck_t* lck,
int flags
);
int qse_fio_unlock (
qse_fio_t* fio,
qse_fio_lck_t* lck,
int flags
);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -154,6 +154,7 @@ qse_fio_t* qse_fio_init (
else else
{ {
int desired_access = 0; int desired_access = 0;
#ifdef QSE_CHAR_IS_MCHAR #ifdef QSE_CHAR_IS_MCHAR
const qse_mchar_t* path_mb = path; const qse_mchar_t* path_mb = path;
#else #else
@ -440,3 +441,32 @@ int qse_fio_chmod (qse_fio_t* fio, int mode)
return QSE_FCHMOD (fio->handle, mode); return QSE_FCHMOD (fio->handle, mode);
#endif #endif
} }
int qse_fio_sync (qse_fio_t* fio)
{
#ifdef _WIN32
return (FlushFileBuffers (fio->handle) == FALSE)? -1: 0;
#else
return QSE_FSYNC (fio->handle);
#endif
}
int qse_fio_lock (qse_fio_t* fio, qse_fio_lck_t* lck, int flags)
{
/* TODO: qse_fio_lock
* struct flock fl;
* fl.l_type = F_RDLCK, F_WRLCK;
* QSE_FCNTL (fio->handle, F_SETLK, &fl);
*/
return -1;
}
int qse_fio_unlock (qse_fio_t* fio, qse_fio_lck_t* lck, int flags)
{
/* TODO: qse_fio_unlock
* struct flock fl;
* fl.l_type = F_UNLCK;
* QSE_FCNTL (fio->handle, F_SETLK, &fl);
*/
return -1;
}

View File

@ -84,6 +84,18 @@
#define QSE_FCHMOD(handle,mode) fchmod(handle,mode) #define QSE_FCHMOD(handle,mode) fchmod(handle,mode)
#endif #endif
#if defined(SYS_fsync)
#define QSE_FSYNC(handle) syscall(SYS_fsync,handle)
#else
#define QSE_FSYNC(handle) fsync(handle)
#endif
#if defined(SYS_fcntl)
#define QSE_FCNTL(handle,cmd,arg) syscall(SYS_fcntl,handle,cmd,arg)
#else
#define QSE_FCNTL(handle,cmd,arg) fcntl(handle,cmd,arg)
#endif
#ifdef SYS_dup2 #ifdef SYS_dup2
#define QSE_DUP2(ofd,nfd) syscall(SYS_dup2,ofd,nfd) #define QSE_DUP2(ofd,nfd) syscall(SYS_dup2,ofd,nfd)
#else #else