| 1 |
#include <windows.h> |
| 2 |
#include <tchar.h> |
| 3 |
#include "Tombo.h" |
| 4 |
#include "UniConv.h" |
| 5 |
#include "TString.h" |
| 6 |
#include "TomboURI.h" |
| 7 |
#include "VarBuffer.h" |
| 8 |
#include "URIScanner.h" |
| 9 |
#include "Repository.h" |
| 10 |
|
| 11 |
#define URILIST_INITSIZE 20 |
| 12 |
#define URILIST_DELTASIZE 10 |
| 13 |
|
| 14 |
///////////////////////////////////////////////// |
| 15 |
////////////////////////////////////////////////// |
| 16 |
// URIList implementation |
| 17 |
////////////////////////////////////////////////// |
| 18 |
////////////////////////////////////////////////// |
| 19 |
|
| 20 |
URIList::URIList() |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
URIList::~URIList() |
| 25 |
{ |
| 26 |
for (DWORD i = 0; i < liList.NumItems(); i++) { |
| 27 |
URIListItem *pItem = liList.GetUnit(i); |
| 28 |
delete pItem->pURI; |
| 29 |
delete [] pItem->pTitle; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
BOOL URIList::Init() |
| 34 |
{ |
| 35 |
if (!liList.Init(URILIST_INITSIZE, URILIST_DELTASIZE)) return FALSE; |
| 36 |
return TRUE; |
| 37 |
} |
| 38 |
|
| 39 |
BOOL URIList::Add(const TomboURI *pURI, LPCTSTR pTitle) |
| 40 |
{ |
| 41 |
URIListItem item; |
| 42 |
if (pURI) { |
| 43 |
item.pURI = new TomboURI(*pURI); |
| 44 |
if (item.pURI == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } |
| 45 |
} |
| 46 |
|
| 47 |
if (pTitle) { |
| 48 |
item.pTitle = StringDup(pTitle); |
| 49 |
if (item.pTitle == NULL) return FALSE; |
| 50 |
} |
| 51 |
|
| 52 |
if (!liList.Add(&item)) return FALSE; |
| 53 |
|
| 54 |
return TRUE; |
| 55 |
} |
| 56 |
|
| 57 |
///////////////////////////////////////////////// |
| 58 |
////////////////////////////////////////////////// |
| 59 |
// URIScanner implementation |
| 60 |
////////////////////////////////////////////////// |
| 61 |
////////////////////////////////////////////////// |
| 62 |
|
| 63 |
////////////////////////////////////////////////// |
| 64 |
// ctor & Initialize |
| 65 |
////////////////////////////////////////////////// |
| 66 |
|
| 67 |
URIScanner::URIScanner() : pBaseURI(NULL), pCurrentURI(NULL), pTop(NULL), pBaseTitle(NULL) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
BOOL URIScanner::Init(IEnumRepository *pRepo, const TomboURI *pURI, BOOL bSKE) |
| 72 |
{ |
| 73 |
// initialize value |
| 74 |
pRepository = pRepo; |
| 75 |
bSkipEncrypt = bSKE; |
| 76 |
iDirection = 1; |
| 77 |
|
| 78 |
// check URI |
| 79 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 80 |
if (!pRepository->GetOption(pURI, &opt)) return FALSE; |
| 81 |
|
| 82 |
if (!opt.bFolder) { |
| 83 |
SetLastError(ERROR_INVALID_PARAMETER); |
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
|
| 87 |
// set URI |
| 88 |
pBaseURI = new TomboURI(*pURI); |
| 89 |
|
| 90 |
return TRUE; |
| 91 |
} |
| 92 |
|
| 93 |
////////////////////////////////////////////////// |
| 94 |
// dtor |
| 95 |
////////////////////////////////////////////////// |
| 96 |
|
| 97 |
URIScanner::~URIScanner() |
| 98 |
{ |
| 99 |
ClearStack(); |
| 100 |
delete pBaseURI; |
| 101 |
delete pBaseTitle; |
| 102 |
} |
| 103 |
|
| 104 |
////////////////////////////////////////////////// |
| 105 |
// treat stack frame |
| 106 |
////////////////////////////////////////////////// |
| 107 |
|
| 108 |
void URIScanner::ClearStack() |
| 109 |
{ |
| 110 |
while(pTop) { |
| 111 |
LeaveFrame(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void URIScanner::LeaveFrame() |
| 116 |
{ |
| 117 |
StackFrame *pSF = pTop; |
| 118 |
pTop = pSF->pNext; |
| 119 |
|
| 120 |
delete pSF->pList; |
| 121 |
delete pSF; |
| 122 |
} |
| 123 |
|
| 124 |
BOOL URIScanner::PushFrame(const TomboURI *pURI) |
| 125 |
{ |
| 126 |
StackFrame *pSF = new StackFrame(); |
| 127 |
if (pSF == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } |
| 128 |
|
| 129 |
BOOL bLoose; |
| 130 |
pSF->pList = pRepository->GetChild(pCurrentURI, bSkipEncrypt, FALSE, &bLoose); |
| 131 |
if (pSF->pList == NULL) { |
| 132 |
DWORD n = GetLastError(); |
| 133 |
if (GetLastError() == ERROR_CANCELLED) { |
| 134 |
StopScan(); |
| 135 |
delete pSF; |
| 136 |
return TRUE; |
| 137 |
} |
| 138 |
return FALSE; |
| 139 |
} |
| 140 |
if (iDirection == 1) { |
| 141 |
pSF->nPos = 0; |
| 142 |
} else { |
| 143 |
pSF->nPos = pSF->pList->GetSize() - 1; |
| 144 |
} |
| 145 |
|
| 146 |
pSF->pNext = pTop; |
| 147 |
pTop = pSF; |
| 148 |
|
| 149 |
return TRUE; |
| 150 |
} |
| 151 |
|
| 152 |
////////////////////////////////////////////////// |
| 153 |
// cutomisable methods |
| 154 |
////////////////////////////////////////////////// |
| 155 |
|
| 156 |
void URIScanner::InitialScan() {} |
| 157 |
void URIScanner::AfterScan() {} |
| 158 |
void URIScanner::PreFolder() {} |
| 159 |
void URIScanner::PostFolder() {} |
| 160 |
void URIScanner::Node() {} |
| 161 |
|
| 162 |
////////////////////////////////////////////////// |
| 163 |
// Scan repository |
| 164 |
////////////////////////////////////////////////// |
| 165 |
|
| 166 |
BOOL URIScanner::FullScan() |
| 167 |
{ |
| 168 |
return Scan(NULL, FALSE); |
| 169 |
} |
| 170 |
|
| 171 |
////////////////////////////////////////////////// |
| 172 |
// Scan repository |
| 173 |
////////////////////////////////////////////////// |
| 174 |
|
| 175 |
BOOL URIScanner::Scan(const TomboURI *pStartURI, BOOL bReverse) |
| 176 |
{ |
| 177 |
// Initialize |
| 178 |
if (bReverse) { |
| 179 |
iDirection = -1; |
| 180 |
} else { |
| 181 |
iDirection = 1; |
| 182 |
} |
| 183 |
bStopScan = FALSE; |
| 184 |
pBaseTitle = new TString(); |
| 185 |
if (!pRepository->GetHeadLine(pBaseURI, pBaseTitle)) return FALSE; |
| 186 |
|
| 187 |
// set marker and call initialize method |
| 188 |
pCurrentURI = pBaseURI; |
| 189 |
pTitle = pBaseTitle->Get(); |
| 190 |
InitialScan(); |
| 191 |
|
| 192 |
|
| 193 |
// create stack frame |
| 194 |
if (pStartURI == NULL || _tcscmp(pStartURI->GetFullURI(), pBaseURI->GetFullURI()) == 0) { |
| 195 |
if (!PushFrame(pCurrentURI)) return FALSE; |
| 196 |
if (bStopScan) { |
| 197 |
AfterScan(); |
| 198 |
return TRUE; |
| 199 |
} |
| 200 |
PreFolder(); |
| 201 |
} else { |
| 202 |
// if pStartURI is passed, traverse the tree to create stack frame. |
| 203 |
if (!MakeFrame(pStartURI)) { |
| 204 |
return FALSE; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
while(pTop) { |
| 209 |
// enum current frame |
| 210 |
while (pTop->nPos >= 0 && pTop->nPos < pTop->pList->GetSize()) { |
| 211 |
|
| 212 |
if (bStopScan) break; |
| 213 |
|
| 214 |
// update marker |
| 215 |
pCurrentURI = pTop->pList->GetURI(pTop->nPos); |
| 216 |
pTitle = pTop->pList->GetTitle(pTop->nPos); |
| 217 |
|
| 218 |
// check folder or not |
| 219 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 220 |
if (!pRepository->GetOption(pCurrentURI, &opt)) return FALSE; |
| 221 |
|
| 222 |
if (opt.bFolder) { |
| 223 |
// create new frame |
| 224 |
if (!PushFrame(pCurrentURI)) return FALSE; |
| 225 |
if (bStopScan) break; |
| 226 |
PreFolder(); |
| 227 |
continue; |
| 228 |
} else { |
| 229 |
Node(); |
| 230 |
} |
| 231 |
pTop->nPos += iDirection; |
| 232 |
} |
| 233 |
|
| 234 |
// when enumeration finished, |
| 235 |
LeaveFrame(); |
| 236 |
if (pTop) { |
| 237 |
pCurrentURI = pTop->pList->GetURI(pTop->nPos); |
| 238 |
pTitle = pTop->pList->GetTitle(pTop->nPos); |
| 239 |
} else { |
| 240 |
pCurrentURI = pBaseURI; |
| 241 |
pTitle = pBaseTitle->Get(); |
| 242 |
} |
| 243 |
PostFolder(); |
| 244 |
if (pTop) { |
| 245 |
pTop->nPos += iDirection; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// call finalize method |
| 250 |
AfterScan(); |
| 251 |
|
| 252 |
return TRUE; |
| 253 |
} |
| 254 |
|
| 255 |
////////////////////////////////////////////////// |
| 256 |
// |
| 257 |
////////////////////////////////////////////////// |
| 258 |
|
| 259 |
BOOL URIScanner::MakeFrame(const TomboURI *pStartURI) |
| 260 |
{ |
| 261 |
// check base uri and start uri |
| 262 |
TString repB, repS; |
| 263 |
if (!pBaseURI->GetRepositoryName(&repB) || !pStartURI->GetRepositoryName(&repS) || |
| 264 |
_tcscmp(repB.Get(), repS.Get()) != 0) return FALSE; |
| 265 |
|
| 266 |
TomboURIItemIterator itrB(pBaseURI); |
| 267 |
TomboURIItemIterator itrS(pStartURI); |
| 268 |
if (!itrB.Init() || !itrS.Init()) return FALSE; |
| 269 |
itrB.First(); itrS.First(); |
| 270 |
while(TRUE) { |
| 271 |
LPCTSTR pB = itrB.Current(); |
| 272 |
LPCTSTR pS = itrS.Current(); |
| 273 |
|
| 274 |
if (pB == NULL || pS == NULL) { |
| 275 |
if (pS == NULL && pB != NULL) return FALSE; |
| 276 |
break; |
| 277 |
} |
| 278 |
|
| 279 |
if (_tcscmp(pB, pS) != 0) return FALSE; |
| 280 |
|
| 281 |
itrB.Next(); |
| 282 |
itrS.Next(); |
| 283 |
} |
| 284 |
|
| 285 |
// OK, stack frames. |
| 286 |
|
| 287 |
const TomboURI *pURI = pCurrentURI; |
| 288 |
|
| 289 |
pTitle = pBaseTitle->Get(); |
| 290 |
|
| 291 |
// stack first frame |
| 292 |
if (!PushFrame(pURI)) return FALSE; |
| 293 |
PreFolder(); |
| 294 |
|
| 295 |
TString shl; |
| 296 |
|
| 297 |
LPCTSTR pElement = itrS.Current(); |
| 298 |
LPCTSTR pHeadLine; |
| 299 |
|
| 300 |
do { |
| 301 |
// get each element |
| 302 |
if (itrS.IsLeaf()) { |
| 303 |
if (!pRepository->GetHeadLine(pStartURI, &shl)) return FALSE; |
| 304 |
pHeadLine = shl.Get(); |
| 305 |
|
| 306 |
} else { |
| 307 |
pHeadLine = pElement; |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
while (pTop->nPos >= 0 && pTop->nPos < pTop->pList->GetSize()) { |
| 312 |
LPCTSTR pElem = pTop->pList->GetTitle(pTop->nPos); |
| 313 |
|
| 314 |
if (_tcscmp(pHeadLine, pElem) == 0) { |
| 315 |
// path is matched. |
| 316 |
|
| 317 |
pCurrentURI = pTop->pList->GetURI(pTop->nPos); |
| 318 |
pTitle = pElem; |
| 319 |
|
| 320 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 321 |
if (!pRepository->GetOption(pCurrentURI, &opt)) return FALSE; |
| 322 |
if (opt.bFolder) { |
| 323 |
if (!PushFrame(pCurrentURI)) return FALSE; |
| 324 |
if (bStopScan) break; |
| 325 |
PreFolder(); |
| 326 |
break; |
| 327 |
} else { |
| 328 |
break; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
pTop->nPos += iDirection; |
| 333 |
} |
| 334 |
|
| 335 |
itrS.Next(); |
| 336 |
} while((pElement = itrS.Current()) != NULL); |
| 337 |
|
| 338 |
|
| 339 |
return TRUE; |
| 340 |
} |