Registry.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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, 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. protected:
  54. bool m_Changed;
  55. kwsys_stl::string m_TopLevel;
  56. bool m_GlobalScope;
  57. #ifdef _WIN32
  58. HKEY HKey;
  59. #endif
  60. // Strip trailing and ending spaces.
  61. char *Strip(char *str);
  62. void SetSubKey(const char* sk);
  63. char *CreateKey(const char *key);
  64. typedef kwsys_stl::map<kwsys_stl::string, kwsys_stl::string> StringToStringMap;
  65. StringToStringMap EntriesMap;
  66. kwsys_stl::string m_SubKey;
  67. bool m_Empty;
  68. bool m_SubKeySpecified;
  69. Registry::RegistryType m_RegistryType;
  70. static const int BUFFER_SIZE;
  71. };
  72. //----------------------------------------------------------------------------
  73. const int RegistryHelper::BUFFER_SIZE = 8192;
  74. //----------------------------------------------------------------------------
  75. Registry::Registry(Registry::RegistryType registryType)
  76. {
  77. m_Opened = false;
  78. m_Locked = false;
  79. this->Helper = 0;
  80. this->Helper = new RegistryHelper(registryType);
  81. }
  82. //----------------------------------------------------------------------------
  83. Registry::~Registry()
  84. {
  85. if ( m_Opened )
  86. {
  87. kwsys_ios::cerr << "Registry::Close should be "
  88. "called here. The registry is not closed."
  89. << kwsys_ios::endl;
  90. }
  91. delete this->Helper;
  92. }
  93. //----------------------------------------------------------------------------
  94. void Registry::SetGlobalScope(bool b)
  95. {
  96. this->Helper->SetGlobalScope(b);
  97. }
  98. //----------------------------------------------------------------------------
  99. bool Registry::GetGlobalScope()
  100. {
  101. return this->Helper->GetGlobalScope();
  102. }
  103. //----------------------------------------------------------------------------
  104. bool Registry::Open(const char *toplevel,
  105. const char *subkey, int readonly)
  106. {
  107. bool res = false;
  108. if ( m_Locked )
  109. {
  110. return 0;
  111. }
  112. if ( m_Opened )
  113. {
  114. if ( !this->Close() )
  115. {
  116. return false;
  117. }
  118. }
  119. if ( !toplevel || !*toplevel )
  120. {
  121. kwsys_ios::cerr << "Registry::Opened() Toplevel not defined" << kwsys_ios::endl;
  122. return false;
  123. }
  124. if ( isspace(toplevel[0]) ||
  125. isspace(toplevel[strlen(toplevel)-1]) )
  126. {
  127. kwsys_ios::cerr << "Toplevel has to start with letter or number and end"
  128. " with one" << kwsys_ios::endl;
  129. return 0;
  130. }
  131. res = this->Helper->Open(toplevel, subkey, readonly);
  132. if ( readonly != Registry::READONLY )
  133. {
  134. m_Locked = true;
  135. }
  136. if ( res )
  137. {
  138. m_Opened = true;
  139. this->Helper->SetTopLevel(toplevel);
  140. }
  141. return res;
  142. }
  143. //----------------------------------------------------------------------------
  144. bool Registry::Close()
  145. {
  146. bool res = false;
  147. if ( m_Opened )
  148. {
  149. res = this->Helper->Close();
  150. }
  151. if ( res )
  152. {
  153. m_Opened = false;
  154. m_Locked = false;
  155. this->Helper->SetChanged(false);
  156. }
  157. return res;
  158. }
  159. //----------------------------------------------------------------------------
  160. bool Registry::ReadValue(const char *subkey,
  161. const char *key,
  162. char *value)
  163. {
  164. *value = 0;
  165. bool res = true;
  166. bool open = false;
  167. if ( ! value )
  168. {
  169. return false;
  170. }
  171. if ( !m_Opened )
  172. {
  173. if ( !this->Open(this->GetTopLevel(), subkey,
  174. Registry::READONLY) )
  175. {
  176. return false;
  177. }
  178. open = true;
  179. }
  180. res = this->Helper->ReadValue(key, value);
  181. if ( open )
  182. {
  183. if ( !this->Close() )
  184. {
  185. res = false;
  186. }
  187. }
  188. return res;
  189. }
  190. //----------------------------------------------------------------------------
  191. bool Registry::DeleteKey(const char *subkey, const char *key)
  192. {
  193. bool res = true;
  194. bool open = false;
  195. if ( !m_Opened )
  196. {
  197. if ( !this->Open(this->GetTopLevel(), subkey,
  198. Registry::READWRITE) )
  199. {
  200. return false;
  201. }
  202. open = true;
  203. }
  204. res = this->Helper->DeleteKey(key);
  205. if ( res )
  206. {
  207. this->Helper->SetChanged(true);
  208. }
  209. if ( open )
  210. {
  211. if ( !this->Close() )
  212. {
  213. res = false;
  214. }
  215. }
  216. return res;
  217. }
  218. //----------------------------------------------------------------------------
  219. bool Registry::DeleteValue(const char *subkey, const char *key)
  220. {
  221. bool res = true;
  222. bool open = false;
  223. if ( !m_Opened )
  224. {
  225. if ( !this->Open(this->GetTopLevel(), subkey,
  226. Registry::READWRITE) )
  227. {
  228. return false;
  229. }
  230. open = true;
  231. }
  232. res = this->Helper->DeleteValue(key);
  233. if ( res )
  234. {
  235. this->Helper->SetChanged(true);
  236. }
  237. if ( open )
  238. {
  239. if ( !this->Close() )
  240. {
  241. res = false;
  242. }
  243. }
  244. return res;
  245. }
  246. //----------------------------------------------------------------------------
  247. bool Registry::SetValue(const char *subkey, const char *key,
  248. const char *value)
  249. {
  250. bool res = true;
  251. bool open = false;
  252. if ( !m_Opened )
  253. {
  254. if ( !this->Open(this->GetTopLevel(), subkey,
  255. Registry::READWRITE) )
  256. {
  257. return false;
  258. }
  259. open = true;
  260. }
  261. res = this->Helper->SetValue( key, value );
  262. if ( res )
  263. {
  264. this->Helper->SetChanged(true);
  265. }
  266. if ( open )
  267. {
  268. if ( !this->Close() )
  269. {
  270. res = false;
  271. }
  272. }
  273. return res;
  274. }
  275. //----------------------------------------------------------------------------
  276. const char* Registry::GetTopLevel()
  277. {
  278. return this->Helper->GetTopLevel();
  279. }
  280. //----------------------------------------------------------------------------
  281. void Registry::SetTopLevel(const char* tl)
  282. {
  283. this->Helper->SetTopLevel(tl);
  284. }
  285. //----------------------------------------------------------------------------
  286. void RegistryHelper::SetTopLevel(const char* tl)
  287. {
  288. if ( tl )
  289. {
  290. m_TopLevel = tl;
  291. }
  292. else
  293. {
  294. m_TopLevel = "";
  295. }
  296. }
  297. //----------------------------------------------------------------------------
  298. RegistryHelper::RegistryHelper(Registry::RegistryType registryType)
  299. {
  300. m_Changed = false;
  301. m_TopLevel = "";
  302. m_SubKey = "";
  303. m_SubKeySpecified = false;
  304. m_Empty = true;
  305. m_GlobalScope = false;
  306. m_RegistryType = registryType;
  307. }
  308. //----------------------------------------------------------------------------
  309. RegistryHelper::~RegistryHelper()
  310. {
  311. }
  312. //----------------------------------------------------------------------------
  313. bool RegistryHelper::Open(const char *toplevel, const char *subkey,
  314. int readonly)
  315. {
  316. #ifdef _WIN32
  317. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  318. {
  319. HKEY scope = HKEY_CURRENT_USER;
  320. if ( this->GetGlobalScope() )
  321. {
  322. scope = HKEY_LOCAL_MACHINE;
  323. }
  324. int res = 0;
  325. kwsys_ios::ostringstream str;
  326. DWORD dwDummy;
  327. str << "Software\\Kitware\\" << toplevel << "\\" << subkey;
  328. if ( readonly == Registry::READONLY )
  329. {
  330. res = ( RegOpenKeyEx(scope, str.str().c_str(),
  331. 0, KEY_READ, &this->HKey) == ERROR_SUCCESS );
  332. }
  333. else
  334. {
  335. res = ( RegCreateKeyEx(scope, str.str().c_str(),
  336. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  337. NULL, &this->HKey, &dwDummy) == ERROR_SUCCESS );
  338. }
  339. return (res != 0);
  340. }
  341. #endif
  342. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  343. {
  344. bool res = false;
  345. int cc;
  346. kwsys_ios::ostringstream str;
  347. if ( !getenv("HOME") )
  348. {
  349. return false;
  350. }
  351. str << getenv("HOME") << "/." << toplevel << "rc";
  352. if ( readonly == Registry::READWRITE )
  353. {
  354. kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app );
  355. if ( ofs.fail() )
  356. {
  357. return false;
  358. }
  359. ofs.close();
  360. }
  361. kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in
  362. #ifndef KWSYS_IOS_USE_ANSI
  363. | kwsys_ios::ios::nocreate
  364. #endif
  365. );
  366. if ( !ifs )
  367. {
  368. return false;
  369. }
  370. if ( ifs->fail())
  371. {
  372. delete ifs;
  373. return false;
  374. }
  375. res = true;
  376. char buffer[BUFFER_SIZE];
  377. while( !ifs->fail() )
  378. {
  379. int found = 0;
  380. ifs->getline(buffer, BUFFER_SIZE);
  381. if ( ifs->fail() || ifs->eof() )
  382. {
  383. break;
  384. }
  385. char *line = this->Strip(buffer);
  386. if ( *line == '#' || *line == 0 )
  387. {
  388. // Comment
  389. continue;
  390. }
  391. int linelen = static_cast<int>(strlen(line));
  392. for ( cc = 0; cc < linelen; cc++ )
  393. {
  394. if ( line[cc] == '=' )
  395. {
  396. char *key = new char[ cc+1 ];
  397. strncpy( key, line, cc );
  398. key[cc] = 0;
  399. char *value = line + cc + 1;
  400. char *nkey = this->Strip(key);
  401. char *nvalue = this->Strip(value);
  402. this->EntriesMap[nkey] = nvalue;
  403. m_Empty = 0;
  404. delete [] key;
  405. found = 1;
  406. break;
  407. }
  408. }
  409. }
  410. ifs->close();
  411. this->SetSubKey( subkey );
  412. delete ifs;
  413. return res;
  414. }
  415. return false;
  416. }
  417. //----------------------------------------------------------------------------
  418. bool RegistryHelper::Close()
  419. {
  420. #ifdef _WIN32
  421. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  422. {
  423. int res;
  424. res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
  425. return (res != 0);
  426. }
  427. #endif
  428. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  429. {
  430. if ( !m_Changed )
  431. {
  432. this->EntriesMap.erase(
  433. this->EntriesMap.begin(),
  434. this->EntriesMap.end());
  435. m_Empty = 1;
  436. this->SetSubKey(0);
  437. return true;
  438. }
  439. kwsys_ios::ostringstream str;
  440. if ( !getenv("HOME") )
  441. {
  442. return false;
  443. }
  444. str << getenv("HOME") << "/." << this->GetTopLevel() << "rc";
  445. kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
  446. if ( !ofs )
  447. {
  448. return false;
  449. }
  450. if ( ofs->fail())
  451. {
  452. delete ofs;
  453. return false;
  454. }
  455. *ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
  456. << "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
  457. << "# coments and empty lines will be deleted. Every line has" << kwsys_ios::endl
  458. << "# to be in format: " << kwsys_ios::endl
  459. << "# key = value" << kwsys_ios::endl
  460. << "#" << kwsys_ios::endl;
  461. if ( !this->EntriesMap.empty() )
  462. {
  463. RegistryHelper::StringToStringMap::iterator it;
  464. for ( it = this->EntriesMap.begin();
  465. it != this->EntriesMap.end();
  466. ++ it )
  467. {
  468. *ofs << it->first.c_str() << " = " << it->second.c_str()<< kwsys_ios::endl;
  469. }
  470. }
  471. this->EntriesMap.erase(
  472. this->EntriesMap.begin(),
  473. this->EntriesMap.end());
  474. ofs->close();
  475. delete ofs;
  476. this->SetSubKey(0);
  477. m_Empty = 1;
  478. return true;
  479. }
  480. return false;
  481. }
  482. //----------------------------------------------------------------------------
  483. bool RegistryHelper::ReadValue(const char *skey, char *value)
  484. {
  485. #ifdef _WIN32
  486. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  487. {
  488. int res = 1;
  489. DWORD dwType, dwSize;
  490. dwType = REG_SZ;
  491. dwSize = BUFFER_SIZE;
  492. res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
  493. (BYTE *)value, &dwSize) == ERROR_SUCCESS );
  494. return (res != 0);
  495. }
  496. #endif
  497. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  498. {
  499. bool res = false;
  500. char *key = this->CreateKey( skey );
  501. if ( !key )
  502. {
  503. return false;
  504. }
  505. RegistryHelper::StringToStringMap::iterator it
  506. = this->EntriesMap.find(key);
  507. if ( it != this->EntriesMap.end() )
  508. {
  509. strcpy(value, it->second.c_str());
  510. res = true;
  511. }
  512. delete [] key;
  513. return res;
  514. }
  515. return false;
  516. }
  517. //----------------------------------------------------------------------------
  518. bool RegistryHelper::DeleteKey(const char* key)
  519. {
  520. #ifdef _WIN32
  521. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  522. {
  523. int res = 1;
  524. res = ( RegDeleteKey( this->HKey, key ) == ERROR_SUCCESS );
  525. return (res != 0);
  526. }
  527. #endif
  528. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  529. {
  530. (void)key;
  531. bool res = false;
  532. return res;
  533. }
  534. return false;
  535. }
  536. //----------------------------------------------------------------------------
  537. bool RegistryHelper::DeleteValue(const char *skey)
  538. {
  539. #ifdef _WIN32
  540. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  541. {
  542. int res = 1;
  543. res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
  544. return (res != 0);
  545. }
  546. #endif
  547. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  548. {
  549. char *key = this->CreateKey( skey );
  550. if ( !key )
  551. {
  552. return false;
  553. }
  554. this->EntriesMap.erase(key);
  555. delete [] key;
  556. return true;
  557. }
  558. return false;
  559. }
  560. //----------------------------------------------------------------------------
  561. bool RegistryHelper::SetValue(const char *skey, const char *value)
  562. {
  563. #ifdef _WIN32
  564. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  565. {
  566. int res = 1;
  567. DWORD len = (DWORD)(value ? strlen(value) : 0);
  568. res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
  569. (CONST BYTE *)(const char *)value,
  570. len+1) == ERROR_SUCCESS );
  571. return (res != 0);
  572. }
  573. #endif
  574. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  575. {
  576. char *key = this->CreateKey( skey );
  577. if ( !key )
  578. {
  579. return 0;
  580. }
  581. this->EntriesMap[key] = value;
  582. delete [] key;
  583. return 1;
  584. }
  585. return false;
  586. }
  587. //----------------------------------------------------------------------------
  588. char *RegistryHelper::CreateKey( const char *key )
  589. {
  590. char *newkey;
  591. if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
  592. {
  593. return 0;
  594. }
  595. int len = strlen(this->m_SubKey.c_str()) + strlen(key) + 1;
  596. newkey = new char[ len+1 ] ;
  597. ::sprintf(newkey, "%s\\%s", this->m_SubKey.c_str(), key);
  598. return newkey;
  599. }
  600. void RegistryHelper::SetSubKey(const char* sk)
  601. {
  602. if ( !sk )
  603. {
  604. m_SubKey = "";
  605. m_SubKeySpecified = false;
  606. }
  607. else
  608. {
  609. m_SubKey = sk;
  610. m_SubKeySpecified = true;
  611. }
  612. }
  613. //----------------------------------------------------------------------------
  614. char *RegistryHelper::Strip(char *str)
  615. {
  616. int cc;
  617. int len;
  618. char *nstr;
  619. if ( !str )
  620. {
  621. return NULL;
  622. }
  623. len = strlen(str);
  624. nstr = str;
  625. for( cc=0; cc<len; cc++ )
  626. {
  627. if ( !isspace( *nstr ) )
  628. {
  629. break;
  630. }
  631. nstr ++;
  632. }
  633. for( cc=(strlen(nstr)-1); cc>=0; cc-- )
  634. {
  635. if ( !isspace( nstr[cc] ) )
  636. {
  637. nstr[cc+1] = 0;
  638. break;
  639. }
  640. }
  641. return nstr;
  642. }
  643. //----------------------------------------------------------------------------
  644. void RegistryHelper::SetGlobalScope(bool b)
  645. {
  646. m_GlobalScope = b;
  647. }
  648. //----------------------------------------------------------------------------
  649. bool RegistryHelper::GetGlobalScope()
  650. {
  651. return m_GlobalScope;
  652. }
  653. } // namespace KWSYS_NAMESPACE