Sync with trunk rev.61910 to get latest improvements and bugfixes.
[reactos.git] / dll / win32 / setupapi / dirid.c
1 /*
2 * Directory id handling
3 *
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "setupapi_private.h"
22
23 #define MAX_SYSTEM_DIRID DIRID_PRINTPROCESSOR
24 #define MIN_CSIDL_DIRID 0x4000
25 #define MAX_CSIDL_DIRID 0x403f
26
27 struct user_dirid
28 {
29 int id;
30 WCHAR *str;
31 };
32
33 static int nb_user_dirids; /* number of user dirids in use */
34 static int alloc_user_dirids; /* number of allocated user dirids */
35 static struct user_dirid *user_dirids;
36 static const WCHAR *system_dirids[MAX_SYSTEM_DIRID+1];
37 static const WCHAR *csidl_dirids[MAX_CSIDL_DIRID-MIN_CSIDL_DIRID+1];
38
39 /* retrieve the string for unknown dirids */
40 static const WCHAR *get_unknown_dirid(void)
41 {
42 static WCHAR *unknown_dirid;
43 static const WCHAR unknown_str[] = {'\\','u','n','k','n','o','w','n',0};
44
45 if (!unknown_dirid)
46 {
47 UINT len = GetSystemDirectoryW( NULL, 0 ) + strlenW(unknown_str);
48 if (!(unknown_dirid = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
49 GetSystemDirectoryW( unknown_dirid, len );
50 strcatW( unknown_dirid, unknown_str );
51 }
52 return unknown_dirid;
53 }
54
55 static const WCHAR *get_csidl_dir(DWORD csidl);
56
57 /* create the string for a system dirid */
58 static const WCHAR *create_system_dirid( int dirid )
59 {
60 static const WCHAR Null[] = {0};
61 static const WCHAR C_Root[] = {'C',':','\\',0};
62 static const WCHAR Drivers[] = {'\\','d','r','i','v','e','r','s',0};
63 static const WCHAR Inf[] = {'\\','i','n','f',0};
64 static const WCHAR Help[] = {'\\','h','e','l','p',0};
65 static const WCHAR Fonts[] = {'\\','f','o','n','t','s',0};
66 static const WCHAR Viewers[] = {'\\','v','i','e','w','e','r','s',0};
67 static const WCHAR System[] = {'\\','s','y','s','t','e','m',0};
68 static const WCHAR Spool[] = {'\\','s','p','o','o','l',0};
69 static const WCHAR UserProfile[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
70
71 WCHAR buffer[MAX_PATH+32], *str;
72 int len;
73
74 switch(dirid)
75 {
76 case DIRID_NULL:
77 return Null;
78 case DIRID_WINDOWS:
79 GetWindowsDirectoryW( buffer, MAX_PATH );
80 break;
81 case DIRID_SYSTEM:
82 GetSystemDirectoryW( buffer, MAX_PATH );
83 break;
84 case DIRID_DRIVERS:
85 GetSystemDirectoryW( buffer, MAX_PATH );
86 strcatW( buffer, Drivers );
87 break;
88 case DIRID_INF:
89 GetWindowsDirectoryW( buffer, MAX_PATH );
90 strcatW( buffer, Inf );
91 break;
92 case DIRID_HELP:
93 GetWindowsDirectoryW( buffer, MAX_PATH );
94 strcatW( buffer, Help );
95 break;
96 case DIRID_FONTS:
97 GetWindowsDirectoryW( buffer, MAX_PATH );
98 strcatW( buffer, Fonts );
99 break;
100 case DIRID_VIEWERS:
101 GetSystemDirectoryW( buffer, MAX_PATH );
102 strcatW( buffer, Viewers );
103 break;
104 case DIRID_APPS:
105 return C_Root; /* FIXME */
106 case DIRID_SHARED:
107 GetWindowsDirectoryW( buffer, MAX_PATH );
108 break;
109 case DIRID_BOOT:
110 return C_Root; /* FIXME */
111 case DIRID_SYSTEM16:
112 GetWindowsDirectoryW( buffer, MAX_PATH );
113 strcatW( buffer, System );
114 break;
115 case DIRID_SPOOL:
116 case DIRID_SPOOLDRIVERS: /* FIXME */
117 GetWindowsDirectoryW( buffer, MAX_PATH );
118 strcatW( buffer, Spool );
119 break;
120 case DIRID_USERPROFILE:
121 if (GetEnvironmentVariableW( UserProfile, buffer, MAX_PATH )) break;
122 return get_csidl_dir(CSIDL_PROFILE);
123 case DIRID_LOADER:
124 return C_Root; /* FIXME */
125 case DIRID_COLOR: /* FIXME */
126 case DIRID_PRINTPROCESSOR: /* FIXME */
127 default:
128 FIXME( "unknown dirid %d\n", dirid );
129 return get_unknown_dirid();
130 }
131 len = (strlenW(buffer) + 1) * sizeof(WCHAR);
132 if ((str = HeapAlloc( GetProcessHeap(), 0, len ))) memcpy( str, buffer, len );
133 return str;
134 }
135
136 static const WCHAR *get_csidl_dir( DWORD csidl )
137 {
138 WCHAR buffer[MAX_PATH], *str;
139 int len;
140
141 if (!SHGetSpecialFolderPathW( NULL, buffer, csidl, TRUE ))
142 {
143 FIXME( "CSIDL %x not found\n", csidl );
144 return get_unknown_dirid();
145 }
146 len = (strlenW(buffer) + 1) * sizeof(WCHAR);
147 if ((str = HeapAlloc( GetProcessHeap(), 0, len ))) memcpy( str, buffer, len );
148 return str;
149 }
150
151 /* retrieve the string corresponding to a dirid, or NULL if none */
152 const WCHAR *DIRID_get_string( int dirid )
153 {
154 int i;
155
156 if (dirid == DIRID_ABSOLUTE || dirid == DIRID_ABSOLUTE_16BIT) dirid = DIRID_NULL;
157
158 if (dirid >= DIRID_USER)
159 {
160 for (i = 0; i < nb_user_dirids; i++)
161 if (user_dirids[i].id == dirid) return user_dirids[i].str;
162 WARN("user id %d not found\n", dirid );
163 return NULL;
164 }
165 else if (dirid >= MIN_CSIDL_DIRID)
166 {
167 if (dirid > MAX_CSIDL_DIRID) return get_unknown_dirid();
168 dirid -= MIN_CSIDL_DIRID;
169 if (!csidl_dirids[dirid]) csidl_dirids[dirid] = get_csidl_dir( dirid );
170 return csidl_dirids[dirid];
171 }
172 else
173 {
174 if (dirid > MAX_SYSTEM_DIRID) return get_unknown_dirid();
175 if (!system_dirids[dirid]) system_dirids[dirid] = create_system_dirid( dirid );
176 return system_dirids[dirid];
177 }
178 }
179
180 /* store a user dirid string */
181 static BOOL store_user_dirid( HINF hinf, int id, WCHAR *str )
182 {
183 int i;
184
185 for (i = 0; i < nb_user_dirids; i++) if (user_dirids[i].id == id) break;
186
187 if (i < nb_user_dirids) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
188 else
189 {
190 if (nb_user_dirids >= alloc_user_dirids)
191 {
192 int new_size = max( 32, alloc_user_dirids * 2 );
193
194 struct user_dirid *new;
195
196 if (user_dirids)
197 new = HeapReAlloc( GetProcessHeap(), 0, user_dirids,
198 new_size * sizeof(*new) );
199 else
200 new = HeapAlloc( GetProcessHeap(), 0,
201 new_size * sizeof(*new) );
202
203 if (!new) return FALSE;
204 user_dirids = new;
205 alloc_user_dirids = new_size;
206 }
207 nb_user_dirids++;
208 }
209 user_dirids[i].id = id;
210 user_dirids[i].str = str;
211 TRACE("id %d -> %s\n", id, debugstr_w(str) );
212 return TRUE;
213 }
214
215
216 /***********************************************************************
217 * SetupSetDirectoryIdA (SETUPAPI.@)
218 */
219 BOOL WINAPI SetupSetDirectoryIdA( HINF hinf, DWORD id, PCSTR dir )
220 {
221 UNICODE_STRING dirW;
222 int i;
223
224 if (!id) /* clear everything */
225 {
226 for (i = 0; i < nb_user_dirids; i++) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
227 nb_user_dirids = 0;
228 return TRUE;
229 }
230 if (id < DIRID_USER)
231 {
232 SetLastError( ERROR_INVALID_PARAMETER );
233 return FALSE;
234 }
235
236 /* duplicate the string */
237 if (!RtlCreateUnicodeStringFromAsciiz( &dirW, dir ))
238 {
239 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
240 return FALSE;
241 }
242 return store_user_dirid( hinf, id, dirW.Buffer );
243 }
244
245
246 /***********************************************************************
247 * SetupSetDirectoryIdW (SETUPAPI.@)
248 */
249 BOOL WINAPI SetupSetDirectoryIdW( HINF hinf, DWORD id, PCWSTR dir )
250 {
251 int i, len;
252 WCHAR *str;
253
254 if (!id) /* clear everything */
255 {
256 for (i = 0; i < nb_user_dirids; i++) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
257 nb_user_dirids = 0;
258 return TRUE;
259 }
260 if (id < DIRID_USER)
261 {
262 SetLastError( ERROR_INVALID_PARAMETER );
263 return FALSE;
264 }
265
266 /* duplicate the string */
267 len = (strlenW(dir)+1) * sizeof(WCHAR);
268 if (!(str = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
269 memcpy( str, dir, len );
270 return store_user_dirid( hinf, id, str );
271 }