(dos_to_posix)[__CYGWIN__]: Return
[lilypond/patrick.git] / flower / file-name.cc
blob2064e072613fa598908a01b467068f3d65512b69
1 /*
2 file-name.cc - implement File_name
4 source file of the Flower Library
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
10 #include "file-name.hh"
12 #include <cstdio>
13 #include <cerrno>
14 using namespace std;
16 #include "config.hh"
18 #if HAVE_SYS_STAT_H
19 #include <sys/stat.h>
20 #endif
22 #ifdef __CYGWIN__
23 #include <sys/cygwin.h>
24 #endif
26 /* We don't have multiple roots, set this to '\0'? */
27 #ifndef ROOTSEP
28 #define ROOTSEP ':'
29 #endif
31 #ifndef DIRSEP
32 #define DIRSEP '/'
33 #endif
35 #ifndef EXTSEP
36 #define EXTSEP '.'
37 #endif
39 #ifdef __CYGWIN__
40 static String
41 dos_to_posix (String file_name)
43 char buf[PATH_MAX] = "";
44 char *s = file_name.get_copy_str0 ();
45 /* ugh: char const* argument gets modified. */
46 int fail = cygwin_conv_to_posix_path (s, buf);
47 delete s;
48 if (!fail)
49 return buf;
50 return file_name;
52 #endif /* __CYGWIN__ */
54 #ifdef __MINGW32__
55 /** Use slash as directory separator. On Windows, they can pretty
56 much be exchanged. */
57 static String
58 slashify (String file_name)
60 file_name.substitute ('\\', '/');
61 file_name.substitute ("//", "/");
62 return file_name;
64 #endif /* __MINGW32__ */
66 /* Join components to full file_name. */
67 String
68 File_name::to_string () const
70 String s;
71 if (!root_.is_empty ())
72 s = root_ + ::to_string (ROOTSEP);
73 if (!dir_.is_empty ())
75 s += dir_;
76 if (!base_.is_empty () || !ext_.is_empty ())
77 s += ::to_string (DIRSEP);
79 s += base_;
80 if (!ext_.is_empty ())
81 s += ::to_string (EXTSEP) + ext_;
82 return s;
85 File_name::File_name (String file_name)
87 #ifdef __CYGWIN__
88 /* All system functions would work, even if we do not convert to
89 posix file_name, but we would think that \foe\bar\baz.ly is in
90 the cwd. */
91 file_name = dos_to_posix (file_name);
92 #endif
93 #ifdef __MINGW32__
94 file_name = slashify (file_name);
95 #endif
97 int i = file_name.index (ROOTSEP);
98 if (i >= 0)
100 root_ = file_name.left_string (i);
101 file_name = file_name.right_string (file_name.length () - i - 1);
104 i = file_name.index_last (DIRSEP);
105 if (i >= 0)
107 dir_ = file_name.left_string (i);
108 file_name = file_name.right_string (file_name.length () - i - 1);
111 i = file_name.index_last ('.');
112 if (i >= 0)
114 base_ = file_name.left_string (i);
115 ext_ = file_name.right_string (file_name.length () - i - 1);
117 else
118 base_ = file_name;