implementing sed

This commit is contained in:
hyung-hwan 2009-03-16 08:36:05 +00:00
parent 69f02f9c8d
commit ad5251570f
4 changed files with 76 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: map.h 90 2009-03-01 09:58:19Z hyunghwan.chung $ * $Id: map.h 101 2009-03-15 14:36:05Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -292,57 +292,96 @@ void qse_map_close (
); );
/******/ /******/
/****f* Common/qse_map_init
* NAME
* qse_map_init - initialize a hash map
* SYNOPSIS
*/
qse_map_t* qse_map_init ( qse_map_t* qse_map_init (
qse_map_t* map, qse_map_t* map,
qse_mmgr_t* mmgr, qse_mmgr_t* mmgr,
qse_size_t capa, qse_size_t capa,
int factor int factor
); );
/******/
/****f* Common/qse_map_fini
* NAME
* qse_map_fini - finalize a hash map
* SYNOPSIS
*/
void qse_map_fini ( void qse_map_fini (
qse_map_t* map qse_map_t* map
); );
/******/
/* get the number of key/value pairs in a map */ /****f* Common/qse_map_getsize
* NAME
* qse_map_getsize - get the number of pairs in a map
* SYNOPSIS
*/
qse_size_t qse_map_getsize ( qse_size_t qse_map_getsize (
qse_map_t* map /* a map */ qse_map_t* map
); );
/******/
/****f* Common/qse_map_getcapa
* NAME
* qse_map_getcapa - get the number of slots in a hash bucket
* SYNOPSIS
*/
qse_size_t qse_map_getcapa ( qse_size_t qse_map_getcapa (
qse_map_t* map /* a map */ qse_map_t* map
); );
/******/
/****f* Common/qse_map_getscale
* NAME
* qse_map_getscale - get the scale factor
* PARAMETERS
* * id - QSE_MAP_KEY or QSE_MAP_VAL
* SYNOPSIS
*/
int qse_map_getscale ( int qse_map_getscale (
qse_map_t* map /* a map */, qse_map_t* map,
qse_map_id_t id /* QSE_MAP_KEY or QSE_MAP_VAL */ qse_map_id_t id /* QSE_MAP_KEY or QSE_MAP_VAL */
); );
/******/
/****f* Common/qse_map_setscale /****f* Common/qse_map_setscale
* NAME * NAME
* qse_map_setscale - set the scale factor * qse_map_setscale - set the scale factor
*
* DESCRIPTION * DESCRIPTION
* The qse_map_setscale() function sets the scale factor of the length * The qse_map_setscale() function sets the scale factor of the length
* of a key and a value. A scale factor determines the actual length of * of a key and a value. A scale factor determines the actual length of
* a key and a value in bytes. A map is created with a scale factor of 1. * a key and a value in bytes. A map is created with a scale factor of 1.
* The scale factor should be larger than 0 and less than 256. * The scale factor should be larger than 0 and less than 256.
* * PARAMETERS
* * id - QSE_MAP_KEY or QSE_MAP_VAL
* * scale - a scale factor in bytes
* NOTES * NOTES
* It is a bad idea to change the scale factor when a map is not empty. * It is a bad idea to change the scale factor when a map is not empty.
*
* SYNOPSIS * SYNOPSIS
*/ */
void qse_map_setscale ( void qse_map_setscale (
qse_map_t* map /* a map */, qse_map_t* map,
qse_map_id_t id /* QSE_MAP_KEY or QSE_MAP_VAL */, qse_map_id_t id,
int scale /* a scale factor */ int scale
); );
/******/ /******/
/****f* Common/qse_map_getcopier
* NAME
* qse_map_getcopier - get a data copier
* PARAMETERS
* * id - QSE_MAP_KEY or QSE_MAP_VAL
* SYNOPSIS
*/
qse_map_copier_t qse_map_getcopier ( qse_map_copier_t qse_map_getcopier (
qse_map_t* map /* a map */, qse_map_t* map,
qse_map_id_t id /* QSE_MAP_KEY or QSE_MAP_VAL */ qse_map_id_t id
); );
/*****/
/****f* Common/qse_map_setcopier /****f* Common/qse_map_setcopier
* NAME * NAME

View File

@ -23,6 +23,7 @@
#include <qse/macros.h> #include <qse/macros.h>
#include <qse/cmn/str.h> #include <qse/cmn/str.h>
#include <qse/cmn/lda.h> #include <qse/cmn/lda.h>
#include <qse/cmn/map.h>
enum qse_sed_errnum_t enum qse_sed_errnum_t
{ {
@ -31,13 +32,13 @@ enum qse_sed_errnum_t
QSE_SED_ETMTXT, /* too much text */ QSE_SED_ETMTXT, /* too much text */
QSE_SED_ECMDNR, /* command not recognized */ QSE_SED_ECMDNR, /* command not recognized */
QSE_SED_ECMDGB, /* command garbled */ QSE_SED_ECMDGB, /* command garbled */
QSE_SED_ELBLTL, /* label too long */
QSE_SED_EREXBL, /* regular expression build error */ QSE_SED_EREXBL, /* regular expression build error */
QSE_SED_EA1PHB, /* address 1 prohibited */ QSE_SED_EA1PHB, /* address 1 prohibited */
QSE_SED_EA2PHB, /* address 2 prohibited */ QSE_SED_EA2PHB, /* address 2 prohibited */
QSE_SED_ENEWLN, /* a new line is expected */ QSE_SED_ENEWLN, /* a new line is expected */
QSE_SED_EBSEXP, /* \ is expected */ QSE_SED_EBSEXP, /* \ is expected */
QSE_SED_ELBLEM /* label name is empty */ QSE_SED_ELABTL, /* label too long */
QSE_SED_ELABEM /* label name is empty */
}; };
enum qse_sed_option_t enum qse_sed_option_t
@ -76,6 +77,8 @@ struct qse_sed_t
qse_sed_c_t* end; qse_sed_c_t* end;
qse_sed_c_t* cur; qse_sed_c_t* cur;
} cmd; } cmd;
qse_map_t labs; /* label map */
}; };

View File

@ -73,10 +73,21 @@ qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
return QSE_NULL; return QSE_NULL;
} }
if (qse_map_init (&sed->labs, mmgr, 128, 70) == QSE_NULL)
{
qse_str_fini (&sed->rexbuf);
sed->errnum = QSE_SED_ENOMEM;
return QSE_NULL;
}
qse_map_setcopier (&sed->labs, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE);
qse_map_setscale (&sed->labs, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t));
/* TODO: use different data structure... */ /* TODO: use different data structure... */
sed->cmd.buf = QSE_MMGR_ALLOC (sed->mmgr, QSE_SIZEOF(qse_sed_c_t) * 1000); sed->cmd.buf = QSE_MMGR_ALLOC (
sed->mmgr, QSE_SIZEOF(qse_sed_c_t) * 1000);
if (sed->cmd.buf == QSE_NULL) if (sed->cmd.buf == QSE_NULL)
{ {
qse_map_fini (&sed->labs);
qse_str_fini (&sed->rexbuf); qse_str_fini (&sed->rexbuf);
return QSE_NULL; return QSE_NULL;
} }
@ -88,6 +99,7 @@ qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr)
void qse_sed_fini (qse_sed_t* sed) void qse_sed_fini (qse_sed_t* sed)
{ {
qse_map_fini (&sed->labs);
qse_str_fini (&sed->rexbuf); qse_str_fini (&sed->rexbuf);
} }
@ -301,7 +313,7 @@ oops:
static qse_str_t* get_label (qse_sed_t* sed, qse_sed_c_t* cmd) static qse_str_t* get_label (qse_sed_t* sed, qse_sed_c_t* cmd)
{ {
qse_cint_t c; qse_cint_t c;
qse_str_t* t = QSE_NULL; qse_str_t* t = QSE_NULL; /* TODO: move this buffer to sed */
/* skip white spaces */ /* skip white spaces */
c = CURSC(sed); c = CURSC(sed);
@ -310,7 +322,7 @@ static qse_str_t* get_label (qse_sed_t* sed, qse_sed_c_t* cmd)
if (IS_LABEL_TERMINATER(c)) if (IS_LABEL_TERMINATER(c))
{ {
/* label name is empty */ /* label name is empty */
sed->errnum = QSE_SED_ELBLEM; sed->errnum = QSE_SED_ELABEM;
return QSE_NULL; return QSE_NULL;
} }
@ -327,7 +339,11 @@ static qse_str_t* get_label (qse_sed_t* sed, qse_sed_c_t* cmd)
} }
while (!IS_LABEL_TERMINATOR(c)); while (!IS_LABEL_TERMINATOR(c));
/* TODO: */
search_label_table (c); search_label_table (c);
qse_map_insert (&sed->labs, QSE_STR_PTR(t), QSE_STR_LEN(t), cmd, 0);
ADVSCP (sed);
return t; return t;
oops: oops:

View File

@ -109,12 +109,4 @@ struct qse_sed_c_t
int negfl; int negfl;
}; };
struct qse_sed_l_t
{
qse_char_t name[9]; /* label name */
qse_sed_c_t* chain;
qse_sed_c_t* address; /* command associated with label */
};
#endif #endif