]> git.tdb.fi Git - libs/core.git/blob - source/fs/utils.cpp
Remove an unnecessary #include
[libs/core.git] / source / fs / utils.cpp
1 #include <cstdio>
2 #include <msp/core/systemerror.h>
3 #include <msp/strings/utils.h>
4 #include "dir.h"
5 #include "path.h"
6 #include "stat.h"
7 #include "utils.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace FS {
13
14 string basename(const Path &p)
15 {
16         return p[-1];
17 }
18
19 Path dirname(const Path &p)
20 {
21         if(p.size()==1)
22         {
23                 if(p.is_absolute())
24                         return p;
25                 return ".";
26         }
27         return p.subpath(0, p.size()-1);
28 }
29
30 string basepart(const string &fn)
31 {
32         unsigned dot = fn.rfind('.');
33         return fn.substr(0, dot);
34 }
35
36 string extpart(const string &fn)
37 {
38         string::size_type dot = fn.rfind('.');
39         if(dot==string::npos)
40                 return string();
41         return fn.substr(dot);
42 }
43
44 Path fix_case(const Path &path)
45 {
46         bool found = true;
47         Path result;
48         for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
49         {
50                 if(!found || *i=="/")
51                         result /= *i;
52                 else
53                 {
54                         list<string> files;
55                         if(result.size())
56                                 files = list_files(result);
57                         else
58                                 files = list_files(".");
59
60                         found = false;
61                         for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
62                                 if(!strcasecmp(*j,*i))
63                                 {
64                                         result /= *j;
65                                         found = true;
66                                 }
67
68                         if(!found)
69                                 result /= *i;
70                 }
71         }
72
73         return result;
74 }
75
76 Path readlink(const Path &link)
77 {
78 #ifdef WIN32
79         (void)link;
80         throw logic_error("no symbolic links on win32");
81 #else
82         char buf[4096];
83         int len = ::readlink(link.str().c_str(), buf, sizeof(buf));
84         if(len==-1)
85                 throw system_error("readlink");
86         return string(buf, len);
87 #endif
88 }
89
90 Path realpath(const Path &path)
91 {
92 #ifdef WIN32
93         if(path.is_absolute())
94                 return path;
95         else
96                 return getcwd()/path;
97 #else
98         list<string> queue(path.begin(), path.end());
99         if(!path.is_absolute())
100         {
101                 Path cwd = getcwd();
102                 queue.insert(queue.begin(), cwd.begin(), cwd.end());
103         }
104
105         Path real;
106         unsigned n_links = 0;
107         while(!queue.empty())
108         {
109                 Path next = real/queue.front();
110                 queue.pop_front();
111
112                 struct stat st = lstat(next);
113                 if(S_ISLNK(st.st_mode))
114                 {
115                         if(++n_links>64)
116                                 throw runtime_error("too many symbolic links");
117                         Path link = readlink(next);
118                         queue.insert(queue.begin(), link.begin(), link.end());
119                 }
120                 else
121                         real = next;
122         }
123
124         return real;
125 #endif
126 }
127
128 void rename(const Path &from, const Path &to)
129 {
130         if(::rename(from.str().c_str(), to.str().c_str())==-1)
131                 throw system_error("rename");
132 }
133
134 void unlink(const Path &path)
135 {
136         if(::unlink(path.str().c_str())==-1)
137                 throw system_error("unlink");
138 }
139
140 Path relative(const Path &path, const Path &base)
141 {
142         Path::Iterator i = path.begin();
143         Path::Iterator j = base.begin();
144         for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
145
146         Path result;
147         for(; j!=base.end(); ++j)
148                 result /= "..";
149         for(; i!=path.end(); ++i)
150                 result /= *i;
151
152         return result;
153 }
154
155 int descendant_depth(const Path &path, const Path &parent)
156 {
157         Path::Iterator i = path.begin();
158         Path::Iterator j = parent.begin();
159         for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ;
160
161         if(j!=parent.end())
162                 return -1;
163         
164         int result = 0;
165         for(; i!=path.end(); ++i)
166                 ++result;
167
168         return result;
169 }
170
171 } // namespace FS
172 } // namespace Msp