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
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
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};
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 );
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;
88 GetWindowsDirectoryW( buffer, MAX_PATH );
91 GetSystemDirectoryW( buffer, MAX_PATH );
94 GetSystemDirectoryW( buffer, MAX_PATH );
95 strcatW( buffer, Drivers );
98 GetWindowsDirectoryW( buffer, MAX_PATH );
99 strcatW( buffer, Inf );
102 GetWindowsDirectoryW( buffer, MAX_PATH );
103 strcatW( buffer, Help );
106 GetWindowsDirectoryW( buffer, MAX_PATH );
107 strcatW( buffer, Fonts );
110 GetSystemDirectoryW( buffer, MAX_PATH );
111 strcatW( buffer, Viewers );
114 return C_Root; /* FIXME */
116 GetWindowsDirectoryW( buffer, MAX_PATH );
119 return C_Root; /* FIXME */
121 GetWindowsDirectoryW( buffer, MAX_PATH );
122 strcatW( buffer, System );
125 case DIRID_SPOOLDRIVERS: /* FIXME */
126 GetWindowsDirectoryW( buffer, MAX_PATH );
127 strcatW( buffer, Spool );
130 return C_Root; /* FIXME */
131 case DIRID_USERPROFILE: /* FIXME */
132 case DIRID_COLOR: /* FIXME */
133 case DIRID_PRINTPROCESSOR: /* FIXME */
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 );
143 /* retrieve the string corresponding to a dirid, or NULL if none */
144 const WCHAR *DIRID_get_string( HINF hinf, int dirid )
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 );
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 )
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 );
176 if (nb_user_dirids >= alloc_user_dirids)
178 int new_size = max( 32, alloc_user_dirids * 2 );
180 struct user_dirid *new;
183 new = HeapReAlloc( GetProcessHeap(), 0, user_dirids,
184 new_size * sizeof(*new) );
186 new = HeapAlloc( GetProcessHeap(), 0,
187 new_size * sizeof(*new) );
189 if (!new) return FALSE;
191 alloc_user_dirids = new_size;
195 user_dirids[i].id = id;
196 user_dirids[i].str = str;
197 TRACE("id %d -> %s\n", id, debugstr_w(str) );
202 /***********************************************************************
203 * SetupSetDirectoryIdA (SETUPAPI.@)
205 BOOL WINAPI SetupSetDirectoryIdA( HINF hinf, DWORD id, PCSTR dir )
210 if (!id) /* clear everything */
212 for (i = 0; i < nb_user_dirids; i++) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
218 SetLastError( ERROR_INVALID_PARAMETER );
222 /* duplicate the string */
223 if (!RtlCreateUnicodeStringFromAsciiz( &dirW, dir ))
225 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
228 return store_user_dirid( hinf, id, dirW.Buffer );
232 /***********************************************************************
233 * SetupSetDirectoryIdW (SETUPAPI.@)
235 BOOL WINAPI SetupSetDirectoryIdW( HINF hinf, DWORD id, PCWSTR dir )
240 if (!id) /* clear everything */
242 for (i = 0; i < nb_user_dirids; i++) HeapFree( GetProcessHeap(), 0, user_dirids[i].str );
248 SetLastError( ERROR_INVALID_PARAMETER );
252 /* duplicate the string */
253 len = (strlenW(dir)+1) * sizeof(WCHAR);
254 if (!(str = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
255 memcpy( str, dir, len );
256 return store_user_dirid( hinf, id, str );