/***************************************************************************** * Sort * * Will sort the list according to setting. The sort method is bubble sort as * we deal with a linked list and only know the current, previous and next * object at any time. It is also perfect for sorting as we add one by * one items. * * Sorting on non-containers are not possible as we have no idea as to how * to compare. As for sorting we expect one iteration since this should be * called after each Add... ****************************************************************************/ void List::Sort() { bool bMore=true; ListNode *n=NULL; ListNode *n2; Object *o1=NULL; Object *o2; int c; if(!m_bContainer) return; switch(m_eSort) { case Nosort: break; case Ascending: while(bMore) { bMore=false; n=NULL; o1 = FindFirst(&n); n2=n; while(o1!=NULL && (o2=FindNext(&n))!=NULL) { c = o1->Compare(o2); if(c==-1) { Swap(&n2,&n); bMore=true; } n2=n; o1=o2; } } break; case Descending: while(bMore) { bMore=false; n=NULL; o1 = FindLast(&n); n2=n; while(o1!=NULL && (o2=FindPast(&n))!=NULL) { c = o1->Compare(o2); if(c==-1) { Swap(&n2,&n); bMore=true; } n2=n; o1=o2; } } break; } }
BString& BString::ReplaceLast(char replaceThis, char withThis) { int32 pos = FindLast(replaceThis); if (pos >= 0 && _MakeWritable() == B_OK) fPrivateData[pos] = withThis; return *this; }
int SuffixArray::LimitedCount( const vector< WORD > &phrase, INDEX min, INDEX &firstMatch, INDEX &lastMatch, INDEX search_start, INDEX search_end ) { // cerr << "FindFirst\n"; INDEX start = search_start; INDEX end = (search_end == -1) ? (m_size-1) : search_end; INDEX mid = FindFirst( phrase, start, end ); // cerr << "done\n"; if (mid == m_size) return 0; // no matches if (min == 1) return 1; // only existance check int matchCount = 1; //cerr << "before...\n"; firstMatch = FindLast( phrase, mid, start, -1 ); matchCount += mid - firstMatch; //cerr << "after...\n"; lastMatch = FindLast( phrase, mid, end, 1 ); matchCount += lastMatch - mid; return matchCount; }
bool TrackerString::EndsWith(const char* string, bool caseSensitivity) const { // If "string" is longer than "this", // we should simply return false int32 position = Length() - (int32)strlen(string); if (position < 0) return false; if (caseSensitivity) return FindLast(string) == position; else return IFindLast(string) == position; }
void *FindLocalizedFile(_In_z_ LPCWSTR wzResourceDllName, LocalizedFileHandler lfh, _In_opt_z_ LPCWSTR modulePathW) { // find path of the modulePath MyString driverPath; MyString modulePath; ClrGetModuleFileName(GetModuleHandleW(modulePathW), modulePath); // Rip off the application name. MyStringIterator trailingSlashLocation = StrEndIter(modulePath); if (FindLast(modulePath, trailingSlashLocation, W('\\'))) MakeString(driverPath, modulePath, StrBeginIter(modulePath), trailingSlashLocation); else // If it's not a full path, look in the current directory driverPath = W("."); // return the first of the local directory's copy or the resource DLL on %PATH% void *hmod = LoadLocalFile(driverPath, wzResourceDllName, lfh); if (hmod == NULL) hmod = LoadSearchPath(wzResourceDllName, lfh); return hmod; }
bool String::EndsWith(const String& str, bool caseSensitive) const { unsigned pos = FindLast(str, Length() - 1, caseSensitive); return pos != NPOS && pos == Length() - str.Length(); }
//---------------------------------------------------------------- CPUTResult CPUTConfigFile::LoadFile(const cString &szFilename) { // Load the file cString szCurrLine; CPUTConfigBlock *pCurrBlock = NULL; FILE *pFile = NULL; int nCurrBlock = 0; CPUTResult result = CPUTOSServices::GetOSServices()->OpenFile(szFilename, &pFile); if(CPUTFAILED(result)) { return result; } _locale_t locale = _get_current_locale(); /* Determine file size */ fseek(pFile, 0, SEEK_END); int nBytes = ftell(pFile); // for text files, this is an overestimate fseek(pFile, 0, SEEK_SET); /* Read the whole thing */ char *pFileContents = new char[nBytes + 1]; nBytes = (int)fread(pFileContents, 1, nBytes, pFile); fclose(pFile); pFileContents[nBytes] = 0; // add 0-terminator /* Count the number of blocks */ const char *pCur = pFileContents; const char *pStart, *pEnd; while(ReadLine(&pStart, &pEnd, &pCur)) { const char *pOpen = FindFirst(pStart, pEnd, '['); const char *pClose = FindLast(pOpen + 1, pEnd, ']'); if (pOpen < pClose) { // This line is a valid block header mnBlockCount++; } } // For files that don't have any blocks, just add the entire file to one block if(mnBlockCount == 0) { mnBlockCount = 1; } pCur = pFileContents; mpBlocks = new CPUTConfigBlock[mnBlockCount]; pCurrBlock = mpBlocks; /* Find the first block first */ while(ReadLine(&pStart, &pEnd, &pCur)) { const char *pOpen = FindFirst(pStart, pEnd, '['); const char *pClose = FindLast(pOpen + 1, pEnd, ']'); if (pOpen < pClose) { // This line is a valid block header pCurrBlock = mpBlocks + nCurrBlock++; AssignStr(pCurrBlock->mszName, pOpen + 1, pClose, locale); std::transform(pCurrBlock->mszName.begin(), pCurrBlock->mszName.end(), pCurrBlock->mszName.begin(), ::tolower); } else if (pStart < pEnd) { // It's a value if (pCurrBlock == NULL) { continue; } const char *pEquals = FindFirst(pStart, pEnd, '='); if (pEquals == pEnd) { // No value, just a key, save it anyway // Optimistically, we assume it's new cString &name = pCurrBlock->mpValues[pCurrBlock->mnValueCount].szName; AssignStr(name, pStart, pEnd, locale); bool dup = false; for(int ii=0;ii<pCurrBlock->mnValueCount;++ii) { if(!pCurrBlock->mpValues[ii].szName.compare(name)) { dup = true; break; } } if(!dup) { pCurrBlock->mnValueCount++; } } else { const char *pNameStart = pStart; const char *pNameEnd = pEquals; const char *pValStart = pEquals + 1; const char *pValEnd = pEnd; RemoveWhitespace(pNameStart, pNameEnd); RemoveWhitespace(pValStart, pValEnd); // Optimistically assume the name is new cString &name = pCurrBlock->mpValues[pCurrBlock->mnValueCount].szName; AssignStr(name, pNameStart, pNameEnd, locale); std::transform(name.begin(), name.end(), name.begin(), ::tolower); bool dup = false; for(int ii=0;ii<pCurrBlock->mnValueCount;++ii) { if(!pCurrBlock->mpValues[ii].szName.compare(name)) { dup = true; break; } } if(!dup) { AssignStr(pCurrBlock->mpValues[pCurrBlock->mnValueCount].szValue, pValStart, pValEnd, locale); pCurrBlock->mnValueCount++; } } } } delete[] pFileContents; return CPUT_SUCCESS; }
int32 TrackerString::FindLast(char ch, int32 beforeOffset) const { char string[2] = {ch, '\0'}; return FindLast(string, beforeOffset); }
int32 TrackerString::FindLast(char ch) const { char string[2] = {ch, '\0'}; return FindLast(string, Length() - 1); }
int32 TrackerString::FindLast(const BString &string, int32 beforeOffset) const { return FindLast(string.String(), beforeOffset); }
int32 TrackerString::FindLast(const char* string) const { return FindLast(string, Length() - 1); }
int32 TrackerString::FindLast(const BString &string) const { return FindLast(string.String(), Length() - 1); }
int32 BString::FindLastChars(const char* string, int32 beforeCharOffset) const { return FindLast(string, UTF8CountBytes(fPrivateData, beforeCharOffset)); }
bool String::EndsWith(const String& str) const { return FindLast(str) == Length() - str.Length(); }