diff options
author | martin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220> | 2009-07-30 20:10:24 +0000 |
---|---|---|
committer | martin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220> | 2009-07-30 20:10:24 +0000 |
commit | 3b8435d744c504a88493f272068453023585837e (patch) | |
tree | a729476323d0d551c2031b3a666193923ceb08a4 | |
parent | cb7c443b8c6ca72aee1b925ad6566a7e9d153dd6 (diff) | |
download | navit-svn-3b8435d744c504a88493f272068453023585837e.tar.gz |
Add:support_wordexp:Added improved wordexp and glob|Thanks to gotwowince@2429
git-svn-id: http://svn.code.sf.net/p/navit/code/trunk/navit/navit@2428 ffa7fe5e-494d-0410-b361-a75ebd5db220
-rw-r--r-- | support/wordexp/Makefile.am | 2 | ||||
-rw-r--r-- | support/wordexp/glob.c | 108 | ||||
-rw-r--r-- | support/wordexp/glob.h | 19 | ||||
-rw-r--r-- | support/wordexp/wordexp.c | 88 |
4 files changed, 186 insertions, 31 deletions
diff --git a/support/wordexp/Makefile.am b/support/wordexp/Makefile.am index 92d133b5..44d5ad27 100644 --- a/support/wordexp/Makefile.am +++ b/support/wordexp/Makefile.am @@ -1,4 +1,4 @@ include $(top_srcdir)/Makefile.inc AM_CPPFLAGS = @NAVIT_CFLAGS@ -I$(top_srcdir)/navit -DMODULE=support_wordexp noinst_LTLIBRARIES = libsupport_wordexp.la -libsupport_wordexp_la_SOURCES = wordexp.c wordexp_init.c wordexp.h +libsupport_wordexp_la_SOURCES = wordexp.c wordexp_init.c wordexp.h glob.c glob.h diff --git a/support/wordexp/glob.c b/support/wordexp/glob.c new file mode 100644 index 00000000..3bb70de1 --- /dev/null +++ b/support/wordexp/glob.c @@ -0,0 +1,108 @@ +/** + * Navit, a modular navigation system. + * Copyright (C) 2005-2008 Navit Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * 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. + */ + +/* + * @file glob.c + */ + +#include <config.h> + +#ifndef HAVE_GLOB +#if defined _WIN32 || defined _WIN32_WCE +#include <windows.h> +#include "glob.h" + +/* + * @brief searches for all the pathnames matching pattern according to the rules + * which is similar to the rules used by common shells. + * here: expanding of ´*´ and ´?´ only in filenames + * @param pattern: no tilde expansion or parameter substitution is done. + * @param flags: not supported here + * @param errfunc: not supported here + * @param pglob: struct with array containing the matched files/directories + * @return FALSE on error. + */ +int glob(const char *pattern, int flags, + int (*errfunc)(const char *epath, int eerrno), + glob_t *pglob) +{ + char *pathend, + *filename; + int pathlen; + HANDLE hFiles; +#ifndef UNICODE + WIN32_FIND_DATA xFindData; + hFiles = FindFirstFile (pattern, &xFindData); +#else + int len = strlen(pattern) * sizeof(wchar_t*); + wchar_t *pathname = malloc(len); + WIN32_FIND_DATAW xFindData; + mbstowcs (pathname, pattern, len); + hFiles = FindFirstFile (pathname, &xFindData); +#endif + + if(hFiles == INVALID_HANDLE_VALUE) + { + return 1; + } + /* store the path information */ + if (NULL == (pathend = strrchr (pattern, '\\'))) /* windows */ + if (NULL == (pathend = strrchr (pattern, '/'))) /* UNIX in windows is sometimes allowed also*/ + pathend = (char *) pattern; + pathlen = pathend - pattern + 1; + + /* glob */ + pglob->gl_pathc = 0; /* number of founded files */ + pglob->gl_offs = 0; /* not needed */ + pglob->gl_pathv = malloc(sizeof(char*)); + + do + { + pglob->gl_pathc++; + pglob->gl_pathv = realloc (pglob->gl_pathv, pglob->gl_pathc * sizeof(char*)); +#ifndef UNICODE + filename = xFindData.cFileName; +#else + len = wcslen(xFindData.cFileName) * sizeof(char*); + filename = malloc (len); + wcstombs (filename, xFindData.cFileName, len); +#endif + pglob->gl_pathv[pglob->gl_pathc - 1] = malloc ((pathlen + strlen (filename) + 1) * sizeof(char*)); + strncpy (pglob->gl_pathv[pglob->gl_pathc - 1], pattern, pathlen); + strcpy (pglob->gl_pathv[pglob->gl_pathc - 1] + pathlen, filename); + } while (FindNextFile (hFiles, &xFindData)); + + FindClose(hFiles); + return 0; +} + +void globfree(glob_t *pglob) +{ + int i; + + for (i=0; i < pglob->gl_pathc; i++) + { + free (pglob->gl_pathv[i]); + } + free (pglob->gl_pathv); + pglob->gl_pathc = 0; +} + +#endif /* _WIN32 || _WIN32_WCE */ +#endif /* HAVE_GLOB */ diff --git a/support/wordexp/glob.h b/support/wordexp/glob.h new file mode 100644 index 00000000..052b76f4 --- /dev/null +++ b/support/wordexp/glob.h @@ -0,0 +1,19 @@ +#ifndef _GLOB_H_ +#define _GLOB_H_ + +#ifndef HAVE_GLOB +#ifdef __MINGW32__ + +typedef struct { + size_t gl_pathc; + char **gl_pathv; + size_t gl_offs; +} glob_t; + +int glob(const char *pattern, int flags, int (*errfunc)(const char *epath, int eerrno), glob_t *pglob); +void globfree(glob_t *pglob); + +#endif +#endif + +#endif /* _GLOB_H_ */ diff --git a/support/wordexp/wordexp.c b/support/wordexp/wordexp.c index 775bc78c..d4ad44a9 100644 --- a/support/wordexp/wordexp.c +++ b/support/wordexp/wordexp.c @@ -1,3 +1,6 @@ + +#include <config.h> + #include <sys/types.h> #include <assert.h> #include <fcntl.h> @@ -6,6 +9,7 @@ #include <string.h> #include <unistd.h> #include "wordexp.h" +#include "glob.h" static int @@ -19,6 +23,13 @@ is_valid_variable_char(char c, int pos) return 0; } +/* + * @brief replace all names of $NAME ${NAME} + * with the corresponding environment variable + * @ param in: the string to be checked + * @ return the expanded string or a copy of the existing string + * must be free() by the calling function +*/ static char * expand_variables(const char *in) { @@ -60,47 +71,64 @@ expand_variables(const char *in) return ret; } -int wordexp(const char * words, wordexp_t * we, int flags) -{ - int error=0; - char *words_expanded; +/* + * @brief minimal realization of wordexp according to IEEE standard + * shall perform word expansion as described in the Shell + * expansion of ´$NAME´ or ´${NAME}´ + * expansion of ´*´ and ´?´ + * @param words: pointer to a string containing one or more words to be expanded + * but here only one word supported + */ +int +wordexp(const char *words, wordexp_t *we, int flags) +{ + int i; + int error; + char *words_expanded; + glob_t pglob; assert(we != NULL); assert(words != NULL); - + + /* expansion of ´$NAME´ or ´${NAME}´ */ words_expanded=expand_variables(words); - we->we_wordc = 1; - we->we_wordv = NULL; - we->we_strings = NULL; - we->we_nbytes = 0; - - we->we_wordv = malloc( we->we_wordc * sizeof( char* ) ); - - we->we_nbytes = strlen( words_expanded ) + 1; - we->we_strings = malloc( we->we_nbytes ); - - we->we_wordv[0] = &we->we_strings[0]; + /* expansion of ´*´, ´?´ */ + error=glob(words_expanded, 0, NULL, &pglob); + if (!error) + { + /* copy the content of struct of glob into struct of wordexp */ + we->we_wordc = pglob.gl_pathc; + we->we_offs = pglob.gl_offs; + we->we_wordv = malloc(we->we_wordc * sizeof(char*)); + for (i=0; i<we->we_wordc; i++) + { + we->we_wordv[i] = strdup(pglob.gl_pathv[i]); + } + globfree(&pglob); + free(words_expanded); + } + else + { + we->we_wordc = 1; + we->we_wordv = malloc(sizeof(char*)); + we->we_wordv[0] = words_expanded; + } - // copy string & terminate - memcpy( we->we_strings, words_expanded, we->we_nbytes -1 ); - we->we_strings[ we->we_nbytes -1 ] = '\0'; - free(words_expanded); - return error; + return error; } + void wordfree(wordexp_t *we) { - assert(we != NULL); - - if ( we->we_wordv ) - free(we->we_wordv); - if ( we->we_strings ) - free(we->we_strings); + int i; - we->we_wordv = NULL; - we->we_strings = NULL; - we->we_nbytes = 0; + for (i=0; i < we->we_wordc; i++) + { + free (we->we_wordv[i]); + } + + free (we->we_wordv); we->we_wordc = 0; } |