-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscm-path.cpp
113 lines (88 loc) · 2.73 KB
/
scm-path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (C) 2011-2013 Robert Kooima
//
// LIBSCM is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITH-
// OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <sstream>
#include "scm-log.hpp"
#include "scm-path.hpp"
//------------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#define PATH_SEPARATOR '\\'
#define PATH_LIST_SEPARATOR ';'
#else
#define PATH_SEPARATOR '/'
#define PATH_LIST_SEPARATOR ':'
#endif
// Does the given path name an existing regular file?
static bool exists(const std::string& path)
{
#ifdef _WIN32
struct __stat64 info;
if (_stat64(path.c_str(), &info) == 0)
return ((info.st_mode & S_IFMT) == S_IFREG);
else
return false;
#else
struct stat info;
if (stat(path.c_str(), &info) == 0)
return ((info.st_mode & S_IFMT) == S_IFREG);
else
return false;
#endif
}
//------------------------------------------------------------------------------
/// Initialize an SCM path search manager
scm_path::scm_path()
{
if (char *val = getenv("SCMPATH"))
{
std::stringstream list(val);
std::string directory;
while (std::getline(list, directory, PATH_LIST_SEPARATOR))
directories.push_back(directory);
}
}
std::string scm_path::search(const std::string& file)
{
std::list<std::string>::iterator i;
// If the given file name is absolute, use it.
if (exists(file))
return file;
// Otherwise, search the SCM path for the file.
for (i = directories.begin(); i != directories.end(); ++i)
{
std::string path = (*i) + PATH_SEPARATOR + file;
if (exists(path))
{
scm_log("scm_path %s found at %s", file.c_str(), path.c_str());
return path;
}
}
// Return the empty string upon failure.
scm_log("* scm_path \"%s\" not found", file.c_str());
return std::string();
}
void scm_path::push(const std::string& dir)
{
directories.push_front(dir);
scm_log("scm_path push %s", directories.front().c_str());
}
void scm_path::pop()
{
assert(!directories.empty());
scm_log("scm_path pop %s", directories.front().c_str());
directories.pop_front();
}
//------------------------------------------------------------------------------