]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-name.cc
2302baa46b6a0647f72be83f4a4e44a043d26f7f
[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 /** Use slash as directory separator.  On Windows, they can pretty
54     much be exchanged.  */
55 static std::string
56 slashify (std::string file_name)
57 {
58   replace_all (file_name, '\\', '/');
59   replace_all (file_name, std::string ("//"), "/");
60   return file_name;
61 }
62
63 /* Join components to full file_name. */
64 std::string
65 File_name::to_string () const
66 {
67   std::string s;
68   if (!root_.empty ())
69     s = root_ + std::to_string (ROOTSEP);
70   if (!dir_.empty ())
71     {
72       s += dir_;
73       if (!base_.empty () || !ext_.empty ())
74         s += std::to_string (DIRSEP);
75     }
76   s += base_;
77   if (!ext_.empty ())
78     s += std::to_string (EXTSEP) + ext_;
79   return s;
80 }
81
82 File_name::File_name (std::string file_name)
83 {
84 #ifdef __CYGWIN__
85   /* All system functions would work, even if we do not convert to
86      posix file_name, but we would think that \foe\bar\baz.ly is in
87      the cwd.  */
88   file_name = dos_to_posix (file_name);
89 #endif
90 #ifdef __MINGW32__
91   file_name = slashify (file_name);
92 #endif
93
94   ssize i = file_name.find (ROOTSEP);
95   if (i != NPOS)
96     {
97       root_ = file_name.substr (0, i);
98       file_name = file_name.substr (i + 1);
99     }
100
101   i = file_name.rfind (DIRSEP);
102   if (i != NPOS)
103     {
104       dir_ = file_name.substr (0, i);
105       file_name = file_name.substr (i + 1);
106     }
107
108   i = file_name.rfind ('.');
109   if (i != NPOS)
110     {
111       base_ = file_name.substr (0, i);
112       ext_ = file_name.substr (i + 1);
113     }
114   else
115     base_ = file_name;
116 }
117
118 bool
119 File_name::is_absolute () const
120 {
121   /*
122     Hmm. Is c:foo absolute?  
123    */
124   return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
125 }
126
127 #if 0 //STD_STRING
128 File_name::File_name (String file_name)
129 {
130   *this = File_name (std::string (file_name));
131 }
132 #endif