1 /*$Id: io_findf.cc,v 26.81 2008/05/27 05:34:00 al Exp $ -*- C++ -*-
2  * Copyright (C) 2001 Albert Davis
3  * Author: Albert Davis <aldavis@gnu.org>
4  *
5  * This file is part of "Gnucap", the Gnu Circuit Analysis Package
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  *------------------------------------------------------------------
22  * Modified by AD.  Sent to me by C-WARE
23  * This file contains the routine to locate a file,
24  *	using a path string for the directories to search.
25  * Interface:
26  *	findfile(filename, paths, mode)
27  *	    filename is the name of the file to be searched for,
28  *	    paths is the path to follow to find it.
29  *	    mode is how you want to open the file
30  *	returns full path name, if successful, else "".
31  *
32  * PATHSEP, ENDDIR are system dependent, defined in md.h
33  */
34 //testing=script,sparse 2006.07.17
35 #include "l_lib.h"
36 /*--------------------------------------------------------------------------*/
findfile(const std::string & filename,const std::string & path,int mode)37 std::string findfile(const std::string& filename, const std::string& path,
38 		     int mode)
39 {
40 #ifdef CHECK_LOCAL_FIRST
41   if (OS::access_ok(filename, mode)) {
42     untested();
43     return filename;
44   }else{
45     untested();
46   }
47 #endif
48 					// for each item in the path
49   for (std::string::const_iterator
50 	 p_ptr=path.begin(); p_ptr!=path.end(); ++p_ptr) {
51     // p_ptr changed internally                  ^^^^^ skip sep
52     std::string target = "";
53     while (*p_ptr != PATHSEP  &&  p_ptr != path.end()) { // copy 1 path item
54       target += *p_ptr++;
55     }
56     if (!target.empty() &&  !strchr(ENDDIR,p_ptr[-1])) {
57       target += *ENDDIR;		// append '/' if needed
58     }else{untested();
59     }
60 
61     target += filename;
62     if (OS::access_ok(target, mode)) {untested();	// found it
63       return target;
64     }else if (p_ptr==path.end()) {	// ran out of path, didn't find it
65       return "";
66     }else{				// else try again
67     }
68   }
69   return ""; // path doesn't exist - didn't go thru loop at all
70 }
71 /*--------------------------------------------------------------------------*/
72 /*--------------------------------------------------------------------------*/
73