s3-registry: only include registry headers when really needed.
[ambi/samba-autobuild/.git] / source3 / registry / reg_util.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer (utility functions)
4  *  Copyright (C) Gerald Carter                     2002-2005
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* Implementation of registry frontend view functions. */
21
22 #include "includes.h"
23 #include "registry.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
27
28 /***********************************************************************
29  Utility function for splitting the base path of a registry path off
30  by setting base and new_path to the apprapriate offsets withing the
31  path.
32
33  WARNING!!  Does modify the original string!
34  ***********************************************************************/
35
36 bool reg_split_path(char *path, char **base, char **new_path)
37 {
38         char *p;
39
40         *new_path = *base = NULL;
41
42         if (!path) {
43                 return false;
44         }
45         *base = path;
46
47         p = strchr(path, '\\');
48
49         if ( p ) {
50                 *p = '\0';
51                 *new_path = p+1;
52         }
53
54         return true;
55 }
56
57 /***********************************************************************
58  Utility function for splitting the base path of a registry path off
59  by setting base and new_path to the appropriate offsets withing the
60  path.
61
62  WARNING!!  Does modify the original string!
63  ***********************************************************************/
64
65 bool reg_split_key(char *path, char **base, char **key)
66 {
67         char *p;
68
69         *key = *base = NULL;
70
71         if (!path) {
72                 return false;
73         }
74
75         *base = path;
76
77         p = strrchr(path, '\\');
78
79         if (p) {
80                 *p = '\0';
81                 *key = p+1;
82         }
83
84         return true;
85 }
86
87 /**
88  * The full path to the registry key is used as database key
89  * after the \'s are converted to /'s.
90  * Leading and trailing '/' and '\' characters are stripped.
91  * Key string is also normalized to UPPER case.
92  */
93
94 char *normalize_reg_path(TALLOC_CTX *ctx, const char *keyname )
95 {
96         char *p;
97         char *nkeyname;
98
99         /* skip leading '/' and '\' chars */
100         p = (char *)keyname;
101         while ((*p == '/') || (*p == '\\')) {
102                 p++;
103         }
104
105         nkeyname = talloc_string_sub(ctx, p, "\\", "/");
106         if (nkeyname == NULL) {
107                 return NULL;
108         }
109
110         /* strip trailing '/' chars */
111         p = strrchr(nkeyname, '/');
112         while ((p != NULL) && (p[1] == '\0')) {
113                 *p = '\0';
114                 p = strrchr(nkeyname, '/');
115         }
116
117         strupper_m(nkeyname);
118
119         return nkeyname;
120 }
121
122 /**
123  * normalize ther registry path in place.
124  */
125 void normalize_dbkey(char *key)
126 {
127         size_t len = strlen(key);
128         string_sub(key, "\\", "/", len+1);
129         strupper_m(key);
130 }
131
132 /**********************************************************************
133  move to next non-delimter character
134 *********************************************************************/
135
136 char *reg_remaining_path(TALLOC_CTX *ctx, const char *key)
137 {
138         char *new_path = NULL;
139         char *p = NULL;
140
141         if (!key || !*key) {
142                 return NULL;
143         }
144
145         new_path = talloc_strdup(ctx, key);
146         if (!new_path) {
147                 return NULL;
148         }
149         /* normalize_reg_path( new_path ); */
150         if (!(p = strchr(new_path, '\\')) ) {
151                 if (!(p = strchr( new_path, '/'))) {
152                         p = new_path;
153                 } else {
154                         p++;
155                 }
156         } else {
157                 p++;
158         }
159
160         return p;
161 }