2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: SearchPath.C /main/7 1996/08/21 15:47:21 drk $ */
27 #if !defined(__uxp__) && !defined(USL)
31 #include <sys/types.h>
35 #include "Exceptions.hh"
37 #include "api/utility.h"
39 /* exported interface */
40 #include "SearchPath.h"
42 #ifdef NEED_STRCASECMP
45 * In case strcasecmp and strncasecmp are not provided by the system
46 * here are ones which do the trick.
50 strcasecmp(register const char *s1,
51 register const char *s2)
56 c1 = isupper(*s1) ? tolower(*s1) : *s1;
57 c2 = isupper(*s2) ? tolower(*s2) : *s2;
63 return (int) (*s1 - *s2);
68 strncasecmp(register const char *s1,
69 register const char *s2,
70 register size_t count)
78 c1 = isupper(*s1) ? tolower(*s1) : *s1;
79 c2 = isupper(*s2) ? tolower(*s2) : *s2;
80 if ((c1 != c2) || (! --count))
85 return (int) (*s1 - *s2);
90 //--------------------------------------------------------------------
91 static int isdir(char* filename)
96 if(stat(filename, &sb) == 0){
97 if(S_ISDIR(sb.st_mode)){
106 //-------------------------------------------------------------------
107 SearchPath::SearchPath( const char *path, ... )
110 search_path_table = new CC_TPtrSlist<CC_String>;
114 va_start ( ap , path);
116 const char *spath = path;
118 CC_String *key = new CC_String(spath);
119 search_path_table->append( key );
120 spath = va_arg ( ap, const char * );
126 //-------------------------------------------------------------------
128 SearchPath::get_real_path( const char *file_name )
130 CC_TPtrSlistIterator<CC_String> path_it( *search_path_table );
133 if (file_name == NULL || *file_name == '\0')
136 // remove storage object specifier
137 if (strncasecmp(file_name, "<OSFILE", 7) == 0) {
138 if ((file_name = strchr(file_name, '>')))
144 while ( path_it() ) {
145 const char *path = (const char *)*path_it.key();
147 char *full_path_name = form( "%s/%s", path, file_name );
148 if (( fp = fopen( full_path_name , "r" )) && !isdir(full_path_name) ) {
150 return ( full_path_name );
161 //-------------------------------------------------------------------
163 SearchPath::replace_file_scope( const char *f_path )
166 if ( !search_path_table->remove( new_path ) ) {
167 throw(Unexpected("Cannot replace file scope\n"));
169 delete new_path; new_path = 0;
172 new_path = new CC_String(f_path);
174 search_path_table->prepend( new_path );