]> 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--2006 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 std::string
40 dos_to_posix (std::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 std::string
57 slashify (std::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 std::string
67 File_name::to_string () const
68 {
69   std::string s;
70   if (!root_.empty ())
71     s = root_ + std::to_string (ROOTSEP);
72   if (!dir_.empty ())
73     {
74       s += dir_;
75       if (!base_.empty () || !ext_.empty ())
76         s += std::to_string (DIRSEP);
77     }
78   s += base_;
79   if (!ext_.empty ())
80     s += std::to_string (EXTSEP) + ext_;
81   return s;
82 }
83
84 File_name::File_name (std::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   ssize i = file_name.find (ROOTSEP);
97   if (i != NPOS)
98     {
99       root_ = file_name.substr (0, i);
100       file_name = file_name.substr (i + 1);
101     }
102
103   i = file_name.rfind (DIRSEP);
104   if (i != NPOS)
105     {
106       dir_ = file_name.substr (0, i);
107       file_name = file_name.substr (i + 1);
108     }
109
110   i = file_name.rfind ('.');
111   if (i != NPOS)
112     {
113       base_ = file_name.substr (0, i);
114       ext_ = file_name.substr (i + 1);
115     }
116   else
117     base_ = file_name;
118 }
119
120 bool
121 File_name::is_absolute () const
122 {
123   /*
124     Hmm. Is c:foo absolute?  
125    */
126   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
127 }
128
129 #if 0 //STD_STRING
130 File_name::File_name (String file_name)
131 {
132   *this = File_name (std::string (file_name));
133 }
134 #endif