diff --git a/src/apps/mail/WIndex.cpp b/src/apps/mail/WIndex.cpp index 40142227b9..9a39c52c16 100644 --- a/src/apps/mail/WIndex.cpp +++ b/src/apps/mail/WIndex.cpp @@ -59,7 +59,7 @@ unsigned long update_crc(unsigned long crc_accum, const char *data_blk_ptr, FileEntry::FileEntry(void) { - + } @@ -67,7 +67,7 @@ FileEntry::FileEntry(const char *entryString) : BString(entryString) { - + } @@ -76,7 +76,7 @@ WIndex::SetTo(const char *dataPath, const char *indexPath) { BFile* dataFile; BFile indexFile; - + dataFile = new BFile(); if (dataFile->SetTo(dataPath, B_READ_ONLY) != B_OK) { @@ -84,12 +84,12 @@ WIndex::SetTo(const char *dataPath, const char *indexPath) } else { bool buildIndex = true; SetTo(dataFile); - + time_t mtime; time_t modified; - + dataFile->GetModificationTime(&mtime); - + if (indexFile.SetTo(indexPath, B_READ_ONLY) == B_OK) { attr_info info; if ((indexFile.GetAttrInfo("WINDEX:version", &info) == B_OK)) { @@ -171,25 +171,25 @@ WIndex::UnflattenIndex(BPositionIO *io) if (fEntryList) free(fEntryList); WIndexHead head; - + io->Seek(0, SEEK_SET); io->Read(&head, sizeof(head)); io->Seek(head.offset, SEEK_SET); - + fEntrySize = head.entrySize; fEntries = head.entries; fMaxEntries = fEntriesPerBlock; fBlockSize = fEntriesPerBlock * fEntrySize; fBlocks = fEntries / fEntriesPerBlock + 1;; fIsSorted = true; - + int32 size = (head.entries + 1) * head.entrySize; if (!(fEntryList = (uint8 *)malloc(size))) return B_ERROR; - + if (fEntries) io->Read(fEntryList, size); - + return B_OK; } @@ -200,7 +200,7 @@ WIndex::FlattenIndex(BPositionIO *io) if (fEntries && !fIsSorted) SortItems(); WIndexHead head; - + head.entries = fEntries; head.entrySize = fEntrySize; head.offset = sizeof(WIndexHead); @@ -208,7 +208,7 @@ WIndex::FlattenIndex(BPositionIO *io) io->Write(&head, sizeof(head)); if (fEntries) io->Write(fEntryList, head.entries * head.entrySize); - + return B_OK; } @@ -220,7 +220,7 @@ WIndex::Lookup(int32 key) return -1; if (!fIsSorted) SortItems(); - + // Binary Search int32 M, Lb, Ub; Lb = 0; @@ -244,11 +244,11 @@ WIndex::AddItem(WIndexEntry *entry) { if (_BlockCheck() == B_ERROR) return B_ERROR; - memcpy(((WIndexEntry *)(fEntryList + (fEntries * fEntrySize))), entry, + memcpy(((WIndexEntry *)(fEntryList + (fEntries * fEntrySize))), entry, fEntrySize); fEntries++; fIsSorted = false; - return B_OK; + return B_OK; } @@ -295,7 +295,7 @@ WIndex::InitIndex(void) int32 WIndex::GetKey(const char *s) { - + int32 key = 0; /*int32 x; int32 a = 84589; @@ -303,16 +303,16 @@ WIndex::GetKey(const char *s) int32 m = 217728; while (*s) { x = *s++ - 'a'; - + key ^= (a * x + b) % m; key <<= 1; }*/ - + key = update_crc(0, s, strlen(s)); - + if (key < 0) // No negative values! key = ~key; - + return key; } @@ -344,14 +344,14 @@ WIndex::FindFirst(const char *word) { if (!fEntries) return -1; - + int32 index; char nword[256]; int32 key; - + NormalizeWord(word, nword); key = GetKey(nword); - + if ((index = Lookup(key)) < 0) return -1; // Find first instance of key @@ -369,13 +369,13 @@ WIndex::GetEntry(int32 index) WIndexEntry *ientry; FileEntry *dentry; char *buffer; - + dentry = new FileEntry(); - + ientry = ItemAt(index); - + int32 size; - + fDataFile->Seek(ientry->offset, SEEK_SET); buffer = dentry->LockBuffer(256); fDataFile->Read(buffer, 256); @@ -392,7 +392,7 @@ WIndex::_GetEntrySize(WIndexEntry *entry, const char *entryData) { // eliminate unused parameter warning (void)entry; - + return strcspn(entryData, "\n\r"); } @@ -409,7 +409,7 @@ WIndex::NormalizeWord(const char *word, char *dest) { const char *src; char *dst; - + // remove dots and copy src = word; dst = dest; @@ -419,54 +419,53 @@ WIndex::NormalizeWord(const char *word, char *dest) src++; } *dst = 0; - + // convert to lower-case - dst = dest; - while (*dst) - *dst++ = tolower(*dst); + for (dst = dest; *dst; dst++) + *dst = tolower(*dst); return dest; } -/* crc32h.c -- package to compute 32-bit CRC one byte at a time using */ -/* the high-bit first (Big-Endian) bit ordering convention */ -/* */ -/* Synopsis: */ -/* gen_crc_table() -- generates a 256-word table containing all CRC */ -/* remainders for every possible 8-bit byte. It */ -/* must be executed (once) before any CRC updates. */ -/* */ -/* unsigned update_crc(crc_accum, data_blk_ptr, data_blk_size) */ -/* unsigned crc_accum; char *data_blk_ptr; int data_blk_size; */ -/* Returns the updated value of the CRC accumulator after */ -/* processing each byte in the addressed block of data. */ -/* */ -/* It is assumed that an unsigned long is at least 32 bits wide and */ -/* that the predefined type char occupies one 8-bit byte of storage. */ -/* */ -/* The generator polynomial used for this version of the package is */ -/* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0 */ -/* as specified in the Autodin/Ethernet/ADCCP protocol standards. */ -/* Other degree 32 polynomials may be substituted by re-defining the */ -/* symbol POLYNOMIAL below. Lower degree polynomials must first be */ -/* multiplied by an appropriate power of x. The representation used */ -/* is that the coefficient of x^0 is stored in the LSB of the 32-bit */ -/* word and the coefficient of x^31 is stored in the most significant */ -/* bit. The CRC is to be appended to the data most significant byte */ -/* first. For those protocols in which bytes are transmitted MSB */ -/* first and in the same order as they are encountered in the block */ -/* this convention results in the CRC remainder being transmitted with */ -/* the coefficient of x^31 first and with that of x^0 last (just as */ -/* would be done by a hardware shift register mechanization). */ -/* */ -/* The table lookup technique was adapted from the algorithm described */ -/* by Avram Perez, Byte-wise CRC Calculations, IEEE Micro 3, 40 (1983).*/ +/* crc32h.c -- package to compute 32-bit CRC one byte at a time using */ +/* the high-bit first (Big-Endian) bit ordering convention */ +/* */ +/* Synopsis: */ +/* gen_crc_table() -- generates a 256-word table containing all CRC */ +/* remainders for every possible 8-bit byte. It */ +/* must be executed (once) before any CRC updates. */ +/* */ +/* unsigned update_crc(crc_accum, data_blk_ptr, data_blk_size) */ +/* unsigned crc_accum; char *data_blk_ptr; int data_blk_size; */ +/* Returns the updated value of the CRC accumulator after */ +/* processing each byte in the addressed block of data. */ +/* */ +/* It is assumed that an unsigned long is at least 32 bits wide and */ +/* that the predefined type char occupies one 8-bit byte of storage. */ +/* */ +/* The generator polynomial used for this version of the package is */ +/* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0 */ +/* as specified in the Autodin/Ethernet/ADCCP protocol standards. */ +/* Other degree 32 polynomials may be substituted by re-defining the */ +/* symbol POLYNOMIAL below. Lower degree polynomials must first be */ +/* multiplied by an appropriate power of x. The representation used */ +/* is that the coefficient of x^0 is stored in the LSB of the 32-bit */ +/* word and the coefficient of x^31 is stored in the most significant */ +/* bit. The CRC is to be appended to the data most significant byte */ +/* first. For those protocols in which bytes are transmitted MSB */ +/* first and in the same order as they are encountered in the block */ +/* this convention results in the CRC remainder being transmitted with */ +/* the coefficient of x^31 first and with that of x^0 last (just as */ +/* would be done by a hardware shift register mechanization). */ +/* */ +/* The table lookup technique was adapted from the algorithm described */ +/* by Avram Perez, Byte-wise CRC Calculations, IEEE Micro 3, 40 (1983).*/ -#define POLYNOMIAL 0x04c11db7L +#define POLYNOMIAL 0x04c11db7L -static unsigned long crc_table[256]; +static unsigned long crc_table[256]; void diff --git a/src/apps/mail/Words.cpp b/src/apps/mail/Words.cpp index 1fb8f8d762..3bc987d979 100644 --- a/src/apps/mail/Words.cpp +++ b/src/apps/mail/Words.cpp @@ -47,7 +47,7 @@ enum { }; -/* +/* MAXMETAPH is the length of the Metaphone code. Four is a good compromise value for English names. For comparing words @@ -55,7 +55,7 @@ enum { length for more precise matches. The default here is 5. -*/ +*/ #define MAXMETAPH 6 @@ -80,7 +80,7 @@ Words::Words(bool useMetaphone) : fUseMetaphone(useMetaphone) { - + } @@ -89,13 +89,13 @@ Words::Words(BPositionIO* thes, bool useMetaphone) WIndex(thes), fUseMetaphone(useMetaphone) { - + } Words::~Words(void) { - + } @@ -119,27 +119,27 @@ Words::BuildIndex(void) char *nptr, *eptr; int64 blockOffset; int32 blockSize; - + // The Word Entry WIndexEntry entry; char entryName[256], *namePtr = entryName; char suffixName[256]; char flags[32], *flagsPtr = flags; - + // State Info int32 state = FIND_WORD; - + // Make sure we are at start of file fDataFile->Seek(0, SEEK_SET); entry.offset = -1; - + // Read blocks from thes until eof while (true) { // Get next block blockOffset = fDataFile->Position(); if ((blockSize = fDataFile->Read(buffer, 16384)) == 0) break; - + // parse block for (nptr = buffer, eptr = buffer + blockSize; nptr < eptr; nptr++) { // Looking for start of word? @@ -163,7 +163,7 @@ Words::BuildIndex(void) // Add base word entry.key = GetKey(entryName); AddItem(&entry); - + // Add suffixed words if any if (flagsPtr != flags) { // printf("Base: %s, flags: %s\n", entryName, flags); @@ -185,7 +185,7 @@ Words::BuildIndex(void) if (*nptr == '/') { *namePtr = 0; // terminate word // printf("Found word: %s\n", entryName); - + state = GET_FLAGS; } else { *namePtr++ = *nptr; // copy word @@ -210,12 +210,12 @@ Words::GetKey(const char* s) int32 key = 0; int32 offset; char c; - + metaphone(s, Metaph, GENERATE); // Compact Metaphone from 6 bytes to 4 - + // printf("%s -> %s: \n", s, Metaph); - + for (sPtr = Metaph, offset = 25; *sPtr; sPtr++, offset -= 5) { c = *sPtr - 'A'; // printf("%d,", int16(c)); @@ -470,13 +470,13 @@ word_match(const char* reference, const char* test) char c1, c2; s1 = test; s2 = reference; - + bool a, b; - + while (*s2 || *s1) { c1 = tolower(*s1); c2 = tolower(*s2); - + if (*s2 && *s1) { if (c1 != c2) { a = (tolower(s1[1]) == c2); @@ -498,7 +498,7 @@ word_match(const char* reference, const char* test) s2++; } // Equivalent Character - else if (vsvfn[c1] == vsvfn[c2]) + else if (vsvfn[(unsigned)c1] == vsvfn[(unsigned)c2]) x++; // Unrelated Character else @@ -513,7 +513,7 @@ word_match(const char* reference, const char* test) if (*s1) s1++; } - + return x; } @@ -522,7 +522,7 @@ int32 suffix_word(char* dst, const char* src, char flag) { char* end; - + end = stpcpy(dst, src); flag = toupper(flag); switch(flag) { @@ -706,46 +706,46 @@ Words::FindBestMatches(BList* matches, const char* s) { int32 index; // printf("*** Looking for %s: ***\n", s); - + if ((index = FindFirst(s)) >= 0) { BString srcWord(s); FileEntry* entry; WIndexEntry* indexEntry; - + int32 key = (ItemAt(index))->key; int32 suffixLength; char word[128], suffixWord[128]; const char *src, *testWord; const char *suffixFlags; char *dst; - + gCmpKey = srcWord.String(); - + uint8 hashTable[32]; uint8 hashValue, highHash, lowHash; for (int32 i = 0; i < 32; i++) hashTable[i] = 0; - + do { indexEntry = ItemAt(index); // Hash the entry offset; we use this to make sure we don't add // the same word file entry twice; // It is possible for the same entry in the words file to have // multiple entries in the index. - + hashValue = indexEntry->offset % 256; highHash = hashValue >> 3; lowHash = 0x01 << (hashValue & 0x07); - + // printf("Testing Entry: %ld: hash=%d, highHash=%d, lowHash=%d\n", - // indexEntry->offset, hashValue, (uint16)highHash, + // indexEntry->offset, hashValue, (uint16)highHash, // (uint16)lowHash); - + // Has this entry offset been seen before? if (!(hashTable[highHash] & lowHash)) { // printf("New Entry\n"); hashTable[highHash] |= lowHash; // Mark this offset so we don't add it twice - + entry = GetEntry(index); src = entry->String(); while (*src && !isalpha(*src)) @@ -758,7 +758,7 @@ Words::FindBestMatches(BList* matches, const char* s) suffixFlags = src + 1; else suffixFlags = src; - + // printf("Base Word: %s\n", word); // printf("Flags: %s\n", suffixFlags); testWord = word; // Test the base word first @@ -769,12 +769,12 @@ Words::FindBestMatches(BList* matches, const char* s) // And does it look close enough to the compare key? // word_match(gCmpKey, testWord) // <= int32((strlen(gCmpKey)-1)/2)) - && word_match(gCmpKey, testWord) + && word_match(gCmpKey, testWord) <= int32(float(strlen(gCmpKey)-1)*.75)) { // printf("Added: %s\n", testWord); matches->AddItem((void*)(new BString(testWord))); } - + // If suffix, transform and test if (*suffixFlags) { // Repeat until valid suffix found or end is reached @@ -796,7 +796,7 @@ Words::FindBestMatches(BList* matches, const char* s) index++; } while (key == (ItemAt(index))->key); - + return matches->CountItems(); } else { return 0;