]> git.donarmstrong.com Git - lilypond.git/blob - lily/streams.cc
06a74a23cbf7885ff7ea5d26cec61c930554335b
[lilypond.git] / lily / streams.cc
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #if HAVE_SYS_STAT_H 
7 #include <sys/stat.h>
8 #endif
9 #include <iostream.h>
10 #include <fstream.h>
11
12 #include "stream.hh"
13 #include "file-path.hh"
14 #include "warn.hh"
15 #include "main.hh"
16
17 #if __GNUC__ > 2
18 ostream *
19 open_file_stream (String filename, std::ios_base::openmode mode)
20 #else
21 ostream *
22 open_file_stream (String filename, int mode)
23 #endif
24 {
25   ostream *os;
26   if ((filename == "-"))
27     os = &cout;
28   else
29     {
30       Path p = split_path (filename);
31       if (!p.dir.empty_b ())
32         if (mkdir (p.dir.ch_C (), 0777) == -1 && errno != EEXIST)
33           error (_f ("can't create directory: `%s'", p.dir));
34       os = new ofstream (filename.ch_C (), mode);
35     }
36   if (!*os)
37     error (_f ("can't open file: `%s'", filename));
38   return os;
39 }
40
41 void
42 close_file_stream (ostream *os)
43 {
44   *os << flush;
45   if (!*os)
46     {
47       warning (_ ("Error syncing file (disk full?)"));
48       exit_status_global = 1;
49     }
50   if (os != &cout)
51     delete os;
52   os = 0;
53 }