a28ba4539721ca2f7832c695fc20fad29aa3de0e
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / SearchPath.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $XConsortium: SearchPath.C /main/7 1996/08/21 15:47:21 drk $ */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #if !defined(USL)
28 #include <strings.h>
29 #endif
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sstream>
33 using namespace std;
34
35 #include "Exceptions.hh"
36 #include "Task.h"
37 #include "api/utility.h"
38
39 /* exported interface */
40 #include "SearchPath.h"
41
42 #ifdef NEED_STRCASECMP
43 #include <ctype.h>
44 /*
45  * In case strcasecmp and strncasecmp are not provided by the system
46  * here are ones which do the trick.
47  */
48
49 int
50 strcasecmp(register const char *s1,
51            register const char *s2)
52 {
53     register int c1, c2;
54
55     while (*s1 && *s2) {
56         c1 = isupper(*s1) ? tolower(*s1) : *s1;
57         c2 = isupper(*s2) ? tolower(*s2) : *s2;
58         if (c1 != c2)
59             return (c1 - c2);
60         s1++;
61         s2++;
62     }
63     return (int) (*s1 - *s2);
64 }
65
66
67 int
68 strncasecmp(register const char *s1,
69             register const char *s2,
70             register size_t count)
71 {
72     register int c1, c2;
73
74     if (!count)
75       return 0;
76
77     while (*s1 && *s2) {
78         c1 = isupper(*s1) ? tolower(*s1) : *s1;
79         c2 = isupper(*s2) ? tolower(*s2) : *s2;
80         if ((c1 != c2) || (! --count))
81             return (c1 - c2);
82         s1++;
83         s2++;
84     }
85     return (int) (*s1 - *s2);
86 }
87 #endif
88
89
90 //--------------------------------------------------------------------
91 static int isdir(char* filename)
92 {
93   int ret = 0;
94   struct stat sb;
95
96   if(stat(filename, &sb) == 0){
97     if(S_ISDIR(sb.st_mode)){
98       ret = 1;
99     }
100   }
101
102   return ret;
103 }
104
105   
106 //-------------------------------------------------------------------
107 SearchPath::SearchPath( const char *path, ... )
108 {
109   
110   search_path_table = new CC_TPtrSlist<CC_String>;
111   new_path = 0;
112
113   va_list ap;
114   va_start ( ap , path);
115
116   const char *spath = path;
117   while ( spath ) {
118     CC_String *key = new CC_String(spath);
119     search_path_table->append( key );
120     spath = va_arg ( ap, const char * );
121   }
122   
123   va_end ( ap );
124 }
125
126 //-------------------------------------------------------------------
127 char *
128 SearchPath::get_real_path( const char *file_name )
129 {
130   CC_TPtrSlistIterator<CC_String> path_it( *search_path_table );
131   FILE *fp = NULL;
132
133   if (file_name == NULL || *file_name == '\0')
134     return NULL;
135
136   // remove storage object specifier
137   if (strncasecmp(file_name, "<OSFILE", 7) == 0) {
138     if ((file_name = strchr(file_name, '>')))
139       file_name++;
140     else
141       return NULL;
142   }
143
144   while ( path_it() ) {
145     const char *path = (const char *)*path_it.key();
146
147     char *full_path_name = form( "%s/%s", path, file_name );
148     if (( fp = fopen( full_path_name , "r" )) && !isdir(full_path_name) ) {
149       fclose( fp );
150       return ( full_path_name );
151     }
152     if(fp) {
153       fclose( fp );
154     }
155   }
156
157   return NULL;
158 }
159
160
161 //-------------------------------------------------------------------    
162 void
163 SearchPath::replace_file_scope( const char *f_path )
164 {
165   if ( new_path ) {
166     if ( !search_path_table->remove( new_path ) ) {
167       throw(Unexpected("Cannot replace file scope\n"));
168     }
169     delete new_path; new_path = 0;
170   }
171
172   new_path = new CC_String(f_path);
173
174   search_path_table->prepend( new_path );
175
176 }
177