]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
ac9e6832e0a351c7bf16ff25a6c124360a66fc70
[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@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "file-name.hh"
11
12 #include <cstdio>
13 #include <cerrno>
14
15 #include "config.hh"
16
17 #if HAVE_SYS_STAT_H
18 #include <sys/stat.h>
19 #endif
20
21 #ifdef __CYGWIN__
22 #include <sys/cygwin.h>
23 #endif
24
25 /* We don't have multiple roots, set this to '\0'? */
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   /* urg, wtf? char const* argument gets modified! */
45   cygwin_conv_to_posix_path (s, buf);
46   delete s;
47   return buf;
48 }
49 #endif /* __CYGWIN__ */
50
51 #ifdef __MINGW32__
52 /** Use slash as directory separator.  On Windows, they can pretty
53     much be exchanged.  */
54 static String
55 slashify (String file_name)
56 {
57   if (file_name.index ('/'))
58     return file_name;
59   file_name.substitute ('\\', '/');
60   file_name.substitute ("\"", "\\\"");
61   file_name.substitute ("\'", "\\\'");
62   return file_name;
63 }
64 #endif /* __MINGW32__ */
65
66 /* Join components to full file_name. */
67 String
68 File_name::to_string () const
69 {
70   String s;
71   if (!root_.is_empty ())
72     s = root_ + ::to_string (ROOTSEP);
73   if (!dir_.is_empty ())
74     s += dir_ + ::to_string (DIRSEP);
75   s += base_;
76   if (!ext_.is_empty ())
77     s += ::to_string (EXTSEP) + ext_;
78   return s;
79 }
80
81 char const *
82 File_name::to_str0 () const
83 {
84   return to_string ().to_str0 ();
85 }
86
87 File_name::File_name (String file_name)
88 {
89 #ifdef __CYGWIN__
90   /* All system functions would work, even if we do not convert to
91      posix file_name, but we would think that \foe\bar\baz.ly is in
92      the cwd.  */
93   file_name = dos_to_posix (file_name);
94 #endif
95 #ifdef __MINGW32__
96   file_name = slashify (file_name);
97 #endif
98
99   int i = file_name.index (ROOTSEP);
100   if (i >= 0)
101     {
102       root_ = file_name.left_string (i);
103       file_name = file_name.right_string (file_name.length () - i - 1);
104     }
105
106   i = file_name.index_last (DIRSEP);
107   if (i >= 0)
108     {
109       dir_ = file_name.left_string (i);
110       file_name = file_name.right_string (file_name.length () - i - 1);
111     }
112
113   i = file_name.index_last ('.');
114   if (i >= 0)
115     {
116       base_ = file_name.left_string (i);
117       ext_ = file_name.right_string (file_name.length () - i - 1);
118     }
119   else
120     base_ = file_name;
121 }