Set version to 9.
[wine/wine64.git] / dlls / setupapi / dirid.c
blob7728f898b07029400033eda36d9bfaa99b3722e1
1 /*
2 * Directory id handling
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "winternl.h"
27 #include "winerror.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "setupapi.h"
32 #include "wine/unicode.h"
33 #include "setupapi_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
38 #define MAX_SYSTEM_DIRID DIRID_PRINTPROCESSOR
40 struct user_dirid
42 int id;
43 WCHAR *str;
46 static int nb_user_dirids; /* number of user dirids in use */
47 static int alloc_user_dirids; /* number of allocated user dirids */
48 static struct user_dirid *user_dirids;
49 static const WCHAR *system_dirids[MAX_SYSTEM_DIRID+1];
51 /* retrieve the string for unknown dirids */
52 static const WCHAR *get_unknown_dirid(void)
54 static WCHAR *unknown_dirid;
55 static const WCHAR unknown_str[] = {'\\','u','n','k','n','o','w','n',0};
57 if (!unknown_dirid)
59 UINT len = GetSystemDirectoryW( NULL, 0 ) + strlenW(unknown_str);
60 if (!(unknown_dirid = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
61 GetSystemDirectoryW( unknown_dirid, len );
62 strcatW( unknown_dirid, unknown_str );
64 return unknown_dirid;
67 /* create the string for a system dirid */
68 static const WCHAR *create_system_dirid( int dirid )
70 static const WCHAR Null[] = {0};
71 static const WCHAR C_Root[] = {'C',':','\\',0};
72 static const WCHAR Drivers[] = {'\\','d','r','i','v','e','r','s',0};
73 static const WCHAR Inf[] = {'\\','i','n','f',0};
74 static const WCHAR Help[] = {'\\','h','e','l','p',0};
75 static const WCHAR Fonts[] = {'\\','f','o','n','t','s',0};
76 static const WCHAR Viewers[] = {'\\','v','i','e','w','e','r','s',0};
77 static const WCHAR System[] = {'\\','s','y','s','t','e','m',0};
78 static const WCHAR Spool[] = {'\\','s','p','o','o','l',0};
80 WCHAR buffer[MAX_PATH+16], *str;
81 int len;
83 switch(dirid)
85 case DIRID_NULL:
86 return Null;
87 case DIRID_WINDOWS:
88 GetWindowsDirectoryW( buffer, MAX_PATH );
89 break;
90 case DIRID_SYSTEM:
91 GetSystemDirectoryW( buffer, MAX_PATH );
92 break;
93 case DIRID_DRIVERS:
94 GetSystemDirectoryW( buffer, MAX_PATH );
95 strcatW( buffer, Drivers );
96 break;
97 case DIRID_INF:
98 GetWindowsDirectoryW( buffer, MAX_PATH );
99 strcatW( buffer, Inf );
100 break;
101 case DIRID_HELP:
102 GetWindowsDirectoryW( buffer, MAX_PATH );
103 strcatW( buffer, Help );
104 break;
105 case DIRID_FONTS:
106 GetWindowsDirectoryW( buffer, MAX_PATH );
107 strcatW( buffer, Fonts );
108 break;
109 case DIRID_VIEWERS:
110 GetSystemDirectoryW( buffer, MAX_PATH );
111 strcatW( buffer, Viewers );
112 break;
113 case DIRID_APPS:
114 return C_Root; /* FIXME */
115 case DIRID_SHARED:
116 GetWindowsDirectoryW( buffer, MAX_PATH );
117 break;
118 case DIRID_BOOT:
119 return C_Root; /* FIXME */
120 case DIRID_SYSTEM16:
121 GetWindowsDirectoryW( buffer, MAX_PATH );
122 strcatW( buffer, System );
123 break;
124 case DIRID_SPOOL:
125 case DIRID_SPOOLDRIVERS: /* FIXME */
126 GetWindowsDirectoryW( buffer, MAX_PATH );
127 strcatW( buffer, Spool );
128 break;
129 case DIRID_LOADER:
130 return C_Root; /* FIXME */
131 case DIRID_USERPROFILE: /* FIXME */
132 case DIRID_COLOR: /* FIXME */
133 case DIRID_PRINTPROCESSOR: /* FIXME */
134 default:
135 FIXME( "unknown dirid %d\n", dirid );
136 return get_unknown_dirid();
138 len = (strlenW(buffer) + 1) * sizeof(WCHAR);
139 if ((str = HeapAlloc( GetProcessHeap(), 0, len ))) memcpy( str, buffer, len );
140 return str;
143 /* retrieve the string corresponding to a dirid, or NULL if none */
144 const WCHAR *DIRID_get_string( HINF hinf, int dirid )
146 int i;
148 if (dirid == DIRID_ABSOLUTE || dirid == DIRID_ABSOLUTE_16BIT) dirid = DIRID_NULL;
150 if (dirid >= DIRID_USER)
152 for (i = 0; i < nb_user_dirids; i++)
153 if (user_dirids[i].id == dirid) return user_dirids[i].str;
154 ERR("user id %d not found\n", dirid );
155 return NULL;
157 else
159 if (dirid > MAX_SYSTEM_DIRID) return get_unknown_dirid();
160 if (dirid == DIRID_SRCPATH) return PARSER_get_src_root( hinf );
161 if (!system_dirids[dirid]) system_dirids[dirid] = create_system_dirid( dirid );
162 return system_dirids[dirid];
166 /* store a user dirid string */
167 static BOOL store_user_dirid( HINF hinf, int id, WCHAR *str )
169 int i;
171 for (i = 0; i < nb_user_dirids; i++) if (user_dirids[i].id == id) break;
173 if (i < nb_user_dirids) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
174 else
176 if (nb_user_dirids >= alloc_user_dirids)
178 int new_size = max( 32, alloc_user_dirids * 2 );
179 struct user_dirid *new = HeapReAlloc( GetProcessHeap(), 0, user_dirids,
180 new_size * sizeof(*new) );
181 if (!new) return FALSE;
182 user_dirids = new;
183 alloc_user_dirids = new_size;
185 nb_user_dirids++;
187 user_dirids[i].id = id;
188 user_dirids[i].str = str;
189 TRACE("id %d -> %s\n", id, debugstr_w(str) );
190 return TRUE;
194 /***********************************************************************
195 * SetupSetDirectoryIdA (SETUPAPI.@)
197 BOOL WINAPI SetupSetDirectoryIdA( HINF hinf, DWORD id, PCSTR dir )
199 UNICODE_STRING dirW;
200 int i;
202 if (!id) /* clear everything */
204 for (i = 0; i < nb_user_dirids; i++) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
205 nb_user_dirids = 0;
206 return TRUE;
208 if (id < DIRID_USER)
210 SetLastError( ERROR_INVALID_PARAMETER );
211 return FALSE;
214 /* duplicate the string */
215 if (!RtlCreateUnicodeStringFromAsciiz( &dirW, dir ))
217 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
218 return FALSE;
220 return store_user_dirid( hinf, id, dirW.Buffer );
224 /***********************************************************************
225 * SetupSetDirectoryIdW (SETUPAPI.@)
227 BOOL WINAPI SetupSetDirectoryIdW( HINF hinf, DWORD id, PCWSTR dir )
229 int i, len;
230 WCHAR *str;
232 if (!id) /* clear everything */
234 for (i = 0; i < nb_user_dirids; i++) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
235 nb_user_dirids = 0;
236 return TRUE;
238 if (id < DIRID_USER)
240 SetLastError( ERROR_INVALID_PARAMETER );
241 return FALSE;
244 /* duplicate the string */
245 len = (strlenW(dir)+1) * sizeof(WCHAR);
246 if (!(str = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
247 memcpy( str, dir, len );
248 return store_user_dirid( hinf, id, str );