vtkWrapTcl.c 23.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkWrapTcl.c
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Will Schroeder's avatar
Will Schroeder committed
10
Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen 
Will Schroeder's avatar
Will Schroeder committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
   of any contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

 * Modified source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Will Schroeder's avatar
Will Schroeder committed
33
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
Will Schroeder's avatar
Will Schroeder committed
34
35
36
37
38
39
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41
42
43

=========================================================================*/

#include <stdio.h>
44
#include <string.h>
45
46
47
48
#include "vtkParse.h"

int numberOfWrappedFunctions = 0;
FunctionInfo *wrappedFunctions[1000];
49
extern FunctionInfo *currentFunction;
50
51


Ken Martin's avatar
Ken Martin committed
52
void output_temp(FILE *fp, int i, int aType, char *Id, int count)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
  /* handle VAR FUNCTIONS */
  if (aType == 5000)
    {
    fprintf(fp,"    vtkTclVoidFuncArg *temp%i = new vtkTclVoidFuncArg;\n",i);
    return;
    }
  
  /* ignore void */
  if (((aType % 10) == 2)&&(!((aType%1000)/100)))
    {
    return;
    }

  /* for const * return types prototype with const */
  if ((i == MAX_ARGS) && (aType%2000 >= 1000))
    {
    fprintf(fp,"    const ");
    }
  else
    {
    fprintf(fp,"    ");
    }

  if ((aType%100)/10 == 1)
    {
    fprintf(fp,"unsigned ");
    }

  switch (aType%10)
    {
    case 1:   fprintf(fp,"float  "); break;
    case 7:   fprintf(fp,"double "); break;
    case 4:   fprintf(fp,"int    "); break;
    case 5:   fprintf(fp,"short  "); break;
    case 6:   fprintf(fp,"long   "); break;
    case 2:     fprintf(fp,"void   "); break;
    case 3:     fprintf(fp,"char   "); break;
    case 9:     fprintf(fp,"%s ",Id); break;
    case 8: return;
    }

Ken Martin's avatar
Ken Martin committed
95
96
97
98
99
100
101
  /* handle array arguements */
  if (count > 1)
    {
    fprintf(fp,"temp%i[%i];\n",i,count);
    return;
    }
  
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  switch ((aType%1000)/100)
    {
    case 1: fprintf(fp, " *"); break; /* act " &" */
    case 2: fprintf(fp, "&&"); break;
    case 3: fprintf(fp, " *"); break;
    case 4: fprintf(fp, "&*"); break;
    case 5: fprintf(fp, "*&"); break;
    case 7: fprintf(fp, "**"); break;
    default: fprintf(fp,"  "); break;
    }
  
  fprintf(fp,"temp%i",i);
  fprintf(fp,";\n");
}

/* when the cpp file doesn't have enough info use the hint file */
void use_hints(FILE *fp)
{
  int  i;

  /* use the hint */
  switch (currentFunction->ReturnType%1000)
    {
    case 301: case 307:  
126
127
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"");
128
129
130
131
132
133
134
135
136
137
      for (i = 0; i < currentFunction->HintSize; i++)
	{
	fprintf(fp,"%%g ");
	}
      fprintf(fp,"\"");
      for (i = 0; i < currentFunction->HintSize; i++)
	{
	fprintf(fp,",temp%i[%i]",MAX_ARGS,i);
	}
      fprintf(fp,");\n");
138
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
139
      break;
Ken Martin's avatar
Ken Martin committed
140
    case 304: case 305: case 306: 
141
142
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"");
143
144
145
146
147
148
149
150
151
152
      for (i = 0; i < currentFunction->HintSize; i++)
	{
	fprintf(fp,"%%i ");
	}
      fprintf(fp,"\"");
      for (i = 0; i < currentFunction->HintSize; i++)
	{
	fprintf(fp,",temp%i[%i]",MAX_ARGS,i);
	}
      fprintf(fp,");\n");
153
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
154
      break;
Ken Martin's avatar
Ken Martin committed
155
    case 313: case 314: case 315: case 316:
156
157
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"");
Ken Martin's avatar
Ken Martin committed
158
159
160
161
162
163
164
165
166
167
      for (i = 0; i < currentFunction->HintSize; i++)
	{
	fprintf(fp,"%%u ");
	}
      fprintf(fp,"\"");
      for (i = 0; i < currentFunction->HintSize; i++)
	{
	fprintf(fp,",temp%i[%i]",MAX_ARGS,i);
	}
      fprintf(fp,");\n");
168
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
Ken Martin's avatar
Ken Martin committed
169
      break;
170
171
172
173
174
175
176
177
    }
}

void return_result(FILE *fp)
{
  switch (currentFunction->ReturnType%1000)
    {
    case 2:
178
      fprintf(fp,"      Tcl_ResetResult(interp);\n"); 
179
      break;
180
181
182
    case 1: case 7: 
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%g\",temp%i);\n",
183
	      MAX_ARGS); 
184
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
185
      break;
186
187
188
    case 4:  
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%i\",temp%i);\n",
189
	      MAX_ARGS); 
190
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
191
192
      break;
    case 5:
193
194
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%hi\",temp%i);\n",
195
	      MAX_ARGS); 
196
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
197
198
      break;
    case 6:
199
200
201
202
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%li\",temp%i);\n",
	      MAX_ARGS);
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
203
      break;
204
205
206
    case 14:
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%u\",temp%i);\n",
207
	      MAX_ARGS); 
208
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
209
      break;
210
211
212
213
214
    case 15: 
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%hu\",temp%i);\n",
	      MAX_ARGS);  
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
215
      break;
216
217
218
219
220
    case 16:  
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%lu\",temp%i);\n",
	      MAX_ARGS);
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
221
222
      break;
    case 13:
223
224
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%hu\",temp%i);\n",
225
	      MAX_ARGS); 
226
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
227
228
      break;
    case 303:
229
      fprintf(fp,"      if (temp%i)\n        {\n        Tcl_SetResult(interp, (char*)temp%i, TCL_VOLATILE);\n",MAX_ARGS,MAX_ARGS); 
230
      fprintf(fp,"        }\n      else\n        {\n");
231
      fprintf(fp,"        Tcl_ResetResult(interp);\n        }\n"); 
232
233
      break;
    case 3:
234
235
      fprintf(fp,"      char tempResult[1024];\n");
      fprintf(fp,"      sprintf(tempResult,\"%%c\",temp%i);\n",
236
	      MAX_ARGS); 
237
      fprintf(fp,"      Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
238
239
240
241
242
243
244
245
246
247
      break;
    case 109:
    case 309:  
      fprintf(fp,"      vtkTclGetObjectFromPointer(interp,(void *)temp%i,%sCommand);\n",MAX_ARGS,currentFunction->ReturnClass);
      break;

    /* handle functions returning vectors */
    /* this is done by looking them up in a hint file */
    case 301: case 307:
    case 304: case 305: case 306:
248
    case 313: case 314: case 315: case 316:      
249
250
251
      use_hints(fp);
      break;
    default:
Bill Lorensen's avatar
Bill Lorensen committed
252
      fprintf(fp,"    Tcl_SetResult(interp, (char *) \"unable to return result.\", TCL_VOLATILE);\n");
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
      break;
    }
}

void handle_return_prototype(FILE *fp)
{
  switch (currentFunction->ReturnType%1000)
    {
    case 109:
    case 309:  
      fprintf(fp,"    int %sCommand(ClientData, Tcl_Interp *, int, char *[]);\n",currentFunction->ReturnClass);
      break;
    }
}

void get_args(FILE *fp, int i)
{
Ken Martin's avatar
Ken Martin committed
270
271
272
273
274
275
276
277
278
279
  int j;
  int start_arg = 2;
  
  /* what arg do we start with */
  for (j = 0; j < i; j++)
    {
    start_arg = start_arg + 
      (currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
    }
  
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  /* handle VAR FUNCTIONS */
  if (currentFunction->ArgTypes[i] == 5000)
    {
    fprintf(fp,"    temp%i->interp = interp;\n",i);
    fprintf(fp,"    temp%i->command = strcpy(new char [strlen(argv[2])+1],argv[2]);\n",i);
    return;
    }

  /* ignore void */
  if (((currentFunction->ArgTypes[i] % 10) == 2)&&
      (!((currentFunction->ArgTypes[i]%1000)/100)))
    {
    return;
    }
  
  switch (currentFunction->ArgTypes[i]%1000)
    {
    case 1: case 7:  
      fprintf(fp,
	      "    if (Tcl_GetDouble(interp,argv[%i],&tempd) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
300
	      start_arg); 
301
302
303
304
      fprintf(fp,"    temp%i = tempd;\n",i);
      break;
    case 4: case 5: case 6: 
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
305
	      start_arg); 
306
307
308
      fprintf(fp,"    temp%i = tempi;\n",i);
      break;
    case 3:
Ken Martin's avatar
Ken Martin committed
309
      fprintf(fp,"    temp%i = *(argv[%i]);\n",i,start_arg);
310
311
312
      break;
    case 13:
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
313
	      start_arg); 
314
315
316
317
      fprintf(fp,"    temp%i = (unsigned char)tempi;\n",i);
      break;
    case 14:
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
318
	      start_arg); 
319
320
321
322
      fprintf(fp,"    temp%i = (unsigned int)tempi;\n",i);
      break;
    case 15:
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
323
	      start_arg); 
324
325
      fprintf(fp,"    temp%i = (unsigned short)tempi;\n",i);
      break;
Ken Martin's avatar
Ken Martin committed
326
327
328
329
330
    case 16:
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
	      start_arg); 
      fprintf(fp,"    temp%i = (unsigned long)tempi;\n",i);
      break;
331
    case 303:
Ken Martin's avatar
Ken Martin committed
332
      fprintf(fp,"    temp%i = argv[%i];\n",i,start_arg);
333
334
335
      break;
    case 109:
    case 309:
336
      fprintf(fp,"    temp%i = (%s *)(vtkTclGetPointerFromObject(argv[%i],(char *) \"%s\",interp,error));\n",i,currentFunction->ArgClasses[i],start_arg,
337
338
339
340
341
	      currentFunction->ArgClasses[i]);
      break;
    case 2:    
    case 9:
      break;
Ken Martin's avatar
Ken Martin committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    default:
      if (currentFunction->ArgCounts[i] > 1)
	{
	for (j = 0; j < currentFunction->ArgCounts[i]; j++)
	  {
	  switch (currentFunction->ArgTypes[i]%100)
	    {
	    case 1: case 7:  
	      fprintf(fp,
		      "    if (Tcl_GetDouble(interp,argv[%i],&tempd) != TCL_OK) error = 1;\n",
		      start_arg); 
	      fprintf(fp,"    temp%i[%i] = tempd;\n",i,j);
	      break;
	    case 4: case 5: case 6: 
	      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
		      start_arg); 
	      fprintf(fp,"    temp%i[%i] = tempi;\n",i,j);
	      break;
	    case 3:
	      fprintf(fp,"    temp%i[%i] = *(argv[%i]);\n",i,j,start_arg);
	      break;
	    case 13:
	      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
		      start_arg); 
	      fprintf(fp,"    temp%i[%i] = (unsigned char)tempi;\n",i,j);
	      break;
	    case 14:
	      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
		      start_arg); 
	      fprintf(fp,"    temp%i[%i] = (unsigned int)tempi;\n",i,j);
	      break;
	    case 15:
	      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
		      start_arg); 
	      fprintf(fp,"    temp%i[%i] = (unsigned short)tempi;\n",i,j);
	      break;
Ken Martin's avatar
Ken Martin committed
378
379
380
381
382
	    case 16:
	      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
		      start_arg); 
	      fprintf(fp,"    temp%i[%i] = (unsigned long)tempi;\n",i,j);
	      break;
Ken Martin's avatar
Ken Martin committed
383
384
385
386
387
	    }
	  start_arg++;
	  }
	}
      
388
389
390
391
392
393
394
395
396
    }
}

void outputFunction(FILE *fp, FileInfo *data)
{
  int i;
  int args_ok = 1;
 
  /* some functions will not get wrapped no matter what else */
397
  if (currentFunction->IsOperator || 
398
      currentFunction->ArrayFailure ||
Ken Martin's avatar
Ken Martin committed
399
400
      !currentFunction->IsPublic ||
      !currentFunction->Name) 
401
402
403
404
405
406
407
408
    {
    return;
    }
  
  /* check to see if we can handle the args */
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
    if ((currentFunction->ArgTypes[i]%10) == 8) args_ok = 0;
Ken Martin's avatar
Ken Martin committed
409
    /* if its a pointer arg make sure we have the ArgCount */
410
411
412
    if ((currentFunction->ArgTypes[i]%1000 >= 100) &&
	(currentFunction->ArgTypes[i]%1000 != 303)&&
	(currentFunction->ArgTypes[i]%1000 != 309)&&
Ken Martin's avatar
Ken Martin committed
413
414
415
416
417
418
419
420
	(currentFunction->ArgTypes[i]%1000 != 109)) 
      {
      if (currentFunction->NumberOfArguments > 1 ||
	  !currentFunction->ArgCounts[i])
	{
	args_ok = 0;
	}
      }
421
422
423
    if ((currentFunction->ArgTypes[i]%100 >= 10)&&
	(currentFunction->ArgTypes[i] != 13)&&
	(currentFunction->ArgTypes[i] != 14)&&
Ken Martin's avatar
Ken Martin committed
424
425
	(currentFunction->ArgTypes[i] != 15)&&
	(currentFunction->ArgTypes[i] != 16)) args_ok = 0;
426
427
428
429
430
431
432
433
434
    }
  if ((currentFunction->ReturnType%10) == 8) args_ok = 0;
  if (((currentFunction->ReturnType%1000)/100 != 3)&&
      ((currentFunction->ReturnType%1000)/100 != 1)&&
      ((currentFunction->ReturnType%1000)/100)) args_ok = 0;
  if (currentFunction->NumberOfArguments && 
      (currentFunction->ArgTypes[0] == 5000)
      &&(currentFunction->NumberOfArguments != 1)) args_ok = 0;

Ken Martin's avatar
Ken Martin committed
435
436
437
438
439
440
  /* we can't handle void * return types */
  if ((currentFunction->ReturnType%1000) == 302) 
    {
    args_ok = 0;
    }
  
441
442
443
444
445
  /* watch out for functions that dont have enough info */
  switch (currentFunction->ReturnType%1000)
    {
    case 301: case 307:
    case 304: case 305: case 306:
446
    case 313: case 314: case 315: case 316:
447
448
449
450
451
452
453
454
455
      args_ok = currentFunction->HaveHint;
      break;
    }
  
  /* if the args are OK and it is not a constructor or destructor */
  if (args_ok && 
      strcmp(data->ClassName,currentFunction->Name) &&
      strcmp(data->ClassName,currentFunction->Name + 1))
    {
Ken Martin's avatar
Ken Martin committed
456
457
458
459
460
461
462
463
464
    int required_args = 0;
    
    /* calc the total required args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      required_args = required_args + 
	(currentFunction->ArgCounts[i] ? currentFunction->ArgCounts[i] : 1);
      }
    
465
    fprintf(fp,"  if ((!strcmp(\"%s\",argv[1]))&&(argc == %i))\n    {\n",
Ken Martin's avatar
Ken Martin committed
466
	    currentFunction->Name, required_args + 2);
467
468
469
470
471
    
    /* process the args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      output_temp(fp, i, currentFunction->ArgTypes[i],
Ken Martin's avatar
Ken Martin committed
472
473
		  currentFunction->ArgClasses[i], 
		  currentFunction->ArgCounts[i]);
474
475
      }
    output_temp(fp, MAX_ARGS,currentFunction->ReturnType,
Ken Martin's avatar
Ken Martin committed
476
		currentFunction->ReturnClass, 0);
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    handle_return_prototype(fp);
    fprintf(fp,"    error = 0;\n\n");
    
    /* now get the required args from the stack */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      get_args(fp,i);
      }
    
    fprintf(fp,"    if (!error)\n      {\n");
    switch (currentFunction->ReturnType%1000)
      {
      case 2:
	fprintf(fp,"      op->%s(",currentFunction->Name);
	break;
      case 109:
	fprintf(fp,"      temp%i = &(op)->%s(",MAX_ARGS,currentFunction->Name);
	break;
      default:
	fprintf(fp,"      temp%i = (op)->%s(",MAX_ARGS,currentFunction->Name);
      }
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      if (i)
	{
	fprintf(fp,",");
	}
      if (currentFunction->ArgTypes[i] == 109)
	{
	fprintf(fp,"*(temp%i)",i);
	}
      else if (currentFunction->ArgTypes[i] == 5000)
	{
	fprintf(fp,"vtkTclVoidFunc,(void *)temp%i",i);
	}
      else
	{
	fprintf(fp,"temp%i",i);
	}
      }
    fprintf(fp,");\n");
    if (currentFunction->NumberOfArguments && 
	(currentFunction->ArgTypes[0] == 5000))
      {
      fprintf(fp,"      op->%sArgDelete(vtkTclVoidFuncArgDelete);\n",
	      currentFunction->Name);
      }
    return_result(fp);
    fprintf(fp,"      return TCL_OK;\n      }\n");
    fprintf(fp,"    }\n");
Ken Martin's avatar
Ken Martin committed
527
    
528
529
530
531
532
533
534
535
    wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
    numberOfWrappedFunctions++;
    }
}

/* print the parsed structures */
void vtkParseOutput(FILE *fp, FileInfo *data)
{
536
  int i,j;
537
538
  
  fprintf(fp,"// tcl wrapper for %s object\n//\n",data->ClassName);
Will Schroeder's avatar
Will Schroeder committed
539
  fprintf(fp,"#include \"vtkSystemIncludes.h\"\n");
540
541
542
543
  fprintf(fp,"#include \"%s.h\"\n\n",data->ClassName);
  fprintf(fp,"#include \"vtkTclUtil.h\"\n");
  if (data->IsConcrete)
    {
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    if (strcmp(data->ClassName, "vtkRenderWindowInteractor") == 0)
      {
      fprintf(fp,"#ifndef _WIN32\n");
      fprintf(fp,"#include \"vtkXRenderWindowTclInteractor.h\"\n");
      fprintf(fp,"#endif\n");

      fprintf(fp,"\nClientData %sNewCommand()\n{\n",data->ClassName);

      fprintf(fp,"#ifndef _WIN32\n");
      fprintf(fp,"  %s *temp = vtkXRenderWindowTclInteractor::New();\n",
	      data->ClassName);
      fprintf(fp,"#else\n");
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
      fprintf(fp,"#endif\n");
      fprintf(fp,"  return ((ClientData)temp);\n}\n\n");
      }
    else
      {
      fprintf(fp,"\nClientData %sNewCommand()\n{\n",data->ClassName);
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
      fprintf(fp,"  return ((ClientData)temp);\n}\n\n");
      }
566
567
568
569
570
571
572
573
    }
  
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    fprintf(fp,"int %sCppCommand(%s *op, Tcl_Interp *interp,\n             int argc, char *argv[]);\n",data->SuperClasses[i],data->SuperClasses[i]);
    }
  fprintf(fp,"int VTKTCL_EXPORT %sCppCommand(%s *op, Tcl_Interp *interp,\n             int argc, char *argv[]);\n",data->ClassName,data->ClassName);
  fprintf(fp,"\nint VTKTCL_EXPORT %sCommand(ClientData cd, Tcl_Interp *interp,\n             int argc, char *argv[])\n{\n",data->ClassName);
574
  fprintf(fp,"  if ((argc == 2)&&(!strcmp(\"Delete\",argv[1]))&& !vtkTclInDelete(interp))\n    {\n");
575
576
  fprintf(fp,"    Tcl_DeleteCommand(interp,argv[0]);\n");
  fprintf(fp,"    return TCL_OK;\n    }\n");
577
  fprintf(fp,"   return %sCppCommand((%s *)(((vtkTclCommandArgStruct *)cd)->Pointer),interp, argc, argv);\n}\n",data->ClassName,data->ClassName);
578
579
580
581
582
583
  
  fprintf(fp,"\nint VTKTCL_EXPORT %sCppCommand(%s *op, Tcl_Interp *interp,\n             int argc, char *argv[])\n{\n",data->ClassName,data->ClassName);
  fprintf(fp,"  int    tempi;\n");
  fprintf(fp,"  double tempd;\n");
  fprintf(fp,"  static char temps[80];\n");
  fprintf(fp,"  int    error;\n\n");
584
585
586
587
  fprintf(fp,"  error = 0; error = error;\n");
  fprintf(fp,"  tempi = 0; tempi = tempi;\n");
  fprintf(fp,"  tempd = 0; tempd = tempd;\n");
  fprintf(fp,"  temps[0] = 0; temps[0] = temps[0];\n\n");
588

589
  fprintf(fp,"  if (argc < 2)\n    {\n    Tcl_SetResult(interp, (char *) \"Could not find requested method.\", TCL_VOLATILE);\n    return TCL_ERROR;\n    }\n");
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607

  /* stick in the typecasting and delete functionality here */
  fprintf(fp,"  if (!interp)\n    {\n");
  fprintf(fp,"    if (!strcmp(\"DoTypecasting\",argv[0]))\n      {\n");
  fprintf(fp,"      if (!strcmp(\"%s\",argv[1]))\n        {\n",
	  data->ClassName);
  fprintf(fp,"        argv[2] = (char *)((void *)op);\n");
  fprintf(fp,"        return TCL_OK;\n        }\n");

  /* check our superclasses */
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    fprintf(fp,"      if (%sCppCommand((%s *)op,interp,argc,argv) == TCL_OK)\n        {\n",
	    data->SuperClasses[i],data->SuperClasses[i]);
    fprintf(fp,"        return TCL_OK;\n        }\n");      
    }
  fprintf(fp,"      }\n    return TCL_ERROR;\n    }\n\n");
  
Ken Martin's avatar
Ken Martin committed
608
609
610
611
612
  /* add the GetSuperClassName */
  if (data->NumberOfSuperClasses)
    {
    fprintf(fp,"  if (!strcmp(\"GetSuperClassName\",argv[1]))\n");
    fprintf(fp,"    {\n");
613
    fprintf(fp,"    Tcl_SetResult(interp,(char *) \"%s\", TCL_VOLATILE);\n",data->SuperClasses[0]);
Ken Martin's avatar
Ken Martin committed
614
615
616
    fprintf(fp,"    return TCL_OK;\n");
    fprintf(fp,"    }\n\n");      
    }
617
618
619
620
621
622
623
624
625
626
  
  /* insert function handling code here */
  for (i = 0; i < data->NumberOfFunctions; i++)
    {
    currentFunction = data->Functions + i;
    outputFunction(fp, data);
    }
  
  /* add the ListInstances method */
  fprintf(fp,"\n  if (!strcmp(\"ListInstances\",argv[1]))\n    {\n");
Jim Miller's avatar
Jim Miller committed
627
  fprintf(fp,"    vtkTclListInstances(interp,(ClientData)%sCommand);\n",data->ClassName);
628
629
630
631
632
633
634
635
636
637
638
639
  fprintf(fp,"    return TCL_OK;\n    }\n");
  
  /* add the ListMethods method */
  fprintf(fp,"\n  if (!strcmp(\"ListMethods\",argv[1]))\n    {\n");
  /* recurse up the tree */
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    fprintf(fp,"    %sCppCommand(op,interp,argc,argv);\n",
	    data->SuperClasses[i]);
    }
  /* now list our methods */
  fprintf(fp,"    Tcl_AppendResult(interp,\"Methods from %s:\\n\",NULL);\n",data->ClassName);
Ken Martin's avatar
Ken Martin committed
640
  fprintf(fp,"    Tcl_AppendResult(interp,\"  GetSuperClassName\\n\",NULL);\n");
641
642
  for (i = 0; i < numberOfWrappedFunctions; i++)
    {
643
644
    int numArgs = 0;

645
    currentFunction = wrappedFunctions[i];
646
647
648
649
650
651
652
653
654
    	
    /* calc the total required args */
    for (j = 0; j < currentFunction->NumberOfArguments; j++)
      {
      numArgs = numArgs + 
	(currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
      }

    if (numArgs > 1)
655
656
      {
      fprintf(fp,"    Tcl_AppendResult(interp,\"  %s\\t with %i args\\n\",NULL);\n",
657
	      currentFunction->Name, numArgs);
658
      }
659
    if (numArgs == 1)
660
661
662
663
      {
	  fprintf(fp,"    Tcl_AppendResult(interp,\"  %s\\t with 1 arg\\n\",NULL);\n",
		  currentFunction->Name);
      }
664
    if (numArgs == 0)
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
      {
      fprintf(fp,"    Tcl_AppendResult(interp,\"  %s\\n\",NULL);\n",
	      currentFunction->Name);
      }
    }
  fprintf(fp,"    return TCL_OK;\n    }\n");
  
  /* try superclasses */
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    fprintf(fp,"\n  if (%sCppCommand((%s *)op,interp,argc,argv) == TCL_OK)\n",
	    data->SuperClasses[i], data->SuperClasses[i]);
    fprintf(fp,"    {\n    return TCL_OK;\n    }\n");
    }
  
  /* add the default print method to Object */
  if (!strcmp("vtkObject",data->ClassName))
    {
    fprintf(fp,"  if ((!strcmp(\"Print\",argv[1]))&&(argc == 2))\n    {\n");
684
    fprintf(fp,"    ostrstream buf;\n");
685
686
687
688
689
    fprintf(fp,"    op->Print(buf);\n");
    fprintf(fp,"    buf.put('\\0');\n");
    fprintf(fp,"    Tcl_SetResult(interp,buf.str(),TCL_VOLATILE);\n");
    fprintf(fp,"    delete buf.str();\n");
    fprintf(fp,"    return TCL_OK;\n    }\n");
690
691
692
693

    fprintf(fp,"  if ((!strcmp(\"AddObserver\",argv[1]))&&(argc == 4))\n    {\n");
    fprintf(fp,"    vtkTclCommand *cbc = new vtkTclCommand;\n");
    fprintf(fp,"    cbc->SetInterp(interp);\n");
694
    fprintf(fp,"    cbc->SetStringCommand(argv[3]);\n");
695
696
697
698
    
    fprintf(fp,"    unsigned long      temp20;\n");
    fprintf(fp,"    temp20 = op->AddObserver(argv[2],cbc);\n");
    fprintf(fp,"    char tempResult[1024];\n");
Bill Lorensen's avatar
Bill Lorensen committed
699
    fprintf(fp,"    sprintf(tempResult,\"%%li\",temp20);\n");
700
701
    fprintf(fp,"    Tcl_SetResult(interp,tempResult,TCL_VOLATILE);\n");
    fprintf(fp,"    return TCL_OK;\n    }\n");
702
703
704
705
706
707
    }
  fprintf(fp,"\n  if ((argc >= 2)&&(!strstr(interp->result,\"Object named:\")))\n    {\n");
  fprintf(fp,"    char temps2[256];\n    sprintf(temps2,\"Object named: %%s, could not find requested method: %%s\\nor the method was called with incorrect arguments.\\n\",argv[0],argv[1]);\n    Tcl_AppendResult(interp,temps2,NULL);\n    }\n");
  fprintf(fp,"  return TCL_ERROR;\n}\n");

}