]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
Web-ja: update introduction
[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 PATH_MAX
81   vector<char> cwd (PATH_MAX);
82 #else
83   vector<char> cwd (1024);
84 #endif
85   while (getcwd (cwd.data (), cwd.size ()) == NULL)
86     {
87       if (errno != ERANGE)
88         {
89           // getcwd () fails.
90           return "";
91         }
92       cwd.resize (cwd.size () * 2);
93     }
94   return string (cwd.data ());
95 }
96
97 /* Join components to full file_name. */
98 string
99 File_name::dir_part () const
100 {
101   string s;
102   if (!root_.empty ())
103     s = root_ + ::to_string (ROOTSEP);
104
105   if (!dir_.empty ())
106     {
107       s += dir_;
108     }
109
110   return s;
111 }
112
113 string
114 File_name::file_part () const
115 {
116   string s;
117   s = base_;
118   if (!ext_.empty ())
119     s += ::to_string (EXTSEP) + ext_;
120   return s;
121 }
122
123 string
124 File_name::to_string () const
125 {
126   string d = dir_part ();
127   string f = file_part ();
128
129   if (!f.empty ()
130       && !dir_.empty ())
131     {
132       d += ::to_string (DIRSEP);
133     }
134
135   return d + f;
136 }
137
138 File_name::File_name (string file_name)
139 {
140 #ifdef __MINGW32__
141   file_name = slashify (file_name);
142 #endif
143
144   ssize i = file_name.find (ROOTSEP);
145   if (i != NPOS)
146     {
147       root_ = file_name.substr (0, i);
148       file_name = file_name.substr (i + 1);
149     }
150
151   i = file_name.rfind (DIRSEP);
152   if (i != NPOS)
153     {
154       dir_ = file_name.substr (0, i);
155       file_name = file_name.substr (i + 1);
156     }
157
158   i = file_name.rfind ('.');
159   if (i != NPOS)
160     {
161       base_ = file_name.substr (0, i);
162       ext_ = file_name.substr (i + 1);
163     }
164   else
165     base_ = file_name;
166 }
167
168 bool
169 File_name::is_absolute () const
170 {
171   /*
172     Hmm. Is c:foo absolute?
173    */
174   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
175 }
176
177 File_name
178 File_name::canonicalized () const
179 {
180   File_name c = *this;
181
182   replace_all (&c.dir_, string ("//"), string ("/"));
183
184   vector<string> components = string_split (c.dir_, '/');
185   vector<string> new_components;
186
187   for (vsize i = 0; i < components.size (); i++)
188     {
189       if (components[i] == "..")
190         new_components.pop_back ();
191       else
192         new_components.push_back (components[i]);
193     }
194
195   c.dir_ = string_join (new_components, "/");
196   return c;
197 }