2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
27 #if defined _WIN32 || defined _WIN32_WCE
32 * @brief searches for all the pathnames matching pattern according to the rules
33 * which is similar to the rules used by common shells.
34 * here: expanding of ´*´ and ´?´ only in filenames
35 * @param pattern: no tilde expansion or parameter substitution is done.
36 * @param flags: not supported here
37 * @param errfunc: not supported here
38 * @param pglob: struct with array containing the matched files/directories
39 * @return FALSE on error.
41 int glob(const char *pattern, int flags,
42 int (*errfunc)(const char *epath, int eerrno),
50 WIN32_FIND_DATA xFindData;
51 hFiles = FindFirstFile (pattern, &xFindData);
53 int len = strlen(pattern) * sizeof(wchar_t*);
54 wchar_t *pathname = malloc(len);
55 WIN32_FIND_DATAW xFindData;
56 mbstowcs (pathname, pattern, len);
57 hFiles = FindFirstFile (pathname, &xFindData);
60 if(hFiles == INVALID_HANDLE_VALUE)
64 /* store the path information */
65 if (NULL == (pathend = max (strrchr (pattern, '\\'), strrchr (pattern, '/'))))
66 pathend = (char *) pattern;
67 pathlen = pathend - pattern + 1;
70 pglob->gl_pathc = 0; /* number of founded files */
71 pglob->gl_offs = 0; /* not needed */
72 pglob->gl_pathv = malloc(sizeof(char*)); /* list of file names */
77 pglob->gl_pathv = realloc (pglob->gl_pathv, pglob->gl_pathc * sizeof(char*));
79 filename = xFindData.cFileName;
81 len = wcslen(xFindData.cFileName) * sizeof(char*);
82 filename = malloc (len);
83 wcstombs (filename, xFindData.cFileName, len);
85 pglob->gl_pathv[pglob->gl_pathc - 1] = malloc ((pathlen + strlen (filename) + 1) * sizeof(char*));
86 strncpy (pglob->gl_pathv[pglob->gl_pathc - 1], pattern, pathlen);
87 // strcpy (pglob->gl_pathv[pglob->gl_pathc - 1] + pathlen - 1, filename);
88 // The above line should be uncommented later. Currently, the blow line needs to be in use.
89 // If not, navit on WinCE / Win32 cannot "find" the maps and bookmarks folder
90 strcpy (pglob->gl_pathv[pglob->gl_pathc - 1] + pathlen, filename);
91 } while (FindNextFile (hFiles, &xFindData));
105 glob_requires_match(const char *pattern, int flags)
108 switch (*pattern++) {
124 glob_recursive(const char *path1, const char *path2, const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob)
127 char *fname,*path=malloc(strlen(path1)+strlen(path2)+2);
130 if (path1[0] && path2[0] && (path1[1] != '\0' || path1[0] != '/'))
133 if (!strlen(pattern)) {
134 dbg(0,"found %s\n",path);
135 pglob->gl_pathv=realloc(pglob->gl_pathv, (pglob->gl_pathc+1)*sizeof(char *));
136 if (!pglob->gl_pathv) {
140 pglob->gl_pathv[pglob->gl_pathc++]=path;
143 dbg(0,"searching for %s in %s\n",pattern,path);
144 flen=strcspn(pattern,"/");
148 fname=malloc(flen+1);
149 strncpy(fname, pattern, flen);
151 if (glob_requires_match(fname, 0)) {
154 dbg(0,"in dir %s search for %s\n",path,fname);
157 while ((de=readdir(dh))) {
158 if (fnmatch(fname,de->d_name,0) == 0) {
159 glob_recursive(path, de->d_name, next, flags, errfunc, pglob);
165 glob_recursive(path, fname, next, flags, errfunc, pglob);
173 glob(const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob)
176 pglob->gl_pathv=NULL;
177 if (pattern[0] == '/')
178 return glob_recursive("/", "", pattern+1, flags, errfunc, pglob);
180 return glob_recursive("", "", pattern, flags, errfunc, pglob);
183 #endif /* _WIN32 || _WIN32_WCE */
185 void globfree(glob_t *pglob)
189 for (i=0; i < pglob->gl_pathc; i++)
191 free (pglob->gl_pathv[i]);
193 free (pglob->gl_pathv);
197 #endif /* HAVE_GLOB */