]> git.lyx.org Git - lyx.git/blob - src/support/PathChanger.cpp
A first batch of potential bugs spotted by llvm/clang
[lyx.git] / src / support / PathChanger.cpp
1 /**
2  * \file Path.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 "support/PathChanger.h"
14
15 // undef PathChanger macro when building PathChanger
16 #undef PathChanger
17
18
19 namespace lyx {
20 namespace support {
21
22 PathChanger::PathChanger(FileName const & path)
23         : popped_(false)
24 {
25         if (!path.empty()) {
26                 pushedDir_ = FileName::getcwd();
27
28                 if (pushedDir_.empty() || !path.chdir()) {
29                         /* FIXME: throw */
30                 }
31         } else {
32                 popped_ = true;
33         }
34 }
35
36
37 PathChanger::~PathChanger()
38 {
39         if (!popped_)
40                 pop();
41 }
42
43
44 int PathChanger::pop()
45 {
46         if (popped_) {
47                 // should throw an exception
48                 // throw logical_error();
49                 return 0;
50         }
51
52         if (!pushedDir_.chdir()) {
53                 // should throw an exception
54                 // throw DirChangeError();
55         }
56         popped_ = true;
57
58         return 0;
59 }
60
61 } // namespace support
62 } // namespace lyx
63
64
65 #define PathChanger(x) unnamed_PathChanger;
66
67 // in merged builds this is not the last line.