summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/Registry.cxx
blob: dcfec52ec1a4ed3a690461a3ba8a2090ae8de7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*=========================================================================

  Program:   KWSys - Kitware System Library
  Module:    $RCSfile$

  Copyright (c) Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Registry.hxx)

#include KWSYS_HEADER(Configure.hxx)
#include KWSYS_HEADER(ios/iostream)
#include KWSYS_HEADER(stl/string)
#include KWSYS_HEADER(stl/map)
#include KWSYS_HEADER(ios/iostream)
#include KWSYS_HEADER(ios/fstream)
#include KWSYS_HEADER(ios/sstream)
// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
# include "Registry.hxx.in"
# include "Configure.hxx.in"
# include "kwsys_stl.hxx.in"
# include "kwsys_stl_string.hxx.in"
# include "kwsys_stl_map.hxx.in"
# include "kwsys_ios_iostream.h.in"
# include "kwsys_ios_fstream.h.in"
# include "kwsys_ios_sstream.h.in"
#endif

#include <ctype.h> // for isspace
#include <stdio.h>
#include <string.h> /* strlen, strncpy */
#include <stdlib.h> /* getenv */

#ifdef _WIN32
# include <windows.h>
#endif


namespace KWSYS_NAMESPACE
{
class RegistryHelper {
public:
  RegistryHelper(Registry::RegistryType registryType);
  virtual ~RegistryHelper();

  // Read a value from the registry.
  virtual bool ReadValue(const char *key, const char **value);

  // Delete a key from the registry.
  virtual bool DeleteKey(const char *key);

  // Delete a value from a given key.
  virtual bool DeleteValue(const char *key);

  // Set value in a given key.
  virtual bool SetValue(const char *key, const char *value);

  // Open the registry at toplevel/subkey.
  virtual bool Open(const char *toplevel, const char *subkey,
    int readonly);

  // Close the registry.
  virtual bool Close();

  // Set the value of changed
  void SetChanged(bool b) { m_Changed = b; }
  void SetTopLevel(const char* tl);
  const char* GetTopLevel() { return m_TopLevel.c_str(); }

  //! Read from local or global scope. On Windows this mean from local machine
  // or local user. On unix this will read from $HOME/.Projectrc or
  // /etc/Project
  void SetGlobalScope(bool b);
  bool GetGlobalScope();

  kwsys_stl::string EncodeKey(const char* str);
  kwsys_stl::string EncodeValue(const char* str);
  kwsys_stl::string DecodeValue(const char* str);

protected:
  bool m_Changed;
  kwsys_stl::string m_TopLevel;
  bool m_GlobalScope;

#ifdef _WIN32
  HKEY HKey;
#endif
  // Strip trailing and ending spaces.
  char *Strip(char *str);
  void SetSubKey(const char* sk);
  kwsys_stl::string CreateKey(const char *key);

  typedef kwsys_stl::map<kwsys_stl::string, kwsys_stl::string> StringToStringMap;
  StringToStringMap EntriesMap;
  kwsys_stl::string m_SubKey;
  bool m_Empty;
  bool m_SubKeySpecified;
  kwsys_stl::string m_HomeDirectory;

  Registry::RegistryType m_RegistryType;
};

//----------------------------------------------------------------------------
#define Registry_BUFFER_SIZE 8192

//----------------------------------------------------------------------------
Registry::Registry(Registry::RegistryType registryType)
{
  m_Opened      = false;
  m_Locked      = false;
  this->Helper = 0;
  this->Helper = new RegistryHelper(registryType);
}

//----------------------------------------------------------------------------
Registry::~Registry()
{
  if ( m_Opened )
    {
    kwsys_ios::cerr << "Registry::Close should be "
                  "called here. The registry is not closed."
                  << kwsys_ios::endl;
    }
  delete this->Helper;
}

//----------------------------------------------------------------------------
void Registry::SetGlobalScope(bool b)
{
  this->Helper->SetGlobalScope(b);
}

//----------------------------------------------------------------------------
bool Registry::GetGlobalScope()
{
  return this->Helper->GetGlobalScope();
}

//----------------------------------------------------------------------------
bool Registry::Open(const char *toplevel,
  const char *subkey, int readonly)
{
  bool res = false;
  if ( m_Locked )
    {
    return res;
    }
  if ( m_Opened )
    {
    if ( !this->Close() )
      {
      return res;
      }
    }
  if ( !toplevel || !*toplevel )
    {
    kwsys_ios::cerr << "Registry::Opened() Toplevel not defined"
      << kwsys_ios::endl;
    return res;
    }

  if ( isspace(toplevel[0]) ||
       isspace(toplevel[strlen(toplevel)-1]) )
    {
    kwsys_ios::cerr << "Toplevel has to start with letter or number and end"
      " with one" << kwsys_ios::endl;
    return res;
    }

  res = this->Helper->Open(toplevel, subkey, readonly);
  if ( readonly != Registry::READONLY )
    {
    m_Locked = true;
    }

  if ( res )
    {
    m_Opened = true;
    this->Helper->SetTopLevel(toplevel);
    }
  return res;
}

//----------------------------------------------------------------------------
bool Registry::Close()
{
  bool res = false;
  if ( m_Opened )
    {
    res = this->Helper->Close();
    }

  if ( res )
    {
    m_Opened = false;
    m_Locked = false;
    this->Helper->SetChanged(false);
    }
  return res;
}

//----------------------------------------------------------------------------
bool Registry::ReadValue(const char *subkey,
  const char *key,
  const char **value)
{
  bool res = false;
  bool open = false;
  if ( ! value )
    {
    return res;
    }
  *value = 0;
  if ( !m_Opened )
    {
    if ( !this->Open(this->GetTopLevel(), subkey,
        Registry::READONLY) )
      {
      return res;
      }
    open = true;
    }
  res = this->Helper->ReadValue(key, value);

  if ( open )
    {
    if ( !this->Close() )
      {
      res = false;
      }
    }
  return res;
}

//----------------------------------------------------------------------------
bool Registry::DeleteKey(const char *subkey, const char *key)
{
  bool res = false;
  bool open = false;
  if ( !m_Opened )
    {
    if ( !this->Open(this->GetTopLevel(), subkey,
        Registry::READWRITE) )
      {
      return res;
      }
    open = true;
    }

  res = this->Helper->DeleteKey(key);
  if ( res )
    {
    this->Helper->SetChanged(true);
    }

  if ( open )
    {
    if ( !this->Close() )
      {
      res = false;
      }
    }
  return res;
}

//----------------------------------------------------------------------------
bool Registry::DeleteValue(const char *subkey, const char *key)
{
  bool res = false;
  bool open = false;
  if ( !m_Opened )
    {
    if ( !this->Open(this->GetTopLevel(), subkey,
        Registry::READWRITE) )
      {
      return res;
      }
    open = true;
    }

  res = this->Helper->DeleteValue(key);
  if ( res )
    {
    this->Helper->SetChanged(true);
    }

  if ( open )
    {
    if ( !this->Close() )
      {
      res = false;
      }
    }
  return res;
}

//----------------------------------------------------------------------------
bool Registry::SetValue(const char *subkey, const char *key,
  const char *value)
{
  bool res = false;
  bool open = false;
  if ( !m_Opened )
    {
    if ( !this->Open(this->GetTopLevel(), subkey,
        Registry::READWRITE) )
      {
      return res;
      }
    open = true;
    }

  res = this->Helper->SetValue( key, value );
  if ( res )
    {
    this->Helper->SetChanged(true);
    }

  if ( open )
    {
    if ( !this->Close() )
      {
      res = false;
      }
    }
  return res;
}

//----------------------------------------------------------------------------
const char* Registry::GetTopLevel()
{
  return this->Helper->GetTopLevel();
}

//----------------------------------------------------------------------------
void Registry::SetTopLevel(const char* tl)
{
  this->Helper->SetTopLevel(tl);
}

//----------------------------------------------------------------------------
void RegistryHelper::SetTopLevel(const char* tl)
{
  if ( tl )
    {
    m_TopLevel = tl;
    }
  else
    {
    m_TopLevel = "";
    }
}

//----------------------------------------------------------------------------
RegistryHelper::RegistryHelper(Registry::RegistryType registryType)
{
  m_Changed = false;
  m_TopLevel    = "";
  m_SubKey  = "";
  m_SubKeySpecified = false;
  m_Empty       = true;
  m_GlobalScope = false;
  m_RegistryType = registryType;
}

//----------------------------------------------------------------------------
RegistryHelper::~RegistryHelper()
{
}


//----------------------------------------------------------------------------
bool RegistryHelper::Open(const char *toplevel, const char *subkey,
  int readonly)
{
  this->EntriesMap.clear();
  m_Empty = 1;

#ifdef _WIN32
  if ( m_RegistryType == Registry::WIN32_REGISTRY)
    {
    HKEY scope = HKEY_CURRENT_USER;
    if ( this->GetGlobalScope() )
      {
      scope = HKEY_LOCAL_MACHINE;
      }
    int res = 0;
    kwsys_ios::ostringstream str;
    DWORD dwDummy;
    str << "Software\\Kitware\\" << toplevel << "\\" << subkey;
    if ( readonly == Registry::READONLY )
      {
      res = ( RegOpenKeyEx(scope, str.str().c_str(),
          0, KEY_READ, &this->HKey) == ERROR_SUCCESS );
      }
    else
      {
      res = ( RegCreateKeyEx(scope, str.str().c_str(),
          0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
          NULL, &this->HKey, &dwDummy) == ERROR_SUCCESS );
      }
    if ( res != 0 )
      {
      this->SetSubKey( subkey );
      }
    return (res != 0);
    }
#endif
  if ( m_RegistryType == Registry::FILE_REGISTRY )
    {
    bool res = false;
    int cc;
    kwsys_ios::ostringstream str;
    const char* homeDirectory;
    if ( (homeDirectory = getenv("HOME")) == 0 )
      {
      if ( (homeDirectory = getenv("USERPROFILE")) == 0 )
        {
        return false;
        }
      }
    m_HomeDirectory = homeDirectory;
    str << m_HomeDirectory.c_str() << "/." << toplevel << "rc";
    if ( readonly == Registry::READWRITE )
      {
      kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app );
      if ( ofs.fail() )
        {
        return false;
        }
      ofs.close();
      }

    kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in
#ifndef KWSYS_IOS_USE_ANSI
      | kwsys_ios::ios::nocreate
#endif
      );
    if ( !ifs )
      {
      return false;
      }
    if ( ifs->fail())
      {
      delete ifs;
      return false;
      }

    res = true;
    char buffer[Registry_BUFFER_SIZE];
    while( !ifs->fail() )
      {
      ifs->getline(buffer, Registry_BUFFER_SIZE);
      if ( ifs->fail() || ifs->eof() )
        {
        break;
        }
      char *line = this->Strip(buffer);
      if ( *line == '#'  || *line == 0 )
        {
        // Comment
        continue;
        }
      int linelen = static_cast<int>(strlen(line));
      for ( cc = 0; cc < linelen; cc++ )
        {
        if ( line[cc] == '=' )
          {
          char *key = new char[ cc+1 ];
          strncpy( key, line, cc );
          key[cc] = 0;
          char *value = line + cc + 1;
          char *nkey = this->Strip(key);
          char *nvalue = this->Strip(value);
          this->EntriesMap[nkey] = this->DecodeValue(nvalue);
          m_Empty = 0;
          delete [] key;
          break;
          }
        }
      }
    ifs->close();
    this->SetSubKey( subkey );
    delete ifs;
    return res;
    }
  return false;
}

//----------------------------------------------------------------------------
bool RegistryHelper::Close()
{
#ifdef _WIN32
  if ( m_RegistryType == Registry::WIN32_REGISTRY)
    {
    int res;
    res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
    return (res != 0);
    }
#endif
  if ( m_RegistryType == Registry::FILE_REGISTRY )
    {
    if ( !m_Changed )
      {
      this->SetSubKey(0);
      return true;
      }

    kwsys_ios::ostringstream str;
    str << m_HomeDirectory.c_str() << "/." << this->GetTopLevel() << "rc";
    kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
    if ( !ofs )
      {
      return false;
      }
    if ( ofs->fail())
      {
      delete ofs;
      return false;
      }
    *ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
      << "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
      << "# comments and empty lines will be deleted. Every line has" << kwsys_ios::endl
      << "# to be in format: " << kwsys_ios::endl
      << "# key = value" << kwsys_ios::endl
      << "#" << kwsys_ios::endl;

    if ( !this->EntriesMap.empty() )
      {
      RegistryHelper::StringToStringMap::iterator it;
      for ( it = this->EntriesMap.begin();
        it != this->EntriesMap.end();
        ++ it )
        {
        *ofs << it->first.c_str() << " = " << this->EncodeValue(it->second.c_str()).c_str() << kwsys_ios::endl;
        }
      }
    this->EntriesMap.clear();
    ofs->close();
    delete ofs;
    this->SetSubKey(0);
    m_Empty = 1;
    return true;
    }
  return false;
}

//----------------------------------------------------------------------------
bool RegistryHelper::ReadValue(const char *skey, const char **value)

{
#ifdef _WIN32
  if ( m_RegistryType == Registry::WIN32_REGISTRY)
    {
    kwsys_stl::string key = this->CreateKey( skey );
    if ( key.empty() )
      {
      return false;
      }
    DWORD dwType, dwSize;
    dwType = REG_SZ;
    char buffer[1024]; // Replace with RegQueryInfoKey
    dwSize = sizeof(buffer);
    int res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
        (BYTE *)buffer, &dwSize) == ERROR_SUCCESS );
    if ( !res )
      {
      return false;
      }
    this->EntriesMap[key] = buffer;
    RegistryHelper::StringToStringMap::iterator it
      = this->EntriesMap.find(key);
    *value = it->second.c_str();
    return true;
    }
#endif
  if ( m_RegistryType == Registry::FILE_REGISTRY )
    {
    bool res = false;
    kwsys_stl::string key = this->CreateKey( skey );
    if ( key.empty() )
      {
      return false;
      }

    RegistryHelper::StringToStringMap::iterator it
      = this->EntriesMap.find(key);
    if ( it != this->EntriesMap.end() )
      {
      *value = it->second.c_str();
      res = true;
      }
    return res;
    }
  return false;
}

//----------------------------------------------------------------------------
bool RegistryHelper::DeleteKey(const char* skey)
{
#ifdef _WIN32
  if ( m_RegistryType == Registry::WIN32_REGISTRY)
    {
    int res = ( RegDeleteKey( this->HKey, skey ) == ERROR_SUCCESS );
    return (res != 0);
    }
#endif
  if ( m_RegistryType == Registry::FILE_REGISTRY )
    {
    kwsys_stl::string key = this->CreateKey( skey );
    if ( key.empty() )
      {
      return false;
      }
    this->EntriesMap.erase(key);
    return true;
    }
  return false;
}

//----------------------------------------------------------------------------
bool RegistryHelper::DeleteValue(const char *skey)
{
#ifdef _WIN32
  if ( m_RegistryType == Registry::WIN32_REGISTRY)
    {
    int res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
    return (res != 0);
    }
#endif
  if ( m_RegistryType == Registry::FILE_REGISTRY )
    {
    kwsys_stl::string key = this->CreateKey( skey );
    if ( key.empty() )
      {
      return false;
      }
    this->EntriesMap.erase(key);
    return true;
    }
  return false;
}

//----------------------------------------------------------------------------
bool RegistryHelper::SetValue(const char *skey, const char *value)
{
#ifdef _WIN32
  if ( m_RegistryType == Registry::WIN32_REGISTRY)
    {
    DWORD len = (DWORD)(value ? strlen(value) : 0);
    int res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
        (CONST BYTE *)(const char *)value,
        len+1) == ERROR_SUCCESS );
    return (res != 0);
    }
#endif
  if ( m_RegistryType == Registry::FILE_REGISTRY )
    {
    kwsys_stl::string key = this->CreateKey( skey );
    if ( key.empty() )
      {
      return 0;
      }
    this->EntriesMap[key] = value;
    return 1;
    }
  return false;
}

//----------------------------------------------------------------------------
kwsys_stl::string RegistryHelper::CreateKey( const char *key )
{
  if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
    {
    return "";
    }
  kwsys_ios::ostringstream ostr;
  ostr << this->EncodeKey(this->m_SubKey.c_str()).c_str()
       << "\\" << this->EncodeKey(key).c_str();
  return ostr.str();
}

//----------------------------------------------------------------------------
void RegistryHelper::SetSubKey(const char* sk)
{
  if ( !sk )
    {
    m_SubKey = "";
    m_SubKeySpecified = false;
    }
  else
    {
    m_SubKey = sk;
    m_SubKeySpecified = true;
    }
}

//----------------------------------------------------------------------------
char *RegistryHelper::Strip(char *str)
{
  int cc;
  size_t len;
  char *nstr;
  if ( !str )
    {
    return NULL;
    }
  len = strlen(str);
  nstr = str;
  for( cc=0; cc < static_cast<int>(len); cc++ )
    {
    if ( !isspace( *nstr ) )
      {
      break;
      }
    nstr ++;
    }
  for( cc= static_cast<int>(strlen(nstr))-1; cc>=0; cc-- )
    {
    if ( !isspace( nstr[cc] ) )
      {
      nstr[cc+1] = 0;
      break;
      }
    }
  return nstr;
}

//----------------------------------------------------------------------------
void RegistryHelper::SetGlobalScope(bool b)
{
  m_GlobalScope = b;
}

//----------------------------------------------------------------------------
bool RegistryHelper::GetGlobalScope()
{
  return m_GlobalScope;
}

//----------------------------------------------------------------------------
kwsys_stl::string RegistryHelper::EncodeKey(const char* str)
{
  kwsys_ios::ostringstream ostr;
  while ( *str )
    {
    switch ( *str )
      {
    case '%': case '=': case '\n': case '\r': case '\t':
      char buffer[4];
      sprintf(buffer, "%%%02X", *str);
      ostr << buffer;
      break;
    default:
      ostr << *str;
      }
    str ++;
    }
  return ostr.str();
}

//----------------------------------------------------------------------------
kwsys_stl::string RegistryHelper::EncodeValue(const char* str)
{
  kwsys_ios::ostringstream ostr;
  while ( *str )
    {
    switch ( *str )
      {
    case '%': case '=': case '\n': case '\r': case '\t':
      char buffer[4];
      sprintf(buffer, "%%%02X", *str);
      ostr << buffer;
      break;
    default:
      ostr << *str;
      }
    str ++;
    }
  return ostr.str();
}

//----------------------------------------------------------------------------
kwsys_stl::string RegistryHelper::DecodeValue(const char* str)
{
  kwsys_ios::ostringstream ostr;
  while ( *str )
    {
    unsigned int val;
    switch ( *str )
      {
    case '%':
      if ( *(str+1) && *(str+2) && sscanf(str+1, "%x", &val) == 1 )
        {
        ostr << static_cast<char>(val);
        str += 2;
        }
      else
        {
        ostr << *str;
        }
      break;
    default:
      ostr << *str;
      }
    str ++;
    }
  return ostr.str();
}

} // namespace KWSYS_NAMESPACE