Beginnings of a script to initialize the .wine directory (with help
[wine.git] / dlls / msi / record.c
blobcf67da2f7b5f2c58bb39d5747bb53d9d8045ebe0
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002 Mike McCormack 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 "winuser.h"
26 #include "winerror.h"
27 #include "wine/debug.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "msipriv.h"
31 #include "objidl.h"
32 #include "winnls.h"
34 #include "query.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 #define MSIFIELD_NULL 0
39 #define MSIFIELD_INT 1
40 #define MSIFIELD_STR 2
41 #define MSIFIELD_WSTR 3
42 #define MSIFIELD_STREAM 4
44 /* maybe we can use a Variant instead of doing it ourselves? */
45 typedef struct tagMSIFIELD
47 UINT type;
48 union
50 INT iVal;
51 LPSTR szVal;
52 LPWSTR szwVal;
53 IStream *stream;
54 } u;
55 } MSIFIELD;
57 typedef struct tagMSIRECORD
59 UINT count; /* as passed to MsiCreateRecord */
60 MSIFIELD fields[1]; /* nb. array size is count+1 */
61 } MSIRECORD;
63 void MSI_FreeField( MSIFIELD *field )
65 switch( field->type )
67 case MSIFIELD_NULL:
68 case MSIFIELD_INT:
69 break;
70 case MSIFIELD_STR:
71 HeapFree( GetProcessHeap(), 0, field->u.szVal);
72 break;
73 case MSIFIELD_WSTR:
74 HeapFree( GetProcessHeap(), 0, field->u.szwVal);
75 break;
76 case MSIFIELD_STREAM:
77 IStream_Release( field->u.stream );
78 break;
79 default:
80 ERR("Invalid field type %d\n", field->type);
84 void MSI_CloseRecord( VOID *arg )
86 MSIRECORD *rec = (MSIRECORD *) arg;
87 UINT i;
89 for( i=0; i<rec->count; i++ )
90 MSI_FreeField( &rec->fields[i] );
93 MSIHANDLE WINAPI MsiCreateRecord( unsigned int cParams )
95 MSIHANDLE handle = 0;
96 UINT sz;
97 MSIRECORD *rec;
99 TRACE("%d\n", cParams);
101 sz = sizeof (MSIRECORD) + sizeof(MSIFIELD)*(cParams+1) ;
102 handle = alloc_msihandle( MSIHANDLETYPE_RECORD, sz,
103 MSI_CloseRecord, (void**) &rec );
104 if( !handle )
105 return 0;
107 rec->count = cParams;
109 return handle;
112 unsigned int WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
114 MSIRECORD *rec;
116 TRACE("%ld\n", handle );
118 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
119 if( !rec )
121 ERR("Record not found!\n");
122 return 0;
125 return rec->count;
128 static BOOL string2intA( LPCSTR str, int *out )
130 int x = 0;
131 LPCSTR p = str;
133 if( *p == '-' ) /* skip the minus sign */
134 p++;
135 while ( *p )
137 if( (*p < '0') || (*p > '9') )
138 return FALSE;
139 x *= 10;
140 x += (*p - '0');
141 p++;
144 if( str[0] == '-' ) /* check if it's negative */
145 x = -x;
146 *out = x;
148 return TRUE;
151 static BOOL string2intW( LPCWSTR str, int *out )
153 int x = 0;
154 LPCWSTR p = str;
156 if( *p == '-' ) /* skip the minus sign */
157 p++;
158 while ( *p )
160 if( (*p < '0') || (*p > '9') )
161 return FALSE;
162 x *= 10;
163 x += (*p - '0');
164 p++;
167 if( str[0] == '-' ) /* check if it's negative */
168 x = -x;
169 *out = x;
171 return TRUE;
174 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, unsigned int iField)
176 MSIRECORD *rec;
177 int ret = 0;
179 TRACE("%ld %d\n", handle, iField );
181 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
182 if( !rec )
183 return MSI_NULL_INTEGER;
185 if( iField > rec->count )
186 return MSI_NULL_INTEGER;
188 switch( rec->fields[iField].type )
190 case MSIFIELD_INT:
191 return rec->fields[iField].u.iVal;
192 case MSIFIELD_STR:
193 if( string2intA( rec->fields[iField].u.szVal, &ret ) )
194 return ret;
195 return MSI_NULL_INTEGER;
196 case MSIFIELD_WSTR:
197 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
198 return ret;
199 return MSI_NULL_INTEGER;
200 default:
201 break;
204 return MSI_NULL_INTEGER;
207 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
209 MSIRECORD *rec;
210 UINT i;
212 TRACE("%ld\n", handle );
214 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
215 if( !rec )
216 return ERROR_INVALID_HANDLE;
218 for( i=0; i<=rec->count; i++)
220 MSI_FreeField( &rec->fields[i] );
221 rec->fields[i].type = MSIFIELD_NULL;
222 rec->fields[i].u.iVal = 0;
225 return ERROR_SUCCESS;
228 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, unsigned int iField, int iVal )
230 MSIRECORD *rec;
232 TRACE("%ld %u %d\n", handle,iField, iVal);
234 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
235 if( !rec )
236 return ERROR_INVALID_HANDLE;
238 if( iField <= rec->count )
240 MSI_FreeField( &rec->fields[iField] );
241 rec->fields[iField].type = MSIFIELD_INT;
242 rec->fields[iField].u.iVal = iVal;
245 return ERROR_SUCCESS;
248 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, unsigned int iField )
250 MSIRECORD *rec;
251 BOOL r = TRUE;
253 TRACE("%ld %d\n", handle,iField );
255 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
256 if( !rec )
257 return ERROR_INVALID_HANDLE;
259 r = ( iField > rec->count ) ||
260 ( rec->fields[iField].type == MSIFIELD_NULL );
262 return r;
265 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, unsigned int iField,
266 LPSTR szValue, DWORD *pcchValue)
268 MSIRECORD *rec;
269 UINT len=0, ret;
270 CHAR buffer[16];
272 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
274 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
275 if( !rec )
276 return ERROR_INVALID_HANDLE;
278 if( iField > rec->count )
279 return ERROR_INVALID_PARAMETER;
281 ret = ERROR_SUCCESS;
282 switch( rec->fields[iField].type )
284 case MSIFIELD_INT:
285 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
286 len = lstrlenA( buffer );
287 lstrcpynA(szValue, buffer, *pcchValue);
288 break;
289 case MSIFIELD_STR:
290 len = lstrlenA( rec->fields[iField].u.szVal );
291 lstrcpynA(szValue, rec->fields[iField].u.szVal, *pcchValue);
292 break;
293 case MSIFIELD_WSTR:
294 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
295 NULL, 0 , NULL, NULL);
296 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
297 szValue, *pcchValue, NULL, NULL);
298 break;
299 default:
300 ret = ERROR_INVALID_PARAMETER;
301 break;
304 if( *pcchValue < len )
305 ret = ERROR_MORE_DATA;
306 *pcchValue = len;
308 return ret;
311 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, unsigned int iField,
312 LPWSTR szValue, DWORD *pcchValue)
314 MSIRECORD *rec;
315 UINT len=0, ret;
316 WCHAR buffer[16];
317 const WCHAR szFormat[] = { '%','d',0 };
319 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
321 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
322 if( !rec )
323 return ERROR_INVALID_HANDLE;
325 if( iField > rec->count )
326 return ERROR_INVALID_PARAMETER;
328 ret = ERROR_SUCCESS;
329 switch( rec->fields[iField].type )
331 case MSIFIELD_INT:
332 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
333 len = lstrlenW( buffer );
334 lstrcpynW(szValue, buffer, *pcchValue);
335 break;
336 case MSIFIELD_WSTR:
337 len = lstrlenW( rec->fields[iField].u.szwVal );
338 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
339 break;
340 case MSIFIELD_STR:
341 len = MultiByteToWideChar( CP_ACP, 0, rec->fields[iField].u.szVal, -1,
342 NULL, 0 );
343 MultiByteToWideChar( CP_ACP, 0, rec->fields[iField].u.szVal, -1,
344 szValue, *pcchValue);
345 break;
346 default:
347 break;
350 if( *pcchValue < len )
351 ret = ERROR_MORE_DATA;
352 *pcchValue = len;
354 return ret;
357 UINT WINAPI MsiRecordDataSize(MSIHANDLE hRecord, unsigned int iField)
359 FIXME("%ld %d\n", hRecord, iField);
360 return 0;
363 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
365 MSIRECORD *rec;
366 LPSTR str;
368 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
370 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
371 if( !rec )
372 return ERROR_INVALID_HANDLE;
374 if( iField > rec->count )
375 return ERROR_INVALID_FIELD;
377 str = HeapAlloc( GetProcessHeap(), 0, (lstrlenA(szValue) + 1)*sizeof str[0]);
378 lstrcpyA( str, szValue );
380 MSI_FreeField( &rec->fields[iField] );
381 rec->fields[iField].type = MSIFIELD_STR;
382 rec->fields[iField].u.szVal = str;
384 return 0;
387 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
389 MSIRECORD *rec;
390 LPWSTR str;
392 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
394 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
395 if( !rec )
396 return ERROR_INVALID_HANDLE;
398 if( iField > rec->count )
399 return ERROR_INVALID_FIELD;
401 str = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(szValue) + 1)*sizeof str[0]);
402 lstrcpyW( str, szValue );
404 MSI_FreeField( &rec->fields[iField] );
405 rec->fields[iField].type = MSIFIELD_WSTR;
406 rec->fields[iField].u.szwVal = str;
408 return 0;
411 UINT WINAPI MsiFormatRecordA(MSIHANDLE hInstall, MSIHANDLE hRecord, LPSTR szResult, DWORD *sz)
413 FIXME("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
414 return ERROR_CALL_NOT_IMPLEMENTED;
417 UINT WINAPI MsiFormatRecordW(MSIHANDLE hInstall, MSIHANDLE hRecord, LPWSTR szResult, DWORD *sz)
419 FIXME("%ld %ld %p %p\n", hInstall, hRecord, szResult, sz);
420 return ERROR_CALL_NOT_IMPLEMENTED;
423 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
425 FIXME("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
426 return ERROR_CALL_NOT_IMPLEMENTED;
429 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE hRecord, unsigned int iField, LPCWSTR szFilename)
431 FIXME("%ld %d %s\n", hRecord, iField, debugstr_w(szFilename));
432 return ERROR_CALL_NOT_IMPLEMENTED;
435 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
437 FIXME("%ld %d %p %p\n",handle,iField,buf,sz);
438 return ERROR_CALL_NOT_IMPLEMENTED;