]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
e5dd6f61602369773f53406d56bc4bd1ef12c4fa
[lilypond.git] / flower / file-name.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond 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 of the License, or
10   (at your option) any later version.
11
12   LilyPond 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 LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "file-name.hh"
22
23 #include <cstdio>
24 #include <cerrno>
25 #include <unistd.h>
26 #include <limits.h>
27
28 using namespace std;
29
30 #include "config.hh"
31
32 #if HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35
36 #ifndef ROOTSEP
37 #define ROOTSEP ':'
38 #endif
39
40 #ifndef DIRSEP
41 #define DIRSEP '/'
42 #endif
43
44 #ifndef EXTSEP
45 #define EXTSEP '.'
46 #endif
47
48 /** Use slash as directory separator.  On Windows, they can pretty
49     much be exchanged.  */
50 #if 0
51 static /* avoid warning */
52 #endif
53 string
54 slashify (string file_name)
55 {
56   replace_all (&file_name, '\\', '/');
57   replace_all (&file_name, string ("//"), "/");
58   return file_name;
59 }
60
61 string
62 dir_name (const string &file_name)
63 {
64   string s = file_name;
65   s = slashify (s);
66   ssize n = s.length ();
67   if (n && s[n - 1] == '/')
68     s[n - 1] = 0;
69   if (s.rfind ('/') != NPOS)
70     s = s.substr (0, s.rfind ('/'));
71   else
72     s = "";
73
74   return s;
75 }
76
77 string
78 get_working_directory ()
79 {
80 #ifdef _GNU_SOURCE
81   char *cwd = get_current_dir_name();
82   string scwd(cwd);
83   free(cwd);
84   return scwd;
85 #else
86   char cwd[PATH_MAX];
87   // getcwd returns NULL upon a failure, contents of cwd would be undefined!
88   return string (getcwd (cwd, PATH_MAX));
89 #endif
90 }
91
92 /* Join components to full file_name. */
93 string
94 File_name::dir_part () const
95 {
96   string s;
97   if (!root_.empty ())
98     s = root_ + ::to_string (ROOTSEP);
99
100   if (!dir_.empty ())
101     {
102       s += dir_;
103     }
104
105   return s;
106 }
107
108 string
109 File_name::file_part () const
110 {
111   string s;
112   s = base_;
113   if (!ext_.empty ())
114     s += ::to_string (EXTSEP) + ext_;
115   return s;
116 }
117
118 string
119 File_name::to_string () const
120 {
121   string d = dir_part ();
122   string f = file_part ();
123
124   if (!f.empty ()
125       && !dir_.empty ())
126     {
127       d += ::to_string (DIRSEP);
128     }
129
130   return d + f;
131 }
132
133 File_name::File_name (string file_name)
134 {
135 #ifdef __MINGW32__
136   file_name = slashify (file_name);
137 #endif
138
139   ssize i = file_name.find (ROOTSEP);
140   if (i != NPOS)
141     {
142       root_ = file_name.substr (0, i);
143       file_name = file_name.substr (i + 1);
144     }
145
146   i = file_name.rfind (DIRSEP);
147   if (i != NPOS)
148     {
149       dir_ = file_name.substr (0, i);
150       file_name = file_name.substr (i + 1);
151     }
152
153   i = file_name.rfind ('.');
154   if (i != NPOS)
155     {
156       base_ = file_name.substr (0, i);
157       ext_ = file_name.substr (i + 1);
158     }
159   else
160     base_ = file_name;
161 }
162
163 bool
164 File_name::is_absolute () const
165 {
166   /*
167     Hmm. Is c:foo absolute?
168    */
169   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
170 }
171
172 File_name
173 File_name::canonicalized () const
174 {
175   File_name c = *this;
176
177   replace_all (&c.dir_, string ("//"), string ("/"));
178
179   vector<string> components = string_split (c.dir_, '/');
180   vector<string> new_components;
181
182   for (vsize i = 0; i < components.size (); i++)
183     {
184       if (components[i] == "..")
185         new_components.pop_back ();
186       else
187         new_components.push_back (components[i]);
188     }
189
190   c.dir_ = string_join (new_components, "/");
191   return c;
192 }