X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Ffile-path.cc;h=65cd517e5a07247fc798277631be1857601679e0;hb=a6a51abfd0195a3cf7d6ea095cf69808852f21ce;hp=f8504396cf0b9fcae5f09403457e709a317d4683;hpb=0fcd8c283c45644a92d2308f1cd0d82a1e63380b;p=lilypond.git diff --git a/flower/file-path.cc b/flower/file-path.cc index f8504396cf..65cd517e5a 100644 --- a/flower/file-path.cc +++ b/flower/file-path.cc @@ -1,10 +1,21 @@ /* - file-path.cc - implement File_path + This file is part of LilyPond, the GNU music typesetter. - source file of the Flower Library - - (c) 1997--2005 Han-Wen Nienhuys + Copyright (C) 1997--2015 Han-Wen Nienhuys Jan Nieuwenhuizen + + 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 . */ #include "file-path.hh" @@ -28,46 +39,32 @@ #define PATHSEP ':' #endif -Array +#include + +vector File_path::directories () const { - return *this; + return dirs_; } void -File_path::parse_path (String p) +File_path::parse_path (const string &p) { - int len; - while ((len = p.length ()) ) - { - int i = p.index (PATHSEP); - if (i <0) - i = len; - append (p.left_string (i)); - p = p.right_string (len - i - 1); - } + concat (dirs_, string_split (p, PATHSEP)); } -static bool -is_file (String file_name) +bool +is_file (const string &file_name) { -#if 0 /* Check if directory. TODO: encapsulate for autoconf */ - struct stat sbuf; - if (stat (file_name.to_str0 (), &sbuf) != 0) - return false; - - if (!(sbuf.st_mode & __S_IFREG)) - return false; -#endif #if !STAT_MACROS_BROKEN struct stat sbuf; - if (stat (file_name.to_str0 (), &sbuf) != 0) + if (stat (file_name.c_str (), &sbuf) != 0) return false; - + return !S_ISDIR (sbuf.st_mode); #endif - if (FILE *f = fopen (file_name.to_str0 (), "r")) + if (FILE *f = fopen (file_name.c_str (), "r")) { fclose (f); return true; @@ -76,18 +73,23 @@ is_file (String file_name) return false; } -static bool -is_dir (String file_name) +bool +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.to_str0 (), &sbuf) != 0) + if (stat (file_name.c_str (), &sbuf) != 0) return false; - + return S_ISDIR (sbuf.st_mode); #endif - if (FILE *f = fopen (file_name.to_str0 (), "r")) + if (FILE *f = fopen (file_name.c_str (), "r")) { fclose (f); return true; @@ -95,7 +97,6 @@ is_dir (String file_name) return false; } - /** Find a file. Check absolute file name, search in the current dir (DUH! FIXME!), @@ -105,14 +106,14 @@ directory, in this order. @return The file name if found, or empty string if not found. */ -String -File_path::find (String name) const +string +File_path::find (const string &name) const { if (!name.length () || (name == "-")) return name; #ifdef __MINGW32__ - if (name[0] == '\\' || (name.length () > 2 && name[2] == '\\')) + if (name.find ('\\') != NPOS) programming_error ("file name not normalized: " + name); #endif /* __MINGW32__ */ @@ -121,55 +122,55 @@ File_path::find (String name) const if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ())) return file_name.to_string (); - for (int i = 0, n = size (); i < n; i++) + for (vsize i = 0; i < dirs_.size (); i++) { - File_name dir = elem (i); + File_name file_name (name); + File_name dir = (string) dirs_[i]; file_name.root_ = dir.root_; dir.root_ = ""; - file_name.dir_ = dir.to_string (); + if (file_name.dir_.empty ()) + file_name.dir_ = dir.to_string (); + else if (!dir.to_string ().empty ()) + file_name.dir_ = dir.to_string () + + ::to_string (DIRSEP) + file_name.dir_; if (is_file (file_name.to_string ())) - return file_name.to_string (); + return file_name.to_string (); } return ""; } -/** Find a file. - -Seach in the current dir (DUH! FIXME?), in the construction-arg -(what's that?, and in any other appended directory, in this order. +/* + Try to find -Search for NAME, or name without extension, or name with any of -EXTENSIONS, in that order. + file.EXT, -@return -The file name if found, or empty string if not found. */ -String -File_path::find (String name, char const *extensions[]) + where EXT is from EXTENSIONS. +*/ +string +File_path::find (const string &name, char const *extensions[]) { + if (name.empty () || name == "-") + return name; + File_name file_name (name); - if (name.is_empty () || name == "-") - file_name.base_ = "-"; - else + string orig_ext = file_name.ext_; + for (int i = 0; extensions[i]; i++) { - String orig_ext = file_name.ext_; - for (int i = 0; extensions[i]; i++) - { - file_name.ext_ = orig_ext; - if (*extensions[i] && !file_name.ext_.is_empty ()) - file_name.ext_ += "."; - file_name.ext_ += extensions[i]; - if (!find (file_name.to_string ()).is_empty ()) - break; - } - /* Reshuffle extension */ - file_name = File_name (file_name.to_string ()); + file_name.ext_ = orig_ext; + if (*extensions[i] && !file_name.ext_.empty ()) + file_name.ext_ += "."; + file_name.ext_ += extensions[i]; + string found = find (file_name.to_string ()); + if (!found.empty ()) + return found; } - return file_name.to_string (); + + return ""; } /** Append a directory, return false if failed. */ bool -File_path::try_append (String s) +File_path::try_append (string s) { if (s == "") s = "."; @@ -181,15 +182,27 @@ File_path::try_append (String s) return false; } -String +string File_path::to_string () const { - String s; - for (int i = 0, n = size (); i < n; i++) + string s; + for (vsize i = 0; i < dirs_.size (); i++) { - s = s + elem (i); - if (i < n - 1) - s += ::to_string (PATHSEP); + s = s + dirs_[i]; + if (i < dirs_.size () - 1) + s += ::to_string (PATHSEP); } return s; } + +void +File_path::append (const string &str) +{ + dirs_.push_back (str); +} + +void +File_path::prepend (const string &str) +{ + dirs_.insert (dirs_.begin (), str); +}