00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "dictionary.H"
00028 #include "primitiveEntry.H"
00029 #include "dictionaryEntry.H"
00030 #include "regExp.H"
00031 #include "OSHA1stream.H"
00032
00033
00034
00035 defineTypeNameAndDebug(Foam::dictionary, 0);
00036
00037 const Foam::dictionary Foam::dictionary::null;
00038
00039
00040
00041
00042 bool Foam::dictionary::findInPatterns
00043 (
00044 const bool patternMatch,
00045 const word& Keyword,
00046 DLList<entry*>::const_iterator& wcLink,
00047 DLList<autoPtr<regExp> >::const_iterator& reLink
00048 ) const
00049 {
00050 if (patternEntries_.size())
00051 {
00052 while (wcLink != patternEntries_.end())
00053 {
00054 if
00055 (
00056 patternMatch
00057 ? reLink()->match(Keyword)
00058 : wcLink()->keyword() == Keyword
00059 )
00060 {
00061 return true;
00062 }
00063
00064 ++reLink;
00065 ++wcLink;
00066 }
00067 }
00068
00069 return false;
00070 }
00071
00072
00073 bool Foam::dictionary::findInPatterns
00074 (
00075 const bool patternMatch,
00076 const word& Keyword,
00077 DLList<entry*>::iterator& wcLink,
00078 DLList<autoPtr<regExp> >::iterator& reLink
00079 )
00080 {
00081 if (patternEntries_.size())
00082 {
00083 while (wcLink != patternEntries_.end())
00084 {
00085 if
00086 (
00087 patternMatch
00088 ? reLink()->match(Keyword)
00089 : wcLink()->keyword() == Keyword
00090 )
00091 {
00092 return true;
00093 }
00094
00095 ++reLink;
00096 ++wcLink;
00097 }
00098 }
00099
00100 return false;
00101 }
00102
00103
00104
00105
00106 Foam::dictionary::dictionary()
00107 :
00108 parent_(dictionary::null)
00109 {}
00110
00111
00112 Foam::dictionary::dictionary
00113 (
00114 const dictionary& parentDict,
00115 const dictionary& dict
00116 )
00117 :
00118 IDLList<entry>(dict, *this),
00119 name_(dict.name()),
00120 parent_(parentDict)
00121 {
00122 forAllIter(IDLList<entry>, *this, iter)
00123 {
00124 hashedEntries_.insert(iter().keyword(), &iter());
00125
00126 if (iter().keyword().isPattern())
00127 {
00128 patternEntries_.insert(&iter());
00129 patternRegexps_.insert
00130 (
00131 autoPtr<regExp>(new regExp(iter().keyword()))
00132 );
00133 }
00134 }
00135 }
00136
00137
00138 Foam::dictionary::dictionary
00139 (
00140 const dictionary& dict
00141 )
00142 :
00143 IDLList<entry>(dict, *this),
00144 name_(dict.name()),
00145 parent_(dictionary::null)
00146 {
00147 forAllIter(IDLList<entry>, *this, iter)
00148 {
00149 hashedEntries_.insert(iter().keyword(), &iter());
00150
00151 if (iter().keyword().isPattern())
00152 {
00153 patternEntries_.insert(&iter());
00154 patternRegexps_.insert
00155 (
00156 autoPtr<regExp>(new regExp(iter().keyword()))
00157 );
00158 }
00159 }
00160 }
00161
00162
00163 Foam::dictionary::dictionary
00164 (
00165 const dictionary* dictPtr
00166 )
00167 :
00168 parent_(dictionary::null)
00169 {
00170 if (dictPtr)
00171 {
00172 operator=(*dictPtr);
00173 }
00174 }
00175
00176
00177 Foam::dictionary::dictionary
00178 (
00179 const dictionary& parentDict,
00180 const Xfer<dictionary>& dict
00181 )
00182 :
00183 parent_(parentDict)
00184 {
00185 transfer(dict());
00186 }
00187
00188
00189 Foam::dictionary::dictionary
00190 (
00191 const Xfer<dictionary>& dict
00192 )
00193 :
00194 parent_(dictionary::null)
00195 {
00196 transfer(dict());
00197 }
00198
00199
00200 Foam::autoPtr<Foam::dictionary> Foam::dictionary::clone() const
00201 {
00202 return autoPtr<dictionary>(new dictionary(*this));
00203 }
00204
00205
00206
00207
00208 Foam::dictionary::~dictionary()
00209 {
00210
00211 }
00212
00213
00214
00215
00216 Foam::label Foam::dictionary::startLineNumber() const
00217 {
00218 if (size())
00219 {
00220 return first()->startLineNumber();
00221 }
00222 else
00223 {
00224 return -1;
00225 }
00226 }
00227
00228
00229 Foam::label Foam::dictionary::endLineNumber() const
00230 {
00231 if (size())
00232 {
00233 return last()->endLineNumber();
00234 }
00235 else
00236 {
00237 return -1;
00238 }
00239 }
00240
00241
00242 Foam::SHA1Digest Foam::dictionary::digest() const
00243 {
00244 OSHA1stream os;
00245
00246
00247 forAllConstIter(IDLList<entry>, *this, iter)
00248 {
00249 os << *iter;
00250 }
00251
00252 return os.digest();
00253 }
00254
00255
00256 bool Foam::dictionary::found(const word& keyword, bool recursive) const
00257 {
00258 if (hashedEntries_.found(keyword))
00259 {
00260 return true;
00261 }
00262 else
00263 {
00264 if (patternEntries_.size())
00265 {
00266 DLList<entry*>::const_iterator wcLink =
00267 patternEntries_.begin();
00268 DLList<autoPtr<regExp> >::const_iterator reLink =
00269 patternRegexps_.begin();
00270
00271
00272 if (findInPatterns(true, keyword, wcLink, reLink))
00273 {
00274 return true;
00275 }
00276 }
00277
00278 if (recursive && &parent_ != &dictionary::null)
00279 {
00280 return parent_.found(keyword, recursive);
00281 }
00282 else
00283 {
00284 return false;
00285 }
00286 }
00287 }
00288
00289
00290 const Foam::entry* Foam::dictionary::lookupEntryPtr
00291 (
00292 const word& keyword,
00293 bool recursive,
00294 bool patternMatch
00295 ) const
00296 {
00297 HashTable<entry*>::const_iterator iter = hashedEntries_.find(keyword);
00298
00299 if (iter == hashedEntries_.end())
00300 {
00301 if (patternMatch && patternEntries_.size())
00302 {
00303 DLList<entry*>::const_iterator wcLink =
00304 patternEntries_.begin();
00305 DLList<autoPtr<regExp> >::const_iterator reLink =
00306 patternRegexps_.begin();
00307
00308
00309 if (findInPatterns(patternMatch, keyword, wcLink, reLink))
00310 {
00311 return wcLink();
00312 }
00313 }
00314
00315 if (recursive && &parent_ != &dictionary::null)
00316 {
00317 return parent_.lookupEntryPtr(keyword, recursive, patternMatch);
00318 }
00319 else
00320 {
00321 return NULL;
00322 }
00323 }
00324
00325 return iter();
00326 }
00327
00328
00329 Foam::entry* Foam::dictionary::lookupEntryPtr
00330 (
00331 const word& keyword,
00332 bool recursive,
00333 bool patternMatch
00334 )
00335 {
00336 HashTable<entry*>::iterator iter = hashedEntries_.find(keyword);
00337
00338 if (iter == hashedEntries_.end())
00339 {
00340 if (patternMatch && patternEntries_.size())
00341 {
00342 DLList<entry*>::iterator wcLink =
00343 patternEntries_.begin();
00344 DLList<autoPtr<regExp> >::iterator reLink =
00345 patternRegexps_.begin();
00346
00347
00348 if (findInPatterns(patternMatch, keyword, wcLink, reLink))
00349 {
00350 return wcLink();
00351 }
00352 }
00353
00354 if (recursive && &parent_ != &dictionary::null)
00355 {
00356 return const_cast<dictionary&>(parent_).lookupEntryPtr
00357 (
00358 keyword,
00359 recursive,
00360 patternMatch
00361 );
00362 }
00363 else
00364 {
00365 return NULL;
00366 }
00367 }
00368
00369 return iter();
00370 }
00371
00372
00373 const Foam::entry& Foam::dictionary::lookupEntry
00374 (
00375 const word& keyword,
00376 bool recursive,
00377 bool patternMatch
00378 ) const
00379 {
00380 const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
00381
00382 if (entryPtr == NULL)
00383 {
00384 FatalIOErrorIn
00385 (
00386 "dictionary::lookupEntry(const word&, bool, bool) const",
00387 *this
00388 ) << "keyword " << keyword << " is undefined in dictionary "
00389 << name()
00390 << exit(FatalIOError);
00391 }
00392
00393 return *entryPtr;
00394 }
00395
00396
00397 Foam::ITstream& Foam::dictionary::lookup
00398 (
00399 const word& keyword,
00400 bool recursive,
00401 bool patternMatch
00402 ) const
00403 {
00404 return lookupEntry(keyword, recursive, patternMatch).stream();
00405 }
00406
00407
00408 bool Foam::dictionary::isDict(const word& keyword) const
00409 {
00410
00411 const entry* entryPtr = lookupEntryPtr(keyword, false, true);
00412
00413 if (entryPtr)
00414 {
00415 return entryPtr->isDict();
00416 }
00417 else
00418 {
00419 return false;
00420 }
00421 }
00422
00423
00424 const Foam::dictionary* Foam::dictionary::subDictPtr(const word& keyword) const
00425 {
00426 const entry* entryPtr = lookupEntryPtr(keyword, false, true);
00427
00428 if (entryPtr)
00429 {
00430 return &entryPtr->dict();
00431 }
00432 else
00433 {
00434 return NULL;
00435 }
00436 }
00437
00438
00439 const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const
00440 {
00441 const entry* entryPtr = lookupEntryPtr(keyword, false, true);
00442
00443 if (entryPtr == NULL)
00444 {
00445 FatalIOErrorIn
00446 (
00447 "dictionary::subDict(const word& keyword) const",
00448 *this
00449 ) << "keyword " << keyword << " is undefined in dictionary "
00450 << name()
00451 << exit(FatalIOError);
00452 }
00453 return entryPtr->dict();
00454 }
00455
00456
00457 Foam::dictionary& Foam::dictionary::subDict(const word& keyword)
00458 {
00459 entry* entryPtr = lookupEntryPtr(keyword, false, true);
00460
00461 if (entryPtr == NULL)
00462 {
00463 FatalIOErrorIn
00464 (
00465 "dictionary::subDict(const word& keyword)",
00466 *this
00467 ) << "keyword " << keyword << " is undefined in dictionary "
00468 << name()
00469 << exit(FatalIOError);
00470 }
00471 return entryPtr->dict();
00472 }
00473
00474
00475 Foam::wordList Foam::dictionary::toc() const
00476 {
00477 wordList keys(size());
00478
00479 label nKeys = 0;
00480 forAllConstIter(IDLList<entry>::const_iterator, *this, iter)
00481 {
00482 keys[nKeys++] = iter().keyword();
00483 }
00484
00485 return keys;
00486 }
00487
00488
00489 Foam::List<Foam::keyType> Foam::dictionary::keys(bool patterns) const
00490 {
00491 List<keyType> keys(size());
00492
00493 label nKeys = 0;
00494 forAllConstIter(IDLList<entry>, *this, iter)
00495 {
00496 if (iter().keyword().isPattern() ? patterns : !patterns)
00497 {
00498 keys[nKeys++] = iter().keyword();
00499 }
00500 }
00501 keys.setSize(nKeys);
00502
00503 return keys;
00504 }
00505
00506
00507 bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
00508 {
00509 HashTable<entry*>::iterator iter = hashedEntries_.find
00510 (
00511 entryPtr->keyword()
00512 );
00513
00514 if (mergeEntry && iter != hashedEntries_.end())
00515 {
00516
00517 if (iter()->isDict() && entryPtr->isDict())
00518 {
00519 iter()->dict().merge(entryPtr->dict());
00520 delete entryPtr;
00521
00522 return true;
00523 }
00524 else
00525 {
00526
00527 IDLList<entry>::replace(iter(), entryPtr);
00528 delete iter();
00529 hashedEntries_.erase(iter);
00530
00531 if (hashedEntries_.insert(entryPtr->keyword(), entryPtr))
00532 {
00533 entryPtr->name() = name_ + "::" + entryPtr->keyword();
00534
00535 if (entryPtr->keyword().isPattern())
00536 {
00537 patternEntries_.insert(entryPtr);
00538 patternRegexps_.insert
00539 (
00540 autoPtr<regExp>(new regExp(entryPtr->keyword()))
00541 );
00542 }
00543
00544 return true;
00545 }
00546 else
00547 {
00548 IOWarningIn("dictionary::add(entry*, bool)", (*this))
00549 << "problem replacing entry "<< entryPtr->keyword()
00550 << " in dictionary " << name() << endl;
00551
00552 IDLList<entry>::remove(entryPtr);
00553 delete entryPtr;
00554 return false;
00555 }
00556 }
00557 }
00558
00559 if (hashedEntries_.insert(entryPtr->keyword(), entryPtr))
00560 {
00561 entryPtr->name() = name_ + "::" + entryPtr->keyword();
00562 IDLList<entry>::append(entryPtr);
00563
00564 if (entryPtr->keyword().isPattern())
00565 {
00566 patternEntries_.insert(entryPtr);
00567 patternRegexps_.insert
00568 (
00569 autoPtr<regExp>(new regExp(entryPtr->keyword()))
00570 );
00571 }
00572
00573 return true;
00574 }
00575 else
00576 {
00577 IOWarningIn("dictionary::add(entry*, bool)", (*this))
00578 << "attempt to add entry "<< entryPtr->keyword()
00579 << " which already exists in dictionary " << name()
00580 << endl;
00581
00582 delete entryPtr;
00583 return false;
00584 }
00585 }
00586
00587
00588 void Foam::dictionary::add(const entry& e, bool mergeEntry)
00589 {
00590 add(e.clone(*this).ptr(), mergeEntry);
00591 }
00592
00593
00594 void Foam::dictionary::add(const keyType& k, const word& w, bool overwrite)
00595 {
00596 add(new primitiveEntry(k, token(w)), overwrite);
00597 }
00598
00599
00600 void Foam::dictionary::add
00601 (
00602 const keyType& k,
00603 const Foam::string& s,
00604 bool overwrite
00605 )
00606 {
00607 add(new primitiveEntry(k, token(s)), overwrite);
00608 }
00609
00610
00611 void Foam::dictionary::add(const keyType& k, const label l, bool overwrite)
00612 {
00613 add(new primitiveEntry(k, token(l)), overwrite);
00614 }
00615
00616
00617 void Foam::dictionary::add(const keyType& k, const scalar s, bool overwrite)
00618 {
00619 add(new primitiveEntry(k, token(s)), overwrite);
00620 }
00621
00622
00623 void Foam::dictionary::add
00624 (
00625 const keyType& k,
00626 const dictionary& d,
00627 bool mergeEntry
00628 )
00629 {
00630 add(new dictionaryEntry(k, *this, d), mergeEntry);
00631 }
00632
00633
00634 void Foam::dictionary::set(entry* entryPtr)
00635 {
00636 entry* existingPtr = lookupEntryPtr(entryPtr->keyword(), false, true);
00637
00638
00639 if (existingPtr && existingPtr->isDict())
00640 {
00641 existingPtr->dict().clear();
00642 }
00643 add(entryPtr, true);
00644 }
00645
00646
00647 void Foam::dictionary::set(const entry& e)
00648 {
00649 set(e.clone(*this).ptr());
00650 }
00651
00652
00653 void Foam::dictionary::set(const keyType& k, const dictionary& d)
00654 {
00655 set(new dictionaryEntry(k, *this, d));
00656 }
00657
00658
00659 bool Foam::dictionary::remove(const word& Keyword)
00660 {
00661 HashTable<entry*>::iterator iter = hashedEntries_.find(Keyword);
00662
00663 if (iter != hashedEntries_.end())
00664 {
00665
00666 DLList<entry*>::iterator wcLink =
00667 patternEntries_.begin();
00668 DLList<autoPtr<regExp> >::iterator reLink =
00669 patternRegexps_.begin();
00670
00671
00672 if (findInPatterns(false, Keyword, wcLink, reLink))
00673 {
00674 patternEntries_.remove(wcLink);
00675 patternRegexps_.remove(reLink);
00676 }
00677
00678 IDLList<entry>::remove(iter());
00679 delete iter();
00680 hashedEntries_.erase(iter);
00681
00682 return true;
00683 }
00684 else
00685 {
00686 return false;
00687 }
00688 }
00689
00690
00691 bool Foam::dictionary::changeKeyword
00692 (
00693 const keyType& oldKeyword,
00694 const keyType& newKeyword,
00695 bool forceOverwrite
00696 )
00697 {
00698
00699 if (oldKeyword == newKeyword)
00700 {
00701 return false;
00702 }
00703
00704 HashTable<entry*>::iterator iter = hashedEntries_.find(oldKeyword);
00705
00706
00707 if (iter == hashedEntries_.end())
00708 {
00709 return false;
00710 }
00711
00712 if (iter()->keyword().isPattern())
00713 {
00714 FatalErrorIn
00715 (
00716 "dictionary::changeKeyword(const word&, const word&, bool)"
00717 ) << "Old keyword "<< oldKeyword
00718 << " is a pattern."
00719 << "Pattern replacement not yet implemented."
00720 << exit(FatalError);
00721 }
00722
00723
00724 HashTable<entry*>::iterator iter2 = hashedEntries_.find(newKeyword);
00725
00726
00727 if (iter2 != hashedEntries_.end())
00728 {
00729 if (forceOverwrite)
00730 {
00731 if (iter2()->keyword().isPattern())
00732 {
00733
00734 DLList<entry*>::iterator wcLink =
00735 patternEntries_.begin();
00736 DLList<autoPtr<regExp> >::iterator reLink =
00737 patternRegexps_.begin();
00738
00739
00740 if (findInPatterns(false, iter2()->keyword(), wcLink, reLink))
00741 {
00742 patternEntries_.remove(wcLink);
00743 patternRegexps_.remove(reLink);
00744 }
00745 }
00746
00747 IDLList<entry>::replace(iter2(), iter());
00748 delete iter2();
00749 hashedEntries_.erase(iter2);
00750
00751 }
00752 else
00753 {
00754 WarningIn
00755 (
00756 "dictionary::changeKeyword(const word&, const word&, bool)"
00757 ) << "cannot rename keyword "<< oldKeyword
00758 << " to existing keyword " << newKeyword
00759 << " in dictionary " << name() << endl;
00760 return false;
00761 }
00762 }
00763
00764
00765 iter()->keyword() = newKeyword;
00766 iter()->name() = name_ + "::" + newKeyword;
00767 hashedEntries_.erase(oldKeyword);
00768 hashedEntries_.insert(newKeyword, iter());
00769
00770 if (newKeyword.isPattern())
00771 {
00772 patternEntries_.insert(iter());
00773 patternRegexps_.insert
00774 (
00775 autoPtr<regExp>(new regExp(newKeyword))
00776 );
00777 }
00778
00779 return true;
00780 }
00781
00782
00783 bool Foam::dictionary::merge(const dictionary& dict)
00784 {
00785
00786 if (this == &dict)
00787 {
00788 FatalErrorIn("dictionary::merge(const dictionary&)")
00789 << "attempted merge to self for dictionary " << name()
00790 << abort(FatalError);
00791 }
00792
00793 bool changed = false;
00794
00795 forAllConstIter(IDLList<entry>, dict, iter)
00796 {
00797 HashTable<entry*>::iterator fnd = hashedEntries_.find(iter().keyword());
00798
00799 if (fnd != hashedEntries_.end())
00800 {
00801
00802
00803 if (fnd()->isDict() && iter().isDict())
00804 {
00805 if (fnd()->dict().merge(iter().dict()))
00806 {
00807 changed = true;
00808 }
00809 }
00810 else
00811 {
00812 add(iter().clone(*this).ptr(), true);
00813 changed = true;
00814 }
00815 }
00816 else
00817 {
00818
00819 add(iter().clone(*this).ptr());
00820 changed = true;
00821 }
00822 }
00823
00824 return changed;
00825 }
00826
00827
00828 void Foam::dictionary::clear()
00829 {
00830 IDLList<entry>::clear();
00831 hashedEntries_.clear();
00832 patternEntries_.clear();
00833 patternRegexps_.clear();
00834 }
00835
00836
00837 void Foam::dictionary::transfer(dictionary& dict)
00838 {
00839
00840
00841 name_ = dict.name_;
00842
00843 IDLList<entry>::transfer(dict);
00844 hashedEntries_.transfer(dict.hashedEntries_);
00845 patternEntries_.transfer(dict.patternEntries_);
00846 patternRegexps_.transfer(dict.patternRegexps_);
00847 }
00848
00849
00850 Foam::Xfer<Foam::dictionary> Foam::dictionary::xfer()
00851 {
00852 return xferMove(*this);
00853 }
00854
00855
00856
00857
00858 Foam::ITstream& Foam::dictionary::operator[](const word& keyword) const
00859 {
00860 return lookup(keyword);
00861 }
00862
00863
00864 void Foam::dictionary::operator=(const dictionary& rhs)
00865 {
00866
00867 if (this == &rhs)
00868 {
00869 FatalErrorIn("dictionary::operator=(const dictionary&)")
00870 << "attempted assignment to self for dictionary " << name()
00871 << abort(FatalError);
00872 }
00873
00874 name_ = rhs.name();
00875 clear();
00876
00877
00878
00879
00880 forAllConstIter(IDLList<entry>, rhs, iter)
00881 {
00882 add(iter().clone(*this).ptr());
00883 }
00884 }
00885
00886
00887 void Foam::dictionary::operator+=(const dictionary& rhs)
00888 {
00889
00890 if (this == &rhs)
00891 {
00892 FatalErrorIn("dictionary::operator+=(const dictionary&)")
00893 << "attempted addition assignment to self for dictionary " << name()
00894 << abort(FatalError);
00895 }
00896
00897 forAllConstIter(IDLList<entry>, rhs, iter)
00898 {
00899 add(iter().clone(*this).ptr());
00900 }
00901 }
00902
00903
00904 void Foam::dictionary::operator|=(const dictionary& rhs)
00905 {
00906
00907 if (this == &rhs)
00908 {
00909 FatalErrorIn("dictionary::operator|=(const dictionary&)")
00910 << "attempted assignment to self for dictionary " << name()
00911 << abort(FatalError);
00912 }
00913
00914 forAllConstIter(IDLList<entry>, rhs, iter)
00915 {
00916 if (!found(iter().keyword()))
00917 {
00918 add(iter().clone(*this).ptr());
00919 }
00920 }
00921 }
00922
00923
00924 void Foam::dictionary::operator<<=(const dictionary& rhs)
00925 {
00926
00927 if (this == &rhs)
00928 {
00929 FatalErrorIn("dictionary::operator<<=(const dictionary&)")
00930 << "attempted assignment to self for dictionary " << name()
00931 << abort(FatalError);
00932 }
00933
00934 forAllConstIter(IDLList<entry>, rhs, iter)
00935 {
00936 set(iter().clone(*this).ptr());
00937 }
00938 }
00939
00940
00941
00942
00943 Foam::dictionary Foam::operator+
00944 (
00945 const dictionary& dict1,
00946 const dictionary& dict2
00947 )
00948 {
00949 dictionary sum(dict1);
00950 sum += dict2;
00951 return sum;
00952 }
00953
00954
00955 Foam::dictionary Foam::operator|
00956 (
00957 const dictionary& dict1,
00958 const dictionary& dict2
00959 )
00960 {
00961 dictionary sum(dict1);
00962 sum |= dict2;
00963 return sum;
00964 }
00965
00966
00967