touched code a little bit

This commit is contained in:
hyung-hwan 2009-05-30 19:19:46 +00:00
parent 7053747ab2
commit 7875def835
7 changed files with 308 additions and 339 deletions

View File

@ -3,7 +3,9 @@
STREAM EDITOR STREAM EDITOR
a stream editor is provided a stream editor is provided
@section sed_io_handler How to write an IO handler @section COMMAND
IO handler has the following prototype... - : label label for b and t commands. a label can be composed of an
alpha-numeric character, an underscope, and an underline.
- # comment
- = print the current line number
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h 151 2009-05-21 06:50:02Z hyunghwan.chung $ * $Id: awk.h 168 2009-05-30 01:19:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -27,6 +27,9 @@
/** @file /** @file
* An embeddable AWK interpreter is defined in this header files. * An embeddable AWK interpreter is defined in this header files.
* *
* @todo
* - change the way to set a custom error string: follow qse_sed_errstr_t
*
* @example awk01.c * @example awk01.c
* This program demonstrates how to use qse_awk_rtx_loop(). * This program demonstrates how to use qse_awk_rtx_loop().
* @example awk02.c * @example awk02.c
@ -38,22 +41,19 @@
*/ */
/** @class qse_awk_t /** @class qse_awk_t
* The qse_awk_t type defines an AWK interpreter. * The qse_awk_t type defines an AWK interpreter. The details are hidden as
* it is a complex type susceptible to misuse.
*/ */
typedef struct qse_awk_t qse_awk_t; typedef struct qse_awk_t qse_awk_t;
/****t* AWK/qse_awk_rtx_t /** @class qse_awk_rtx_t
* NAME * The qse_awk_rtx_t type defines a runtime context. The details are hidden
* qse_awk_rtx_t - define an AWK runtime context type * as it is a complex type susceptible to misuse.
* SYNOPSIS
*/ */
typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */ typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */
/******/
/****m* AWK/QSE_AWK_VAL_HDR /**
* NAME * The QSE_AWK_VAL_HDR defines the common header of a value type.
* QSE_AWK_VAL_HDR - define the common header of a value
* SYNOPSIS
*/ */
#if QSE_SIZEOF_INT == 2 #if QSE_SIZEOF_INT == 2
# define QSE_AWK_VAL_HDR \ # define QSE_AWK_VAL_HDR \
@ -64,42 +64,31 @@ typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */
unsigned int type: 3; \ unsigned int type: 3; \
unsigned int ref: 29 unsigned int ref: 29
#endif #endif
/******/
#define QSE_AWK_VAL_TYPE(x) ((x)->type) #define QSE_AWK_VAL_TYPE(x) ((x)->type)
/****s* AWK/qse_awk_val_t /**
* NAME * The qse_awk_val_t type is an abstract value type
* qse_awk_val_t - define an abstract value type
* SYNOPSIS
*/ */
struct qse_awk_val_t struct qse_awk_val_t
{ {
QSE_AWK_VAL_HDR; QSE_AWK_VAL_HDR;
}; };
typedef struct qse_awk_val_t qse_awk_val_t; typedef struct qse_awk_val_t qse_awk_val_t;
/******/
/****s* AWK/qse_awk_val_nil_t /**
* NAME * The qse_awk_val_nil_t type is a nil value type. The type field is
* qse_awk_val_nil_t - define a nil value type * QSE_AWK_VAL_NIL.
* DESCRIPTION
* The type field is QSE_AWK_VAL_NIL.
* SYNOPSIS
*/ */
struct qse_awk_val_nil_t struct qse_awk_val_nil_t
{ {
QSE_AWK_VAL_HDR; QSE_AWK_VAL_HDR;
}; };
typedef struct qse_awk_val_nil_t qse_awk_val_nil_t; typedef struct qse_awk_val_nil_t qse_awk_val_nil_t;
/******/
/****s* AWK/qse_awk_val_int_t /**
* NAME * The qse_awk_val_int_t type is an integer number type. The type field is
* qse_awk_val_int_t - define an integer number type * QSE_AWK_VAL_INT.
* DESCRIPTION
* The type field is QSE_AWK_VAL_INT.
* SYNOPSIS
*/ */
struct qse_awk_val_int_t struct qse_awk_val_int_t
{ {
@ -108,14 +97,10 @@ struct qse_awk_val_int_t
void* nde; void* nde;
}; };
typedef struct qse_awk_val_int_t qse_awk_val_int_t; typedef struct qse_awk_val_int_t qse_awk_val_int_t;
/******/
/****s* AWK/qse_awk_val_real_t /**
* NAME * The qse_awk_val_real_t type is a floating-point number type. The type field
* qse_awk_val_real_t - define a floating-point number type * is QSE_AWK_VAL_REAL.
* DESCRIPTION
* The type field is QSE_AWK_VAL_REAL.
* SYNOPSIS
*/ */
struct qse_awk_val_real_t struct qse_awk_val_real_t
{ {
@ -124,14 +109,10 @@ struct qse_awk_val_real_t
void* nde; void* nde;
}; };
typedef struct qse_awk_val_real_t qse_awk_val_real_t; typedef struct qse_awk_val_real_t qse_awk_val_real_t;
/******/
/****s* AWK/qse_awk_val_str_t /**
* NAME * The qse_awk_val_str_t type is a string type. The type field is
* qse_awk_val_str_t - define a string type * QSE_AWK_VAL_STR.
* DESCRIPTION
* The type field is QSE_AWK_VAL_STR.
* SYNOPSIS
*/ */
struct qse_awk_val_str_t struct qse_awk_val_str_t
{ {
@ -140,14 +121,10 @@ struct qse_awk_val_str_t
qse_size_t len; qse_size_t len;
}; };
typedef struct qse_awk_val_str_t qse_awk_val_str_t; typedef struct qse_awk_val_str_t qse_awk_val_str_t;
/******/
/****s* AWK/qse_awk_val_rex_t /**
* NAME * The qse_awk_val_rex_t type is a regular expression type. The type field
* qse_awk_val_rex_t - define a regular expression type * is QSE_AWK_VAL_REX.
* DESCRIPTION
* The type field is QSE_AWK_VAL_REX.
* SYNOPSIS
*/ */
struct qse_awk_val_rex_t struct qse_awk_val_rex_t
{ {
@ -157,7 +134,6 @@ struct qse_awk_val_rex_t
void* code; void* code;
}; };
typedef struct qse_awk_val_rex_t qse_awk_val_rex_t; typedef struct qse_awk_val_rex_t qse_awk_val_rex_t;
/******/
/* QSE_AWK_VAL_MAP */ /* QSE_AWK_VAL_MAP */
struct qse_awk_val_map_t struct qse_awk_val_map_t
@ -1546,30 +1522,23 @@ qse_bool_t qse_awk_rtx_isstaticval (
qse_awk_val_t* val qse_awk_val_t* val
); );
/****f* AWK/qse_awk_rtx_refupval /**
* NAME * The qse_awk_rtx_refupval() function increments a reference count of a
* qse_awk_rtx_refupval - increment a reference count * value @a val.
* SYNOPSIS
*/ */
void qse_awk_rtx_refupval ( void qse_awk_rtx_refupval (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx, /**< a runtime context */
qse_awk_val_t* val qse_awk_val_t* val /**< a value */
); );
/******/
/****f* AWK/qse_awk_rtx_refdownval /**
* NAME * The qse_awk_rtx_refdownval() function decrements a reference count of
* qse_awk_rtx_refdownval - decrement a reference count * a value @a val. It destroys the value if it has reached the count of 0.
* DESCRIPTION
* The qse_awk_rtx_refdownval() function decrements a reference count of
* a value. It destroys the value if it has reached the count of 0.
* SYNOPSIS
*/ */
void qse_awk_rtx_refdownval ( void qse_awk_rtx_refdownval (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx, /**< a runtime context */
qse_awk_val_t* val qse_awk_val_t* val /**< a value */
); );
/******/
void qse_awk_rtx_refdownval_nofree ( void qse_awk_rtx_refdownval_nofree (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx,
@ -1581,98 +1550,95 @@ qse_bool_t qse_awk_rtx_valtobool (
qse_awk_val_t* val qse_awk_val_t* val
); );
/****f* AWK/qse_awk_rtx_valtostr /**
* NAME * The qse_awk_rtx_valtostr() function convers a value val to a string as
* qse_awk_rtx_valtostr - convert a value to a string * instructed in the parameter out. Before the call to the function, you
* DESCRIPTION * should initialize a variable of the qse_awk_rtx_valtostr_out_t type.
* The qse_awk_rtx_valtostr() function convers a value val to a string as
* instructed in the parameter out. Before the call to the function, you
* should initialize a variable of the qse_awk_rtx_valtostr_out_t type.
* *
* The type field is one of the following qse_awk_rtx_valtostr_type_t values: * The type field is one of the following qse_awk_rtx_valtostr_type_t values:
* * QSE_AWK_RTX_VALTOSTR_CPL
* * QSE_AWK_RTX_VALTOSTR_CPLDUP
* * QSE_AWK_RTX_VALTOSTR_STRP
* * QSE_AWK_RTX_VALTOSTR_STRPCAT
* It can optionally be ORed with QSE_AWK_RTX_VALTOSTR_PRINT. The option
* causes the function to use OFMT for real number conversion. Otherwise,
* it uses CONVFMT.
* *
* You should initialize or free other fields before and after the call * - QSE_AWK_RTX_VALTOSTR_CPL
* depending on the type field as shown in EXAMPLES. * - QSE_AWK_RTX_VALTOSTR_CPLDUP
* - QSE_AWK_RTX_VALTOSTR_STRP
* - QSE_AWK_RTX_VALTOSTR_STRPCAT
* *
* RETURN * It can optionally be ORed with QSE_AWK_RTX_VALTOSTR_PRINT. The option
* The qse_awk_rtx_valtostr() function returns the pointer to the string * causes the function to use OFMT for real number conversion. Otherwise,
* converted on success, and QSE_NULL on failure. * it uses CONVFMT.
* *
* EXAMPLES * You should initialize or free other fields before and after the call
* If you have a static buffer, use QSE_AWK_RTX_VALTOSTR_CPL. * depending on the type field as shown below.
* qse_awk_rtx_valtostr_out_t out;
* qse_char_t buf[100];
* out.type = QSE_AWK_RTX_VALTOSTR_CPL;
* out.u.cpl.ptr = buf;
* out.u.cpl.len = QSE_COUNTOF(buf);
* if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops;
* qse_printf (QSE_T("%.*s\n"), (int)out.u.cpl.len, out.u.cpl.ptr);
* *
* When unsure of the size of the string after conversion, you can use * If you have a static buffer, use QSE_AWK_RTX_VALTOSTR_CPL.
* QSE_AWK_RTX_VALTOSTR_CPLDUP. However, you should free the memory block * @code
* pointed to by the u.cpldup.ptr field after use. * qse_awk_rtx_valtostr_out_t out;
* qse_awk_rtx_valtostr_out_t out; * qse_char_t buf[100];
* out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; * out.type = QSE_AWK_RTX_VALTOSTR_CPL;
* if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; * out.u.cpl.ptr = buf;
* qse_printf (QSE_T("%.*s\n"), (int)out.u.cpldup.len, out.u.cpldup.ptr); * out.u.cpl.len = QSE_COUNTOF(buf);
* qse_awk_rtx_free (rtx, out.u.cpldup.ptr); * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops;
* qse_printf (QSE_T("%.*s\n"), (int)out.u.cpl.len, out.u.cpl.ptr);
* @endcode
* *
* You may like to store the result in a dynamically resizable string. * When unsure of the size of the string after conversion, you can use
* Consider QSE_AWK_RTX_VALTOSTR_STRP. * QSE_AWK_RTX_VALTOSTR_CPLDUP. However, you should free the memory block
* qse_awk_rtx_valtostr_out_t out; * pointed to by the u.cpldup.ptr field after use.
* qse_str_t str; * @code
* qse_str_init (&str, qse_awk_rtx_getmmgr(rtx), 100); * qse_awk_rtx_valtostr_out_t out;
* out.type = QSE_AWK_RTX_VALTOSTR_STRP; * out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
* out.u.strp = str; * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops;
* if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpldup.len, out.u.cpldup.ptr);
* qse_printf (QSE_T("%.*s\n"), * qse_awk_rtx_free (rtx, out.u.cpldup.ptr);
* (int)QSE_STR_LEN(out.u.strp), QSE_STR_PTR(out.u.strp)); * @endcode
* qse_str_fini (&str);
* *
* If you want to append the converted string to an existing dynamically * You may like to store the result in a dynamically resizable string.
* resizable string, QSE_AWK_RTX_VALTOSTR_STRPCAT is the answer. The usage is * Consider QSE_AWK_RTX_VALTOSTR_STRP.
* the same as QSE_AWK_RTX_VALTOSTR_STRP except that you have to use the * @code
* u.strpcat field instead of the u.strp field. * qse_awk_rtx_valtostr_out_t out;
* SYNOPSIS * qse_str_t str;
* qse_str_init (&str, qse_awk_rtx_getmmgr(rtx), 100);
* out.type = QSE_AWK_RTX_VALTOSTR_STRP;
* out.u.strp = str;
* if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops;
* qse_printf (QSE_T("%.*s\n"),
* (int)QSE_STR_LEN(out.u.strp), QSE_STR_PTR(out.u.strp));
* qse_str_fini (&str);
* @endcode
*
* If you want to append the converted string to an existing dynamically
* resizable string, QSE_AWK_RTX_VALTOSTR_STRPCAT is the answer. The usage is
* the same as QSE_AWK_RTX_VALTOSTR_STRP except that you have to use the
* u.strpcat field instead of the u.strp field.
*
* @return the pointer to a string converted on success, QSE_NULL on failure
*/ */
qse_char_t* qse_awk_rtx_valtostr ( qse_char_t* qse_awk_rtx_valtostr (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx, /**< a runtime context */
qse_awk_val_t* val, qse_awk_val_t* val, /**< a vlaue */
qse_awk_rtx_valtostr_out_t* out qse_awk_rtx_valtostr_out_t* out /**< a output buffer */
); );
/******/ /******/
/****f* AWK/qse_awk_rtx_valtocpldup /**
* NAME * The qse_awk_rtx_valtocpldup() function provides a shortcut to the
* qse_awk_rtx_valtocpldup - convert a value to a string * qse_awk_rtx_valtostr() function with the QSE_AWK_RTX_VALTOSTR_CPLDUP type.
* DESCRIPTION * It returns the pointer to a string converted from @a val and stores its
* The qse_awk_rtx_valtocpldup() function provides a shortcut to the * length to memory pointed to by @a len. You should free the returned
* qse_awk_rtx_valtostr() function with the QSE_AWK_RTX_VALTOSTR_CPLDUP type. * memory block after use. See the code snippet below for a simple usage.
* It returns the pointer to the string converted and stores the length *
* to memory pointed to by the parameter len. You should free the returned * @code
* memory block after use. * ptr = qse_awk_rtx_valtocpldup (rtx, v, &len);
* RETURN * if (str == QSE_NULL) handle_error();
* It returns the pointer to the string converted on success, and * qse_printf (QSE_T("%.*s\n"), (int)len, ptr);
* QSE_NULL on failure. * qse_awk_rtx_free (rtx, ptr);
* EXAMPLE * @endcode
* The example shows a simple usage of the function. *
* ptr = qse_awk_rtx_valtocpldup (rtx, v, &len); * @return the pointer to a string converted on success, QSE_NULL on failure
* if (str == QSE_NULL) goto oops;
* qse_printf (QSE_T("%.*s\n"), (int)len, ptr);
* qse_awk_rtx_free (rtx, ptr);
* SYNOPSIS
*/ */
qse_char_t* qse_awk_rtx_valtocpldup ( qse_char_t* qse_awk_rtx_valtocpldup (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx, /**< a runtime context */
qse_awk_val_t* val, qse_awk_val_t* val, /**< a value to convert */
qse_size_t* len qse_size_t* len /**< result length */
); );
/******/ /******/
@ -1721,30 +1687,24 @@ int qse_awk_rtx_strtonum (
); );
/******/ /******/
/****f* AWK/qse_awk_rtx_alloc /**
* NAME * The qse_awk_rtx_alloc() function allocats a memory block of @a size bytes
* qse_awk_rtx_alloc - allocate memory * using the memory manager associated with a runtime context @a rtx.
* RETURN * @return the pointer to a memory block on success, QSE_NULL on failure.
* The qse_awk_rtx_alloc() function returns a pointer to the memory block
* allocated on success, and QSE_NULL on failure.
* SYNOPSIS
*/ */
void* qse_awk_rtx_alloc ( void* qse_awk_rtx_alloc (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx, /**< a runtime context */
qse_size_t size qse_size_t size /**< block size in bytes */
); );
/******/
/****f* AWK/qse_awk_rtx_free /**
* NAME * The qse_awk_rtx_free() function frees a memory block pointed to by @a ptr
* qse_awk_rtx_free - free memory * using the memory manager of a runtime ocntext @a rtx.
* SYNOPSIS
*/ */
void qse_awk_rtx_free ( void qse_awk_rtx_free (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx, /**< a runtime context */
void* ptr void* ptr /**< a memory block pointer */
); );
/******/
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: pio.h 75 2009-02-22 14:10:34Z hyunghwan.chung $ * $Id: pio.h 168 2009-05-30 01:19:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -23,6 +23,13 @@
#include <qse/macros.h> #include <qse/macros.h>
#include <qse/cmn/tio.h> #include <qse/cmn/tio.h>
/** @file
* Pipe I/O
* @todo
* - rename flags to option
* - write code for win32
*/
enum qse_pio_open_flag_t enum qse_pio_open_flag_t
{ {
/* enable ase_char_t based IO */ /* enable ase_char_t based IO */
@ -65,7 +72,7 @@ enum qse_pio_io_flag_t
QSE_PIO_WAIT_NORETRY = (1 << 5) QSE_PIO_WAIT_NORETRY = (1 << 5)
}; };
enum qse_pio_err_t enum qse_pio_errnum_t
{ {
QSE_PIO_ENOERR = 0, QSE_PIO_ENOERR = 0,
QSE_PIO_ENOMEM, /* out of memory */ QSE_PIO_ENOMEM, /* out of memory */
@ -76,7 +83,7 @@ enum qse_pio_err_t
}; };
typedef enum qse_pio_hid_t qse_pio_hid_t; typedef enum qse_pio_hid_t qse_pio_hid_t;
typedef enum qse_pio_err_t qse_pio_err_t; typedef enum qse_pio_errnum_t qse_pio_errnum_t;
#ifdef _WIN32 #ifdef _WIN32
/* <winnt.h> => typedef PVOID HANDLE; */ /* <winnt.h> => typedef PVOID HANDLE; */
@ -101,21 +108,18 @@ struct qse_pio_pin_t
qse_pio_t* self; qse_pio_t* self;
}; };
/****s* Common/qse_pio_t /**
* NAME * The qse_pio_t type defines a pipe I/O type
* qse_pio_t - define an pipe IO type
* SYNOPSIS
*/ */
struct qse_pio_t struct qse_pio_t
{ {
QSE_DEFINE_COMMON_FIELDS(pio) QSE_DEFINE_COMMON_FIELDS(pio)
int flags; int flags;
qse_pio_err_t errnum; qse_pio_errnum_t errnum;
qse_pio_pid_t child; qse_pio_pid_t child;
qse_pio_pin_t pin[3]; qse_pio_pin_t pin[3];
}; };
/*****/
#define QSE_PIO_ERRNUM(pio) ((pio)->errnum) #define QSE_PIO_ERRNUM(pio) ((pio)->errnum)
#define QSE_PIO_FLAGS(pio) ((pio)->flags) #define QSE_PIO_FLAGS(pio) ((pio)->flags)
@ -128,40 +132,27 @@ extern "C" {
QSE_DEFINE_COMMON_FUNCTIONS (pio) QSE_DEFINE_COMMON_FUNCTIONS (pio)
/****f* Common/qse_pio_open /**
* NAME * The qse_pio_open() function opens pipes to a child process.
* qse_pio_open - open pipes to a child process * QSE_PIO_SHELL drives the function to execute the command via /bin/sh.
* * If flags is clear of QSE_PIO_SHELL, you should pass the full program path.
* DESCRIPTION
* QSE_PIO_SHELL drives the funcpion to execute the command via /bin/sh.
* If flags is clear of QSE_PIO_SHELL, you should pass the full program path.
*
* SYNOPSIS
*/ */
qse_pio_t* qse_pio_open ( qse_pio_t* qse_pio_open (
qse_mmgr_t* mmgr, qse_mmgr_t* mmgr, /**< a memory manager */
qse_size_t ext, qse_size_t ext, /**< extension size */
const qse_char_t* cmd, const qse_char_t* cmd, /**< a command to execute */
int flags int flags /**< options */
); );
/******/
/****f* Common/qse_pio_close /**
* NAME * The qse_pio_close() function closes pipes to a child process.
* qse_pio_close - close pipes to a child process
*
* SYNOPSIS
*/ */
void qse_pio_close ( void qse_pio_close (
qse_pio_t* pio qse_pio_t* pio
); );
/******/
/****f* Common/qse_pio_init /**
* NAME * The qse_pio_init() function initializes pipes to a child process.
* qse_pio_init - initialize pipes to a child process
*
* SYNOPSIS
*/ */
qse_pio_t* qse_pio_init ( qse_pio_t* qse_pio_init (
qse_pio_t* pio, qse_pio_t* pio,
@ -169,18 +160,13 @@ qse_pio_t* qse_pio_init (
const qse_char_t* path, const qse_char_t* path,
int flags int flags
); );
/******/
/****f* Common/qse_pio_fini /**
* NAME * The qse_pio_fini() function finalizes pipes to a child process.
* qse_pio_fini - finalize pipes to a child process
*
* SYNOPSIS
*/ */
void qse_pio_fini ( void qse_pio_fini (
qse_pio_t* pio qse_pio_t* pio
); );
/******/
int qse_pio_getflags ( int qse_pio_getflags (
qse_pio_t* pio qse_pio_t* pio
@ -189,7 +175,7 @@ int qse_pio_getflags (
void qse_pio_setflags ( void qse_pio_setflags (
qse_pio_t* pio, qse_pio_t* pio,
int flags, int flags,
int op int opt
); );
/****f* Common/qse_pio_geterrnum /****f* Common/qse_pio_geterrnum
@ -198,7 +184,7 @@ void qse_pio_setflags (
* *
* SYNOPSIS * SYNOPSIS
*/ */
qse_pio_err_t qse_pio_geterrnum ( qse_pio_errnum_t qse_pio_geterrnum (
qse_pio_t* pio qse_pio_t* pio
); );
/******/ /******/

View File

@ -36,8 +36,8 @@
* @endcode * @endcode
* *
* @todo * @todo
* - to allow flexible numbers of commands * - allow flexible numbers of commands
* - to allow configurable recursion depth for a regular expression * - allow configurable recursion depth for a regular expression
* *
* @example sed01.c * @example sed01.c
* This example shows how to embed a basic stream editor. * This example shows how to embed a basic stream editor.

View File

@ -1,5 +1,5 @@
/* /*
* $Id: pio.c 76 2009-02-22 14:18:06Z hyunghwan.chung $ * $Id: pio.c 168 2009-05-30 01:19:46Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -415,7 +415,7 @@ void qse_pio_setflags (qse_pio_t* pio, int flags, int op)
else /*if (op < 0)*/ pio->flags &= ~flags; else /*if (op < 0)*/ pio->flags &= ~flags;
} }
qse_pio_err_t qse_pio_geterrnum (qse_pio_t* pio) qse_pio_errnum_t qse_pio_geterrnum (qse_pio_t* pio)
{ {
return pio->errnum; return pio->errnum;
} }

View File

@ -137,9 +137,9 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
qse_map_setcopier (&sed->tmp.labs, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE); qse_map_setcopier (&sed->tmp.labs, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE);
qse_map_setscale (&sed->tmp.labs, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t)); qse_map_setscale (&sed->tmp.labs, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t));
/* TODO: use different data structure... */ sed->cmd.len = 256; /* TODO: parameterize this value */
sed->cmd.buf = QSE_MMGR_ALLOC ( sed->cmd.buf = QSE_MMGR_ALLOC (
sed->mmgr, QSE_SIZEOF(qse_sed_cmd_t) * 1000); sed->mmgr, QSE_SIZEOF(qse_sed_cmd_t) * sed->cmd.len);
if (sed->cmd.buf == QSE_NULL) if (sed->cmd.buf == QSE_NULL)
{ {
qse_map_fini (&sed->tmp.labs); qse_map_fini (&sed->tmp.labs);
@ -148,7 +148,12 @@ static qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
return QSE_NULL; return QSE_NULL;
} }
sed->cmd.cur = sed->cmd.buf; sed->cmd.cur = sed->cmd.buf;
sed->cmd.end = sed->cmd.buf + 1000 - 1; sed->cmd.end = sed->cmd.buf + sed->cmd.len - 1;
#if 0
sed->cmd.lb = &sed->cmd.fb; /* on init, the last points to the first */
sed->fb.len = 0; /* the block has no data yet */
#endif
if (qse_lda_init (&sed->e.txt.appended, mmgr, 32) == QSE_NULL) if (qse_lda_init (&sed->e.txt.appended, mmgr, 32) == QSE_NULL)
{ {
@ -405,7 +410,7 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a)
} }
else if (c >= QSE_T('0') && c <= QSE_T('9')) else if (c >= QSE_T('0') && c <= QSE_T('9'))
{ {
qse_sed_line_t lno = 0; qse_size_t lno = 0;
do do
{ {
lno = lno * 10 + c - QSE_T('0'); lno = lno * 10 + c - QSE_T('0');
@ -417,7 +422,7 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a)
if (lno == 0) return QSE_NULL; if (lno == 0) return QSE_NULL;
a->type = QSE_SED_ADR_LINE; a->type = QSE_SED_ADR_LINE;
a->u.line = lno; a->u.lno = lno;
} }
else if (c == QSE_T('\\')) else if (c == QSE_T('\\'))
{ {
@ -1947,7 +1952,7 @@ static int match_a (qse_sed_t* sed, qse_sed_cmd_t* cmd, qse_sed_adr_t* a)
switch (a->type) switch (a->type)
{ {
case QSE_SED_ADR_LINE: case QSE_SED_ADR_LINE:
return (sed->e.in.num == a->u.line)? 1: 0; return (sed->e.in.num == a->u.lno)? 1: 0;
case QSE_SED_ADR_REX: case QSE_SED_ADR_REX:
{ {
@ -2033,8 +2038,8 @@ static int match_address (qse_sed_t* sed, qse_sed_cmd_t* cmd)
/* stepping address */ /* stepping address */
cmd->state.c_ready = 1; cmd->state.c_ready = 1;
if (sed->e.in.num < cmd->a1.u.line) return 0; if (sed->e.in.num < cmd->a1.u.lno) return 0;
if ((sed->e.in.num - cmd->a1.u.line) % cmd->a2.u.line == 0) return 1; if ((sed->e.in.num - cmd->a1.u.lno) % cmd->a2.u.lno == 0) return 1;
return 0; return 0;
} }
else if (cmd->a2.type != QSE_SED_ADR_NONE) else if (cmd->a2.type != QSE_SED_ADR_NONE)
@ -2047,7 +2052,7 @@ static int match_address (qse_sed_t* sed, qse_sed_cmd_t* cmd)
if (n == 0) if (n == 0)
{ {
if (cmd->a2.type == QSE_SED_ADR_LINE && if (cmd->a2.type == QSE_SED_ADR_LINE &&
sed->e.in.num > cmd->a2.u.line) sed->e.in.num > cmd->a2.u.lno)
{ {
/* exit the range */ /* exit the range */
cmd->state.a1_matched = 0; cmd->state.a1_matched = 0;
@ -2074,7 +2079,7 @@ static int match_address (qse_sed_t* sed, qse_sed_cmd_t* cmd)
} }
if (cmd->a2.type == QSE_SED_ADR_LINE && if (cmd->a2.type == QSE_SED_ADR_LINE &&
sed->e.in.num >= cmd->a2.u.line) sed->e.in.num >= cmd->a2.u.lno)
{ {
/* the line number specified in the second /* the line number specified in the second
* address is equal to or less than the current * address is equal to or less than the current

View File

@ -22,114 +22,9 @@
#include <qse/sed/sed.h> #include <qse/sed/sed.h>
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
typedef qse_int_t qse_sed_line_t;
typedef struct qse_sed_adr_t qse_sed_adr_t; typedef struct qse_sed_adr_t qse_sed_adr_t;
typedef struct qse_sed_cmd_t qse_sed_cmd_t; typedef struct qse_sed_cmd_t qse_sed_cmd_t;
typedef struct qse_sed_cmd_blk_t qse_sed_cmd_blk_t;
/**
* The qse_sed_t type defines a stream editor
*/
struct qse_sed_t
{
QSE_DEFINE_COMMON_FIELDS (sed)
qse_sed_errstr_t errstr; /**< error string getter */
qse_sed_errnum_t errnum; /**< stores an error number */
qse_char_t errmsg[128]; /**< error message holder */
qse_size_t errlin; /**< no of the line where an error occurred */
int option; /**< stores options */
/** source text pointers */
struct
{
qse_size_t lnum; /**< line number */
qse_cint_t cc; /**< last character read */
const qse_char_t* ptr; /**< beginning of the source text */
const qse_char_t* end; /**< end of the source text */
const qse_char_t* cur; /**< current source text pointer */
} src;
/** temporary data for compiling */
struct
{
qse_str_t rex; /**< regular expression buffer */
qse_str_t lab; /**< label name buffer */
/** data structure to compile command groups */
struct
{
/** current level of command group nesting */
int level;
/** keeps track of the begining of nested groups */
qse_sed_cmd_t* cmd[128];
} grp;
/** a table storing labels seen */
qse_map_t labs;
} tmp;
/** compiled commands */
struct
{
qse_sed_cmd_t* buf; /**< buffer holding compiled commands */
qse_sed_cmd_t* end; /**< end of the buffer */
qse_sed_cmd_t* cur; /**< points next to the last command */
} cmd;
/** data for execution */
struct
{
/** data needed for output streams and files */
struct
{
qse_sed_io_fun_t fun; /**< an output handler */
qse_sed_io_arg_t arg; /**< output handling data */
qse_char_t buf[2048];
qse_size_t len;
int eof;
/*****************************************************/
/* the following two fields are very tightly-coupled.
* don't make any partial changes */
qse_map_t files;
qse_sed_t* files_ext;
/*****************************************************/
} out;
/** data needed for input streams */
struct
{
qse_sed_io_fun_t fun; /**< an input handler */
qse_sed_io_arg_t arg; /**< input handling data */
qse_char_t xbuf[1]; /**< a read-ahead buffer */
int xbuf_len; /**< data length in the buffer */
qse_char_t buf[2048]; /**< input buffer */
qse_size_t len; /**< data length in the buffer */
qse_size_t pos; /**< current position in the buffer */
int eof; /**< EOF indicator */
qse_str_t line; /**< pattern space */
qse_size_t num; /**< current line number */
} in;
/** text buffers */
struct
{
qse_lda_t appended;
qse_str_t read;
qse_str_t held;
qse_str_t subst;
} txt;
/** indicates if a successful substitution has been made
* since the last read on the input stream. */
int subst_done;
} e;
};
struct qse_sed_adr_t struct qse_sed_adr_t
{ {
@ -144,8 +39,8 @@ struct qse_sed_adr_t
union union
{ {
qse_sed_line_t line; qse_size_t lno;
void* rex; void* rex;
} u; } u;
}; };
@ -226,4 +121,125 @@ struct qse_sed_cmd_t
} state; } state;
}; };
struct qse_sed_cmd_blk_t
{
qse_size_t len;
qse_sed_cmd_t buf[512];
qse_sed_cmd_blk_t* next;
};
/**
* The qse_sed_t type defines a stream editor
*/
struct qse_sed_t
{
QSE_DEFINE_COMMON_FIELDS (sed)
qse_sed_errstr_t errstr; /**< error string getter */
qse_sed_errnum_t errnum; /**< stores an error number */
qse_char_t errmsg[128]; /**< error message holder */
qse_size_t errlin; /**< no of the line where an error occurred */
int option; /**< stores options */
/** source text pointers */
struct
{
qse_size_t lnum; /**< line number */
qse_cint_t cc; /**< last character read */
const qse_char_t* ptr; /**< beginning of the source text */
const qse_char_t* end; /**< end of the source text */
const qse_char_t* cur; /**< current source text pointer */
} src;
/** temporary data for compiling */
struct
{
qse_str_t rex; /**< regular expression buffer */
qse_str_t lab; /**< label name buffer */
/** data structure to compile command groups */
struct
{
/** current level of command group nesting */
int level;
/** keeps track of the begining of nested groups */
qse_sed_cmd_t* cmd[128];
} grp;
/** a table storing labels seen */
qse_map_t labs;
} tmp;
/** compiled commands */
struct
{
qse_size_t len; /**< buffer size */
qse_sed_cmd_t* buf; /**< buffer holding compiled commands */
qse_sed_cmd_t* end; /**< end of the buffer */
qse_sed_cmd_t* cur; /**< points next to the last command */
} cmd;
#if 0
struct
{
qse_sed_cmd_blk_t fb; /**< the first block is static */
qse_sed_cmd_blk_t* lb; /**< points to the last block */
} cmd;
#endif
/** data for execution */
struct
{
/** data needed for output streams and files */
struct
{
qse_sed_io_fun_t fun; /**< an output handler */
qse_sed_io_arg_t arg; /**< output handling data */
qse_char_t buf[2048];
qse_size_t len;
int eof;
/*****************************************************/
/* the following two fields are very tightly-coupled.
* don't make any partial changes */
qse_map_t files;
qse_sed_t* files_ext;
/*****************************************************/
} out;
/** data needed for input streams */
struct
{
qse_sed_io_fun_t fun; /**< an input handler */
qse_sed_io_arg_t arg; /**< input handling data */
qse_char_t xbuf[1]; /**< a read-ahead buffer */
int xbuf_len; /**< data length in the buffer */
qse_char_t buf[2048]; /**< input buffer */
qse_size_t len; /**< data length in the buffer */
qse_size_t pos; /**< current position in the buffer */
int eof; /**< EOF indicator */
qse_str_t line; /**< pattern space */
qse_size_t num; /**< current line number */
} in;
/** text buffers */
struct
{
qse_lda_t appended;
qse_str_t read;
qse_str_t held;
qse_str_t subst;
} txt;
/** indicates if a successful substitution has been made
* since the last read on the input stream. */
int subst_done;
} e;
};
#endif #endif