Registry.cxx 19 KB

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