From c010af38cbfba38db050962e6864c38659b57189 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 9 Jan 2009 08:12:26 +0000 Subject: [PATCH] added qse_strspl(). --- qse/include/qse/cmn/str.h | 22 +++ qse/lib/cmn/pio.c | 7 +- qse/lib/cmn/str_utl.c | 282 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 306 insertions(+), 5 deletions(-) create mode 100644 qse/lib/cmn/str_utl.c diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index 5447bbf9..4791d332 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -396,12 +396,34 @@ qse_size_t qse_str_nccat ( qse_size_t len ); +/****f* qse.cmn.str/qse_strspl + * NAME + * qse_strspl - split a string + * + * SYNOPSIS + */ +int qse_strspl ( + qse_char_t* s, + const qse_char_t* delim, + qse_char_t lquote, + qse_char_t rquote, + qse_char_t escape, + qse_size_t* count +); +/******/ +/****f* qse.cmn.str/qse_mbstowcs + * NAME + * qse_mbstowcs - convert a multibyte string to a wide character string + * + * SYNOPSIS + */ qse_size_t qse_mbstowcs ( const qse_mchar_t* mbs, qse_wchar_t* wcs, qse_size_t* wcslen ); +/******/ /****f* qse.cmn.str/qse_mbsntowcsn * NAME diff --git a/qse/lib/cmn/pio.c b/qse/lib/cmn/pio.c index f2c75687..89c3b938 100644 --- a/qse/lib/cmn/pio.c +++ b/qse/lib/cmn/pio.c @@ -175,6 +175,7 @@ qse_pio_t* qse_pio_init ( { const qse_mchar_t* mcmd; qse_mchar_t* argv[4]; + extern char** environ; #ifdef QSE_CHAR_IS_MCHAR mcmd = cmd; @@ -210,16 +211,12 @@ qse_pio_t* qse_pio_init ( argv[2] = mcmd; argv[3] = QSE_NULL; - /* TODO: */ - { - extern char** environ; QSE_EXECVE (QSE_MT("/bin/sh"), argv, environ); - } } else { /* TODO: need to parse the command in a simple manner */ - //execl ("full path needed", mcmd, marg1, marg2, QSE_NULL); + QSE_EXECVE (mcmd, argv, environ); } child_oops: diff --git a/qse/lib/cmn/str_utl.c b/qse/lib/cmn/str_utl.c new file mode 100644 index 00000000..b72926e3 --- /dev/null +++ b/qse/lib/cmn/str_utl.c @@ -0,0 +1,282 @@ +/* + * $Id$ + * + Copyright 2006-2008 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. + */ + +#includle +#include "chr.h" + +int qse_strspl ( + qse_char_t* s, const qse_char_t* delim, + qse_char_t lquote, qse_char_t rquote, + qse_char_t escape, qse_size_t* count) +{ + qse_char_t* p = s, *d; + qse_char_t* sp = QSE_NULL, * ep = QSE_NULL; + int delim_mode; + qse_size_t cnt = 0; + + if (delim == QSE_NULL) delim_mode = 0; + else + { + delim_mode = 1; + for (d = (qse_char_t*)delim; *d != QSE_T('\0'); d++) + if (!QSE_ISSPACE(*d)) delim_mode = 2; + } + + if (delim_mode == 0) + { + /* skip preceding space characters */ + while (QSE_ISSPACE(*p)) p++; + + /* when 0 is given as "delim", it has an effect of cutting + preceding and trailing space characters off "s". */ + if (lquote != QSE_T('\0') && *p == lquote) + { + qse_strcpy (p, p + 1); + + for (;;) + { + if (*p == QSE_T('\0')) return -1; + + if (escape != QSE_T('\0') && *p == escape) + { + qse_strcpy (p, p + 1); + } + else + { + if (*p == rquote) + { + p++; + break; + } + } + + if (sp == 0) sp = p; + ep = p; + p++; + } + while (QSE_ISSPACE(*p)) p++; + if (*p != QSE_T('\0')) return -1; + + if (sp == 0 && ep == 0) s[0] = QSE_T('\0'); + else + { + ep[1] = QSE_T('\0'); + if (s != (qse_char_t*)sp) qse_strcpy (s, sp); + cnt++; + } + } + else + { + while (*p) + { + if (!QSE_ISSPACE(*p)) + { + if (sp == 0) sp = p; + ep = p; + } + p++; + } + + if (sp == 0 && ep == 0) s[0] = QSE_T('\0'); + else + { + ep[1] = QSE_T('\0'); + if (s != (qse_char_t*)sp) qse_strcpy (s, sp); + cnt++; + } + } + } + else if (delim_mode == 1) + { + qse_char_t* o; + + while (*p) + { + o = p; + while (QSE_ISSPACE(*p)) p++; + if (o != p) { qse_strcpy (o, p); p = o; } + + if (lquote != QSE_T('\0') && *p == lquote) + { + qse_strcpy (p, p + 1); + + for (;;) + { + if (*p == QSE_T('\0')) return -1; + + if (escape != QSE_T('\0') && *p == escape) + { + qse_strcpy (p, p + 1); + } + else + { + if (*p == rquote) + { + *p++ = QSE_T('\0'); + cnt++; + break; + } + } + p++; + } + } + else + { + o = p; + for (;;) + { + if (*p == QSE_T('\0')) + { + if (o != p) cnt++; + break; + } + if (QSE_ISSPACE (*p)) + { + *p++ = QSE_T('\0'); + cnt++; + break; + } + p++; + } + } + } + } + else /* if (delim_mode == 2) */ + { + qse_char_t* o; + int ok; + + while (*p != QSE_T('\0')) + { + o = p; + while (QSE_ISSPACE(*p)) p++; + if (o != p) { qse_strcpy (o, p); p = o; } + + if (lquote != QSE_T('\0') && *p == lquote) + { + qse_strcpy (p, p + 1); + + for (;;) + { + if (*p == QSE_T('\0')) return -1; + + if (escape != QSE_T('\0') && *p == escape) + { + qse_strcpy (p, p + 1); + } + else + { + if (*p == rquote) + { + *p++ = QSE_T('\0'); + cnt++; + break; + } + } + p++; + } + + ok = 0; + while (QSE_ISSPACE(*p)) p++; + if (*p == QSE_T('\0')) ok = 1; + for (d = (qse_char_t*)delim; *d != QSE_T('\0'); d++) + { + if (*p == *d) + { + ok = 1; + qse_strcpy (p, p + 1); + break; + } + } + if (ok == 0) return -1; + } + else + { + o = p; sp = ep = 0; + + for (;;) + { + if (*p == QSE_T('\0')) + { + if (ep) + { + ep[1] = QSE_T('\0'); + p = &ep[1]; + } + cnt++; + break; + } + for (d = (qse_char_t*)delim; *d != QSE_T('\0'); d++) + { + if (*p == *d) + { + if (sp == QSE_NULL) + { + qse_strcpy (o, p); p = o; + *p++ = QSE_T('\0'); + } + else + { + qse_strcpy (&ep[1], p); + qse_strcpy (o, sp); + o[ep - sp + 1] = QSE_T('\0'); + p = &o[ep - sp + 2]; + } + cnt++; + /* last empty field after delim */ + if (*p == QSE_T('\0')) cnt++; + goto exit_point; + } + } + + if (!QSE_ISSPACE (*p)) + { + if (sp == QSE_NULL) sp = p; + ep = p; + } + p++; + } +exit_point: + ; + } + } + } + + + *count = cnt; + return 0; +} + +int qse_strspls ( + qse_char_t* s, const qse_char_t* delim, + qse_char_t lquote, qse_char_t rquote, + qse_char_t escape, qse_size_t* count, qse_char_t* ptrs[]) +{ + qse_size_t cnt = *count, i; + + if (qse_strspl (s, delim, lquote, rquote, escape, count) == -1) return -1; + for (i = 0; i < *count && i < cnt; i++) + { + ptrs[i] = s; + s += qse_strlen(s) + 1; + } + + return 0; +} + +