.ly version update.
[lilypond.git] / lily / streams.cc
blobebc42cc15f20a84306c1f9e6aa12089b153e2421
1 #include "config.h"
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
10 #include <iostream>
11 #include <fstream>
13 #include "stream.hh"
14 #include "file-path.hh"
15 #include "warn.hh"
16 #include "main.hh"
18 #if __GNUC__ > 2
19 std::ostream *
20 open_file_stream (String filename, std::ios_base::openmode mode)
21 #else
22 std::ostream *
23 open_file_stream (String filename, int mode)
24 #endif
26 std::ostream *os;
27 if ((filename == "-"))
28 os = &std::cout;
29 else
31 Path p = split_path (filename);
32 if (!p.dir.empty_b ())
33 if (mkdir (p.dir.to_str0 (), 0777) == -1 && errno != EEXIST)
34 error (_f ("can't create directory: `%s'", p.dir));
35 os = new std::ofstream (filename.to_str0 (), mode);
37 if (!*os)
38 error (_f ("can't open file: `%s'", filename));
39 return os;
42 void
43 close_file_stream (std::ostream *os)
45 *os << std::flush;
46 if (!*os)
48 warning (_ ("Error syncing file (disk full?)"));
49 exit_status_global = 1;
51 if (os != &std::cout)
52 delete os;
53 os = 0;