]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
(is_absolute): new method.
[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--2005 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 using namespace std;
15
16 #include "config.hh"
17
18 #if HAVE_SYS_STAT_H
19 #include <sys/stat.h>
20 #endif
21
22 #ifdef __CYGWIN__
23 #include <sys/cygwin.h>
24 #endif
25
26 #ifndef ROOTSEP
27 #define ROOTSEP ':'
28 #endif
29
30 #ifndef DIRSEP
31 #define DIRSEP '/'
32 #endif
33
34 #ifndef EXTSEP
35 #define EXTSEP '.'
36 #endif
37
38 #ifdef __CYGWIN__
39 static String
40 dos_to_posix (String file_name)
41 {
42   char buf[PATH_MAX] = "";
43   char *s = file_name.get_copy_str0 ();
44   /* ugh: char const* argument gets modified.  */
45   int fail = cygwin_conv_to_posix_path (s, buf);
46   delete s;
47   if (!fail)
48     return buf;
49   return file_name;
50 }
51 #endif /* __CYGWIN__ */
52
53 #ifdef __MINGW32__
54 /** Use slash as directory separator.  On Windows, they can pretty
55     much be exchanged.  */
56 static String
57 slashify (String file_name)
58 {
59   file_name.substitute ('\\', '/');
60   file_name.substitute ("//", "/");
61   return file_name;
62 }
63 #endif /* __MINGW32__ */
64
65 /* Join components to full file_name. */
66 String
67 File_name::to_string () const
68 {
69   String s;
70   if (!root_.is_empty ())
71     s = root_ + ::to_string (ROOTSEP);
72   if (!dir_.is_empty ())
73     {
74       s += dir_;
75       if (!base_.is_empty () || !ext_.is_empty ())
76         s += ::to_string (DIRSEP);
77     }
78   s += base_;
79   if (!ext_.is_empty ())
80     s += ::to_string (EXTSEP) + ext_;
81   return s;
82 }
83
84 File_name::File_name (String file_name)
85 {
86 #ifdef __CYGWIN__
87   /* All system functions would work, even if we do not convert to
88      posix file_name, but we would think that \foe\bar\baz.ly is in
89      the cwd.  */
90   file_name = dos_to_posix (file_name);
91 #endif
92 #ifdef __MINGW32__
93   file_name = slashify (file_name);
94 #endif
95
96   int i = file_name.index (ROOTSEP);
97   if (i >= 0)
98     {
99       root_ = file_name.left_string (i);
100       file_name = file_name.right_string (file_name.length () - i - 1);
101     }
102
103   i = file_name.index_last (DIRSEP);
104   if (i >= 0)
105     {
106       dir_ = file_name.left_string (i);
107       file_name = file_name.right_string (file_name.length () - i - 1);
108     }
109
110   i = file_name.index_last ('.');
111   if (i >= 0)
112     {
113       base_ = file_name.left_string (i);
114       ext_ = file_name.right_string (file_name.length () - i - 1);
115     }
116   else
117     base_ = file_name;
118 }
119
120 bool
121 File_name::is_absolute () const
122 {
123   return (dir_[0] == DIRSEP);
124 }