]> git.lyx.org Git - lyx.git/blob - src/support/copy.cpp
some de-boostification
[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 using std::ifstream;
27 using std::ofstream;
28 using std::ios;
29 using std::string;
30
31
32 namespace lyx {
33 namespace support {
34
35 bool 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 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(),
55                         ios::binary | ios::out | ios::trunc);
56                 if (!ofs)
57                         return false;
58                 ofs.close();
59                 if (!support::chmod(to, mode))
60                         return false;
61         }
62
63         ofstream ofs(to.toFilesystemEncoding().c_str(),
64                         ios::binary | ios::out | ios::trunc);
65         if (!ofs)
66                 return false;
67
68         ofs << ifs.rdbuf();
69         return ofs.good();
70 }
71
72 } // namespace support
73 } // namespace lyx