1/*
2* $Id: varsub.c,v 1.9 2008/03/20 16:38:34 jms Exp $
3*
4* Revision History
5* ===================
6* $Log: varsub.c,v $
7* Revision 1.9 2008/03/20 16:38:34 jms
8* q14/q15: range correction
9*
10* Revision 1.8 2008/03/20 16:36:47 jms
11* q14/15 format change
12*
13* Revision 1.7 2006/05/31 22:25:21 jms
14* Rework UnifInt calls in varsub to handle lack of PROTO defn in windows
15*
16* Revision 1.6 2006/05/25 22:30:44 jms
17* qgen porting for 32b/64b
18*
19* Revision 1.5 2006/05/25 16:08:52 jms
20* Rework UnifInt call for query 3
21*
22* Revision 1.4 2006/04/26 23:20:05 jms
23* Data type clenaup for qgen
24*
25* Revision 1.3 2005/11/03 14:50:44 jms
26* solaris porting changes
27*
28* Revision 1.2 2005/01/03 20:08:59 jms
29* change line terminations
30*
31* Revision 1.1.1.1 2004/11/24 23:31:47 jms
32* re-establish external server
33*
34* Revision 1.1.1.1 2003/04/03 18:54:21 jms
35* recreation after CVS crash
36*
37* Revision 1.1.1.1 2003/04/03 18:54:21 jms
38* initial checkin
39*
40*
41*/
42#include "config.h"
43#include <stdio.h>
44#ifndef _POSIX_SOURCE
45#include <malloc.h>
46#endif /* POSIX_SOURCE */
47#if (defined(_POSIX_)||!defined(WIN32))
48#include <unistd.h>
49#endif /* WIN32 */
50#include <string.h>
51#include "config.h"
52#include "dss.h"
53#include "tpcd.h"
54#ifdef ADHOC
55#include "adhoc.h"
56extern adhoc_t adhocs[];
57#endif /* ADHOC */
58void permute(long *a, int c, long s);
59#define MAX_PARAM 10 /* maximum number of parameter substitutions in a query */
60
61extern long Seed[];
62extern char **asc_date;
63extern double flt_scale;
64extern distribution q13a, q13b;
65
66long brands[25] = {11,12,13,14,15,21,22,23,24,25,31,32,33,34,35,
67 41,42,43,44,45,51,52,53,54,55};
68long sizes[50] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
69 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
70 41,42,43,44,45,46,47,48,49,50};
71long ccode[25] = {10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34};
72char *defaults[24][11] =
73{
74 {"90", NULL, NULL,
75 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 1 */
76 {"15", "BRASS", "EUROPE",
77 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 2 */
78 {"BUILDING", "1995-03-15", NULL,
79 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 3 */
80 {"1993-07-01", NULL, NULL,
81 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 4 */
82 {"ASIA", "1994-01-01", NULL,
83 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 5 */
84 {"1994-01-01", ".06", "24",
85 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 6 */
86 {"FRANCE", "GERMANY", NULL,
87 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 7 */
88 {"BRAZIL", "AMERICA", "ECONOMY ANODIZED STEEL",
89 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},/* 8 */
90 {"green", NULL, NULL,
91 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 9 */
92 {"1993-10-01", NULL, NULL,
93 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 10 */
94 {"GERMANY", "0.0001", NULL,
95 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 11 */
96 {"MAIL", "SHIP", "1994-01-01",
97 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 12 */
98 {"special", "requests", NULL,
99 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 13 */
100 {"1995-09-01", NULL, NULL,
101 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 14 */
102 {"1996-01-01", NULL, NULL,
103 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 15 */
104 {"Brand#45", "MEDIUM POLISHED", "49",
105 "14","23","45","19","3","36","9", NULL}, /* 16 */
106 {"Brand#23", "MED BOX", NULL,
107 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 17 */
108 {"300", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 18 */
109 {"Brand#12", "Brand#23", "Brand#34", "1", "10", "20", NULL, NULL, NULL, NULL, NULL}, /* 19 */
110 {"forest", "1994-01-01", "CANADA", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 20 */
111 {"SAUDI ARABIA", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* 21 */
112 {"13","31","23", "29", "30", "18", "17", NULL, NULL, NULL, NULL}, /* 22 */
113 {NULL,NULL,NULL,NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* UF1 */
114 {NULL,NULL,NULL,NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* UF2 */
115};
116void
117varsub(int qnum, int vnum, int flags)
118{
119 static char param[11][128];
120 static char formats[23][128];
121 static FILE *lfp = NULL;
122 static int bInit = 0;
123 long *lptr;
124 char *ptr;
125 int i = 0;
126 DSS_HUGE tmp_date, tmp1, tmp2;
127
128 if (!bInit)
129 {
130 sprintf(formats[4], "19%s-%s-01", HUGE_DATE_FORMAT, HUGE_DATE_FORMAT);
131 sprintf(formats[5], "19%s-01-01", HUGE_DATE_FORMAT);
132 sprintf(formats[6], "19%s-01-01", HUGE_DATE_FORMAT);
133 sprintf(formats[7], "0.%s", HUGE_DATE_FORMAT); /* used by q6 */
134 sprintf(formats[10], "19%s-%s-01", HUGE_DATE_FORMAT, HUGE_DATE_FORMAT);
135 sprintf(formats[12], "19%s-01-01", HUGE_DATE_FORMAT);
136 sprintf(formats[14], "19%s-%s-01", HUGE_DATE_FORMAT, HUGE_DATE_FORMAT);
137 sprintf(formats[15], "19%s-%s-01", HUGE_DATE_FORMAT, HUGE_DATE_FORMAT);
138 sprintf(formats[16], "Brand#%s%s", HUGE_FORMAT, HUGE_FORMAT);
139 sprintf(formats[17], "Brand#%s%s", HUGE_FORMAT, HUGE_FORMAT);
140 sprintf(formats[19], "Brand#%s%s", HUGE_FORMAT, HUGE_FORMAT);
141 sprintf(formats[20], "19%s-01-01", HUGE_DATE_FORMAT);
142 bInit = 1;
143 }
144
145 if (vnum == 0)
146 {
147 if ((flags & DFLT) == 0)
148 {
149 switch(qnum)
150 {
151 case 1:
152 sprintf(param[1], HUGE_FORMAT, UnifInt((DSS_HUGE)60,(DSS_HUGE)120,qnum));
153 param[2][0] = '\0';
154 break;
155 case 2:
156 sprintf(param[1], HUGE_FORMAT,
157 UnifInt((DSS_HUGE)P_SIZE_MIN, (DSS_HUGE)P_SIZE_MAX, qnum));
158 pick_str(&p_types_set, qnum, param[3]);
159 ptr = param[3] + (int)strlen(param[3]);
160 while (*(ptr - 1) != ' ') ptr--;
161 strcpy(param[2], ptr);
162 pick_str(&regions, qnum, param[3]);
163 param[4][0] = '\0';
164 break;
165 case 3:
166 pick_str(&c_mseg_set, qnum, param[1]);
167 /*
168 * pick a random offset within the month of march and add the
169 * appropriate magic numbers to position the output functions
170 * at the start of March '95
171 */
172 RANDOM(tmp_date, 0, 30, qnum);
173 strcpy(param[2], *(asc_date + tmp_date + 1155));
174 param[3][0] = '\0';
175 break;
176 case 4:
177 tmp_date = UnifInt((DSS_HUGE)1,(DSS_HUGE)58,qnum);
178 sprintf(param[1],formats[4],
179 93 + tmp_date/12, tmp_date%12 + 1);
180 param[2][0] = '\0';
181 break;
182 case 5:
183 pick_str(&regions, qnum, param[1]);
184 tmp_date = UnifInt((DSS_HUGE)93, (DSS_HUGE)97,qnum);
185 sprintf(param[2], formats[5], tmp_date);
186 param[3][0] = '\0';
187 break;
188 case 6:
189 tmp_date = UnifInt((DSS_HUGE)93,(DSS_HUGE)97,qnum);
190 sprintf(param[1], formats[6], tmp_date);
191 sprintf(param[2], formats[7],
192 UnifInt((DSS_HUGE)2, (DSS_HUGE)9, qnum));
193 sprintf(param[3], HUGE_FORMAT, UnifInt((DSS_HUGE)24, (DSS_HUGE)25, qnum));
194 param[4][0] = '\0';
195 break;
196 case 7:
197 tmp_date = pick_str(&nations2, qnum, param[1]);
198 while (pick_str(&nations2, qnum, param[2]) == tmp_date);
199 param[3][0] = '\0';
200 break;
201 case 8:
202 tmp_date = pick_str(&nations2, qnum, param[1]);
203 tmp_date = nations.list[tmp_date].weight;
204 strcpy(param[2], regions.list[tmp_date].text);
205 pick_str(&p_types_set, qnum, param[3]);
206 param[4][0] = '\0';
207 break;
208 case 9:
209 pick_str(&colors, qnum, param[1]);
210 param[2][0] = '\0';
211 break;
212 case 10:
213 tmp_date = UnifInt((DSS_HUGE)1,(DSS_HUGE)24,qnum);
214 sprintf(param[1],formats[10],
215 93 + tmp_date/12, tmp_date%12 + 1);
216 param[2][0] = '\0';
217 break;
218 case 11:
219 pick_str(&nations2, qnum, param[1]);
220 sprintf(param[2], "%11.10f", Q11_FRACTION / flt_scale );
221 param[3][0] = '\0';
222 break;
223 case 12:
224 tmp_date = pick_str(&l_smode_set, qnum, param[1]);
225 while (tmp_date == pick_str(&l_smode_set, qnum, param[2]));
226 tmp_date = UnifInt((DSS_HUGE)93,(DSS_HUGE)97,qnum);
227 sprintf(param[3], formats[12], tmp_date);
228 param[4][0] = '\0';
229 break;
230 case 13:
231 pick_str(&q13a, qnum, param[1]);
232 pick_str(&q13b, qnum, param[2]);
233 param[3][0] = '\0';
234 break;
235 case 14:
236 tmp_date = UnifInt((DSS_HUGE)0,(DSS_HUGE)59,qnum);
237 sprintf(param[1],formats[14],
238 93 + tmp_date/12, tmp_date%12 + 1);
239 param[2][0] = '\0';
240 break;
241 case 15:
242 tmp_date = UnifInt((DSS_HUGE)0,(DSS_HUGE)57,qnum);
243 sprintf(param[1],formats[15],
244 93 + tmp_date/12, tmp_date%12 + 1);
245 param[2][0] = '\0';
246 break;
247 case 16:
248 tmp1 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
249 tmp2 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
250 sprintf(param[1], formats[16], tmp1, tmp2);
251 pick_str(&p_types_set, qnum, param[2]);
252 ptr = param[2] + (int)strlen(param[2]);
253 while (*(--ptr) != ' ');
254 *ptr = '\0';
255 lptr = &sizes[0];
256 permute(lptr,50,qnum);
257 for (i=3; i <= MAX_PARAM; i++)
258 sprintf(param[i], "%ld", sizes[i - 3]);
259 break;
260 case 17:
261 tmp1 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
262 tmp2 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
263 sprintf(param[1], formats[17], tmp1, tmp2);
264 pick_str(&p_cntr_set, qnum, param[2]);
265 param[3][0] = '\0';
266 break;
267 case 18:
268 sprintf(param[1], HUGE_FORMAT, UnifInt((DSS_HUGE)312, (DSS_HUGE)315, qnum));
269 param[2][0] = '\0';
270 break;
271 case 19:
272 tmp1 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
273 tmp2 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
274 sprintf(param[1], formats[19], tmp1, tmp2);
275 tmp1 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
276 tmp2 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
277 sprintf(param[2], formats[19], tmp1, tmp2);
278 tmp1 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
279 tmp2 = UnifInt((DSS_HUGE)1, (DSS_HUGE)5, qnum);
280 sprintf(param[3], formats[19], tmp1, tmp2);
281 sprintf(param[4], HUGE_FORMAT, UnifInt((DSS_HUGE)1, (DSS_HUGE)10, qnum));
282 sprintf(param[5], HUGE_FORMAT, UnifInt((DSS_HUGE)10, (DSS_HUGE)20, qnum));
283 sprintf(param[6], HUGE_FORMAT, UnifInt((DSS_HUGE)20, (DSS_HUGE)30, qnum));
284 param[7][0] = '\0';
285 break;
286 case 20:
287 pick_str(&colors, qnum, param[1]);
288 tmp_date = UnifInt((DSS_HUGE)93,(DSS_HUGE)97,qnum);
289 sprintf(param[2], formats[20], tmp_date);
290 pick_str(&nations2, qnum, param[3]);
291 param[4][0] = '\0';
292 break;
293 case 21:
294 pick_str(&nations2, qnum, param[1]);
295 param[2][0] = '\0';
296 break;
297 case 22:
298 lptr = &ccode[0];
299 permute(lptr,25, qnum);
300 for (i=0; i <= 7; i++)
301 sprintf(param[i+1], "%ld", 10 + ccode[i]);
302 param[8][0] = '\0';
303 break;
304 case 23:
305 case 24:
306 break;
307 default:
308 fprintf(stderr,
309 "No variable definitions available for query %d\n",
310 qnum);
311 return;
312 }
313 }
314
315 if (flags & LOG)
316 {
317 if (lfp == NULL)
318 {
319 lfp = fopen(lfile, "a");
320 OPEN_CHECK(lfp, lfile);
321 }
322 fprintf(lfp, "%d", qnum);
323 for (i=1; i <= 10; i++)
324 if (flags & DFLT)
325 {
326 if (defaults[qnum - 1][i - 1] == NULL)
327 break;
328 else
329 fprintf(lfp, "\t%s", defaults[qnum - 1][i - 1]);
330 }
331 else
332 {
333 if (param[i][0] == '\0')
334 break;
335 else
336 fprintf(lfp, "\t%s", param[i]);
337 }
338 fprintf(lfp, "\n");
339 }
340 }
341 else
342 {
343 if (flags & DFLT)
344 {
345 /* to allow -d to work at all scale factors */
346 if (qnum == 11 && vnum == 2)
347 fprintf(ofp, "%11.10f", Q11_FRACTION/flt_scale);
348 else
349 if (defaults[qnum - 1][vnum - 1])
350 fprintf(ofp, "%s", defaults[qnum - 1][vnum - 1]);
351 else
352 fprintf(stderr,
353 "Bad default request (q: %d, p: %d)\n",
354 qnum, vnum);
355 }
356 else
357 {
358 if (param[vnum] && vnum <= MAX_PARAM)
359 fprintf(ofp, "%s", param[vnum]);
360 else
361 fprintf(stderr, "Bad parameter request (q: %d, p: %d)\n",
362 qnum, vnum);
363 }
364 }
365 return;
366}
367