]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
* SConstruct: Further development.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7                  Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include <limits.h>
13
14 #include "config.hh"
15
16 #if HAVE_SYS_STAT_H 
17 #include <sys/stat.h>
18 #endif
19
20 #ifdef __CYGWIN__
21 #include <sys/cygwin.h>
22 #endif
23
24 #include "file-name.hh"
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 /* Join components to full file_name. */
53 String
54 File_name::to_string () const
55 {
56   String s;
57   if (!root_.is_empty ())
58     s = root_ + ::to_string (ROOTSEP);
59   if (!dir_.is_empty ())
60     s += dir_ + ::to_string (DIRSEP);
61   s += base_;
62   if (!ext_.is_empty ())
63     s += ::to_string (EXTSEP) + ext_;
64   return s;
65 }
66
67 char const*
68 File_name::to_str0 () const
69 {
70   return to_string ().to_str0 ();
71 }
72
73 File_name::File_name (String file_name)
74 {
75 #ifdef __CYGWIN__
76   /* All system functions would work, even if we don't convert to
77      posix file_name, but we'd think that \foe\bar\baz.ly is in the cwd.
78      On by default.  */
79   file_name = dos_to_posix (file_name);
80 #endif
81
82   int i = file_name.index (ROOTSEP);
83   if (i >= 0)
84     {
85       root_ = file_name.left_string (i);
86       file_name = file_name.right_string (file_name.length () - i - 1);
87     }
88
89   i = file_name.index_last (DIRSEP);
90   if (i >= 0)
91     {
92       dir_ = file_name.left_string (i);
93       file_name = file_name.right_string (file_name.length () - i - 1);
94     }
95
96   i = file_name.index_last ('.');
97   if (i >= 0)
98     {
99       base_ = file_name.left_string (i);
100       ext_ = file_name.right_string (file_name.length () - i - 1);
101     }
102   else
103     base_ = file_name;
104 }