void ReadFile(int64_t *PID[],Index *phone, char *infile) { FILE *fp; if((fp=fopen(infile,"r"))==NULL) { //文本只读方式重新打开文件 printf("cannot open file\n"); return ; } int64_t key=0, value=0; int64_t index[Array]= {0}, pre = 0, count = 0; int32_t mod = 0; while(fscanf(fp,"%lld%lld",&key,&value) != EOF) { mod = key % Array ; // printf("first---pre:%d\tkey:%d\tindex:%d\n",pre,key,index[mod]); if( pre != key && pre != 0) { // printf("%lld\t%lld\n",key,value); InsertKey(phone,pre,index[pre%Array]-count,count); count = 0; } PID[mod][index[mod]] = value; ++index[mod]; ++count; pre = key; } InsertKey(phone,pre,index[pre%Array]-count,count); }
void RecorderDlg::OnInsKey(wxCommandEvent &event) { wxString str; VRenderFrame* vr_frame = (VRenderFrame*)m_frame; if (!vr_frame) return; Interpolator* interpolator = vr_frame->GetInterpolator(); if (!interpolator) return; long item = m_keylist->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); int index = -1; if (item != -1) { str = m_keylist->GetItemText(item); long id; str.ToLong(&id); index = interpolator->GetKeyIndex(id); } str = m_duration_text->GetValue(); double duration; str.ToDouble(&duration); int interpolation = m_interpolation_cmb->GetSelection(); InsertKey(index, duration, interpolation); m_keylist->Update(); m_keylist->SetItemState(item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); }
int main () { DictHndl testHash; ListHndl testLookUp; testHash = NewDictionary (10); InsertKey (testHash, "cat", 123); InsertKey (testHash, "dog", 234); InsertKey (testHash, "fog", 2345); InsertKey (testHash, "fog", 245); testLookUp = LookUp (testHash, "fog"); MoveFirst (testLookUp); while (!OffEnd (testLookUp)) { int temp = *(int*) GetCurrent (testLookUp); printf ("%d ", temp); MoveNext (testLookUp); } printf ("\n"); if (IsIn (testHash, "dog")) { printf ("dog is in the table\n"); } DeleteKey (testHash, "cat"); DeleteKey (testHash, "dog"); if (IsIn (testHash, "dog")) { printf ("dog is still in the table\n"); } testLookUp = LookUp (testHash, "dog"); FreeDictionary (testHash); return (0); }
void Keys::InitKeys() { if (init_done) return; for (register UINT code = 0; code < 200; code++) { InsertKey(code); } init_done = true; }
void RecorderDlg::OnSetKey(wxCommandEvent &event) { wxString str = m_duration_text->GetValue(); double duration; str.ToDouble(&duration); int interpolation = m_interpolation_cmb->GetSelection(); InsertKey(-1, duration, interpolation); m_keylist->Update(); }
int main () { DictHndl testDict; ListHndl testLookUp; testDict = NewDictionary (10); InsertKey (testDict, "cat", 123); InsertKey (testDict, "dog", 234); InsertKey (testDict, "fog", 2345); InsertKey (testDict, "fog", 245); testLookUp = LookUp (testDict, "cat"); printf ("%d\n" , *(int*) GetFirst (testLookUp)); PrintKeyLocations (testDict, "fog"); if (IsIn (testDict, "dog")) { printf ("dog is in the table\n"); } DeleteKey (testDict, "cat"); DeleteKey (testDict, "dog"); if (IsIn (testDict, "dog")) { printf ("dog is still in the table\n"); } FreeDictionary (testDict); return (0); }
void INIFile::Section::setStringValue(const std::string& key, const std::string& newValue) { if(hasKey(key)) { getKey(key)->setStringValue(newValue); } else { // create new key if(isValidKeyName(key) == false) { std::cerr << "INIFile: Cannot create key with name " << key << "!" << std::endl; return; } Key* curKey = new Key(key, newValue); Key* pKey = KeyRoot; if(pKey == NULL) { // Section has no key yet if(nextLine == NULL) { // no line after this section declaration nextLine = curKey; curKey->prevLine = this; } else { // lines after this section declaration nextLine->prevLine = curKey; curKey->nextLine = nextLine; nextLine = curKey; curKey->prevLine = this; } } else { // Section already has some keys while(pKey->nextKey != NULL) { pKey = pKey->nextKey; } if(pKey->nextLine == NULL) { // no line after this key pKey->nextLine = curKey; curKey->prevLine = pKey; } else { // lines after this section declaration pKey->nextLine->prevLine = curKey; curKey->nextLine = pKey->nextLine; pKey->nextLine = curKey; curKey->prevLine = pKey; } } InsertKey(curKey); } }
bool BTreeDriver::TestDeleteCurrent() { Status status; BTreeFile *btf; bool res; btf = new BTreeFile(status, "BTreeTest6"); if (status != OK) { std::cerr << "ERROR: Couldn't create a BTreeFile" << std::endl; minibase_errors.show_errors(); std::cerr << "Hit [enter] to continue..." << std::endl; std::cin.get(); exit(1); } std::cout << "Starting Test 6..." << std::endl; std::cout << "BTreeIndex created successfully." << std::endl; std::cout << "Inserting entries..." << std::endl; InsertRange(btf, 1, 10); char low[MAX_KEY_LENGTH]; toString(3, low); BTreeFileScan* scan = btf->OpenScan(low, NULL); std::cout << "Deleting entries..." << std::endl; RecordID rid; char* keyPtr; scan->GetNext(rid, keyPtr); // 3 scan->GetNext(rid, keyPtr); // 4 scan->DeleteCurrent(); // 5 scan->GetNext(rid, keyPtr); // 6 scan->GetNext(rid, keyPtr); // 7 scan->GetNext(rid, keyPtr); // 8 scan->DeleteCurrent(); delete scan; res = TestAbsent(btf, 4); res = TestAbsent(btf, 8); std::cout << "Inserting duplicate entry..." << std::endl; InsertKey(btf, 1, 3); toString(1, low); scan = btf->OpenScan(low, NULL); std::cout << "Deleting entry..." << std::endl; scan->GetNext(rid, keyPtr); // 1 scan->DeleteCurrent(); // 1 delete scan; res = TestAbsent(btf, 1, 1); res = TestPresent(btf, 1, 3); if (btf->DestroyFile() != OK) { std::cerr << "Error destroying BTreeFile" << std::endl; res = false; } delete btf; return res; }
bool BTreeDriver::TestInsertsWithIndexSplits() { Status status; BTreeFile *btf; bool res; btf = new BTreeFile(status, "BTreeTest3"); if (status != OK) { std::cerr << "ERROR: Couldn't create a BTreeFile" << std::endl; minibase_errors.show_errors(); std::cerr << "Hit [enter] to continue..." << std::endl; std::cin.get(); exit(1); } std::cout << "Starting Test 3..." << std::endl; std::cout << "BTreeIndex created successfully." << std::endl; // Insert a bunch of long keys into the tree until we cause the root // node to split. std::cout << "Inserting 31 large keys..." << std::endl; res = InsertRange(btf, 1, 31, 1, 20); res = res && TestNumLeafPages(btf, 2); res = res && TestNumEntries(btf, 31); std::cout << "Inserting keys until root splits." << std::endl; PageID rootId = btf->header->GetRootPageID(); PageID newRootId; int key = 32; int numInserted = 0; PageID leftPid, rightPid; IndexPage *leftPage; IndexPage *rightPage; while (true) { res = res && InsertKey(btf, key, 1, 20); newRootId = btf->header->GetRootPageID(); // there was a split. Check balance. if (newRootId != rootId) { IndexPage *ip; if (MINIBASE_BM->PinPage(newRootId, (Page *&) ip) == FAIL) { std::cerr << "Error pinning root page." << std::endl; res = false; } if (ip->GetNumOfRecords() != 1) { std::cerr << "Error: Expected 1 key in root, got " << ip->GetNumOfRecords() << std::endl; res = false; } leftPid = ip->GetPrevPage(); char *rightKey; ip->GetMinKeyValue(rightKey, rightPid); if (MINIBASE_BM->PinPage(leftPid, (Page *&) leftPage) == FAIL) { std::cerr << "Error pinning left leaf page." << std::endl; res = false; } if (MINIBASE_BM->PinPage(rightPid, (Page *&) rightPage) == FAIL) { std::cerr << "Error pinning right leaf page." << std::endl; res = false; } res = res && TestBalance(btf, leftPage, rightPage); if (MINIBASE_BM->UnpinPage(leftPid, CLEAN) == FAIL) { std::cerr << "Error unpinning left leaf page." << std::endl; res = false; } if (MINIBASE_BM->UnpinPage(rightPid, CLEAN) == FAIL) { std::cerr << "Error unpinning right leaf page." << std::endl; res = false; } if (MINIBASE_BM->UnpinPage(newRootId, CLEAN) == FAIL) { std::cerr << "Error unpinning root page." << std::endl; res = false; } break; } key++; numInserted++; } res = res && TestNumEntries(btf, key); if (btf->DestroyFile() != OK) { std::cerr << "Error destroying BTreeFile" << std::endl; res = false; } delete btf; return res; }
bool BTreeDriver::TestInsertsWithLeafSplits() { Status status; BTreeFile *btf; bool res; LeafPage *leftPage; LeafPage *rightPage; btf = new BTreeFile(status, "BTreeTest2"); if (status != OK) { std::cerr << "ERROR: Couldn't create a BTreeFile" << std::endl; minibase_errors.show_errors(); std::cerr << "Hit [enter] to continue..." << std::endl; std::cin.get(); exit(1); } std::cout << "Starting Test 2..." << std::endl; std::cout << "BTreeIndex created successfully." << std::endl; std::cout << "Inserting 30 large keys..." << std::endl; res = InsertRange(btf, 1, 30, 1, 20); res = res && TestNumLeafPages(btf, 1); res = res && TestNumEntries(btf, 30); // Insert some more long keys. This should cause the tree to split. std::cout << "Causing split with new key in right node..." << std::endl; res = res && InsertKey(btf, 31, 1, 20); res = res && TestNumLeafPages(btf, 2); res = res && TestNumEntries(btf, 31); // Make sure that the split happened as expected. PageID leftPid = btf->GetLeftLeaf(); if (MINIBASE_BM->PinPage(leftPid, (Page *&) leftPage) == FAIL) { std::cerr << "Error pinning left leaf page." << std::endl; res = false; } PageID rightPid = leftPage->GetNextPage(); if (MINIBASE_BM->PinPage(rightPid, (Page *&) rightPage) == FAIL) { std::cerr << "Error pinning right leaf page." << std::endl; res = false; } res = res && TestBalance(btf, leftPage, rightPage); if (MINIBASE_BM->UnpinPage(leftPid, CLEAN) == FAIL) { std::cerr << "Error unpinning left leaf page." << std::endl; res = false; } if (MINIBASE_BM->UnpinPage(rightPid, CLEAN) == FAIL) { std::cerr << "Error unpinning right leaf page." << std::endl; res = false; } // We destroy and rebuild the file before to reduce // dependence on working Delete function. if (btf->DestroyFile() != OK) { std::cerr << "Error destroying BTreeFile" << std::endl; res = false; } delete btf; // Create a new tree. btf = new BTreeFile(status, "BTreeTest2"); if (status != OK) { std::cerr << "ERROR: Couldn't create a BTreeFile" << std::endl; minibase_errors.show_errors(); std::cerr << "Hit [enter] to continue..." << std::endl; std::cin.get(); exit(1); } // Insert 30 long keys. std::cout << "Inserting 30 large keys..." << std::endl; res = InsertRange(btf, 2, 31, 1, 20); res = res && TestNumLeafPages(btf, 1); res = res && TestNumEntries(btf, 30); // Insert some more nodes with long keys into the tree. // This should cause the tree to split. std::cout << "Causing split with new key in left node..." << std::endl; res = res && InsertKey(btf, 1, 1, 20); res = res && TestNumLeafPages(btf, 2); res = res && TestNumEntries(btf, 31); // Make sure that the tree is balanced. leftPid = btf->GetLeftLeaf(); if (MINIBASE_BM->PinPage(leftPid, (Page *&) leftPage) == FAIL) { std::cerr << "Error pinning left leaf page." << std::endl; res = false; } rightPid = leftPage->GetNextPage(); if (MINIBASE_BM->PinPage(rightPid, (Page *&) rightPage) == FAIL) { std::cerr << "Error pinning right leaf page." << std::endl; res = false; } res = res && TestBalance(btf, leftPage, rightPage); if (MINIBASE_BM->UnpinPage(leftPid, CLEAN) == FAIL) { std::cerr << "Error unpinning left leaf page." << std::endl; res = false; } if (MINIBASE_BM->UnpinPage(rightPid, CLEAN) == FAIL) { std::cerr << "Error unpinning right leaf page." << std::endl; res = false; } if (btf->DestroyFile() != OK) { std::cerr << "Error destroying BTreeFile" << std::endl; res = false; } delete btf; // That's all, folks. return res; }
int main ( int argc, char* argv []) { int i, j; DictHndl library; int numBooks; int dictSize; int parser; int parseSave; int numSearches; FILE *libraryBooks; FILE *searchBooks; char buffer[1000]; if (argc != 3) { fprintf (stderr, "usage: %s filename filename\n", argv[0]); return 1; } libraryBooks = fopen( argv[1], "r"); searchBooks = fopen ( argv[2], "r"); if (libraryBooks == 0 || searchBooks == 0) { fprintf (stderr, "File does not exist\n"); return 1; } fgets (buffer, 1000, libraryBooks); parser = 0; while (buffer[parser] >= '0' && buffer[parser] <= '9') { parser++; } parseSave = parser; parser--; numBooks = 0; for (j = 1; parser >= 0; j = j * 10) { numBooks += (buffer[parser] - '0') * j; parser--; } parser = parseSave + 1; while (buffer[parser] >= '0' && buffer[parser] <= '9') { parser++; } dictSize = 0; for (j = 1; parser > parseSave; j = j * 10) { dictSize += (buffer[parser] - '0') * j; parser--; } library = NewDictionary (dictSize); for (i = 0; i < numBooks; i++) { int reparser = 0; int tempLocation = 0; parser = 0; fgets (buffer, 1000, libraryBooks); while (buffer[parser] != ',') { if (parser == 999) { fprintf (stderr, "Improper Library Data File\n"); return 1; } parser++; } parseSave = parser; parser--; for (j = 1; parser >= 0; j = j * 10) { tempLocation += (buffer[parser] - '0') * j; parser--; } parser = parseSave; parser += 2; while (buffer[parser] != '\n' && buffer[parser] != EOF && buffer[parser] != '\0') { buffer[reparser] = buffer[parser]; parser++; reparser++; } buffer[reparser] = '\0'; InsertKey (library, buffer, tempLocation); } fgets (buffer, 1000, searchBooks); parser = 0; while (buffer[parser] >= '0' && buffer[parser] <= '9') { parser++; } parser--; numSearches = 0; for (j = 1; parser >= 0; j = j * 10) { numSearches += (buffer[parser] - '0') * j; parser--; } for (i = 0; i < numSearches; i++) { fgets (buffer, 1000, searchBooks); j = 0; while (buffer[j] != '\n' && buffer[j] != '\0' && buffer[j] != EOF) { j++; } buffer[j] = '\0'; PrintKeyLocations (library, buffer); } FreeDictionary (library); fclose (libraryBooks); fclose (searchBooks); return 0; }
// Initialises the keypress dictionary. void KeyInfo::InitialiseKeyMap() { InsertKey(InputType::Backspace, 8, std::string("Backspace")); InsertKey(InputType::Tab, 9, std::string("Tab")); InsertKey(InputType::Clear, 12, std::string("Clear")); InsertKey(InputType::Return, 13, std::string("Return")); InsertKey(InputType::Pause, 19, std::string("Pause")); InsertKey(InputType::Escape, 27, std::string("Escape")); InsertKey(InputType::Space, ' ', std::string("Space")); InsertKey(InputType::Exclaim, '!', std::string("Exclaim")); InsertKey(InputType::DoubleQuote, '"', std::string("DoubleQuote")); InsertKey(InputType::Hash, '#', std::string("Hash")); InsertKey(InputType::Dollar, '$', std::string("Dollar")); InsertKey(InputType::Ampersand, '&', std::string("Ampersand")); InsertKey(InputType::Quote, '\'', std::string("Quote")); InsertKey(InputType::LeftParen, '(', std::string("LeftParen")); InsertKey(InputType::RightParen, ')', std::string("RightParen")); InsertKey(InputType::Asterisk, '*', std::string("Asterisk")); InsertKey(InputType::Plus, '+', std::string("Plus")); InsertKey(InputType::Comma, ',', std::string("Comma")); InsertKey(InputType::Minus, '-', std::string("Minus")); InsertKey(InputType::Period, '.', std::string("Period")); InsertKey(InputType::Slash, '/', std::string("Slash")); InsertKey(InputType::Num0, '0', std::string("Num0")); InsertKey(InputType::Num1, '1', std::string("Num1")); InsertKey(InputType::Num2, '2', std::string("Num2")); InsertKey(InputType::Num3, '3', std::string("Num3")); InsertKey(InputType::Num4, '4', std::string("Num4")); InsertKey(InputType::Num5, '5', std::string("Num5")); InsertKey(InputType::Num6, '6', std::string("Num6")); InsertKey(InputType::Num7, '7', std::string("Num7")); InsertKey(InputType::Num8, '8', std::string("Num8")); InsertKey(InputType::Num9, '9', std::string("Num9")); InsertKey(InputType::Colon, ':', std::string("Colon")); InsertKey(InputType::Semicolon, ';', std::string("Semicolon")); InsertKey(InputType::Less, '<', std::string("Less")); InsertKey(InputType::Equals, '=', std::string("Equals")); InsertKey(InputType::Greater, '>', std::string("Greater")); InsertKey(InputType::Question, '?', std::string("Question")); InsertKey(InputType::At, '@', std::string("At")); InsertKey(InputType::LeftBracket, '[', std::string("LeftBracket")); InsertKey(InputType::Backslash, '\\', std::string("Backslash")); InsertKey(InputType::RightBracket, ']', std::string("RightBracket")); InsertKey(InputType::Caret, '^', std::string("Caret")); InsertKey(InputType::Underscore, '_', std::string("Underscore")); InsertKey(InputType::BackQuote, '`', std::string("BackQuote")); InsertKey(InputType::A, 'a', std::string("a")); InsertKey(InputType::B, 'b', std::string("b")); InsertKey(InputType::C, 'c', std::string("c")); InsertKey(InputType::D, 'd', std::string("d")); InsertKey(InputType::E, 'e', std::string("e")); InsertKey(InputType::F, 'f', std::string("f")); InsertKey(InputType::G, 'g', std::string("g")); InsertKey(InputType::H, 'h', std::string("h")); InsertKey(InputType::I, 'i', std::string("i")); InsertKey(InputType::J, 'j', std::string("j")); InsertKey(InputType::K, 'k', std::string("k")); InsertKey(InputType::L, 'l', std::string("l")); InsertKey(InputType::M, 'm', std::string("m")); InsertKey(InputType::N, 'n', std::string("n")); InsertKey(InputType::O, 'o', std::string("o")); InsertKey(InputType::P, 'p', std::string("p")); InsertKey(InputType::Q, 'q', std::string("q")); InsertKey(InputType::R, 'r', std::string("r")); InsertKey(InputType::S, 's', std::string("s")); InsertKey(InputType::T, 't', std::string("t")); InsertKey(InputType::U, 'u', std::string("u")); InsertKey(InputType::V, 'v', std::string("v")); InsertKey(InputType::W, 'w', std::string("w")); InsertKey(InputType::X, 'x', std::string("x")); InsertKey(InputType::Y, 'y', std::string("y")); InsertKey(InputType::Z, 'z', std::string("z")); InsertKey(InputType::Delete, 127, std::string("Delete")); InsertKey(InputType::KeyPad0, 0, std::string("KeyPad0")); InsertKey(InputType::KeyPad1, 0, std::string("KeyPad1")); InsertKey(InputType::KeyPad2, 0, std::string("KeyPad2")); InsertKey(InputType::KeyPad3, 0, std::string("KeyPad3")); InsertKey(InputType::KeyPad4, 0, std::string("KeyPad4")); InsertKey(InputType::KeyPad5, 0, std::string("KeyPad5")); InsertKey(InputType::KeyPad6, 0, std::string("KeyPad6")); InsertKey(InputType::KeyPad7, 0, std::string("KeyPad7")); InsertKey(InputType::KeyPad8, 0, std::string("KeyPad8")); InsertKey(InputType::KeyPad9, 0, std::string("KeyPad9")); InsertKey(InputType::KeyPadPeriod, 0, std::string("KeyPadPeriod")); InsertKey(InputType::KeyPadDivide, 0, std::string("KeyPadDivide")); InsertKey(InputType::KeyPadMultiply, 0, std::string("KeyPadMultiply")); InsertKey(InputType::KeyPadMinus, 0, std::string("KeyPadMinus")); InsertKey(InputType::KeyPadPlus, 0, std::string("KeyPadPlus")); InsertKey(InputType::KeyPadEnter, 0, std::string("KeyPadEnter")); InsertKey(InputType::KeyPadEquals, 0, std::string("KeyPadEquals")); InsertKey(InputType::Up, 0, std::string("Up")); InsertKey(InputType::Down, 0, std::string("Down")); InsertKey(InputType::Right, 0, std::string("Right")); InsertKey(InputType::Left, 0, std::string("Left")); InsertKey(InputType::Insert, 0, std::string("Insert")); InsertKey(InputType::Home, 0, std::string("Home")); InsertKey(InputType::End, 0, std::string("End")); InsertKey(InputType::PageUp, 0, std::string("PageUp")); InsertKey(InputType::PageDown, 0, std::string("PageDown")); InsertKey(InputType::F1, 0, std::string("F1")); InsertKey(InputType::F2, 0, std::string("F2")); InsertKey(InputType::F3, 0, std::string("F3")); InsertKey(InputType::F4, 0, std::string("F4")); InsertKey(InputType::F5, 0, std::string("F5")); InsertKey(InputType::F6, 0, std::string("F6")); InsertKey(InputType::F7, 0, std::string("F7")); InsertKey(InputType::F8, 0, std::string("F8")); InsertKey(InputType::F9, 0, std::string("F9")); InsertKey(InputType::F10, 0, std::string("F10")); InsertKey(InputType::F11, 0, std::string("F11")); InsertKey(InputType::F12, 0, std::string("F12")); InsertKey(InputType::F13, 0, std::string("F13")); InsertKey(InputType::F14, 0, std::string("F14")); InsertKey(InputType::F15, 0, std::string("F15")); InsertKey(InputType::NumLock, 0, std::string("NumLock")); InsertKey(InputType::CapsLock, 0, std::string("CapsLock")); InsertKey(InputType::ScrollLock, 0, std::string("ScrollLock")); InsertKey(InputType::RightShift, 0, std::string("RightShift")); InsertKey(InputType::LeftShift, 0, std::string("LeftShift")); InsertKey(InputType::RightControl, 0, std::string("RightControl")); InsertKey(InputType::LeftControl, 0, std::string("LeftControl")); InsertKey(InputType::RightAlt, 0, std::string("RightAlt")); InsertKey(InputType::LeftAlt, 0, std::string("LeftAlt")); InsertKey(InputType::RightMeta, 0, std::string("RightMeta")); InsertKey(InputType::LeftMeta, 0, std::string("LeftMetav")); InsertKey(InputType::LeftSuper, 0, std::string("LeftSuper")); InsertKey(InputType::RightSuper, 0, std::string("RightSuper")); InsertKey(InputType::Mode, 0, std::string("Mode")); InsertKey(InputType::Compose, 0, std::string("Compose")); InsertKey(InputType::Help, 0, std::string("Help")); InsertKey(InputType::Print, 0, std::string("Print")); InsertKey(InputType::SysReq, 0, std::string("SysReq")); InsertKey(InputType::Break, 0, std::string("Break")); InsertKey(InputType::Menu, 0, std::string("Menu")); InsertKey(InputType::Power, 0, std::string("Power")); InsertKey(InputType::Euro, 0, std::string("Euro")); InsertKey(InputType::Undo, 0, std::string("Undo")); }
void IniFile::readFile() { if((SectionRoot = new SectionEntry("",0,0)) == nullptr) throw(std::bad_alloc()); SectionEntry *curSectionEntry = SectionRoot; std::string completeLine; int lineNum = 0; bool SyntaxError = false; CommentEntry *curEntry = nullptr; CommentEntry *newCommentEntry; SectionEntry *newSectionEntry; KeyEntry *newKeyEntry; bool readfinished = false; while(!readfinished) { lineNum++; completeLine = ""; uint8_t tmp; size_t size = _stream.sizeg(); while(static_cast<uint32_t>(_stream.tellg()) < size-1) { tmp = _stream.get(); if(static_cast<uint32_t>(_stream.tellg()) == size-1) { readfinished = true; break; } else if(tmp == '\n') { break; } else if(tmp != '\r') { completeLine += tmp; } } const uint8_t *line = reinterpret_cast<const uint8_t*>(completeLine.c_str()); SyntaxError = false; int ret; ret = getNextChar(line,0); if(ret == -1) { // empty line or comment if((newCommentEntry = new CommentEntry(completeLine)) == nullptr) throw(std::bad_alloc()); if(curEntry == nullptr) { FirstLine = newCommentEntry; curEntry = newCommentEntry; } else { curEntry->nextEntry = newCommentEntry; newCommentEntry->prevEntry = curEntry; curEntry = newCommentEntry; } } else { if(line[ret] == '[') { // section line int sectionstart = ret+1; int sectionend = skipName(line,ret+1); if((line[sectionend] != ']') || (getNextChar(line,sectionend+1) != -1)) { SyntaxError = true; } else { // valid section line if((newSectionEntry = new SectionEntry(completeLine,sectionstart,sectionend-sectionstart)) == nullptr) throw(std::bad_alloc()); if(curEntry == nullptr) { FirstLine = newSectionEntry; curEntry = newSectionEntry; } else { curEntry->nextEntry = newSectionEntry; newSectionEntry->prevEntry = curEntry; curEntry = newSectionEntry; } InsertSection(newSectionEntry); curSectionEntry = newSectionEntry; } } else { // might be key/value line int keystart = ret; int keyend = skipKey(line,keystart); if(keystart == keyend) { SyntaxError = true; } else { ret = getNextChar(line,keyend); if((ret == -1) ||(line[ret] != '=')) { SyntaxError = true; } else { int valuestart = getNextChar(line,ret+1); int valueend; if(valuestart == -1) { SyntaxError = true; } else { if(line[valuestart] == '"') { // now get the next '"' valueend = getNextQuote(line,valuestart+1); if((valueend == -1) || (getNextChar(line,valueend+1) != -1)) { SyntaxError = true; } else { // valid key/value line if((newKeyEntry = new KeyEntry(completeLine,keystart,keyend-keystart,valuestart+1,valueend-valuestart-1)) == nullptr) throw(std::bad_alloc()); if(FirstLine == nullptr) { FirstLine = newKeyEntry; curEntry = newKeyEntry; } else { curEntry->nextEntry = newKeyEntry; newKeyEntry->prevEntry = curEntry; curEntry = newKeyEntry; } InsertKey(curSectionEntry,newKeyEntry); } } else { valueend = skipValue(line,valuestart); if(getNextChar(line,valueend) != -1) { SyntaxError = true; } else { // valid key/value line if((newKeyEntry = new KeyEntry(completeLine,keystart,keyend-keystart,valuestart,valueend-valuestart)) == nullptr) throw(std::bad_alloc()); if(FirstLine == nullptr) { FirstLine = newKeyEntry; curEntry = newKeyEntry; } else { curEntry->nextEntry = newKeyEntry; newKeyEntry->prevEntry = curEntry; curEntry = newKeyEntry; } InsertKey(curSectionEntry,newKeyEntry); } } } } } } } if(SyntaxError == true) { if(completeLine.size() < 100) { // there are some buggy ini-files which have a lot of waste at the end of the file // and it makes no sense to print all this stuff out. just skip it std::cerr << "IniFile: Syntax-Error in line " << lineNum << ":" << completeLine << " !" << std::endl; } // save this line as a comment if((newCommentEntry = new CommentEntry(completeLine)) == nullptr) throw(std::bad_alloc()); if(curEntry == nullptr) { FirstLine = newCommentEntry; curEntry = newCommentEntry; } else { curEntry->nextEntry = newCommentEntry; newCommentEntry->prevEntry = curEntry; curEntry = newCommentEntry; } } } }
/** Sets the std::string that is adressed by the section/key pair to value. If the section and/or the key does not exist it will be created. A valid sectionname/keyname is not allowed to contain '[',']',';' or '#' and can not start or end with whitespaces (' ' or '\\t'). \param section sectionname \param key keyname \param value value that should be set */ void IniFile::setStringValue(std::string section, std::string key, std::string value) { CommentEntry *curEntry = nullptr; SectionEntry *curSection = getSection(section); if(curSection == nullptr) { // create new section // test for valid section name bool NonNormalCharFound = false; for(unsigned int i = 0; i < section.size(); i++) { if( (!isNormalChar(section[i])) && (!isWhitespace(section[i])) ) { NonNormalCharFound = true; } } if(isWhitespace(section[0]) || isWhitespace(section[section.size()-1])) { NonNormalCharFound = true; } if(NonNormalCharFound == true) { std::cerr << "IniFile: Cannot create section with name " << section << "!" << std::endl; return; } std::string completeLine = "[" + section + "]"; if((curSection = new SectionEntry(completeLine,1,section.size())) == nullptr) throw(std::bad_alloc()); if(FirstLine == nullptr) { FirstLine = curSection; } else { curEntry = FirstLine; while(curEntry->nextEntry != nullptr) { curEntry = curEntry->nextEntry; } curEntry->nextEntry = curSection; curSection->prevEntry = curEntry; curEntry = curSection; } InsertSection(curSection); } KeyEntry *curKey = getKey(curSection,key); if(curKey == nullptr) { // create new key // test for valid key name bool NonNormalCharFound = false; for(unsigned int i = 0; i < key.size(); i++) { if( (!isNormalChar(key[i])) && (!isWhitespace(key[i])) ) { NonNormalCharFound = true; } } if(isWhitespace(key[0]) || isWhitespace(key[key.size()-1])) { NonNormalCharFound = true; } if(NonNormalCharFound == true) { std::cerr << "IniFile: Cannot create key with name " << key << "!" << std::endl; return; } std::string completeLine; int KeyStringStart; int KeyStringLength; int ValueStringStart; int ValueStringLength; if(value == "") { completeLine = key + " = \"" + value + "\"" ; KeyStringStart = 0; KeyStringLength = key.size(); ValueStringStart = key.size()+4; ValueStringLength = value.size(); } else { // test for non normal char NonNormalCharFound = false; for(unsigned int i = 0; i < value.size(); i++) { if(!isNormalChar(value[i])) { NonNormalCharFound = true; } } if(NonNormalCharFound == true) { completeLine = key + " = \"" + value + "\"" ; KeyStringStart = 0; KeyStringLength = key.size(); ValueStringStart = key.size()+4; ValueStringLength = value.size(); } else { completeLine = key + " = " + value; KeyStringStart = 0; KeyStringLength = key.size(); ValueStringStart = key.size()+3; ValueStringLength = value.size(); } } if((curKey = new KeyEntry(completeLine,KeyStringStart,KeyStringLength,ValueStringStart,ValueStringLength)) == nullptr) throw(std::bad_alloc()); if(curEntry != nullptr) { curEntry->nextEntry = curKey; curKey->prevEntry = curEntry; } else { KeyEntry *pKey = curSection->KeyRoot; if(pKey == nullptr) { // Section has no key yet if(curSection->nextEntry == nullptr) { // no line after this section declaration curSection->nextEntry = curKey; curKey->prevEntry = curSection; } else { // lines after this section declaration curSection->nextEntry->prevEntry = curKey; curKey->nextEntry = curSection->nextEntry; curSection->nextEntry = curKey; curKey->prevEntry = curSection; } } else { // Section already has some keys while(pKey->nextKey != nullptr) { pKey = pKey->nextKey; } if(pKey->nextEntry == nullptr) { // no line after this key pKey->nextEntry = curKey; curKey->prevEntry = pKey; } else { // lines after this section declaration pKey->nextEntry->prevEntry = curKey; curKey->nextEntry = pKey->nextEntry; pKey->nextEntry = curKey; curKey->prevEntry = pKey; } } } InsertKey(curSection,curKey); } else { curKey->CompleteLine.replace(curKey->ValueStringBegin,curKey->ValueStringLength,value); } }