]> git.lyx.org Git - lyx.git/blob - src/support/copy.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / support / copy.cpp
1 /**
2  * \file copy.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14
15 #include "support/FileName.h"
16 #include "support/lyxlib.h"
17
18 #ifdef HAVE_SYS_STAT_H
19 # include <sys/stat.h>
20 #endif
21 #ifdef HAVE_SYS_TYPES_H
22 # include <sys/types.h>
23 #endif
24
25
26 namespace lyx {
27
28
29 using std::ifstream;
30 using std::ofstream;
31 using std::ios;
32 using std::string;
33
34
35 bool lyx::support::chmod(FileName const & file, unsigned long int mode)
36 {
37 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
38         if (::chmod(file.toFilesystemEncoding().c_str(), mode_t(mode)) != 0)
39                 return false;
40 #else
41         // FIXME: "File permissions are ignored on this system."
42 #endif
43         return true;
44 }
45
46
47 bool lyx::support::copy(FileName const & from, FileName const & to, unsigned long int mode)
48 {
49         ifstream ifs(from.toFilesystemEncoding().c_str(), ios::binary | ios::in);
50         if (!ifs)
51                 return false;
52
53         if (mode != (unsigned long int)-1) {
54                 ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
55                 if (!ofs)
56                         return false;
57                 ofs.close();
58                 if (!support::chmod(to, mode))
59                         return false;
60         }
61
62         ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
63         if (!ofs)
64                 return false;
65
66         ofs << ifs.rdbuf();
67         return ofs.good();
68 }
69
70
71 } // namespace lyx