Registry.cxx 19 KB

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