2 file-name.cc - implement File_name
4 source file of the Flower Library
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-name.hh"
23 #include <sys/cygwin.h>
40 dos_to_posix (std::string file_name)
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);
51 #endif /* __CYGWIN__ */
53 /** Use slash as directory separator. On Windows, they can pretty
56 slashify (std::string file_name)
58 replace_all (file_name, '\\', '/');
59 replace_all (file_name, std::String ("//"), "/");
63 /* Join components to full file_name. */
65 File_name::to_string () const
69 s = root_ + std::to_string (ROOTSEP);
73 if (!base_.empty () || !ext_.empty ())
74 s += std::to_string (DIRSEP);
78 s += std::to_string (EXTSEP) + ext_;
82 File_name::File_name (std::string file_name)
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
88 file_name = dos_to_posix (file_name);
91 file_name = slashify (file_name);
94 ssize i = file_name.find (ROOTSEP);
97 root_ = file_name.substr (0, i);
98 file_name = file_name.substr (i + 1);
101 i = file_name.rfind (DIRSEP);
104 dir_ = file_name.substr (0, i);
105 file_name = file_name.substr (i + 1);
108 i = file_name.rfind ('.');
111 base_ = file_name.substr (0, i);
112 ext_ = file_name.substr (i + 1);
119 File_name::is_absolute () const
122 Hmm. Is c:foo absolute?
124 return (dir_.length () && dir_[0] == DIRSEP) || root_.length ();
128 File_name::File_name (String file_name)
130 *this = File_name (std::string (file_name));