]> git.donarmstrong.com Git - lilypond.git/blob - lily/sources.cc
Merge branch 'master' of carldsorensen@git.sv.gnu.org:/srv/git/lilypond into fret...
[lilypond.git] / lily / sources.cc
1 /*
2   source.cc -- implement Sources
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "sources.hh"
10
11 #include "config.hh"
12 #include "source-file.hh"
13 #include "file-name.hh"
14 #include "file-path.hh"
15
16 Sources::Sources ()
17 {
18   path_ = 0;
19 }
20
21
22 Sources::Sources (Sources const &)
23 {
24   assert (false);
25 }
26
27
28 void
29 Sources::set_path (File_path *f)
30 {
31   path_ = f;
32 }
33
34 /**
35    Open a file. If the name is not absolute, look in CURRENT_DIR first.
36    Afterwards, check the rest of the path_.
37
38    FILE_STRING the name of the file to be opened.
39    CURRENT_DIR a path to a directory, either absolute or relative to the
40      working directory.
41 */
42 Source_file *
43 Sources::get_file (string file_string, string const& current_dir)
44 {  
45   if (file_string != "-")
46     {
47       // First, check for a path relative to the directory of the
48       // file currently being parsed.
49       if (current_dir.length ()
50           && file_string.length ()
51           && !File_name (file_string).is_absolute ()
52           && is_file (current_dir + DIRSEP + file_string))
53         file_string = current_dir + DIRSEP + file_string;
54
55       // Otherwise, check the rest of the path.
56       else if (path_)
57         {
58           string file_string_o = path_->find (file_string);
59           if ((file_string_o == "") && (file_string != ""))
60             return 0;
61
62           file_string = file_string_o;
63         }
64     }
65
66   Source_file *f = new Source_file (file_string);
67   add (f);
68   return f;
69 }
70
71 void
72 Sources::add (Source_file *sourcefile)
73 {
74   sourcefiles_.push_back (sourcefile);
75 }
76
77 Sources::~Sources ()
78 {
79   for (vsize i = 0; i < sourcefiles_.size (); i++)
80     {
81       sourcefiles_[i]->unprotect ();
82     }
83 }
84