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 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
/*=========================================================================
Program: ITK-SNAP
Module: $RCSfile: Registry.cxx,v $
Language: C++
Date: $Date: 2007/12/30 04:05:12 $
Version: $Revision: 1.2 $
Copyright (c) 2007 Paul A. Yushkevich
This file is part of ITK-SNAP
ITK-SNAP 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 3 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, see <http://www.gnu.org/licenses/>.
-----
Copyright (c) 2003 Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "Registry.h"
#include "IRISVectorTypes.h"
#include "itkXMLFile.h"
#include <stdio.h>
#include <cstdlib>
#include <cstdarg>
#include <fstream>
#include <iomanip>
#include "itksys/SystemTools.hxx"
#include "IRISException.h"
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
using namespace std;
/** Reader for XML files */
class RegistryXMLFileReader : public itk::XMLReader<Registry>
{
public:
irisITKObjectMacro(RegistryXMLFileReader, itk::XMLReader<Registry>)
protected:
RegistryXMLFileReader() {}
virtual int CanReadFile(const char *name);
virtual void StartElement(const char *name, const char **atts);
virtual void EndElement(const char *name);
virtual void CharacterDataHandler(const char *inData, int inLength);
static int strcmpi(const char *str1, const char *str2)
{
return itksys::SystemTools::Strucmp(str1, str2);
}
// The folder stack
std::list<Registry *> m_FolderStack;
};
int RegistryXMLFileReader::CanReadFile(const char *name)
{
if ( !itksys::SystemTools::FileExists(name)
|| itksys::SystemTools::FileIsDirectory(name)
|| itksys::SystemTools::FileLength(name) == 0 )
{
return 0;
}
return 1;
}
void RegistryXMLFileReader::StartElement(const char *name, const char **atts)
{
// If this is the root-level element <registry>, it can be ignored
if(strcmpi(name, "registry") == 0)
{
// Put the target registry on the stack
assert(this->GetOutputObject());
m_FolderStack.push_back(this->GetOutputObject());
return;
}
// If the stack is empty throw up
if(m_FolderStack.size() == 0)
throw IRISException("Problem parsing Registry XML file. The file might not be valid.");
// Read the attributes into a map
typedef std::map<std::string, std::string> AttributeMap;
typedef AttributeMap::const_iterator AttrIter;
AttributeMap attrmap;
for(int i = 0; atts[i] != NULL && atts[i+1] != NULL; i+=2)
attrmap[itksys::SystemTools::LowerCase(atts[i])] = atts[i+1];
// Process tags
if(strcmpi(name, "folder") == 0)
{
AttrIter itKey = attrmap.find("key");
if(itKey == attrmap.end())
throw IRISException("Missing 'key' attribute to <folder> element");
// Create a new folder and place it on the stack
Registry &newFolder = m_FolderStack.back()->Folder(itKey->second);
m_FolderStack.push_back(&newFolder);
}
else if(strcmpi(name, "entry") == 0)
{
AttrIter itKey = attrmap.find("key");
if(itKey == attrmap.end())
throw IRISException("Missing 'key' attribute to <entry> element");
AttrIter itValue = attrmap.find("value");
if(itValue == attrmap.end())
throw IRISException("Missing 'value' attribute to <entry> element");
// Create a new entry (TODO: decode!)
m_FolderStack.back()->Entry(itKey->second) = RegistryValue(itValue->second);
}
else
throw IRISException("Unknown XML element <%s>", name);
}
void RegistryXMLFileReader::EndElement(const char *name)
{
if(strcmpi(name, "registry") == 0)
{
m_FolderStack.clear();
}
else if(strcmpi(name, "folder") == 0)
{
m_FolderStack.pop_back();
}
}
void RegistryXMLFileReader::CharacterDataHandler(const char *inData, int inLength)
{
return;
}
RegistryValue
::RegistryValue()
{
m_Null = true;
m_String = "";
}
RegistryValue
::RegistryValue(const std::string &input)
{
m_Null = false;
m_String = input;
}
RegistryValue&
Registry::Entry(const std::string &key)
{
// Get the containing folder
StringType::size_type iDot = key.find_first_of('.');
// There is a subfolder
if(iDot != key.npos)
{
StringType child = key.substr(0,iDot);
StringType childKey = key.substr(iDot+1);
return Folder(child).Entry(childKey);
}
// Search for the key and return it if found
EntryIterator it = m_EntryMap.find(key);
if(it != m_EntryMap.end())
return it->second;
// Key was not found, create a null entry
m_EntryMap[key] = RegistryValue();
return m_EntryMap[key];
}
Registry::StringType
Registry::Key(const char *format,...)
{
// A string for prinf-ing
static char buffer[1024];
// Do the printf operation
va_list al;
va_start(al,format);
vsprintf(buffer,format,al);
va_end(al);
// Use the string as parameter
return StringType(buffer);
}
int
Registry
::GetEntryKeys(StringListType &targetArray)
{
// Iterate through keys in ascending order
for(EntryIterator it=m_EntryMap.begin();it!=m_EntryMap.end();++it)
{
// Put the key in the array
targetArray.push_back(it->first);
}
// Return the number of keys copied
return targetArray.size();
}
int
Registry
::GetFolderKeys(StringListType &targetArray)
{
// Iterate through keys in ascending order
for(FolderIterator it=m_FolderMap.begin();it!=m_FolderMap.end();++it)
{
// Put the key in the array
targetArray.push_back(it->first);
}
// Return the number of keys copied
return targetArray.size();
}
bool Registry::HasEntry(const Registry::StringType &key)
{
// Get the containing folder
StringType::size_type iDot = key.find_first_of('.');
// There is a subfolder
if(iDot != key.npos)
{
StringType child = key.substr(0,iDot);
StringType childKey = key.substr(iDot+1);
return Folder(child).HasEntry(childKey);
}
// Search for the key and return it if found
EntryIterator it = m_EntryMap.find(key);
return it != m_EntryMap.end();
}
bool Registry::HasFolder(const Registry::StringType &key)
{
// Get the containing folder
StringType::size_type iDot = key.find_first_of('.');
// There is a subfolder
if(iDot != key.npos)
{
StringType child = key.substr(0,iDot);
StringType childKey = key.substr(iDot+1);
return Folder(child).HasFolder(childKey);
}
// Search for the key and return it if found
FolderIterator it = m_FolderMap.find(key);
return it != m_FolderMap.end();
}
void
Registry
::Write(ostream &sout,const StringType &prefix)
{
// Write the entries in this folder
for(EntryIterator ite = m_EntryMap.begin();ite != m_EntryMap.end(); ++ite)
{
// Only write the non-null entries
if(!ite->second.IsNull())
{
// Write the key =
sout << prefix << Encode(ite->first) << " = ";
// Write the encoded value
sout << Encode(ite->second.GetInternalString()) << endl;
}
}
// Write the folders
for(FolderIterator itf = m_FolderMap.begin(); itf != m_FolderMap.end(); ++itf)
{
// Write the folder contents (recursive, contents prefixed with full path name)
itf->second->Write(sout, prefix + itf->first + "." );
}
}
void
Registry
::WriteXML(ostream &sout, const StringType &prefix)
{
// Write the entries in this folder
for(EntryIterator ite = m_EntryMap.begin();ite != m_EntryMap.end(); ++ite)
{
// Only write the non-null entries
if(!ite->second.IsNull())
{
// Write the key
sout << prefix << "<entry key=\"" << EncodeXML(ite->first) << "\"";
// Write the encoded value
sout << " value=\"" << EncodeXML(ite->second.GetInternalString()) << "\" />" << endl;
}
}
// Write the folders
for(FolderIterator itf = m_FolderMap.begin(); itf != m_FolderMap.end(); ++itf)
{
// Write the folder tag
sout << prefix << "<folder key=\"" << EncodeXML(itf->first) << "\" >" << endl;
// Write the folder contents (recursive, contents prefixed with full path name)
itf->second->WriteXML(sout, prefix + " ");
// Close the folder
sout << prefix << "</folder>" << endl;
}
}
void
Registry
::SetFlagAddIfNotFound(bool yesno)
{
// Set the internal value
m_AddIfNotFound = yesno;
// Propagate to all the children folders
for(FolderIterator itf = m_FolderMap.begin(); itf != m_FolderMap.end(); ++itf)
{
itf->second->SetFlagAddIfNotFound(yesno);
}
}
void Registry::CleanEmptyFolders()
{
// Iterate over all the subfolders
FolderMapType::iterator itf = m_FolderMap.begin();
while(itf != m_FolderMap.end())
{
itf->second->CleanEmptyFolders();
if(itf->second->IsEmpty())
m_FolderMap.erase(itf++);
else
itf++;
}
}
bool Registry::IsZeroSizeArray()
{
return
this->HasEntry("ArraySize") &&
this->Entry("ArraySize")[(unsigned int) 0] == 0 &&
this->m_EntryMap.size() == 1 &&
this->m_FolderMap.size() == 0;
}
void Registry::CleanZeroSizeArrays()
{
// Iterate over all the subfolders
FolderMapType::iterator itf = m_FolderMap.begin();
while(itf != m_FolderMap.end())
{
// Do the recursive part
itf->second->CleanZeroSizeArrays();
// Check if it has the array size key
if(itf->second->IsZeroSizeArray())
m_FolderMap.erase(itf++);
else
itf++;
}
}
void
Registry
::CollectKeys(StringListType &keyList,const StringType &prefix)
{
// Go through the children
for(FolderIterator itf = m_FolderMap.begin(); itf != m_FolderMap.end(); ++itf)
{
// Collect the child's keys with a new prefix
itf->second->CollectKeys(keyList, prefix + itf->first + ".");
}
// Add the keys in this folder
for(EntryIterator ite = m_EntryMap.begin();ite != m_EntryMap.end(); ++ite)
{
// Add the key to the collection list
keyList.push_back(prefix + ite->first);
}
}
void
Registry
::Update(const Registry ®)
{
// Go through the children
for(FolderIterator itf = reg.m_FolderMap.begin();
itf != reg.m_FolderMap.end(); ++itf)
{
// Update the sub-folder
this->Folder(itf->first).Update(*(itf->second));
}
// Add the keys in this folder
for(EntryConstIterator ite = reg.m_EntryMap.begin();
ite != reg.m_EntryMap.end(); ++ite)
{
RegistryValue &entry = Entry(ite->first);
entry = ite->second;
}
}
Registry::StringType
Registry
::FindValue(const StringType& value)
{
// Add the keys in this folder
for(EntryIterator ite = m_EntryMap.begin();ite != m_EntryMap.end(); ++ite)
{
if(ite->second.GetInternalString() == value)
return ite->first;
}
return "";
}
void
Registry
::RemoveKeys(const char *match)
{
// Create a match substring
string sMatch = (match) ? match : 0;
// Search and delete from the map
EntryMapType newMap;
for(EntryIterator it=m_EntryMap.begin(); it != m_EntryMap.end(); it++)
{
if(it->first.find(sMatch) != 0)
newMap[it->first] = it->second;
}
m_EntryMap = newMap;
}
void
Registry
::Clear()
{
m_EntryMap.clear();
m_FolderMap.clear();
}
bool Registry::IsEmpty() const
{
return m_EntryMap.size() == 0 && m_FolderMap.size() == 0;
}
Registry::StringType
Registry
::EncodeXML(const StringType &input)
{
IRISOStringStream oss;
for(unsigned int i=0; i < input.length() ; i++)
{
// Map the character to positive integer (0..255)
char c = input[i];
// There are special characters not allowed in XML
switch(c)
{
case '<' :
oss << "<"; break;
case '>' :
oss << ">"; break;
case '&' :
oss << "&"; break;
case '\'' :
oss << "'"; break;
case '\"' :
oss << """; break;
default:
oss << c; break;
}
}
// Return the resulting string
return oss.str();
}
Registry::StringType Registry::DecodeXML(const Registry::StringType &input)
{
return input;
}
Registry::StringType
Registry
::Encode(const StringType &input)
{
IRISOStringStream oss;
for(unsigned int i=0; i < input.length() ; i++)
{
// Map the character to positive integer (0..255)
char c = input[i];
int v = static_cast<int>(static_cast<unsigned char>(c));
// We only print ASCII codes
if(v <= 0x20 || v >= 0x7f || c == '%')
{
// Replace character by a escape string
oss << '%';
oss << setw(2) << setfill('0') << hex << v;
}
else
{
// Just copy the character
oss << c;
}
}
// Return the resulting string
return oss.str();
}
Registry::StringType
Registry::Decode(const StringType &input)
{
// Create an input stream
IRISIStringStream iss(input);
IRISOStringStream oss;
// Read until we run out or crash
while(iss.good())
{
// Read the next character
char c = (char) iss.get();
// Check if the character needs to be translated
if(!isprint(c))
{
continue;
}
else if(c != '%')
{
// Just copy the character
oss.put(c);
}
else
{
// A pair of chars
char c1,c2;
// Read the hex digit
iss >> c1;
iss >> c2;
// Reconstruct the hex
int d1 = (c1 < 'a') ? c1 - '0' : c1 - 'a' + 10;
int d2 = (c2 < 'a') ? c2 - '0' : c2 - 'a' + 10;
int digit = d1 * 16 + d2;
// See if the read succeeded, only then use the digit
if(iss.good())
oss << (char)digit;
// A good place to throw an exception (for strict interpretation)
}
}
// Return the result
return oss.str();
}
void
Registry
::Read(istream &sin, ostream &oss)
{
unsigned int lineNumber = 1;
while(sin.good())
{
// Read a line from the file
char buffer[1024];
sin.getline(buffer,1024);
// Find the first character in the string
StringType line = buffer;
StringType::size_type iToken = line.find_first_not_of(" \t\v\r\n");
// Skip blank lines
if(iToken == line.npos)
continue;
// Check if it's a comment
if(line[iToken] == '#')
continue;
// Find an equal sign in the string
StringType::size_type iOper = line.find_first_of('=');
if(iOper == line.npos)
{
// Not a valid line
oss << std::setw(5) << lineNumber << " : Missing '='; line ignored." << endl;
continue;
}
if(iOper == iToken)
{
// Missing key
oss << std::setw(5) << lineNumber << " : Missing key before '='; line ignored." << endl;
continue;
}
// Extract the key
string key = Decode(
line.substr(iToken,line.find_first_of(" \t\v\r\n=",iToken) - iToken));
// Extract the value
StringType::size_type iValue = line.find_first_not_of(" \t\v\r\n=",iOper);
string value = "";
if (iValue != line.npos)
{
value = line.substr(iValue);
}
// Now the key-value pair is present. Add it using the normal interface
Entry(key) = RegistryValue(Decode(value));
++lineNumber;
}
}
Registry &
Registry
::Folder(const string &key)
{
// Find the first separator in the key string
StringType::size_type iDot = key.find_first_of('.');
// Recurse on the immideate folder
if(iDot != key.npos)
{
StringType child = key.substr(0,iDot);
StringType childKey = key.substr(iDot+1);
return Folder(child).Folder(childKey);
}
// Get the folder, adding if necessary
FolderIterator it = m_FolderMap.find(key);
if(it != m_FolderMap.end())
return *(it->second);
// Add the folder
m_FolderMap[key] = new Registry();
m_FolderMap[key]->m_AddIfNotFound = m_AddIfNotFound;
return *m_FolderMap[key];
}
Registry
::Registry()
{
m_AddIfNotFound = false;
}
Registry
::Registry(const char *fname)
{
ReadFromFile(fname);
}
Registry::Registry(const Registry &source)
{
*this = source;
}
void Registry::operator =(const Registry &source)
{
this->Clear();
this->Update(source);
this->m_AddIfNotFound = source.m_AddIfNotFound;
}
Registry::
~Registry()
{
// Delete all the sub-folders
for(FolderIterator itf = m_FolderMap.begin(); itf != m_FolderMap.end(); ++itf)
delete itf->second;
}
bool Registry::operator == (const Registry &other) const
{
// Compare the folders
if(m_FolderMap.size() != other.m_FolderMap.size())
return false;
for(FolderIterator it1 = m_FolderMap.begin(), it2 = other.m_FolderMap.begin();
it1 != m_FolderMap.end(); ++it1, ++it2)
{
// Compare keys
const StringType &key1 = it1->first;
const StringType &key2 = it2->first;
if(key1 != key2)
return false;
// Compare subfolder contents (recursively)
if(*(it1->second) != *(it2->second))
return false;
}
// Compare the entries
if(m_EntryMap.size() != other.m_EntryMap.size())
return false;
for(EntryConstIterator it1 = m_EntryMap.begin(), it2 = other.m_EntryMap.begin();
it1 != m_EntryMap.end(); ++it1, ++it2)
{
// Compare keys
const StringType &key1 = it1->first;
const StringType &key2 = it2->first;
if(key1 != key2)
return false;
// Compare subfolder contents (recursively)
if(it1->second != it2->second)
return false;
}
return true;
}
bool Registry::operator != (const Registry &other) const
{
return ! (*this == other);
}
/**
* Write the folder to a disk
*/
void
Registry
::WriteToFile(const char *pathname, const char *header)
{
// Open the file
ofstream sout(pathname,std::ios::out);
// Set the stream to be picky
sout.exceptions(std::ios::failbit);
// Write the header
if(header)
sout << header << endl;
// Write to the stream
Write(sout,"");
}
void Registry::WriteToXMLFile(const char *pathname, const char *header)
{
// Open the file
ofstream sout(pathname,std::ios::out);
// Set the stream to be picky
sout.exceptions(std::ios::failbit);
// Write the XML string
sout << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << endl;
// Write the header
if(header)
sout << "<!--" << header << "-->" << endl;
// Write the DOCTYPE content
sout << "<!DOCTYPE registry [" << endl
<< "<!ELEMENT registry (entry*,folder*)>" << endl
<< "<!ELEMENT folder (entry*,folder*)>" << endl
<< "<!ELEMENT entry EMPTY>" << endl
<< "<!ATTLIST folder key CDATA #REQUIRED>" << endl
<< "<!ATTLIST entry key CDATA #REQUIRED>" << endl
<< "<!ATTLIST entry value CDATA #REQUIRED>" << endl
<< "]>" << endl;
// Write to the stream
sout << "<registry>" << endl;
WriteXML(sout, " ");
sout << "</registry>" << endl;
}
/**
* Read folder from file
*/
void
Registry
::ReadFromFile(const char *pathname)
{
// Create output stream
ifstream sin(pathname,std::ios::in);
if(!sin.good())
throw IOException("Unable to open the Registry file");
ReadFromStream(sin);
}
void Registry
::ReadFromStream(istream &sin)
{
// Create an error stream
IRISOStringStream serr;
try
{
// Try reading
Read(sin,serr);
}
catch(...)
{
throw IOException("Unable to read the Registry file");
}
// If the error stream is not empty, throw an exception
if(serr.str().length())
throw SyntaxException(serr.str().c_str());
}
void Registry::ReadFromXMLFile(const char *pathname)
{
SmartPtr<RegistryXMLFileReader> reader = RegistryXMLFileReader::New();
reader->SetOutputObject(this);
reader->SetFilename(pathname);
reader->GenerateOutputInformation();
}
|