]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
*** empty log message ***
[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 /* Join components to full file_name. */
52 String
53 File_name::to_string () const
54 {
55   String s;
56   if (!root_.is_empty ())
57     s = root_ + ::to_string (ROOTSEP);
58   if (!dir_.is_empty ())
59     s += dir_ + ::to_string (DIRSEP);
60   s += base_;
61   if (!ext_.is_empty ())
62     s += ::to_string (EXTSEP) + ext_;
63   return s;
64 }
65
66 char const*
67 File_name::to_str0 () const
68 {
69   return to_string ().to_str0 ();
70 }
71
72 File_name::File_name (String file_name)
73 {
74 #ifdef __CYGWIN__
75   /* All system functions would work, even if we don't convert to
76      posix file_name, but we'd think that \foe\bar\baz.ly is in the cwd.
77      On by default.  */
78   file_name = dos_to_posix (file_name);
79 #endif
80
81   int i = file_name.index (ROOTSEP);
82   if (i >= 0)
83     {
84       root_ = file_name.left_string (i);
85       file_name = file_name.right_string (file_name.length () - i - 1);
86     }
87
88   i = file_name.index_last (DIRSEP);
89   if (i >= 0)
90     {
91       dir_ = file_name.left_string (i);
92       file_name = file_name.right_string (file_name.length () - i - 1);
93     }
94
95   i = file_name.index_last ('.');
96   if (i >= 0)
97     {
98       base_ = file_name.left_string (i);
99       ext_ = file_name.right_string (file_name.length () - i - 1);
100     }
101   else
102     base_ = file_name;
103 }