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