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