]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
fd69d379d5a4e42b0aec4af31493ea0dfc9d7e43
[lilypond.git] / flower / file-name.cc
1 /*
2   file-name.cc - implement File_name
3
4   source file of the Flower Library
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "file-name.hh"
11
12 #include <cstdio>
13 #include <cerrno>
14 #include <unistd.h>
15
16 using namespace std;
17
18 #include "config.hh"
19
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23
24 #ifdef __CYGWIN__
25 #include <sys/cygwin.h>
26 #endif
27
28 #ifndef ROOTSEP
29 #define ROOTSEP ':'
30 #endif
31
32 #ifndef DIRSEP
33 #define DIRSEP '/'
34 #endif
35
36 #ifndef EXTSEP
37 #define EXTSEP '.'
38 #endif
39
40 #ifdef __CYGWIN__
41 static string
42 dos_to_posix (string file_name)
43 {
44   char buf[PATH_MAX] = "";
45   char s[PATH_MAX];
46   file_name.copy (s, PATH_MAX - 1);
47   /* ugh: char const* argument gets modified.  */
48   int fail = cygwin_conv_to_posix_path (s, buf);
49   if (!fail)
50     return buf;
51   return file_name;
52 }
53 #endif /* __CYGWIN__ */
54
55 /** Use slash as directory separator.  On Windows, they can pretty
56     much be exchanged.  */
57 #if 0
58 static /* avoid warning */
59 #endif 
60 string
61 slashify (string file_name)
62 {
63   replace_all (file_name, '\\', '/');
64   replace_all (file_name, string ("//"), "/");
65   return file_name;
66 }
67
68 string
69 dir_name (string const file_name)
70 {
71   string s = file_name;
72   s = slashify (s);
73   ssize n = s.length ();
74   if (n && s[n - 1] == '/')
75     s[n - 1] = 0;
76   if (s.rfind ('/') != NPOS)
77     s = s.substr (0, s.rfind ('/'));
78   else
79     s = "";
80   
81   return s;
82 }
83
84 string
85 get_working_directory ()
86 {
87   char cwd[PATH_MAX];
88   getcwd (cwd, PATH_MAX);
89
90   return string (cwd);
91 }
92
93 /* Join components to full file_name. */
94 string
95 File_name::dir_part () const
96 {
97   string s;
98   if (!root_.empty ())
99     s = root_ + ::to_string (ROOTSEP);
100
101   if (!dir_.empty ())
102     {
103       s += dir_;
104     }
105
106   return s;
107 }
108
109
110 string
111 File_name::file_part () const
112 {
113   string s;
114   s = base_;
115   if (!ext_.empty ())
116     s += ::to_string (EXTSEP) + ext_;
117   return s;
118 }
119
120 string
121 File_name::to_string () const
122 {
123   string d = dir_part ();
124   string f = file_part ();
125
126   if (!f.empty ()
127       && !dir_.empty())
128     {
129       d += ::to_string (DIRSEP);
130     }
131
132   return d + f;
133 }
134
135 File_name::File_name (string file_name)
136 {
137 #ifdef __CYGWIN__
138   /* All system functions would work, even if we do not convert to
139      posix file_name, but we would think that \foe\bar\baz.ly is in
140      the cwd.  */
141   file_name = dos_to_posix (file_name);
142 #endif
143 #ifdef __MINGW32__
144   file_name = slashify (file_name);
145 #endif
146
147   ssize i = file_name.find (ROOTSEP);
148   if (i != NPOS)
149     {
150       root_ = file_name.substr (0, i);
151       file_name = file_name.substr (i + 1);
152     }
153
154   i = file_name.rfind (DIRSEP);
155   if (i != NPOS)
156     {
157       dir_ = file_name.substr (0, i);
158       file_name = file_name.substr (i + 1);
159     }
160
161   i = file_name.rfind ('.');
162   if (i != NPOS)
163     {
164       base_ = file_name.substr (0, i);
165       ext_ = file_name.substr (i + 1);
166     }
167   else
168     base_ = file_name;
169 }
170
171 bool
172 File_name::is_absolute () const
173 {
174   /*
175     Hmm. Is c:foo absolute?  
176    */
177   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
178 }
179