Develop and Download Open Source Software

Browse CVS Repository

Contents of /tombo/Tombo/Src/TomboURI.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.14 - (show annotations) (download) (as text)
Wed Aug 23 15:31:54 2006 UTC (17 years, 7 months ago) by hirami
Branch: MAIN
CVS Tags: Tombo_2_0b2, Tombo_2_0b3, Tombo_2_0b1, Tombo_2_0b4, B228, B229, B226, B227, B224, B225, B222, B223, B231, B230, HEAD
Changes since 1.13: +4 -5 lines
File MIME type: text/x-c++src
- Multi repository support(yet completed)
- Fixes to YAE
	* Hang up when *.tdt points invalid position.

1 #include <windows.h>
2 #include <tchar.h>
3 #include "TString.h"
4 #include "TomboURI.h"
5
6 static LPCTSTR pURIPrefix = TEXT("tombo://");
7
8 /////////////////////////////////////////////
9 /////////////////////////////////////////////
10 // URI implimentation
11 /////////////////////////////////////////////
12 /////////////////////////////////////////////
13
14 /////////////////////////////////////////////
15 // ctor & dtor
16 /////////////////////////////////////////////
17
18 TomboURI::TomboURI()
19 {
20 }
21
22 TomboURI::TomboURI(const TomboURI &u)
23 {
24 uri.Set(u.uri);
25 nMaxPathItem = u.nMaxPathItem;
26 }
27
28 TomboURI::~TomboURI()
29 {
30 }
31
32 TomboURI &TomboURI::operator =(const TomboURI &val)
33 {
34 uri.Set(val.uri);
35 nMaxPathItem = val.nMaxPathItem;
36 return *this;
37 }
38
39 /////////////////////////////////////////////
40 // initialize
41 /////////////////////////////////////////////
42
43 BOOL TomboURI::Init(LPCTSTR pURI)
44 {
45 // check header string
46 if (_tcsnicmp(pURI, pURIPrefix, 8) != 0) {
47 SetLastError(ERROR_INVALID_DATA);
48 return FALSE;
49 }
50
51 nMaxPathItem = 0;
52
53 // count repository string length
54 LPCTSTR p = pURI + 8;
55 LPCTSTR q;
56 DWORD nLv = 0;
57 while (1) {
58 q = GetNextSep(p);
59 if (q == NULL) {
60 int n = _tcslen(p);
61 if (nMaxPathItem < n) nMaxPathItem = n;
62 break;
63 }
64 if (nMaxPathItem < (q - p)) nMaxPathItem = q - p;
65 p = q + 1;
66 nLv++;
67 }
68
69 // check like "tombo:///"
70 if (nLv == 0) {
71 SetLastError(ERROR_INVALID_DATA);
72 return FALSE;
73 }
74
75 return uri.Set(pURI);
76 }
77
78 BOOL TomboURI::Init(const TomboURI &u)
79 {
80 uri.Set(u.uri);
81 nMaxPathItem = u.nMaxPathItem;
82 return TRUE;
83 }
84 /////////////////////////////////////////////
85 // Yet another initializer
86 /////////////////////////////////////////////
87 // This function is obsoleted and will be removed in the future.
88
89 BOOL TomboURI::InitByNotePath(LPCTSTR pRepoName, LPCTSTR pNotePath)
90 {
91 TString s;
92 if (!s.Alloc(9 + _tcslen(pRepoName) + _tcslen(pNotePath) + 1)) return FALSE;
93 wsprintf(s.Get(), TEXT("tombo://%s/"), pRepoName);
94 LPTSTR p = s.Get() + 9 + _tcslen(pRepoName);
95
96 LPCTSTR q = pNotePath;
97 if (*q == TEXT('\\')) q++;
98
99 while(*q) {
100 if (*q == TEXT('\\')) {
101 *p++ = TEXT('/');
102 q++;
103 continue;
104 }
105 #if defined(PLATFORM_WIN32)
106 if (IsDBCSLeadByte(*q)) {
107 *p++ = *q++;
108 }
109 #endif
110 *p++ = *q++;
111 }
112 *p = TEXT('\0');
113 return Init(s.Get());
114 }
115
116 /////////////////////////////////////////////
117 // skip to next separator
118 /////////////////////////////////////////////
119
120 LPCTSTR TomboURI::GetNextSep(LPCTSTR pPartPath)
121 {
122 LPCTSTR p = pPartPath;
123 while (*p) {
124 if (*p == TEXT('/')) return p;
125 p = CharNext(p);
126 }
127 return NULL;
128 }
129
130 /////////////////////////////////////////////
131 // get repository name
132 /////////////////////////////////////////////
133
134 BOOL TomboURI::GetRepositoryName(TString *pRepo) const
135 {
136 LPCTSTR p = uri.Get() + 8;
137 LPCTSTR q = GetNextSep(p);
138
139 if (!pRepo->Alloc(q - p + 1)) return FALSE;
140 _tcsncpy(pRepo->Get(), p, q - p);
141 *(pRepo->Get() + (q - p)) = TEXT('\0');
142
143 return TRUE;
144 }
145
146 /////////////////////////////////////////////
147 // get path part
148 /////////////////////////////////////////////
149
150 LPCTSTR TomboURI::GetPath() const
151 {
152 LPCTSTR p = uri.Get() + _tcslen(pURIPrefix);
153
154 // skip repository part
155 p = _tcschr(p, TEXT('/'));
156 return p;
157 }
158
159 /////////////////////////////////////////////
160 // Is the URI point to leaf node?
161 /////////////////////////////////////////////
162
163 BOOL TomboURI::IsLeaf() const
164 {
165 if (_tcslen(GetPath() + 1) == 0) return FALSE; // root
166 LPCTSTR p = uri.Get();
167 return *(p + _tcslen(p) - 1) != TEXT('/');
168 }
169
170 /////////////////////////////////////////////
171 // Root node?
172 /////////////////////////////////////////////
173
174 BOOL TomboURI::IsRoot() const
175 {
176 LPCTSTR p = _tcschr(uri.Get() + _tcslen(TEXT("tombo://")), TEXT('/'));
177 if (p == NULL) return FALSE; // invalid URI
178 if (*(p+1) == TEXT('\0')) return TRUE;
179 return FALSE;
180 }
181
182 /////////////////////////////////////////////
183 // get parent path
184 /////////////////////////////////////////////
185
186 BOOL TomboURI::GetParent(TomboURI *pParent) const
187 {
188 LPCTSTR p = GetPath();
189 LPCTSTR pBase = p;
190
191 if (*p) p++;
192
193 LPCTSTR q = NULL;
194 while (*p) {
195 if (*p == TEXT('/') && *(p+1) != TEXT('\0')) {
196 q = p;
197 }
198 p = CharNext(p);
199 }
200
201 TString s;
202 if (q == NULL) {
203 // result is root node.
204 DWORD n = GetPath() - uri.Get() + 1;
205 if (!s.Alloc(n + 1)) return FALSE;
206 _tcsncpy(s.Get(), uri.Get(), n);
207 *(s.Get() + n) = TEXT('\0');
208
209 } else {
210 if (!s.Alloc(q - uri.Get() + 2)) return FALSE;
211 _tcsncpy(s.Get(), uri.Get(), q - uri.Get());
212 *(s.Get() + (q - uri.Get())) = TEXT('\0');
213
214 _tcscat(s.Get(), TEXT("/"));
215 }
216 if (!pParent->Init(s.Get())) return FALSE;
217
218 return TRUE;
219 }
220
221 /////////////////////////////////////////////
222 // get folder to attach new note
223 /////////////////////////////////////////////
224
225 BOOL TomboURI::GetAttachFolder(TomboURI *pAttach) const
226 {
227 if (IsLeaf()) {
228 if (!GetParent(pAttach)) return FALSE;
229 } else {
230 *pAttach = *this;
231 }
232 return TRUE;
233 }
234
235 /////////////////////////////////////////////
236 // get parent path
237 /////////////////////////////////////////////
238
239 BOOL TomboURI::GetFilePath(TString *pPath) const
240 {
241 LPCTSTR p = GetPath();
242 if (*p) *p++;
243
244 if (!pPath->Alloc(_tcslen(p) + 1)) return FALSE;
245 _tcscpy(pPath->Get(), p);
246
247 LPTSTR q = pPath->Get();
248 while(q = _tcschr(q, TEXT('/'))) {
249 *q = TEXT('\\');
250 }
251 return TRUE;
252 }
253
254 /////////////////////////////////////////////
255 // get base name
256 /////////////////////////////////////////////
257
258 BOOL TomboURI::GetBaseName(TString *pBase) const
259 {
260 LPCTSTR p = GetPath();
261 LPCTSTR q = NULL;
262 while(*p) {
263 if (*p == TEXT('/') && *(p+1) != TEXT('\0')) {
264 q = p;
265 p++;
266 continue;
267 }
268 #if defined(PLATFORM_WIN32)
269 p = CharNext(p);
270 #else
271 p++;
272 #endif
273 }
274
275 if (q == NULL) {
276 // if url point to root
277 if (!pBase->Set(TEXT(""))) return FALSE;
278 return TRUE;
279 }
280 if (!pBase->Set(q + 1)) return FALSE;
281 if (*(q + _tcslen(q) - 1) == TEXT('/')) {
282 *(pBase->Get() + _tcslen(pBase->Get()) - 1) = TEXT('\0');
283 }
284 return TRUE;
285 }
286
287 /////////////////////////////////////////////
288 /////////////////////////////////////////////
289 // iterator implimentation
290 /////////////////////////////////////////////
291 /////////////////////////////////////////////
292
293 BOOL TomboURIItemIterator::Init()
294 {
295 // reallocate buffer
296 if (pBuf) delete[] pBuf;
297 pBuf = new TCHAR[pURI->GetMaxPathItem() + 1];
298 if (pBuf == NULL) return FALSE;
299
300 return TRUE;
301 }
302
303 void TomboURIItemIterator::First()
304 {
305 // seek head of path
306 LPCTSTR p = TomboURI::GetNextSep(pURI->GetFullURI() + 8);
307 p++;
308 nPos = p - pURI->GetFullURI();
309
310 Next();
311 }
312
313 LPCTSTR TomboURIItemIterator::Current()
314 {
315 if (*pBuf == TEXT('\0')) return NULL;
316 return pBuf;
317 }
318
319 void TomboURIItemIterator::Next()
320 {
321 LPCTSTR p = pURI->GetFullURI() + nPos;
322
323 LPTSTR q = pBuf;
324 while (*p && *p != TEXT('/')) {
325 #if defined(PLATFORM_WIN32)
326 if (IsDBCSLeadByte(*p)) {
327 *q++ = *p++;
328 }
329 #endif
330 *q++ = *p++;
331 }
332 *q = TEXT('\0');
333 if (*p == TEXT('/')) p++;
334
335 nPos = p - pURI->GetFullURI();
336 }
337
338 BOOL TomboURIItemIterator::IsLeaf()
339 {
340 LPCTSTR p = pURI->GetFullURI() + nPos;
341 return (*p == TEXT('\0')) && (*(p-1) != TEXT('/'));
342 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26