]> git.donarmstrong.com Git - lilypond.git/blob - lily/sources.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / sources.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "sources.hh"
21
22 #include "config.hh"
23 #include "source-file.hh"
24 #include "file-name.hh"
25 #include "file-path.hh"
26
27 using std::string;
28
29 Sources::Sources ()
30 {
31   path_ = 0;
32 }
33
34 void
35 Sources::set_path (File_path *f)
36 {
37   path_ = f;
38 }
39
40 /**
41    Open a file. If the name is not absolute, look in CURRENT_DIR first.
42    Afterwards, check the rest of the path_.
43
44    FILE_STRING the name of the file to be opened.
45    CURRENT_DIR a path to a directory, either absolute or relative to the
46      working directory.
47 */
48 Source_file *
49 Sources::get_file (string file_string, string const &current_dir)
50 {
51   if (file_string != "-")
52     {
53       // First, check for a path relative to the directory of the
54       // file currently being parsed.
55       if (current_dir.length ()
56           && file_string.length ()
57           && !File_name (file_string).is_absolute ()
58           && is_file (current_dir + DIRSEP + file_string))
59         file_string = current_dir + DIRSEP + file_string;
60
61       // Otherwise, check the rest of the path.
62       else if (path_)
63         {
64           string file_string_o = path_->find (file_string);
65           if ((file_string_o == "") && (file_string != ""))
66             return 0;
67
68           file_string = file_string_o;
69         }
70     }
71
72   Source_file *f = new Source_file (file_string);
73   add (f);
74   return f;
75 }
76
77 void
78 Sources::add (Source_file *sourcefile)
79 {
80   sourcefiles_.push_back (sourcefile);
81 }
82
83 Sources::~Sources ()
84 {
85   for (vsize i = 0; i < sourcefiles_.size (); i++)
86     {
87       sourcefiles_[i]->unprotect ();
88     }
89 }
90