Registry.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. return (res != 0);
  343. }
  344. #endif
  345. if ( m_RegistryType == Registry::FILE_REGISTRY )
  346. {
  347. bool res = false;
  348. int cc;
  349. kwsys_ios::ostringstream str;
  350. const char* homeDirectory;
  351. if ( (homeDirectory = getenv("HOME")) == 0 )
  352. {
  353. if ( (homeDirectory = getenv("USERPROFILE")) == 0 )
  354. {
  355. return false;
  356. }
  357. }
  358. m_HomeDirectory = homeDirectory;
  359. str << m_HomeDirectory << "/." << toplevel << "rc";
  360. if ( readonly == Registry::READWRITE )
  361. {
  362. kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app );
  363. if ( ofs.fail() )
  364. {
  365. return false;
  366. }
  367. ofs.close();
  368. }
  369. kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in
  370. #ifndef KWSYS_IOS_USE_ANSI
  371. | kwsys_ios::ios::nocreate
  372. #endif
  373. );
  374. if ( !ifs )
  375. {
  376. return false;
  377. }
  378. if ( ifs->fail())
  379. {
  380. delete ifs;
  381. return false;
  382. }
  383. res = true;
  384. char buffer[Registry_BUFFER_SIZE];
  385. while( !ifs->fail() )
  386. {
  387. ifs->getline(buffer, Registry_BUFFER_SIZE);
  388. if ( ifs->fail() || ifs->eof() )
  389. {
  390. break;
  391. }
  392. char *line = this->Strip(buffer);
  393. if ( *line == '#' || *line == 0 )
  394. {
  395. // Comment
  396. continue;
  397. }
  398. int linelen = static_cast<int>(strlen(line));
  399. for ( cc = 0; cc < linelen; cc++ )
  400. {
  401. if ( line[cc] == '=' )
  402. {
  403. char *key = new char[ cc+1 ];
  404. strncpy( key, line, cc );
  405. key[cc] = 0;
  406. char *value = line + cc + 1;
  407. char *nkey = this->Strip(key);
  408. char *nvalue = this->Strip(value);
  409. this->EntriesMap[nkey] = this->DecodeValue(nvalue);
  410. m_Empty = 0;
  411. delete [] key;
  412. break;
  413. }
  414. }
  415. }
  416. ifs->close();
  417. this->SetSubKey( subkey );
  418. delete ifs;
  419. return res;
  420. }
  421. return false;
  422. }
  423. //----------------------------------------------------------------------------
  424. bool RegistryHelper::Close()
  425. {
  426. #ifdef _WIN32
  427. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  428. {
  429. int res;
  430. res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
  431. return (res != 0);
  432. }
  433. #endif
  434. if ( m_RegistryType == Registry::FILE_REGISTRY )
  435. {
  436. if ( !m_Changed )
  437. {
  438. this->EntriesMap.erase(
  439. this->EntriesMap.begin(),
  440. this->EntriesMap.end());
  441. m_Empty = 1;
  442. this->SetSubKey(0);
  443. return true;
  444. }
  445. kwsys_ios::ostringstream str;
  446. str << m_HomeDirectory << "/." << this->GetTopLevel() << "rc";
  447. kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
  448. if ( !ofs )
  449. {
  450. return false;
  451. }
  452. if ( ofs->fail())
  453. {
  454. delete ofs;
  455. return false;
  456. }
  457. *ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
  458. << "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
  459. << "# comments and empty lines will be deleted. Every line has" << kwsys_ios::endl
  460. << "# to be in format: " << kwsys_ios::endl
  461. << "# key = value" << kwsys_ios::endl
  462. << "#" << kwsys_ios::endl;
  463. if ( !this->EntriesMap.empty() )
  464. {
  465. RegistryHelper::StringToStringMap::iterator it;
  466. for ( it = this->EntriesMap.begin();
  467. it != this->EntriesMap.end();
  468. ++ it )
  469. {
  470. *ofs << it->first.c_str() << " = " << this->EncodeValue(it->second.c_str()).c_str() << kwsys_ios::endl;
  471. }
  472. }
  473. this->EntriesMap.erase(
  474. this->EntriesMap.begin(),
  475. this->EntriesMap.end());
  476. ofs->close();
  477. delete ofs;
  478. this->SetSubKey(0);
  479. m_Empty = 1;
  480. return true;
  481. }
  482. return false;
  483. }
  484. //----------------------------------------------------------------------------
  485. bool RegistryHelper::ReadValue(const char *skey, const char **value)
  486. {
  487. #ifdef _WIN32
  488. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  489. {
  490. kwsys_stl::string key = this->CreateKey( skey );
  491. if ( key.empty() )
  492. {
  493. return false;
  494. }
  495. int res = 1;
  496. DWORD dwType, dwSize;
  497. dwType = REG_SZ;
  498. dwSize = Registry_BUFFER_SIZE;
  499. char buffer[1024]; // Replace with RegQueryInfoKey
  500. res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
  501. (BYTE *)buffer, &dwSize) == ERROR_SUCCESS );
  502. if ( !res )
  503. {
  504. return false;
  505. }
  506. this->EntriesMap[key] = buffer;
  507. RegistryHelper::StringToStringMap::iterator it
  508. = this->EntriesMap.find(key);
  509. *value = it->second.c_str();
  510. return true;
  511. }
  512. #endif
  513. if ( m_RegistryType == Registry::FILE_REGISTRY )
  514. {
  515. bool res = false;
  516. kwsys_stl::string key = this->CreateKey( skey );
  517. if ( key.empty() )
  518. {
  519. return false;
  520. }
  521. RegistryHelper::StringToStringMap::iterator it
  522. = this->EntriesMap.find(key);
  523. if ( it != this->EntriesMap.end() )
  524. {
  525. *value = it->second.c_str();
  526. res = true;
  527. }
  528. return res;
  529. }
  530. return false;
  531. }
  532. //----------------------------------------------------------------------------
  533. bool RegistryHelper::DeleteKey(const char* skey)
  534. {
  535. #ifdef _WIN32
  536. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  537. {
  538. int res = 1;
  539. res = ( RegDeleteKey( this->HKey, skey ) == ERROR_SUCCESS );
  540. return (res != 0);
  541. }
  542. #endif
  543. if ( m_RegistryType == Registry::FILE_REGISTRY )
  544. {
  545. kwsys_stl::string key = this->CreateKey( skey );
  546. if ( key.empty() )
  547. {
  548. return false;
  549. }
  550. this->EntriesMap.erase(key);
  551. return true;
  552. }
  553. return false;
  554. }
  555. //----------------------------------------------------------------------------
  556. bool RegistryHelper::DeleteValue(const char *skey)
  557. {
  558. #ifdef _WIN32
  559. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  560. {
  561. int res = 1;
  562. res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
  563. return (res != 0);
  564. }
  565. #endif
  566. if ( m_RegistryType == Registry::FILE_REGISTRY )
  567. {
  568. kwsys_stl::string key = this->CreateKey( skey );
  569. if ( key.empty() )
  570. {
  571. return false;
  572. }
  573. this->EntriesMap.erase(key);
  574. return true;
  575. }
  576. return false;
  577. }
  578. //----------------------------------------------------------------------------
  579. bool RegistryHelper::SetValue(const char *skey, const char *value)
  580. {
  581. #ifdef _WIN32
  582. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  583. {
  584. int res = 1;
  585. DWORD len = (DWORD)(value ? strlen(value) : 0);
  586. res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
  587. (CONST BYTE *)(const char *)value,
  588. len+1) == ERROR_SUCCESS );
  589. return (res != 0);
  590. }
  591. #endif
  592. if ( m_RegistryType == Registry::FILE_REGISTRY )
  593. {
  594. kwsys_stl::string key = this->CreateKey( skey );
  595. if ( key.empty() )
  596. {
  597. return 0;
  598. }
  599. this->EntriesMap[key] = value;
  600. return 1;
  601. }
  602. return false;
  603. }
  604. //----------------------------------------------------------------------------
  605. kwsys_stl::string RegistryHelper::CreateKey( const char *key )
  606. {
  607. if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
  608. {
  609. return "";
  610. }
  611. kwsys_ios::ostringstream ostr;
  612. ostr << this->EncodeKey(this->m_SubKey.c_str()) << "\\" << this->EncodeKey(key);
  613. return ostr.str();
  614. }
  615. //----------------------------------------------------------------------------
  616. void RegistryHelper::SetSubKey(const char* sk)
  617. {
  618. if ( !sk )
  619. {
  620. m_SubKey = "";
  621. m_SubKeySpecified = false;
  622. }
  623. else
  624. {
  625. m_SubKey = sk;
  626. m_SubKeySpecified = true;
  627. }
  628. }
  629. //----------------------------------------------------------------------------
  630. char *RegistryHelper::Strip(char *str)
  631. {
  632. int cc;
  633. int len;
  634. char *nstr;
  635. if ( !str )
  636. {
  637. return NULL;
  638. }
  639. len = strlen(str);
  640. nstr = str;
  641. for( cc=0; cc<len; cc++ )
  642. {
  643. if ( !isspace( *nstr ) )
  644. {
  645. break;
  646. }
  647. nstr ++;
  648. }
  649. for( cc=(strlen(nstr)-1); cc>=0; cc-- )
  650. {
  651. if ( !isspace( nstr[cc] ) )
  652. {
  653. nstr[cc+1] = 0;
  654. break;
  655. }
  656. }
  657. return nstr;
  658. }
  659. //----------------------------------------------------------------------------
  660. void RegistryHelper::SetGlobalScope(bool b)
  661. {
  662. m_GlobalScope = b;
  663. }
  664. //----------------------------------------------------------------------------
  665. bool RegistryHelper::GetGlobalScope()
  666. {
  667. return m_GlobalScope;
  668. }
  669. //----------------------------------------------------------------------------
  670. kwsys_stl::string RegistryHelper::EncodeKey(const char* str)
  671. {
  672. kwsys_ios::ostringstream ostr;
  673. while ( *str )
  674. {
  675. switch ( *str )
  676. {
  677. case '%': case '=': case '\n': case '\r': case '\t':
  678. char buffer[4];
  679. sprintf(buffer, "%%%02X", *str);
  680. ostr << buffer;
  681. break;
  682. default:
  683. ostr << *str;
  684. }
  685. str ++;
  686. }
  687. return ostr.str();
  688. }
  689. //----------------------------------------------------------------------------
  690. kwsys_stl::string RegistryHelper::EncodeValue(const char* str)
  691. {
  692. kwsys_ios::ostringstream ostr;
  693. while ( *str )
  694. {
  695. switch ( *str )
  696. {
  697. case '%': case '=': case '\n': case '\r': case '\t':
  698. char buffer[4];
  699. sprintf(buffer, "%%%02X", *str);
  700. ostr << buffer;
  701. break;
  702. default:
  703. ostr << *str;
  704. }
  705. str ++;
  706. }
  707. return ostr.str();
  708. }
  709. //----------------------------------------------------------------------------
  710. kwsys_stl::string RegistryHelper::DecodeValue(const char* str)
  711. {
  712. kwsys_ios::ostringstream ostr;
  713. while ( *str )
  714. {
  715. int val;
  716. switch ( *str )
  717. {
  718. case '%':
  719. if ( *(str+1) && *(str+2) && sscanf(str+1, "%x", &val) == 1 )
  720. {
  721. ostr << (char)val;
  722. str += 2;
  723. }
  724. else
  725. {
  726. ostr << *str;
  727. }
  728. break;
  729. default:
  730. ostr << *str;
  731. }
  732. str ++;
  733. }
  734. return ostr.str();
  735. }
  736. } // namespace KWSYS_NAMESPACE