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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
/*
* XMail by Davide Libenzi ( Intranet and Internet mail server )
* Copyright (C) 1999,..,2004 Davide Libenzi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <davidel@xmailserver.org>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "Array.h"
#include "ShBlocks.h"
#include "StrUtils.h"
#include "SList.h"
#include "BuffSock.h"
#include "MiscUtils.h"
#include "TabIndex.h"
/* The index version MUST be incremented at every file format change */
#define TAB_INDEX_CURR_VERSION 1
#define TAB_SAMPLE_LINES 32
#define TAB_MIN_HASH_SIZE 17
#define TAB_INDEX_DIR "tabindex"
#define TAB_INDEX_MAGIC (*(SYS_UINT32 *) "ABDL")
#define KEY_BUFFER_SIZE 1024
#define TAB_RECORD_BUFFER_SIZE 2048
#define TOKEN_SEP_STR "\t"
#define TAB_INIT_RESSET_SIZE 128
struct HashLink {
SysListHead LLink;
TabIdxUINT uOffset;
};
struct HashNode {
SysListHead NodeList;
TabIdxUINT uCount;
};
struct HashFileHeader {
SYS_UINT32 uMagic;
SYS_UINT32 uVersion;
TabIdxUINT uHashSize;
};
struct TabHashIndex {
HashFileHeader HFH;
FILE *pIndexFile;
};
struct IndexLookupData {
ARRAY_HANDLE hArray;
long lRecCount;
FILE *pTabFile;
TabIdxUINT *pHTblC;
long lArIdx;
long lHTblI;
};
static int TbixCalcHashSize(FILE *pTabFile, char *pszLineBuffer, int iBufferSize);
static int TbixFreeHash(HashNode *pHash, int iHashSize);
static int TbixBuildKey(char *pszKey, va_list Args, bool bCaseSens);
static int TbixBuildKey(char *pszKey, char const *const *ppszTabTokens,
int const *piFieldsIdx, bool bCaseSens);
static TabIdxUINT TbixHashData(char const *pData, long lSize);
static int TbixOpenIndex(char const *pszIndexFile, TabHashIndex &THI);
static int TbixCloseIndex(TabHashIndex &THI);
static int TbixCheckIndex(char const *pszIndexFile);
static TabIdxUINT *TbixReadTable(TabHashIndex &THI, TabIdxUINT uHashVal);
static char **TbixLoadRecord(FILE *pTabFile, TabIdxUINT uOffset);
static int TbixCalcHashSize(FILE *pTabFile, char *pszLineBuffer, int iBufferSize)
{
int iSampleLines = 0;
unsigned long ulOrigOffset = (unsigned long) ftell(pTabFile);
unsigned long ulCurrOffset = 0;
unsigned long ulLineSize = 0;
rewind(pTabFile);
while (iSampleLines < TAB_SAMPLE_LINES) {
if (MscGetString(pTabFile, pszLineBuffer, iBufferSize - 1) == NULL)
break;
unsigned long ulOffset = (unsigned long) ftell(pTabFile);
if (!IsEmptyString(pszLineBuffer) &&
pszLineBuffer[0] != TAB_COMMENT_CHAR) {
ulLineSize += ulOffset - ulCurrOffset;
++iSampleLines;
}
ulCurrOffset = ulOffset;
}
if (iSampleLines == 0) {
fseek(pTabFile, ulOrigOffset, SEEK_SET);
return TAB_MIN_HASH_SIZE;
}
ulLineSize /= iSampleLines;
fseek(pTabFile, 0, SEEK_END);
unsigned long ulFileSize = (unsigned long) ftell(pTabFile);
fseek(pTabFile, ulOrigOffset, SEEK_SET);
int iHashSize = (int) (ulFileSize / ulLineSize) + TAB_MIN_HASH_SIZE;
while (!IsPrimeNumber(iHashSize))
++iHashSize;
return iHashSize;
}
char *TbixGetIndexFile(char const *pszTabFilePath, int const *piFieldsIdx, char *pszIndexFile)
{
char szFileDir[SYS_MAX_PATH] = "";
char szFileName[SYS_MAX_PATH] = "";
MscSplitPath(pszTabFilePath, szFileDir, sizeof(szFileDir),
szFileName, sizeof(szFileName), NULL, 0);
sprintf(pszIndexFile, "%s%s%s%s-", szFileDir, TAB_INDEX_DIR, SYS_SLASH_STR, szFileName);
for (int i = 0; piFieldsIdx[i] != INDEX_SEQUENCE_TERMINATOR; i++) {
char szIndex[64] = "";
sprintf(szIndex, "%02d", piFieldsIdx[i]);
strcat(pszIndexFile, szIndex);
}
strcat(pszIndexFile, ".hdx");
return pszIndexFile;
}
static int TbixFreeHash(HashNode *pHash, int iHashSize)
{
for (int i = 0; i < iHashSize; i++) {
SysListHead *pHead = &pHash[i].NodeList;
SysListHead *pLLink;
while ((pLLink = SYS_LIST_FIRST(pHead)) != NULL) {
HashLink *pHL = SYS_LIST_ENTRY(pLLink, HashLink, LLink);
SYS_LIST_DEL(&pHL->LLink);
SysFree(pHL);
}
}
SysFree(pHash);
return 0;
}
int TbixCreateIndex(char const *pszTabFilePath, int const *piFieldsIdx, bool bCaseSens,
int (*pHashFunc) (char const *const *, int const *, TabIdxUINT *, bool))
{
/* Adjust hash function */
if (pHashFunc == NULL)
pHashFunc = TbixCalculateHash;
/* Build index file name */
char szIndexFile[SYS_MAX_PATH] = "";
if (TbixGetIndexFile(pszTabFilePath, piFieldsIdx, szIndexFile) < 0)
return ErrGetErrorCode();
FILE *pTabFile = fopen(pszTabFilePath, "rb");
if (pTabFile == NULL) {
ErrSetErrorCode(ERR_FILE_OPEN, pszTabFilePath);
return ERR_FILE_OPEN;
}
/* Calculate file lookup hash size */
char szLineBuffer[TAB_RECORD_BUFFER_SIZE] = "";
int iHashSize = TbixCalcHashSize(pTabFile, szLineBuffer, sizeof(szLineBuffer));
/* Alloc and init hash records */
HashNode *pHash = (HashNode *) SysAlloc(iHashSize * sizeof(HashNode));
if (pHash == NULL) {
fclose(pTabFile);
return ErrGetErrorCode();
}
int i;
for (i = 0; i < iHashSize; i++) {
SYS_INIT_LIST_HEAD(&pHash[i].NodeList);
pHash[i].uCount = 0;
}
/* Setup indexes records */
for (;;) {
/* Get current offset */
TabIdxUINT uFileOffset = (TabIdxUINT) ftell(pTabFile);
if (MscGetString(pTabFile, szLineBuffer, sizeof(szLineBuffer) - 1) == NULL)
break;
if (szLineBuffer[0] == TAB_COMMENT_CHAR)
continue;
char **ppszTabTokens = StrGetTabLineStrings(szLineBuffer);
if (ppszTabTokens == NULL)
continue;
/* Calculate hash value */
TabIdxUINT uHashVal = 0;
if ((*pHashFunc)(ppszTabTokens, piFieldsIdx, &uHashVal, bCaseSens) == 0) {
int iHashIndex = (int) (uHashVal % (TabIdxUINT) iHashSize);
HashLink *pHL = (HashLink *) SysAlloc(sizeof(HashLink));
if (pHL == NULL) {
StrFreeStrings(ppszTabTokens);
TbixFreeHash(pHash, iHashSize);
fclose(pTabFile);
return ErrGetErrorCode();
}
pHL->uOffset = uFileOffset;
SYS_LIST_ADDT(&pHL->LLink, &pHash[iHashIndex].NodeList);
++pHash[iHashIndex].uCount;
}
StrFreeStrings(ppszTabTokens);
}
fclose(pTabFile);
/* Write index file */
FILE *pIndexFile = fopen(szIndexFile, "wb");
if (pIndexFile == NULL) {
TbixFreeHash(pHash, iHashSize);
ErrSetErrorCode(ERR_FILE_CREATE, szIndexFile);
return ERR_FILE_CREATE;
}
/* Write file header */
HashFileHeader HFH;
ZeroData(HFH);
HFH.uMagic = TAB_INDEX_MAGIC;
HFH.uVersion = TAB_INDEX_CURR_VERSION;
HFH.uHashSize = (TabIdxUINT) iHashSize;
if (!fwrite(&HFH, sizeof(HFH), 1, pIndexFile)) {
fclose(pIndexFile);
SysRemove(szIndexFile);
TbixFreeHash(pHash, iHashSize);
ErrSetErrorCode(ERR_FILE_WRITE);
return ERR_FILE_WRITE;
}
/* Dump main table */
TabIdxUINT uCurrOffset = sizeof(HFH) + iHashSize * sizeof(TabIdxUINT);
for (i = 0; i < iHashSize; i++) {
TabIdxUINT uTableOffset = 0;
if (pHash[i].uCount != 0) {
uTableOffset = uCurrOffset;
uCurrOffset += (pHash[i].uCount + 1) * sizeof(TabIdxUINT);
}
if (!fwrite(&uTableOffset, sizeof(uTableOffset), 1, pIndexFile)) {
fclose(pIndexFile);
SysRemove(szIndexFile);
TbixFreeHash(pHash, iHashSize);
ErrSetErrorCode(ERR_FILE_WRITE);
return ERR_FILE_WRITE;
}
}
/* Dump hash tables */
for (i = 0; i < iHashSize; i++) {
TabIdxUINT uRecCount = pHash[i].uCount;
if (uRecCount != 0) {
if (!fwrite(&uRecCount, sizeof(uRecCount), 1, pIndexFile)) {
fclose(pIndexFile);
SysRemove(szIndexFile);
TbixFreeHash(pHash, iHashSize);
ErrSetErrorCode(ERR_FILE_WRITE);
return ERR_FILE_WRITE;
}
SysListHead *pHead = &pHash[i].NodeList;
SysListHead *pLLink;
SYS_LIST_FOR_EACH(pLLink, pHead) {
HashLink *pHL = SYS_LIST_ENTRY(pLLink, HashLink, LLink);
TabIdxUINT uRecordOffset = pHL->uOffset;
if (!fwrite(&uRecordOffset, sizeof(uRecordOffset), 1, pIndexFile)) {
fclose(pIndexFile);
SysRemove(szIndexFile);
TbixFreeHash(pHash, iHashSize);
ErrSetErrorCode(ERR_FILE_WRITE);
return ERR_FILE_WRITE;
}
}
}
}
fclose(pIndexFile);
TbixFreeHash(pHash, iHashSize);
return 0;
}
static int TbixBuildKey(char *pszKey, va_list Args, bool bCaseSens)
{
char const *pszToken;
SetEmptyString(pszKey);
for (int i = 0; (pszToken = va_arg(Args, char *)) != NULL; i++) {
if (i > 0)
strcat(pszKey, TOKEN_SEP_STR);
strcat(pszKey, pszToken);
}
if (!bCaseSens)
StrLower(pszKey);
return 0;
}
static int TbixBuildKey(char *pszKey, char const *const *ppszTabTokens,
int const *piFieldsIdx, bool bCaseSens)
{
int iFieldsCount = StrStringsCount(ppszTabTokens);
SetEmptyString(pszKey);
for (int i = 0; piFieldsIdx[i] != INDEX_SEQUENCE_TERMINATOR; i++) {
if (piFieldsIdx[i] < 0 || piFieldsIdx[i] >= iFieldsCount) {
ErrSetErrorCode(ERR_BAD_TAB_INDEX_FIELD);
return ERR_BAD_TAB_INDEX_FIELD;
}
if (i > 0)
strcat(pszKey, TOKEN_SEP_STR);
strcat(pszKey, ppszTabTokens[piFieldsIdx[i]]);
}
if (!bCaseSens)
StrLower(pszKey);
return 0;
}
static TabIdxUINT TbixHashData(char const *pData, long lSize)
{
unsigned long ulHashVal = MscHashString(pData, lSize);
/*
* We may want to use upper bits also, in architectures
* where sizeof(long) > 4 ...
*/
return (TabIdxUINT) ulHashVal;
}
int TbixCalculateHash(char const *const *ppszTabTokens, int const *piFieldsIdx,
TabIdxUINT *puHashVal, bool bCaseSens)
{
char szKey[KEY_BUFFER_SIZE] = "";
if (TbixBuildKey(szKey, ppszTabTokens, piFieldsIdx, bCaseSens) < 0)
return ErrGetErrorCode();
*puHashVal = TbixHashData(szKey, strlen(szKey));
return 0;
}
static int TbixOpenIndex(char const *pszIndexFile, TabHashIndex &THI)
{
FILE *pIndexFile = fopen(pszIndexFile, "rb");
if (pIndexFile == NULL) {
ErrSetErrorCode(ERR_FILE_OPEN, pszIndexFile);
return ERR_FILE_OPEN;
}
/* Read header and check signature */
ZeroData(THI);
if (!fread(&THI.HFH, sizeof(THI.HFH), 1, pIndexFile)) {
fclose(pIndexFile);
ErrSetErrorCode(ERR_FILE_READ, pszIndexFile);
return ERR_FILE_READ;
}
if (THI.HFH.uMagic != TAB_INDEX_MAGIC || THI.HFH.uVersion != TAB_INDEX_CURR_VERSION) {
fclose(pIndexFile);
ErrSetErrorCode(ERR_BAD_INDEX_FILE, pszIndexFile);
return ERR_BAD_INDEX_FILE;
}
THI.pIndexFile = pIndexFile;
return 0;
}
static int TbixCloseIndex(TabHashIndex &THI)
{
fclose(THI.pIndexFile);
ZeroData(THI);
return 0;
}
static int TbixCheckIndex(char const *pszIndexFile)
{
TabHashIndex THI;
if (TbixOpenIndex(pszIndexFile, THI) < 0)
return ErrGetErrorCode();
TbixCloseIndex(THI);
return 0;
}
static TabIdxUINT *TbixReadTable(TabHashIndex &THI, TabIdxUINT uHashVal)
{
TabIdxUINT uHashIndex = uHashVal % THI.HFH.uHashSize, uTableOffset, uTableSize;
unsigned long ulTableOffset = sizeof(HashFileHeader) + uHashIndex * sizeof(TabIdxUINT);
if (fseek(THI.pIndexFile, ulTableOffset, SEEK_SET) != 0) {
ErrSetErrorCode(ERR_BAD_INDEX_FILE);
return NULL;
}
if (!fread(&uTableOffset, sizeof(uTableOffset), 1, THI.pIndexFile)) {
ErrSetErrorCode(ERR_FILE_READ);
return NULL;
}
if (uTableOffset == 0) {
ErrSetErrorCode(ERR_RECORD_NOT_FOUND);
return NULL;
}
if (fseek(THI.pIndexFile, uTableOffset, SEEK_SET) != 0) {
ErrSetErrorCode(ERR_BAD_INDEX_FILE);
return NULL;
}
if (!fread(&uTableSize, sizeof(uTableSize), 1, THI.pIndexFile)) {
ErrSetErrorCode(ERR_FILE_READ);
return NULL;
}
TabIdxUINT *pOffTable = (TabIdxUINT *) SysAlloc((uTableSize + 1) * sizeof(TabIdxUINT));
if (pOffTable == NULL)
return NULL;
pOffTable[0] = uTableSize;
if (!fread(&pOffTable[1], uTableSize * sizeof(TabIdxUINT), 1, THI.pIndexFile)) {
SysFree(pOffTable);
ErrSetErrorCode(ERR_FILE_READ);
return NULL;
}
return pOffTable;
}
char **TbixLookup(char const *pszTabFilePath, int const *piFieldsIdx, bool bCaseSens, ...)
{
/* Build index file name */
char szIndexFile[SYS_MAX_PATH] = "";
if (TbixGetIndexFile(pszTabFilePath, piFieldsIdx, szIndexFile) < 0)
return NULL;
/* Calculate key & hash */
va_list Args;
char szRefKey[KEY_BUFFER_SIZE] = "";
va_start(Args, bCaseSens);
if (TbixBuildKey(szRefKey, Args, bCaseSens) < 0) {
va_end(Args);
return NULL;
}
va_end(Args);
TabIdxUINT uHashVal = TbixHashData(szRefKey, strlen(szRefKey));
/* Open index */
TabHashIndex THI;
if (TbixOpenIndex(szIndexFile, THI) < 0)
return NULL;
/* Try to lookup records */
TabIdxUINT *pHashTable = TbixReadTable(THI, uHashVal);
TbixCloseIndex(THI);
if (pHashTable == NULL)
return NULL;
/* Search for the matched one */
FILE *pTabFile = fopen(pszTabFilePath, "rb");
if (pTabFile == NULL) {
SysFree(pHashTable);
ErrSetErrorCode(ERR_FILE_OPEN, pszTabFilePath);
return NULL;
}
int iHashNodes = (int) pHashTable[0];
for (int i = 0; i < iHashNodes; i++) {
char **ppszTabTokens = TbixLoadRecord(pTabFile, pHashTable[i + 1]);
if (ppszTabTokens != NULL) {
char szKey[KEY_BUFFER_SIZE] = "";
if (TbixBuildKey(szKey, ppszTabTokens, piFieldsIdx, bCaseSens) == 0) {
if (bCaseSens) {
if (strcmp(szKey, szRefKey) == 0) {
fclose(pTabFile);
SysFree(pHashTable);
return ppszTabTokens;
}
} else {
if (stricmp(szKey, szRefKey) == 0) {
fclose(pTabFile);
SysFree(pHashTable);
return ppszTabTokens;
}
}
}
StrFreeStrings(ppszTabTokens);
}
}
fclose(pTabFile);
SysFree(pHashTable);
ErrSetErrorCode(ERR_RECORD_NOT_FOUND);
return NULL;
}
static char **TbixLoadRecord(FILE *pTabFile, TabIdxUINT uOffset)
{
char szLineBuffer[TAB_RECORD_BUFFER_SIZE] = "";
if (fseek(pTabFile, uOffset, SEEK_SET) != 0) {
ErrSetErrorCode(ERR_BAD_INDEX_FILE);
return NULL;
}
if (MscGetString(pTabFile, szLineBuffer, sizeof(szLineBuffer) - 1) == NULL) {
ErrSetErrorCode(ERR_FILE_READ);
return NULL;
}
return StrGetTabLineStrings(szLineBuffer);
}
int TbixCheckIndex(char const *pszTabFilePath, int const *piFieldsIdx, bool bCaseSens,
int (*pHashFunc) (char const *const *, int const *, TabIdxUINT *, bool))
{
SYS_FILE_INFO FI_Tab;
if (SysGetFileInfo(pszTabFilePath, FI_Tab) < 0)
return ErrGetErrorCode();
/* Build index file name */
char szIndexFile[SYS_MAX_PATH] = "";
if (TbixGetIndexFile(pszTabFilePath, piFieldsIdx, szIndexFile) < 0)
return ErrGetErrorCode();
/* Compare TAB <-> Index dates */
SYS_FILE_INFO FI_Index;
if (SysGetFileInfo(szIndexFile, FI_Index) < 0 || FI_Tab.tMod > FI_Index.tMod ||
TbixCheckIndex(szIndexFile) < 0) {
/* Rebuild the index */
if (TbixCreateIndex(pszTabFilePath, piFieldsIdx, bCaseSens, pHashFunc) < 0)
return ErrGetErrorCode();
}
return 0;
}
INDEX_HANDLE TbixOpenHandle(char const *pszTabFilePath, int const *piFieldsIdx,
TabIdxUINT const *puHashVal, int iNumVals)
{
int i;
long lRecCount;
ARRAY_HANDLE hArray;
TabHashIndex THI;
char szIndexFile[SYS_MAX_PATH] = "";
if (TbixGetIndexFile(pszTabFilePath, piFieldsIdx, szIndexFile) < 0 ||
(hArray = ArrayCreate(TAB_INIT_RESSET_SIZE)) == INVALID_ARRAY_HANDLE)
return INVALID_INDEX_HANDLE;
if (TbixOpenIndex(szIndexFile, THI) < 0) {
ArrayFree(hArray, MscSysFreeCB, NULL);
return INVALID_INDEX_HANDLE;
}
for (i = 0, lRecCount = 0; i < iNumVals; i++) {
TabIdxUINT *pHashTable = TbixReadTable(THI, puHashVal[i]);
if (pHashTable != NULL) {
if (ArrayAppend(hArray, pHashTable) < 0) {
SysFree(pHashTable);
ArrayFree(hArray, MscSysFreeCB, NULL);
TbixCloseIndex(THI);
return INVALID_INDEX_HANDLE;
}
lRecCount += (long) pHashTable[0];
}
}
TbixCloseIndex(THI);
if (lRecCount == 0) {
ArrayFree(hArray, MscSysFreeCB, NULL);
ErrSetErrorCode(ERR_RECORD_NOT_FOUND);
return INVALID_INDEX_HANDLE;
}
/* Open tab file */
FILE *pTabFile = fopen(pszTabFilePath, "rb");
if (pTabFile == NULL) {
ArrayFree(hArray, MscSysFreeCB, NULL);
ErrSetErrorCode(ERR_FILE_OPEN, pszTabFilePath);
return INVALID_INDEX_HANDLE;
}
/* Setup lookup struct */
IndexLookupData *pILD = (IndexLookupData *) SysAlloc(sizeof(IndexLookupData));
if (pILD == NULL) {
fclose(pTabFile);
ArrayFree(hArray, MscSysFreeCB, NULL);
return INVALID_INDEX_HANDLE;
}
pILD->hArray = hArray;
pILD->lRecCount = lRecCount;
pILD->pTabFile = pTabFile;
return (INDEX_HANDLE) pILD;
}
int TbixCloseHandle(INDEX_HANDLE hIndexLookup)
{
IndexLookupData *pILD = (IndexLookupData *) hIndexLookup;
fclose(pILD->pTabFile);
ArrayFree(pILD->hArray, MscSysFreeCB, NULL);
SysFree(pILD);
return 0;
}
long TbixLookedUpRecords(INDEX_HANDLE hIndexLookup)
{
IndexLookupData *pILD = (IndexLookupData *) hIndexLookup;
return pILD->lRecCount;
}
char **TbixFirstRecord(INDEX_HANDLE hIndexLookup)
{
IndexLookupData *pILD = (IndexLookupData *) hIndexLookup;
pILD->lArIdx = 0;
pILD->lHTblI = 0;
if ((pILD->pHTblC = (TabIdxUINT *) ArrayGet(pILD->hArray,
pILD->lArIdx)) == NULL) {
ErrSetErrorCode(ERR_RECORD_NOT_FOUND);
return NULL;
}
return TbixLoadRecord(pILD->pTabFile, pILD->pHTblC[++pILD->lHTblI]);
}
char **TbixNextRecord(INDEX_HANDLE hIndexLookup)
{
IndexLookupData *pILD = (IndexLookupData *) hIndexLookup;
if (pILD->pHTblC == NULL) {
ErrSetErrorCode(ERR_BAD_SEQUENCE);
return NULL;
}
if (pILD->lHTblI == (long) pILD->pHTblC[0]) {
pILD->lArIdx++;
if ((pILD->pHTblC = (TabIdxUINT *) ArrayGet(pILD->hArray,
pILD->lArIdx)) == NULL) {
ErrSetErrorCode(ERR_RECORD_NOT_FOUND);
return NULL;
}
pILD->lHTblI = 0;
}
return TbixLoadRecord(pILD->pTabFile, pILD->pHTblC[++pILD->lHTblI]);
}
|