1 // Copyright (C) 2016 James Buren
2 //
3 // This file is part of revdep.
4 //
5 // revdep is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // revdep is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with revdep. If not, see <http://www.gnu.org/licenses/>.
17
18 #include "utility.h"
19 #include <fstream>
20 #include <glob.h>
21 #include <sys/stat.h>
22
23 void split(const std::string &in, StringVector &out, char delimiter) {
24 size_t i = 0;
25 size_t j = in.find(delimiter);
26
27 while(j != std::string::npos) {
28 out.push_back(in.substr(i, j - i));
29 i = ++j;
30 j = in.find(delimiter, j);
31 }
32
33 out.push_back(in.substr(i));
34 }
35
36 void ReadRdConf(const std::string &path, StringVector &dirs) {
37 std::ifstream fin;
38
39 fin.open(path.c_str());
40
41 if(!fin.is_open()) {
42 return;
43 }
44
45 std::string line;
46
47 while(getline(fin, line)) {
48 if(line[0] != '#' && line.length() > 0) {
49 dirs.push_back(line);
50 }
51 }
52
53 fin.close();
54 }
55
56 bool ReadLdConf(const std::string &path, StringVector &dirs, int maxDepth) {
57 if(maxDepth <= 0) {
58 return false;
59 }
60
61 std::ifstream fin;
62
63 fin.open(path.c_str());
64
65 if(!fin.is_open()) {
66 return false;
67 }
68
69 std::string line;
70
71 while(getline(fin, line)) {
72 if(line[0] == '#') {
73 continue;
74 }
75
76 if(line.compare(0, 8, "include ") == 0) {
77 glob_t g;
78
79 if(glob(line.substr(8).c_str(), 0, NULL, &g) == 0) {
80 for( size_t i = 0 ; i < g.gl_pathc ; ++i ) {
81 if(!ReadLdConf(g.gl_pathv[i], dirs, maxDepth - 1)) {
82 globfree(&g);
83 fin.close();
84 return false;
85 }
86 }
87 }
88
89 globfree(&g);
90 } else if(line.length() > 0) {
91 dirs.push_back(line);
92 }
93 }
94
95 fin.close();
96
97 return true;
98 }
99
100 bool IsFile(const std::string &path) {
101 struct stat st;
102
103 if(lstat(path.c_str(), &st) == -1) {
104 return false;
105 }
106
107 return S_ISREG(st.st_mode);
108 }
|