2 file-name.cc - implement File_name
4 source file of the Flower Library
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-name.hh"
25 #include <sys/cygwin.h>
42 dos_to_posix (string file_name
)
44 char buf
[PATH_MAX
] = "";
45 char s
[PATH_MAX
] = {0};
46 file_name
.copy (s
, PATH_MAX
- 1);
47 /* ugh: char const* argument gets modified. */
48 int fail
= cygwin_conv_to_posix_path (s
, buf
);
53 #endif /* __CYGWIN__ */
55 /** Use slash as directory separator. On Windows, they can pretty
58 static /* avoid warning */
61 slashify (string file_name
)
63 replace_all (file_name
, '\\', '/');
64 replace_all (file_name
, string ("//"), "/");
69 dir_name (string
const file_name
)
73 ssize n
= s
.length ();
74 if (n
&& s
[n
- 1] == '/')
76 if (s
.rfind ('/') != NPOS
)
77 s
= s
.substr (0, s
.rfind ('/'));
85 get_working_directory ()
88 getcwd (cwd
, PATH_MAX
);
93 /* Join components to full file_name. */
95 File_name::dir_part () const
99 s
= root_
+ ::to_string (ROOTSEP
);
111 File_name::file_part () const
116 s
+= ::to_string (EXTSEP
) + ext_
;
121 File_name::to_string () const
123 string d
= dir_part ();
124 string f
= file_part ();
129 d
+= ::to_string (DIRSEP
);
135 File_name::File_name (string file_name
)
138 /* All system functions would work, even if we do not convert to
139 posix file_name, but we would think that \foe\bar\baz.ly is in
141 file_name
= dos_to_posix (file_name
);
144 file_name
= slashify (file_name
);
147 ssize i
= file_name
.find (ROOTSEP
);
150 root_
= file_name
.substr (0, i
);
151 file_name
= file_name
.substr (i
+ 1);
154 i
= file_name
.rfind (DIRSEP
);
157 dir_
= file_name
.substr (0, i
);
158 file_name
= file_name
.substr (i
+ 1);
161 i
= file_name
.rfind ('.');
164 base_
= file_name
.substr (0, i
);
165 ext_
= file_name
.substr (i
+ 1);
172 File_name::is_absolute () const
175 Hmm. Is c:foo absolute?
177 return (dir_
.length () && dir_
[0] == DIRSEP
) || root_
.length ();
183 File_name::canonicalized () const
187 replace_all (c
.dir_
, string ("//"), string ("/"));
189 vector
<string
> components
= string_split (c
.dir_
, '/');
190 vector
<string
> new_components
;
192 for (vsize i
= 0; i
< components
.size (); i
++)
194 if (components
[i
] == "..")
195 new_components
.pop_back ();
197 new_components
.push_back (components
[i
]);
200 c
.dir_
= string_join (new_components
, "/");