summaryrefslogtreecommitdiff
path: root/examples/Registry/test_registry_iterator.cpp
blob: c54af455eeae8ccd31cca13c21c0ad5f2804bcd7 (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
// This example uses the ACE_Registry class to iterator through the
// entries in the predefined registries. The host of iteration can be
// varied through argv[1]. If no host is specified the local host is
// used. This is very similar to how regedt32 starts up.  
//
// This examples points the cool iterators in ACE_Registry


#include "ace/Registry.h"


// Indentation while printing names
static const u_long INDENTATION_LEVEL = 3;

// Prototypes
static int print_naming_context (ACE_Registry::Naming_Context &naming_context,
				 u_long indentation);
static void print_object (const ACE_Registry::Istring &name,
			  u_long indentation);
static void print_context (ACE_Registry::Naming_Context &parent,
			   const ACE_Registry::Istring &name,
			   u_long indentation);
static void indent (u_long indentation);

int
main (int argc, char *argv[])
{
  int result;
  ACE_Registry::Naming_Context naming_context;

  LPCTSTR host = 0;
  if (argc == 2)
    host = ACE_WIDE_STRING (argv[1]);

  // Connect to a predefined naming context
  result = ACE_Predefined_Naming_Contexts::connect (naming_context,
						    HKEY_LOCAL_MACHINE, 
						    // HKEY_CLASSES_ROOT,
						    // HKEY_USERS,
						    // HKEY_CURRENT_USER, 
						    host);
							
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Predefined_Naming_Contexts::connect failed"), -1);
  
  // Print contents of naming context
  result = ::print_naming_context (naming_context, 0);
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "print_naming_context failed"), -1);

  return 0;
}


// Print contents of <naming_context>
static int
print_naming_context (ACE_Registry::Naming_Context &naming_context,
		      u_long indentation)
{
  ACE_Registry::Binding_List list;
  
  // Get the list of all entries
  int result = naming_context.list (list);
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Registry::Naming_Context::list"), -1);
  
  // Iterator through all entries
  for (ACE_Registry::Binding_List::iterator i = list.begin ();
       i != list.end ();
       i++)
    {
      // Yeeesss! STL rules!
      ACE_Registry::Binding &binding = *i;

      if (binding.type () == ACE_Registry::OBJECT)
	// If object
	::print_object (binding.name (),
			indentation);
      else
	// If context
	::print_context (naming_context,
			 binding.name (),
			 indentation);
    }
  return 0;
}


// Print an object with <name>
static void
print_object (const ACE_Registry::Istring &name,
	      u_long indentation)
{
  // Set indentation
  ::indent (indentation);
  cout << name << endl;
}


// Print an context with <name> and <parent>
static void
print_context (ACE_Registry::Naming_Context &parent,
	       const ACE_Registry::Istring &name,
	       u_long indentation)
{  
  // Set indentation
  indent (indentation);
  cout << name << endl;
  
  ACE_Registry::Naming_Context child_context;
  // Find child context
  int result = parent.resolve_context (name, 
				       child_context,
				       KEY_READ);
  if (result != 0)
    ACE_ERROR ((LM_ERROR, "%s %s\n", "ACE_Registry::Naming_Context::resolve_context failed for:", name.c_str ()));
  else 
    {
      // Print contents of the child
      result = ::print_naming_context (child_context,
				       indentation + INDENTATION_LEVEL);  
      if (result != 0)
	ACE_ERROR ((LM_ERROR, "%p\n", "print_naming_context failed"));
    }
}


// Pretty formating
static void
indent (u_long indentation)
{
  for (; indentation > 0; indentation--)
    cout << " ";
}