Registry.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. int res = 0;
  431. if ( !m_Changed )
  432. {
  433. this->EntriesMap.erase(
  434. this->EntriesMap.begin(),
  435. this->EntriesMap.end());
  436. m_Empty = 1;
  437. this->SetSubKey(0);
  438. return 1;
  439. }
  440. kwsys_ios::ostringstream str;
  441. if ( !getenv("HOME") )
  442. {
  443. return 0;
  444. }
  445. str << getenv("HOME") << "/." << this->GetTopLevel() << "rc";
  446. kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
  447. if ( !ofs )
  448. {
  449. return 0;
  450. }
  451. if ( ofs->fail())
  452. {
  453. delete ofs;
  454. return 0;
  455. }
  456. *ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
  457. << "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
  458. << "# coments and empty lines will be deleted. Every line has" << kwsys_ios::endl
  459. << "# to be in format: " << kwsys_ios::endl
  460. << "# key = value" << kwsys_ios::endl
  461. << "#" << kwsys_ios::endl;
  462. if ( !this->EntriesMap.empty() )
  463. {
  464. RegistryHelper::StringToStringMap::iterator it;
  465. for ( it = this->EntriesMap.begin();
  466. it != this->EntriesMap.end();
  467. ++ it )
  468. {
  469. *ofs << it->first.c_str() << " = " << it->second.c_str()<< kwsys_ios::endl;
  470. }
  471. }
  472. this->EntriesMap.erase(
  473. this->EntriesMap.begin(),
  474. this->EntriesMap.end());
  475. ofs->close();
  476. delete ofs;
  477. res = 1;
  478. this->SetSubKey(0);
  479. m_Empty = 1;
  480. return res;
  481. }
  482. return false;
  483. }
  484. //----------------------------------------------------------------------------
  485. bool RegistryHelper::ReadValue(const char *skey, char *value)
  486. {
  487. #ifdef _WIN32
  488. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  489. {
  490. int res = 1;
  491. DWORD dwType, dwSize;
  492. dwType = REG_SZ;
  493. dwSize = BUFFER_SIZE;
  494. res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
  495. (BYTE *)value, &dwSize) == ERROR_SUCCESS );
  496. return (res != 0);
  497. }
  498. #endif
  499. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  500. {
  501. int res = 0;
  502. char *key = this->CreateKey( skey );
  503. if ( !key )
  504. {
  505. return 0;
  506. }
  507. RegistryHelper::StringToStringMap::iterator it
  508. = this->EntriesMap.find(key);
  509. if ( it != this->EntriesMap.end() )
  510. {
  511. strcpy(value, it->second.c_str());
  512. res = 1;
  513. }
  514. delete [] key;
  515. return res;
  516. }
  517. return false;
  518. }
  519. //----------------------------------------------------------------------------
  520. bool RegistryHelper::DeleteKey(const char* key)
  521. {
  522. #ifdef _WIN32
  523. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  524. {
  525. int res = 1;
  526. res = ( RegDeleteKey( this->HKey, key ) == ERROR_SUCCESS );
  527. return (res != 0);
  528. }
  529. #endif
  530. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  531. {
  532. (void)key;
  533. int res = 0;
  534. return res;
  535. }
  536. return false;
  537. }
  538. //----------------------------------------------------------------------------
  539. bool RegistryHelper::DeleteValue(const char *skey)
  540. {
  541. #ifdef _WIN32
  542. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  543. {
  544. int res = 1;
  545. res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
  546. return (res != 0);
  547. }
  548. #endif
  549. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  550. {
  551. char *key = this->CreateKey( skey );
  552. if ( !key )
  553. {
  554. return 0;
  555. }
  556. this->EntriesMap.erase(key);
  557. delete [] key;
  558. return 1;
  559. }
  560. return false;
  561. }
  562. //----------------------------------------------------------------------------
  563. bool RegistryHelper::SetValue(const char *skey, const char *value)
  564. {
  565. #ifdef _WIN32
  566. if ( m_RegistryType == Registry::WIN32_REGISTRY)
  567. {
  568. int res = 1;
  569. DWORD len = (DWORD)(value ? strlen(value) : 0);
  570. res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
  571. (CONST BYTE *)(const char *)value,
  572. len+1) == ERROR_SUCCESS );
  573. return (res != 0);
  574. }
  575. #endif
  576. if ( m_RegistryType == Registry::UNIX_REGISTRY )
  577. {
  578. char *key = this->CreateKey( skey );
  579. if ( !key )
  580. {
  581. return 0;
  582. }
  583. this->EntriesMap[key] = value;
  584. delete [] key;
  585. return 1;
  586. }
  587. return false;
  588. }
  589. //----------------------------------------------------------------------------
  590. char *RegistryHelper::CreateKey( const char *key )
  591. {
  592. char *newkey;
  593. if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
  594. {
  595. return 0;
  596. }
  597. int len = strlen(this->m_SubKey.c_str()) + strlen(key) + 1;
  598. newkey = new char[ len+1 ] ;
  599. ::sprintf(newkey, "%s\\%s", this->m_SubKey.c_str(), key);
  600. return newkey;
  601. }
  602. void RegistryHelper::SetSubKey(const char* sk)
  603. {
  604. if ( !sk )
  605. {
  606. m_SubKey = "";
  607. m_SubKeySpecified = false;
  608. }
  609. else
  610. {
  611. m_SubKey = sk;
  612. m_SubKeySpecified = true;
  613. }
  614. }
  615. //----------------------------------------------------------------------------
  616. char *RegistryHelper::Strip(char *str)
  617. {
  618. int cc;
  619. int len;
  620. char *nstr;
  621. if ( !str )
  622. {
  623. return NULL;
  624. }
  625. len = strlen(str);
  626. nstr = str;
  627. for( cc=0; cc<len; cc++ )
  628. {
  629. if ( !isspace( *nstr ) )
  630. {
  631. break;
  632. }
  633. nstr ++;
  634. }
  635. for( cc=(strlen(nstr)-1); cc>=0; cc-- )
  636. {
  637. if ( !isspace( nstr[cc] ) )
  638. {
  639. nstr[cc+1] = 0;
  640. break;
  641. }
  642. }
  643. return nstr;
  644. }
  645. //----------------------------------------------------------------------------
  646. void RegistryHelper::SetGlobalScope(bool b)
  647. {
  648. m_GlobalScope = b;
  649. }
  650. //----------------------------------------------------------------------------
  651. bool RegistryHelper::GetGlobalScope()
  652. {
  653. return m_GlobalScope;
  654. }
  655. } // namespace KWSYS_NAMESPACE