-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFixPath.cpp
More file actions
212 lines (168 loc) · 4.14 KB
/
FixPath.cpp
File metadata and controls
212 lines (168 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/******************************************************************************\
*
* File: FixPath.cpp
* Creation date: May 30, 2017 18:34
* Author: ClassBuilder
* XXXX
* Purpose: Method implementations of class 'FixPath'
*
* Modifications: @INSERT_MODIFICATIONS(* )
* May 30, 2017 21:15 JV
* Updated code of method 'ResolvePath'
* May 30, 2017 19:46 JV
* Added method 'MergePaths'
* Updated code of method 'ResolvePath'
* May 30, 2017 19:37 JV
* Added method 'ResolvePath'
* May 30, 2017 18:44 JV
* Updated interface of method 'FindDir'
* Updated code of method '~FixPath'
* Updated code of method 'FixPath'
* Updated relation 'FixPath <>-->> Dir'
* May 30, 2017 18:34 JV
* Added method 'DestructorInclude'
* Added method 'ConstructorInclude'
* Added method 'LeaveDirectory'
* Added method 'FindPath'
* Added method 'EnterDirectory'
* Added method '~FixPath'
* Added method 'FixPath'
* Added relation 'FixPath <>-->> Path'
*
* Copyright 2017, XXXXX
* All rights are reserved. Reproduction in whole or part is prohibited
* without the written consent of the copyright owner.
*
\******************************************************************************/
//@START_USER1
//@END_USER1
// Master include file
#include "FixPathInclude.h"
//@START_USER2
#include <sys/stat.h>
inline bool FileExists(string filename) {
struct stat fileInfo;
return stat(filename.c_str(), &fileInfo) == 0;
}
//@END_USER2
// Static members
/*@NOTE_115
Constructor method.
*/
FixPath::FixPath() //@INIT_115
{//@CODE_115
ConstructorInclude();
// Put in your own code
}//@CODE_115
/*@NOTE_19
Destructor method.
*/
FixPath::~FixPath()
{//@CODE_19
DestructorInclude();
// Put in your own code
}//@CODE_19
void FixPath::EnterDirectory(char* path)
{//@CODE_118
Dir* pDir = FindDir(path);
if (!pDir)
{
pDir = new Dir(this, path);
}
pDir->Increment();
}//@CODE_118
Dir* FixPath::FindDir(const string& path)
{//@CODE_116
DirIterator iDir(this);
while (++iDir)
{
if (path == iDir->GetPath())
{
return iDir;
}
}
return 0;
}//@CODE_116
void FixPath::LeaveDirectory(char* path)
{//@CODE_119
Dir* pDir = FindDir(path);
if (pDir)
{
pDir->Decrement();
if (pDir->GetCnt() == 0)
{
delete pDir;
}
}
}//@CODE_119
string FixPath::MergePaths(const string& dir, const string& relPath)
{//@CODE_132
size_t pos = 0;
if (relPath.find("./") == 0)
{
pos = 2;
}
while (relPath.find("../", pos) == pos)
{
pos += 3;
}
string rPath = relPath.substr(pos);
size_t rpos = dir.length();
for (size_t i = 0; i < pos/3 && rpos != string::npos; i++)
{
rpos = dir.rfind('/', rpos-1);
}
if (rpos != string::npos)
{
return dir.substr(0, rpos) + '/' + rPath;
}
else
{
return relPath;
}
}//@CODE_132
string FixPath::ResolvePath(char* path)
{//@CODE_130
string str = path;
if (path[0] != '/')
{
string lastFailedPath;
DirIterator iDir(this);
while (++iDir)
{
str = MergePaths(iDir->GetPath(), path);
if (str != lastFailedPath)
{
if (FileExists(str))
{
break;
}
else
{
lastFailedPath = str;
}
}
}
}
return str;
}//@CODE_130
//{{AFX DO NOT EDIT CODE BELOW THIS LINE !!!
/*@NOTE_20
Method which must be called first in a constructor.
*/
void FixPath::ConstructorInclude()
{
INIT_MULTI_OWNED_ACTIVE(FixPath, FixPath, Dir, Dir)
}
/*@NOTE_21
Method which must be called first in a destructor.
*/
void FixPath::DestructorInclude()
{
EXIT_MULTI_OWNED_ACTIVE(FixPath, FixPath, Dir, Dir)
}
// Methods for the relation(s) of the class
METHODS_MULTI_OWNED_ACTIVE(FixPath, FixPath, Dir, Dir)
METHODS_ITERATOR_MULTI_ACTIVE(FixPath, FixPath, Dir, Dir)
//}}AFX DO NOT EDIT CODE ABOVE THIS LINE !!!
//@START_USER3