Added parser template and made AVISplitter use it.
[wine] / dlls / msi / order.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002 Mike McCormack 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "objbase.h"
30 #include "objidl.h"
31 #include "msipriv.h"
32 #include "winnls.h"
33
34 #include "query.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37
38
39 /* below is the query interface to a table */
40
41 typedef struct tagMSIORDERVIEW
42 {
43     MSIVIEW        view;
44     MSIDATABASE   *db;
45     MSIVIEW       *table;
46     UINT          *reorder;
47     UINT           num_cols;
48     UINT           cols[1];
49 } MSIORDERVIEW;
50
51 static UINT ORDER_compare( MSIORDERVIEW *ov, UINT a, UINT b, UINT *swap )
52 {
53     UINT r, i, a_val = 0, b_val = 0;
54
55     *swap = 0;
56     for( i=0; i<ov->num_cols; i++ )
57     {
58         r = ov->table->ops->fetch_int( ov->table, a, ov->cols[i], &a_val );
59         if( r != ERROR_SUCCESS )
60             return r;
61
62         r = ov->table->ops->fetch_int( ov->table, b, ov->cols[i], &b_val );
63         if( r != ERROR_SUCCESS )
64             return r;
65
66         if( a_val != b_val )
67         {
68             if( a_val > b_val )
69                 *swap = 1;
70             break;
71         }
72     }
73
74     return ERROR_SUCCESS;
75 }
76
77 static UINT ORDER_mergesort( MSIORDERVIEW *ov, UINT left, UINT right )
78 {
79     UINT r, centre = (left + right)/2, temp, swap = 0, i, j;
80     UINT *array = ov->reorder;
81
82     if( left == right )
83         return ERROR_SUCCESS;
84
85     /* sort the left half */
86     r = ORDER_mergesort( ov, left, centre );
87     if( r != ERROR_SUCCESS )
88         return r;
89
90     /* sort the right half */
91     r = ORDER_mergesort( ov, centre+1, right );
92     if( r != ERROR_SUCCESS )
93         return r;
94
95     for( i=left, j=centre+1; (i<=centre) && (j<=right); i++ )
96     {
97         r = ORDER_compare( ov, array[i], array[j], &swap );
98         if( r != ERROR_SUCCESS )
99             return r;
100         if( swap )
101         { 
102             temp = array[j];
103             memmove( &array[i+1], &array[i], (j-i)*sizeof (UINT) );
104             array[i] = temp;
105             j++;
106             centre++;
107         }
108     }
109
110     return ERROR_SUCCESS;
111 }
112
113 static UINT ORDER_verify( MSIORDERVIEW *ov, UINT num_rows )
114 {
115     UINT i, swap, r;
116
117     for( i=1; i<num_rows; i++ )
118     {
119         r = ORDER_compare( ov, ov->reorder[i-1], ov->reorder[i], &swap );
120         if( r != ERROR_SUCCESS )
121             return r;
122         if( !swap )
123             continue;
124         ERR("Bad order! %d\n", i);
125         return ERROR_FUNCTION_FAILED;
126     }
127
128     return ERROR_SUCCESS;
129 }
130
131 static UINT ORDER_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
132 {
133     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
134
135     TRACE("%p %d %d %p\n", ov, row, col, val );
136
137     if( !ov->table )
138          return ERROR_FUNCTION_FAILED;
139
140     row = ov->reorder[ row ];
141
142     return ov->table->ops->fetch_int( ov->table, row, col, val );
143 }
144
145 static UINT ORDER_execute( struct tagMSIVIEW *view, MSIRECORD *record )
146 {
147     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
148     UINT r, num_rows = 0, i;
149
150     TRACE("%p %p\n", ov, record);
151
152     if( !ov->table )
153          return ERROR_FUNCTION_FAILED;
154
155     r = ov->table->ops->execute( ov->table, record );
156     if( r != ERROR_SUCCESS )
157         return r;
158
159     r = ov->table->ops->get_dimensions( ov->table, &num_rows, NULL );
160     if( r != ERROR_SUCCESS )
161         return r;
162
163     ov->reorder = HeapAlloc( GetProcessHeap(), 0, num_rows*sizeof(UINT) );
164     if( !ov->reorder )
165         return ERROR_FUNCTION_FAILED;
166
167     for( i=0; i<num_rows; i++ )
168         ov->reorder[i] = i;
169
170     r = ORDER_mergesort( ov, 0, num_rows - 1 );
171     if( r != ERROR_SUCCESS )
172         return r;
173
174     r = ORDER_verify( ov, num_rows );
175     if( r != ERROR_SUCCESS )
176         return r;
177
178     return ERROR_SUCCESS;
179 }
180
181 static UINT ORDER_close( struct tagMSIVIEW *view )
182 {
183     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
184
185     TRACE("%p\n", ov );
186
187     if( !ov->table )
188          return ERROR_FUNCTION_FAILED;
189
190     HeapFree( GetProcessHeap(), 0, ov->reorder );
191     ov->reorder = NULL;
192
193     return ov->table->ops->close( ov->table );
194 }
195
196 static UINT ORDER_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
197 {
198     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
199
200     TRACE("%p %p %p\n", ov, rows, cols );
201
202     if( !ov->table )
203          return ERROR_FUNCTION_FAILED;
204
205     return ov->table->ops->get_dimensions( ov->table, rows, cols );
206 }
207
208 static UINT ORDER_get_column_info( struct tagMSIVIEW *view,
209                 UINT n, LPWSTR *name, UINT *type )
210 {
211     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
212
213     TRACE("%p %d %p %p\n", ov, n, name, type );
214
215     if( !ov->table )
216          return ERROR_FUNCTION_FAILED;
217
218     return ov->table->ops->get_column_info( ov->table, n, name, type );
219 }
220
221 static UINT ORDER_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
222 {
223     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
224
225     TRACE("%p %d %ld\n", ov, eModifyMode, hrec );
226
227     if( !ov->table )
228          return ERROR_FUNCTION_FAILED;
229
230     return ov->table->ops->modify( ov->table, eModifyMode, hrec );
231 }
232
233 static UINT ORDER_delete( struct tagMSIVIEW *view )
234 {
235     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
236
237     TRACE("%p\n", ov );
238
239     if( ov->table )
240         ov->table->ops->delete( ov->table );
241
242     HeapFree( GetProcessHeap(), 0, ov->reorder );
243     ov->reorder = NULL;
244
245     msiobj_release( &ov->db->hdr );
246     HeapFree( GetProcessHeap(), 0, ov );
247
248     return ERROR_SUCCESS;
249 }
250
251
252 MSIVIEWOPS order_ops =
253 {
254     ORDER_fetch_int,
255     NULL,
256     NULL,
257     NULL,
258     ORDER_execute,
259     ORDER_close,
260     ORDER_get_dimensions,
261     ORDER_get_column_info,
262     ORDER_modify,
263     ORDER_delete
264 };
265
266 UINT ORDER_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table )
267 {
268     MSIORDERVIEW *ov = NULL;
269     UINT count = 0, r;
270
271     TRACE("%p\n", ov );
272
273     r = table->ops->get_dimensions( table, NULL, &count );
274     if( r != ERROR_SUCCESS )
275     {
276         ERR("can't get table dimensions\n");
277         return r;
278     }
279
280     ov = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
281                     sizeof *ov + sizeof (UINT) * count );
282     if( !ov )
283         return ERROR_FUNCTION_FAILED;
284     
285     /* fill the structure */
286     ov->view.ops = &order_ops;
287     msiobj_addref( &db->hdr );
288     ov->db = db;
289     ov->table = table;
290     ov->reorder = NULL;
291     ov->num_cols = 0;
292     *view = (MSIVIEW*) ov;
293
294     return ERROR_SUCCESS;
295 }
296
297 UINT ORDER_AddColumn( MSIVIEW *view, LPWSTR name )
298 {
299     MSIORDERVIEW *ov = (MSIORDERVIEW*)view;
300     UINT n, count, r;
301     MSIVIEW *table;
302
303     TRACE("%p adding %s\n", ov, debugstr_w( name ) );
304
305     if( ov->view.ops != &order_ops )
306         return ERROR_FUNCTION_FAILED;
307
308     table = ov->table;
309     if( !table )
310         return ERROR_FUNCTION_FAILED;
311     if( !table->ops->get_dimensions )
312         return ERROR_FUNCTION_FAILED;
313     if( !table->ops->get_column_info )
314         return ERROR_FUNCTION_FAILED;
315
316     r = table->ops->get_dimensions( table, NULL, &count );
317     if( r != ERROR_SUCCESS )
318         return r;
319
320     if( ov->num_cols >= count )
321         return ERROR_FUNCTION_FAILED;
322
323     r = VIEW_find_column( table, name, &n );
324     if( r != ERROR_SUCCESS )
325         return r;
326
327     ov->cols[ov->num_cols] = n;
328     TRACE("Ordering by column %s (%d)\n", debugstr_w( name ), n);
329
330     ov->num_cols++;
331
332     return ERROR_SUCCESS;
333 }