/*
- file-path.cc - implement File_path
+ This file is part of LilyPond, the GNU music typesetter.
- source file of the Flower Library
-
- (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+ Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
Jan Nieuwenhuizen <janneke@gnu.org>
+
+ LilyPond is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ LilyPond is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
*/
#include "file-path.hh"
#include <cstdio>
#include <cerrno>
-using namespace std;
#include "config.hh"
#if HAVE_SYS_STAT_H
#define PATHSEP ':'
#endif
-Array<Std_string>
+vector<string>
File_path::directories () const
{
return dirs_;
}
+#include <algorithm>
void
-File_path::parse_path (Std_string p)
+File_path::parse_path (string p)
{
- int len;
- while ((len = p.length ()))
- {
- int i = p.find (PATHSEP);
- if (i < 0)
- i = len;
- append (String (p, 0, i));
- p = String (p, i + 1);
- }
+ concat (dirs_, string_split (p, PATHSEP));
}
bool
-is_file (Std_string file_name)
+is_file (string file_name)
{
-#if 0 /* Check if directory. TODO: encapsulate for autoconf */
- struct stat sbuf;
- if (stat (file_name.c_str (), &sbuf) != 0)
- return false;
-
- if (! (sbuf.st_mode & __S_IFREG))
- return false;
-#endif
-
#if !STAT_MACROS_BROKEN
struct stat sbuf;
if (stat (file_name.c_str (), &sbuf) != 0)
}
bool
-is_dir (Std_string file_name)
+is_dir (string file_name)
{
+ /*
+ canonicalize; in particular, trailing slashes should disappear.
+ */
+ file_name = File_name (file_name).to_string ();
+
#if !STAT_MACROS_BROKEN
struct stat sbuf;
if (stat (file_name.c_str (), &sbuf) != 0)
@return
The file name if found, or empty string if not found. */
-Std_string
-File_path::find (Std_string name) const
+string
+File_path::find (string name) const
{
if (!name.length () || (name == "-"))
return name;
#ifdef __MINGW32__
- if (name.find ('\\') >= 0)
+ if (name.find ('\\') != NPOS)
programming_error ("file name not normalized: " + name);
#endif /* __MINGW32__ */
if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
return file_name.to_string ();
- for (int i = 0; i < dirs_.size (); i++)
+ for (vsize i = 0; i < dirs_.size (); i++)
{
File_name file_name (name);
- File_name dir = (Std_string) dirs_[i];
+ File_name dir = (string) dirs_[i];
file_name.root_ = dir.root_;
dir.root_ = "";
if (file_name.dir_.empty ())
where EXT is from EXTENSIONS.
*/
-Std_string
-File_path::find (Std_string name, char const *extensions[])
+string
+File_path::find (string name, char const *extensions[])
{
if (name.empty () || name == "-")
return name;
File_name file_name (name);
- Std_string orig_ext = file_name.ext_;
+ string orig_ext = file_name.ext_;
for (int i = 0; extensions[i]; i++)
{
file_name.ext_ = orig_ext;
if (*extensions[i] && !file_name.ext_.empty ())
file_name.ext_ += ".";
file_name.ext_ += extensions[i];
- Std_string found = find (file_name.to_string ());
+ string found = find (file_name.to_string ());
if (!found.empty ())
return found;
}
/** Append a directory, return false if failed. */
bool
-File_path::try_append (Std_string s)
+File_path::try_append (string s)
{
if (s == "")
s = ".";
return false;
}
-Std_string
+string
File_path::to_string () const
{
- Std_string s;
- for (int i = 0; i < dirs_.size (); i++)
+ string s;
+ for (vsize i = 0; i < dirs_.size (); i++)
{
s = s + dirs_[i];
if (i < dirs_.size () - 1)
}
void
-File_path::append (Std_string str)
+File_path::append (string str)
{
- dirs_.push (str);
+ dirs_.push_back (str);
}
void
-File_path::prepend (Std_string str)
+File_path::prepend (string str)
{
- dirs_.insert (str, 0);
+ dirs_.insert (dirs_.begin (), str);
}