Registry.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(Registry.hxx)
  12. #include KWSYS_HEADER(Configure.hxx)
  13. #include KWSYS_HEADER(ios/iostream)
  14. #include KWSYS_HEADER(stl/string)
  15. #include KWSYS_HEADER(stl/map)
  16. #include KWSYS_HEADER(ios/iostream)
  17. #include KWSYS_HEADER(ios/fstream)
  18. #include KWSYS_HEADER(ios/sstream)
  19. #include <ctype.h> // for isspace
  20. #include <stdio.h>
  21. #ifdef _WIN32
  22. # include <windows.h>
  23. #endif
  24. namespace KWSYS_NAMESPACE
  25. {
  26. class RegistryHelper {
  27. public:
  28. RegistryHelper(Registry::RegistryType registryType);
  29. virtual ~RegistryHelper();
  30. // Read a value from the registry.
  31. virtual bool ReadValue(const char *key, const char **value);
  32. // Delete a key from the registry.
  33. virtual bool DeleteKey(const char *key);
  34. // Delete a value from a given key.
  35. virtual bool DeleteValue(const char *key);
  36. // Set value in a given key.
  37. virtual bool SetValue(const char *key,
  38. const char *value);
  39. // Open the registry at toplevel/subkey.
  40. virtual bool Open(const char *toplevel, const char *subkey,
  41. int readonly);
  42. // Close the registry.
  43. virtual bool Close();
  44. // Set the value of changed
  45. void SetChanged(bool b) { m_Changed = b; }
  46. void SetTopLevel(const char* tl);
  47. const char* GetTopLevel() { return m_TopLevel.c_str(); }
  48. //! Read from local or global scope. On Windows this mean from local machine
  49. // or local user. On unix this will read from $HOME/.Projectrc or
  50. // /etc/Project
  51. void SetGlobalScope(bool b);
  52. bool GetGlobalScope();
  53. kwsys_stl::string EncodeKey(const char* str);
  54. kwsys_stl::string EncodeValue(const char* str);
  55. kwsys_stl::string DecodeValue(const char* str);
  56. protected:
  57. bool m_Changed;
  58. kwsys_stl::string m_TopLevel;
  59. bool m_GlobalScope;
  60. #ifdef _WIN32
  61. HKEY HKey;
  62. #endif
  63. // Strip trailing and ending spaces.
  64. char *Strip(char *str);
  65. void SetSubKey(const char* sk);
  66. kwsys_stl::string CreateKey(const char *key);
  67. typedef kwsys_stl::map<kwsys_stl::string, kwsys_stl::string> StringToStringMap;
  68. StringToStringMap EntriesMap;
  69. kwsys_stl::string m_SubKey;
  70. bool m_Empty;
  71. bool m_SubKeySpecified;
  72. kwsys_stl::string m_HomeDirectory;
  73. Registry::RegistryType m_RegistryType;
  74. };
  75. //----------------------------------------------------------------------------
  76. #define Registry_BUFFER_SIZE 8192
  77. //----------------------------------------------------------------------------
  78. Registry::Registry(Registry::RegistryType registryType)
  79. {
  80. m_Opened = false;
  81. m_Locked = false;
  82. this->Helper = 0;
  83. this->Helper = new RegistryHelper(registryType);
  84. }
  85. //----------------------------------------------------------------------------
  86. Registry::~Registry()
  87. {
  88. if ( m_Opened )
  89. {
  90. kwsys_ios::cerr << "Registry::Close should be "
  91. "called here. The registry is not closed."
  92. << kwsys_ios::endl;
  93. }
  94. delete this->Helper;
  95. }
  96. //----------------------------------------------------------------------------
  97. void Registry::SetGlobalScope(bool b)
  98. {
  99. this->Helper->SetGlobalScope(b);
  100. }
  101. //----------------------------------------------------------------------------
  102. bool Registry::GetGlobalScope()
  103. {
  104. return this->Helper->GetGlobalScope();
  105. }
  106. //----------------------------------------------------------------------------
  107. bool Registry::Open(const char *toplevel,
  108. const char *subkey, int readonly)
  109. {
  110. bool res = false;
  111. if ( m_Locked )
  112. {
  113. return 0;
  114. }
  115. if ( m_Opened )
  116. {
  117. if ( !this->Close() )
  118. {
  119. return false;
  120. }
  121. }
  122. if ( !toplevel || !*toplevel )
  123. {
  124. kwsys_ios::cerr << "Registry::Opened() Toplevel not defined" << kwsys_ios::endl;
  125. return false;
  126. }
  127. if ( isspace(toplevel[0]) ||
  128. isspace(toplevel[strlen(toplevel)-1]) )
  129. {
  130. kwsys_ios::cerr << "Toplevel has to start with letter or number and end"
  131. " with one" << kwsys_ios::endl;
  132. return 0;
  133. }
  134. res = this->Helper->Open(toplevel, subkey, readonly);
  135. if ( readonly != Registry::READONLY )
  136. {
  137. m_Locked = true;
  138. }
  139. if ( res )
  140. {
  141. m_Opened = true;
  142. this->Helper->SetTopLevel(toplevel);
  143. }
  144. return res;
  145. }
  146. //----------------------------------------------------------------------------
  147. bool Registry::Close()
  148. {
  149. bool res = false;
  150. if ( m_Opened )
  151. {
  152. res = this->Helper->Close();
  153. }
  154. if ( res )
  155. {
  156. m_Opened = false;
  157. m_Locked = false;
  158. this->Helper->SetChanged(false);
  159. }
  160. return res;
  161. }
  162. //----------------------------------------------------------------------------
  163. bool Registry::ReadValue(const char *subkey,
  164. const char *key,
  165. const char **value)
  166. {
  167. *value = 0;
  168. bool res = true;
  169. bool open = false;
  170. if ( ! value )
  171. {
  172. return false;
  173. }
  174. if ( !m_Opened )
  175. {
  176. if ( !this->Open(this->GetTopLevel(), subkey,
  177. Registry::READONLY) )
  178. {
  179. return false;
  180. }
  181. open = true;
  182. }
  183. res = this->Helper->ReadValue(key, value);
  184. if ( open )
  185. {
  186. if ( !this->Close() )
  187. {
  188. res = false;
  189. }
  190. }
  191. return res;
  192. }
  193. //----------------------------------------------------------------------------
  194. bool Registry::DeleteKey(const char *subkey, const char *key)
  195. {
  196. bool res = true;
  197. bool open = false;
  198. if ( !m_Opened )
  199. {
  200. if ( !this->Open(this->GetTopLevel(), subkey,
  201. Registry::READWRITE) )
  202. {
  203. return false;
  204. }
  205. open = true;
  206. }
  207. res = this->Helper->DeleteKey(key);
  208. if ( res )
  209. {
  210. this->Helper->SetChanged(true);
  211. }
  212. if ( open )
  213. {
  214. if ( !this->Close() )
  215. {
  216. res = false;
  217. }
  218. }
  219. return res;
  220. }
  221. //----------------------------------------------------------------------------
  222. bool Registry::DeleteValue(const char *subkey, const char *key)
  223. {
  224. bool res = true;
  225. bool open = false;
  226. if ( !m_Opened )
  227. {
  228. if ( !this->Open(this->GetTopLevel(), subkey,
  229. Registry::READWRITE) )
  230. {
  231. return false;
  232. }
  233. open = true;
  234. }
  235. res = this->Helper->DeleteValue(key);
  236. if ( res )
  237. {
  238. this->Helper->SetChanged(true);
  239. }
  240. if ( open )
  241. {
  242. if ( !this->Close() )
  243. {
  244. res = false;
  245. }
  246. }
  247. return res;
  248. }
  249. //----------------------------------------------------------------------------
  250. bool Registry::SetValue(const char *subkey, const char *key,
  251. const char *value)
  252. {
  253. bool res = true;
  254. bool open = false;
  255. if ( !m_Opened )
  256. {
  257. if ( !this->Open(this->GetTopLevel(), subkey,
  258. Registry::READWRITE) )
  259. {
  260. return false;
  261. }
  262. open = true;
  263. }
  264. res = this->Helper->SetValue( key, value );
  265. if ( res )
  266. {
  267. this->Helper->SetChanged(true);
  268. }
  269. if ( open )
  270. {
  271. if ( !this->Close() )
  272. {
  273. res = false;
  274. }
  275. }
  276. return res;
  277. }
  278. //----------------------------------------------------------------------------
  279. const char* Registry::GetTopLevel()
  280. {
  281. return this->Helper->GetTopLevel();
  282. }
  283. //----------------------------------------------------------------------------
  284. void Registry::SetTopLevel(const char* tl)
  285. {
  286. this->Helper->SetTopLevel(tl);
  287. }
  288. //----------------------------------------------------------------------------
  289. void RegistryHelper::SetTopLevel(const char* tl)
  290. {
  291. if ( tl )
  292. {
  293. m_TopLevel = tl;
  294. }
  295. else
  296. {
  297. m_TopLevel = "";
  298. }
  299. }
  300. //----------------------------------------------------------------------------
  301. RegistryHelper::RegistryHelper(Registry::RegistryType registryType)
  302. {
  303. m_Changed = false;
  304. m_TopLevel = "";
  305. m_SubKey = "";
  306. m_SubKeySpecified = false;
  307. m_Empty = true;
  308. m_GlobalScope = false;
  309. m_RegistryType = registryType;
  310. }
  311. //----------------------------------------------------------------------------
  312. RegistryHelper::~RegistryHelper()
  313. {
  314. }
  315. //----------------------------------------------------------------------------
  316. bool RegistryHelper::Open(const char *toplevel, const char *subkey,
  317. int readonly)
  318. {
  319. #ifdef _WIN32
  320. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  321. {
  322. HKEY scope = HKEY_CURRENT_USER;
  323. if ( this->GetGlobalScope() )
  324. {
  325. scope = HKEY_LOCAL_MACHINE;
  326. }
  327. int res = 0;
  328. kwsys_ios::ostringstream str;
  329. DWORD dwDummy;
  330. str << "Software\\Kitware\\" << toplevel << "\\" << subkey;
  331. if ( readonly == Registry::READONLY )
  332. {
  333. res = ( RegOpenKeyEx(scope, str.str().c_str(),
  334. 0, KEY_READ, &this->HKey) == ERROR_SUCCESS );
  335. }
  336. else
  337. {
  338. res = ( RegCreateKeyEx(scope, str.str().c_str(),
  339. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  340. NULL, &this->HKey, &dwDummy) == ERROR_SUCCESS );
  341. }
  342. if ( res != 0 )
  343. {
  344. this->SetSubKey( subkey );
  345. }
  346. return (res != 0);
  347. }
  348. #endif
  349. if ( m_RegistryType == Registry::FILE_REGISTRY )
  350. {
  351. bool res = false;
  352. int cc;
  353. kwsys_ios::ostringstream str;
  354. const char* homeDirectory;
  355. if ( (homeDirectory = getenv("HOME")) == 0 )
  356. {
  357. if ( (homeDirectory = getenv("USERPROFILE")) == 0 )
  358. {
  359. return false;
  360. }
  361. }
  362. m_HomeDirectory = homeDirectory;
  363. str << m_HomeDirectory << "/." << toplevel << "rc";
  364. if ( readonly == Registry::READWRITE )
  365. {
  366. kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app );
  367. if ( ofs.fail() )
  368. {
  369. return false;
  370. }
  371. ofs.close();
  372. }
  373. kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in
  374. #ifndef KWSYS_IOS_USE_ANSI
  375. | kwsys_ios::ios::nocreate
  376. #endif
  377. );
  378. if ( !ifs )
  379. {
  380. return false;
  381. }
  382. if ( ifs->fail())
  383. {
  384. delete ifs;
  385. return false;
  386. }
  387. res = true;
  388. char buffer[Registry_BUFFER_SIZE];
  389. while( !ifs->fail() )
  390. {
  391. ifs->getline(buffer, Registry_BUFFER_SIZE);
  392. if ( ifs->fail() || ifs->eof() )
  393. {
  394. break;
  395. }
  396. char *line = this->Strip(buffer);
  397. if ( *line == '#' || *line == 0 )
  398. {
  399. // Comment
  400. continue;
  401. }
  402. int linelen = static_cast<int>(strlen(line));
  403. for ( cc = 0; cc < linelen; cc++ )
  404. {
  405. if ( line[cc] == '=' )
  406. {
  407. char *key = new char[ cc+1 ];
  408. strncpy( key, line, cc );
  409. key[cc] = 0;
  410. char *value = line + cc + 1;
  411. char *nkey = this->Strip(key);
  412. char *nvalue = this->Strip(value);
  413. this->EntriesMap[nkey] = this->DecodeValue(nvalue);
  414. m_Empty = 0;
  415. delete [] key;
  416. break;
  417. }
  418. }
  419. }
  420. ifs->close();
  421. this->SetSubKey( subkey );
  422. delete ifs;
  423. return res;
  424. }
  425. return false;
  426. }
  427. //----------------------------------------------------------------------------
  428. bool RegistryHelper::Close()
  429. {
  430. #ifdef _WIN32
  431. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  432. {
  433. int res;
  434. res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
  435. return (res != 0);
  436. }
  437. #endif
  438. if ( m_RegistryType == Registry::FILE_REGISTRY )
  439. {
  440. if ( !m_Changed )
  441. {
  442. this->EntriesMap.erase(
  443. this->EntriesMap.begin(),
  444. this->EntriesMap.end());
  445. m_Empty = 1;
  446. this->SetSubKey(0);
  447. return true;
  448. }
  449. kwsys_ios::ostringstream str;
  450. str << m_HomeDirectory << "/." << this->GetTopLevel() << "rc";
  451. kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
  452. if ( !ofs )
  453. {
  454. return false;
  455. }
  456. if ( ofs->fail())
  457. {
  458. delete ofs;
  459. return false;
  460. }
  461. *ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
  462. << "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
  463. << "# comments and empty lines will be deleted. Every line has" << kwsys_ios::endl
  464. << "# to be in format: " << kwsys_ios::endl
  465. << "# key = value" << kwsys_ios::endl
  466. << "#" << kwsys_ios::endl;
  467. if ( !this->EntriesMap.empty() )
  468. {
  469. RegistryHelper::StringToStringMap::iterator it;
  470. for ( it = this->EntriesMap.begin();
  471. it != this->EntriesMap.end();
  472. ++ it )
  473. {
  474. *ofs << it->first.c_str() << " = " << this->EncodeValue(it->second.c_str()).c_str() << kwsys_ios::endl;
  475. }
  476. }
  477. this->EntriesMap.erase(
  478. this->EntriesMap.begin(),
  479. this->EntriesMap.end());
  480. ofs->close();
  481. delete ofs;
  482. this->SetSubKey(0);
  483. m_Empty = 1;
  484. return true;
  485. }
  486. return false;
  487. }
  488. //----------------------------------------------------------------------------
  489. bool RegistryHelper::ReadValue(const char *skey, const char **value)
  490. {
  491. #ifdef _WIN32
  492. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  493. {
  494. kwsys_stl::string key = this->CreateKey( skey );
  495. if ( key.empty() )
  496. {
  497. return false;
  498. }
  499. int res = 1;
  500. DWORD dwType, dwSize;
  501. dwType = REG_SZ;
  502. char buffer[1024]; // Replace with RegQueryInfoKey
  503. dwSize = sizeof(buffer);
  504. res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
  505. (BYTE *)buffer, &dwSize) == ERROR_SUCCESS );
  506. if ( !res )
  507. {
  508. return false;
  509. }
  510. this->EntriesMap[key] = buffer;
  511. RegistryHelper::StringToStringMap::iterator it
  512. = this->EntriesMap.find(key);
  513. *value = it->second.c_str();
  514. return true;
  515. }
  516. #endif
  517. if ( m_RegistryType == Registry::FILE_REGISTRY )
  518. {
  519. bool res = false;
  520. kwsys_stl::string key = this->CreateKey( skey );
  521. if ( key.empty() )
  522. {
  523. return false;
  524. }
  525. RegistryHelper::StringToStringMap::iterator it
  526. = this->EntriesMap.find(key);
  527. if ( it != this->EntriesMap.end() )
  528. {
  529. *value = it->second.c_str();
  530. res = true;
  531. }
  532. return res;
  533. }
  534. return false;
  535. }
  536. //----------------------------------------------------------------------------
  537. bool RegistryHelper::DeleteKey(const char* skey)
  538. {
  539. #ifdef _WIN32
  540. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  541. {
  542. int res = 1;
  543. res = ( RegDeleteKey( this->HKey, skey ) == ERROR_SUCCESS );
  544. return (res != 0);
  545. }
  546. #endif
  547. if ( m_RegistryType == Registry::FILE_REGISTRY )
  548. {
  549. kwsys_stl::string key = this->CreateKey( skey );
  550. if ( key.empty() )
  551. {
  552. return false;
  553. }
  554. this->EntriesMap.erase(key);
  555. return true;
  556. }
  557. return false;
  558. }
  559. //----------------------------------------------------------------------------
  560. bool RegistryHelper::DeleteValue(const char *skey)
  561. {
  562. #ifdef _WIN32
  563. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  564. {
  565. int res = 1;
  566. res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
  567. return (res != 0);
  568. }
  569. #endif
  570. if ( m_RegistryType == Registry::FILE_REGISTRY )
  571. {
  572. kwsys_stl::string key = this->CreateKey( skey );
  573. if ( key.empty() )
  574. {
  575. return false;
  576. }
  577. this->EntriesMap.erase(key);
  578. return true;
  579. }
  580. return false;
  581. }
  582. //----------------------------------------------------------------------------
  583. bool RegistryHelper::SetValue(const char *skey, const char *value)
  584. {
  585. #ifdef _WIN32
  586. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  587. {
  588. int res = 1;
  589. DWORD len = (DWORD)(value ? strlen(value) : 0);
  590. res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
  591. (CONST BYTE *)(const char *)value,
  592. len+1) == ERROR_SUCCESS );
  593. return (res != 0);
  594. }
  595. #endif
  596. if ( m_RegistryType == Registry::FILE_REGISTRY )
  597. {
  598. kwsys_stl::string key = this->CreateKey( skey );
  599. if ( key.empty() )
  600. {
  601. return 0;
  602. }
  603. this->EntriesMap[key] = value;
  604. return 1;
  605. }
  606. return false;
  607. }
  608. //----------------------------------------------------------------------------
  609. kwsys_stl::string RegistryHelper::CreateKey( const char *key )
  610. {
  611. if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
  612. {
  613. return "";
  614. }
  615. kwsys_ios::ostringstream ostr;
  616. ostr << this->EncodeKey(this->m_SubKey.c_str()) << "\\" << this->EncodeKey(key);
  617. return ostr.str();
  618. }
  619. //----------------------------------------------------------------------------
  620. void RegistryHelper::SetSubKey(const char* sk)
  621. {
  622. if ( !sk )
  623. {
  624. m_SubKey = "";
  625. m_SubKeySpecified = false;
  626. }
  627. else
  628. {
  629. m_SubKey = sk;
  630. m_SubKeySpecified = true;
  631. }
  632. }
  633. //----------------------------------------------------------------------------
  634. char *RegistryHelper::Strip(char *str)
  635. {
  636. int cc;
  637. int len;
  638. char *nstr;
  639. if ( !str )
  640. {
  641. return NULL;
  642. }
  643. len = strlen(str);
  644. nstr = str;
  645. for( cc=0; cc<len; cc++ )
  646. {
  647. if ( !isspace( *nstr ) )
  648. {
  649. break;
  650. }
  651. nstr ++;
  652. }
  653. for( cc=(strlen(nstr)-1); cc>=0; cc-- )
  654. {
  655. if ( !isspace( nstr[cc] ) )
  656. {
  657. nstr[cc+1] = 0;
  658. break;
  659. }
  660. }
  661. return nstr;
  662. }
  663. //----------------------------------------------------------------------------
  664. void RegistryHelper::SetGlobalScope(bool b)
  665. {
  666. m_GlobalScope = b;
  667. }
  668. //----------------------------------------------------------------------------
  669. bool RegistryHelper::GetGlobalScope()
  670. {
  671. return m_GlobalScope;
  672. }
  673. //----------------------------------------------------------------------------
  674. kwsys_stl::string RegistryHelper::EncodeKey(const char* str)
  675. {
  676. kwsys_ios::ostringstream ostr;
  677. while ( *str )
  678. {
  679. switch ( *str )
  680. {
  681. case '%': case '=': case '\n': case '\r': case '\t':
  682. char buffer[4];
  683. sprintf(buffer, "%%%02X", *str);
  684. ostr << buffer;
  685. break;
  686. default:
  687. ostr << *str;
  688. }
  689. str ++;
  690. }
  691. return ostr.str();
  692. }
  693. //----------------------------------------------------------------------------
  694. kwsys_stl::string RegistryHelper::EncodeValue(const char* str)
  695. {
  696. kwsys_ios::ostringstream ostr;
  697. while ( *str )
  698. {
  699. switch ( *str )
  700. {
  701. case '%': case '=': case '\n': case '\r': case '\t':
  702. char buffer[4];
  703. sprintf(buffer, "%%%02X", *str);
  704. ostr << buffer;
  705. break;
  706. default:
  707. ostr << *str;
  708. }
  709. str ++;
  710. }
  711. return ostr.str();
  712. }
  713. //----------------------------------------------------------------------------
  714. kwsys_stl::string RegistryHelper::DecodeValue(const char* str)
  715. {
  716. kwsys_ios::ostringstream ostr;
  717. while ( *str )
  718. {
  719. int val;
  720. switch ( *str )
  721. {
  722. case '%':
  723. if ( *(str+1) && *(str+2) && sscanf(str+1, "%x", &val) == 1 )
  724. {
  725. ostr << (char)val;
  726. str += 2;
  727. }
  728. else
  729. {
  730. ostr << *str;
  731. }
  732. break;
  733. default:
  734. ostr << *str;
  735. }
  736. str ++;
  737. }
  738. return ostr.str();
  739. }
  740. } // namespace KWSYS_NAMESPACE