]> git.lyx.org Git - features.git/blob - src/pathstack.C
7788e0eb3770181bcb6f10cd534317c9d9050c22
[features.git] / src / pathstack.C
1 // lyx-stack.C : implementation of PathStack class
2 //   this file is part of LyX, the High Level Word Processor
3 //   copyright (C) 1995-1996, Matthias Ettrich and the LyX Team
4
5 #include <config.h>
6 #include <unistd.h>
7
8 #ifdef __GNUG__
9 #pragma implementation "pathstack.h"
10 #endif
11
12 #include "pathstack.h"
13 #include "support/filetools.h"
14 #include "debug.h"
15 #include "LString.h"
16 #include "gettext.h"
17
18 // temporary hack
19 #include "lyx_gui_misc.h"
20
21 // global path stack
22 PathStack lyxPathStack;
23
24 // Standard constructor
25 PathStack::PathStack(string const & string)
26         : Path(string)
27 {
28         Next = 0;
29 }
30
31 // Destructor
32 PathStack::~PathStack()
33 {
34         if (Next)
35                 delete Next;
36 }
37
38 // Changes to directory
39 int PathStack::PathPush(string const & Path)
40 {
41         // checks path string validity
42         if (Path.empty()) return 1;
43
44         PathStack *NewNode;
45
46         // gets current directory and switch to new one
47         string CurrentPath = GetCWD();
48         if ((CurrentPath.empty()) || chdir(Path.c_str())) {
49                 WriteFSAlert(_("Error: Could not change to directory: "), 
50                              Path);
51                 return 2;
52         }
53
54         lyxerr.debug() << "PathPush: " << Path << endl;
55         // adds new node
56         NewNode = new PathStack(CurrentPath);
57         NewNode->Next = Next;
58         Next = NewNode;
59         return 0;
60 }
61
62 // Goes back to previous directory
63 int PathStack::PathPop()
64 {
65         // checks stack validity and extracts old node
66         PathStack *OldNode = Next;
67         if (!OldNode) {
68                 WriteAlert (_("LyX Internal Error:"), _("Path Stack underflow."));
69                 return 1;
70         }
71         Next = OldNode->Next;
72         OldNode->Next = 0;
73
74         // switches to old directory
75         int Result = 0;
76         if (chdir(OldNode->Path.c_str())) {
77                 WriteFSAlert(_("Error: Could not change to directory: "), 
78                              Path);
79                 Result = 2;
80         }
81         lyxerr.debug() << "PathPop: " << OldNode->Path << endl;
82         delete OldNode;
83
84         return Result;
85 }
86