~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/file/fileUtil_name.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileUtil_name.c,v $
 37  * Date    :  $Date: 2005/06/01 20:01:42 $
 38  * Version :  $Revision: 1.7 $
 39  * CVS Id  :  $Id: fileUtil_name.c,v 1.7 2005/06/01 20:01:42 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  *            file_name.c
 45  *
 46  *            functions to parse and match filenames to introduce integers and
 47  *            wildcards
 48  *
 49  *********
 50 */
 51 
 52 #include "fileUtil_name.h"
 53 
 54 #if HAVE_CONFIG_H
 55   #include <config.h>
 56 #endif
 57 
 58 #include <stdio.h>
 59 #include <dirent.h>
 60 #ifdef HAVE_FNMATCH
 61 #include <fnmatch.h>
 62 #endif
 63 #include <string.h>
 64 #include <sys/param.h>
 65 #include <tina/sys/sysDef.h>
 66 #include <tina/sys/sysPro.h>
 67 #include <tina/file/fileUtil_util.h>
 68 
 69 static char *wcard;
 70 
 71 #ifndef MINGW /* most of this won't build with mingw */
 72 
 73 #ifndef HAVE_SCANDIR
 74 
 75 #include <sys/types.h>
 76 #include <stdlib.h>
 77 #include <limits.h>
 78 
 79 /*
 80  * BSD functions alphasort and scandir not supported by some OS's such as SOLARIS 
 81  */
 82 
 83 int alphasort(const void *d1, const void *d2)
 84 {
 85     return(strcmp((*(struct dirent **)d1)->d_name, (*(struct dirent **)d2)->d_name));
 86 }
 87 
 88 
 89 int scandir(char *dirname, struct dirent *(*namelist[]), int (*select)(struct dirent *), int (*dcomp)())
 90 {
 91   DIR *dirp;
 92   struct dirent *res;
 93   static struct dirent *tdir[256];
 94   int tdirsize = sizeof(struct dirent);
 95   register int i=0;
 96 
 97   if ((dirp = opendir(dirname)) == NULL)
 98     return -1;
 99 
100 /* 256 should be big enough for a spool directory of any size 
101  * big enough for 128 jobs anyway.
102  */
103   if ((*namelist = (struct dirent **) calloc(256, sizeof(struct dirent *))) == NULL)
104         {
105                 closedir(dirp);
106     return -1;
107         }
108 
109   if ((res = (struct dirent *) malloc(tdirsize + _POSIX_PATH_MAX)) == NULL)
110         {
111                 closedir(dirp);
112     return -1;
113         }
114 
115   while (readdir_r(dirp, res) != NULL) {
116     if (select(res)) {
117       if (((*namelist)[i] = (struct dirent *) malloc(tdirsize + _POSIX_PATH_MAX)) == NULL)
118                         {
119                                 closedir(dirp);
120         return -1;
121                         }
122     memcpy((*namelist)[i], res, sizeof(res)+_POSIX_PATH_MAX);
123     i++;
124     }
125   }
126 
127   if (dcomp != NULL)
128     qsort((char *) &((*namelist)[0]), i, sizeof(struct dirent *), dcomp);
129 
130         closedir(dirp);
131   return i;
132 }
133 #endif /* !HAVE_SCANDIR */
134 
135 
136 static int scandir_sel(struct dirent *ent)
137 {
138    if (wcard)
139      return(!fnmatch(wcard, ent->d_name, 0));
140    return(0);
141  }
142 
143 #endif /* MINGW */
144 
145 int fname_resolve(char *pdir, char *pat, char **fname)
146 {
147 #ifndef MINGW
148    struct dirent **namelist;
149    int    n;
150    char   dir[MAXPATHLEN];
151 
152    if (*fname == NULL)
153      return(0);
154    if (pdir == NULL)
155      dirname(dir, *fname); 
156    else
157      (void)strncpy(dir, pdir, MAXPATHLEN);
158    if(!(wcard = strrchr(pat, '/')))
159       wcard = pat;
160    else
161       wcard++;
162    if ((n = scandir(dir, &namelist, (int (*)(struct dirent *))(scandir_sel), alphasort)) < 1)
163      return(0);
164 
165    string_append(*fname, dir, "/", namelist[0]->d_name, NULL);
166    return(1);
167 #else
168          return(0);
169 #endif /* !MINGW */
170 }
171 
172 
173 void parse_fname(char *bname, char *fname, int fnum)
174 {
175    int nWidth = 1, tmpVal;
176    char filePrepend[1024], fileAppend[1024];
177 
178   if (bname != NULL) 
179     strcpy(fname, bname);
180 
181   if(strchr(fname, '?') != NULL)
182   {
183     tmpVal = strcspn(fname, "?");
184     strncpy(filePrepend, fname, tmpVal); filePrepend[tmpVal]='\0';
185     nWidth = strspn(&fname[tmpVal], "?");
186     strcpy(fileAppend, &fname[tmpVal+nWidth]);
187 
188     sprintf(fname, "%s%0*d%s", filePrepend, nWidth, fnum, fileAppend);
189   }
190   else if (strchr(fname, '#') != NULL)
191   {
192     tmpVal = strcspn(fname, "#");
193     strncpy(filePrepend, fname, tmpVal); filePrepend[tmpVal]='\0';
194     nWidth = strspn(&fname[tmpVal], "#");
195     strcpy(fileAppend, &fname[tmpVal+nWidth]);
196 
197     sprintf(fname, "%s%0*d%s", filePrepend, nWidth, fnum, fileAppend);
198   }
199 
200   return;
201 }
202 
203 
204 
205 /* old version
206 void parse_fname(char *bname, char *fname, int fnum)
207 {
208   char *ptr1, *ptr2;
209   char numstr[256];
210   char conststr[256];
211   int length;
212 
213   if (bname != NULL) 
214     strcpy(fname, bname);
215 
216   ptr2 = ptr1 = strchr(fname, '?');
217   if (ptr1 != NULL)
218   {
219     while (ptr1)
220     {
221       while(*(++ptr2) == '?');
222       length = (int)(ptr2 - ptr1);
223 
224       sprintf(conststr, "%%0%dd", length);
225       sprintf(numstr, conststr, fnum);
226 
227       strncpy(ptr1, numstr, length);
228       ptr2 = ptr1 = strchr(fname, '?');
229     }
230   }
231   else
232   {
233     ptr2 = ptr1 = strchr(fname, '#');
234     while(ptr1)
235     {
236       while(*(++ptr2) == '#');
237       length = (int)(ptr2 - ptr1);
238 
239       sprintf(conststr, "%%0%dd", length);
240       sprintf(numstr, conststr, fnum);
241 
242       strncpy(ptr1, numstr, length);
243       ptr2 = ptr1 = strchr(fname, '#');
244     }
245   }
246 
247   return;
248 }
249 */
250 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.