/* tokenize.c */ /* remap Copyright (C) 2006 Fabrice Ducos, fabrice.ducos@icare.univ-lille1.fr This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include "tokenize.h" static const size_t STRING_MAXLEN = 255; static const size_t ERRMSG_MAXLEN = 255; etok_type tokenize(const char *pattern, const char *a_string, size_t *ntokens, char ***ptokens) { regex_t regex; size_t nsub; size_t isub; regmatch_t *subs; int err; char **tokens; err = regcomp(®ex, pattern, REG_EXTENDED); if (err != 0) { #ifdef DEBUG char err_msg[ERRMSG_MAXLEN + 1]; regerror(err, ®ex, err_msg, ERRMSG_MAXLEN + 1); printf("tokenize: %s\n", err_msg); #endif regfree(®ex); return ETOK_REGCOMP; } nsub = regex.re_nsub + 1; tokens = (char **) malloc(nsub * sizeof(char *)); assert(tokens); assert(nsub > 0); subs = (regmatch_t *) calloc(nsub, sizeof(regmatch_t)); if (subs == NULL) { regfree(®ex); return ETOK_MEMORY; } err = regexec(®ex, a_string, nsub, subs, 0); if (err == REG_NOMATCH) { regfree(®ex); return ETOK_NOMATCH; } else if (err == REG_ESPACE) { regfree(®ex); return ETOK_ESPACE; } for (isub = 0 ; isub < nsub ; isub++) { regmatch_t *sub = &subs[isub]; size_t sublen = sub->rm_eo - sub->rm_so; tokens[isub] = (char *) malloc((sublen + 1)*sizeof(char)); assert(tokens[isub]); strncpy(tokens[isub], a_string + sub->rm_so, sublen); tokens[isub][sublen] = '\0'; } free(subs); *ntokens = nsub; *ptokens = tokens; regfree(®ex); return ETOK_SUCCESS; } void free_tokens(size_t ntokens, char **tokens) { size_t itoken; assert(tokens); assert(*tokens); for (itoken = 0 ; itoken < ntokens ; itoken++) { free(tokens[itoken]); } free(tokens); }