vtkWrapTclInit.c 6.84 KB
Newer Older
1 2 3 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Ken Martin's avatar
Ken Martin committed
5
char *Capitalized(const char *input)
6
{
Ken Martin's avatar
Ken Martin committed
7
  char *result;
8
  size_t i;
Andy Cedilnik's avatar
Andy Cedilnik committed
9

Ken Martin's avatar
Ken Martin committed
10
  result = strdup(input);
11
  if (result[0] > 90)
Ken Martin's avatar
Ken Martin committed
12 13 14 15 16 17 18 19 20 21
    {
    result[0] -= 32;
    }
  for (i = 1; i < strlen(result); i++)
    {
    if ((result[i] > 64)&&(result[i] < 91))
      {
      result[i] += 32;
      }
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
22

Ken Martin's avatar
Ken Martin committed
23 24 25 26
  return result;
}

/* this roputine creates the init file */
27
static void CreateInitFile(const char *libName,
28
                           int numClasses, char **classes,
29 30
                           int numCommands, char **commands,
                           const char *version,
31
                           FILE *fout)
Ken Martin's avatar
Ken Martin committed
32 33 34
{
  /* we have to make sure that the name is the correct case */
  char *kitName = Capitalized(libName);
35
  int i;
Ken Martin's avatar
Ken Martin committed
36 37 38 39
  char **capcommands = (char **)malloc(numCommands*sizeof(char *));

  /* capitalize commands just once */
  for (i = 0; i < numCommands; i++)
40
    {
Ken Martin's avatar
Ken Martin committed
41
    capcommands[i] = Capitalized(commands[i]);
42
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
43

Ken Martin's avatar
Ken Martin committed
44 45 46 47 48 49
  fprintf(fout,"#include \"vtkTclUtil.h\"\n");
  fprintf(fout,"#include \"vtkVersion.h\"\n");
  fprintf(fout,"#define VTK_TCL_TO_STRING(x) VTK_TCL_TO_STRING0(x)\n");
  fprintf(fout,"#define VTK_TCL_TO_STRING0(x) #x\n");

  fprintf(fout,
Andy Cedilnik's avatar
Andy Cedilnik committed
50 51
    "extern \"C\"\n"
    "{\n"
52
    "#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)\n"
Andy Cedilnik's avatar
Andy Cedilnik committed
53 54 55 56 57 58
    "  typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, CONST84 char *[]);\n"
    "#else\n"
    "  typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, char *[]);\n"
    "#endif\n"
    "}\n"
    "\n");
59

60
  for (i = 0; i < numClasses; i++)
Ken Martin's avatar
Ken Martin committed
61
    {
62
    fprintf(fout,"int %s_TclCreate(Tcl_Interp *interp);\n",classes[i]);
Ken Martin's avatar
Ken Martin committed
63
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
64

65
  if (!strcmp(kitName,"Vtkcommoncoretcl"))
66
    {
Ken Martin's avatar
Ken Martin committed
67
    fprintf(fout,"int vtkCreateCommand(ClientData cd, Tcl_Interp *interp,\n"
Andy Cedilnik's avatar
Andy Cedilnik committed
68
      "               int argc, char *argv[]);\n");
69 70 71
    fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
    fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
    fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
72 73 74
    }
  else
    {
Ken Martin's avatar
Ken Martin committed
75 76 77
    fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
    fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
    fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
78
    }
79
  fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
Ken Martin's avatar
Ken Martin committed
80
  fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
81

Ken Martin's avatar
Ken Martin committed
82 83 84
  for (i = 0; i < numCommands; i++)
    {
    fprintf(fout,
Andy Cedilnik's avatar
Andy Cedilnik committed
85 86
      "\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
      capcommands[i]);
Ken Martin's avatar
Ken Martin committed
87
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
88

Ken Martin's avatar
Ken Martin committed
89
  fprintf(fout,
Andy Cedilnik's avatar
Andy Cedilnik committed
90
    "\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n",
Ken Martin's avatar
Ken Martin committed
91 92
    kitName);
  fprintf(fout,
Andy Cedilnik's avatar
Andy Cedilnik committed
93
    "\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
Ken Martin's avatar
Ken Martin committed
94
    kitName);
Andy Cedilnik's avatar
Andy Cedilnik committed
95

96
  /* create an extern ref to the generic delete function */
Ken Martin's avatar
Ken Martin committed
97
  fprintf(fout,"\nextern void vtkTclGenericDeleteObject(ClientData cd);\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
98

99
  if (!strcmp(kitName,"Vtkcommoncoretcl"))
Ken Martin's avatar
Ken Martin committed
100 101
    {
    fprintf(fout,
Andy Cedilnik's avatar
Andy Cedilnik committed
102
      "extern \"C\"\n{\nvoid vtkCommonDeleteAssocData(ClientData cd)\n");
Ken Martin's avatar
Ken Martin committed
103 104 105 106
    fprintf(fout,"  {\n");
    fprintf(fout,"  vtkTclInterpStruct *tis = static_cast<vtkTclInterpStruct*>(cd);\n");
    fprintf(fout,"  delete tis;\n  }\n}\n");
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
107

108
  /* the main declaration */
Ken Martin's avatar
Ken Martin committed
109
  fprintf(fout,
Andy Cedilnik's avatar
Andy Cedilnik committed
110
    "\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
Ken Martin's avatar
Ken Martin committed
111
  fprintf(fout,"  return %s_Init(interp);\n}\n",kitName);
Andy Cedilnik's avatar
Andy Cedilnik committed
112

Ken Martin's avatar
Ken Martin committed
113
  fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
Andy Cedilnik's avatar
Andy Cedilnik committed
114
    kitName);
115
  if (!strcmp(kitName,"Vtkcommoncoretcl"))
116
    {
Ken Martin's avatar
Ken Martin committed
117 118 119
    fprintf(fout,
      "  vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
    fprintf(fout,
120
      "  info->Number = 0; info->InDelete = 0; info->DebugOn = 0; info->DeleteExistingObjectOnNew = 0;\n");
Ken Martin's avatar
Ken Martin committed
121 122 123 124 125 126 127 128 129
    fprintf(fout,"\n");
    fprintf(fout,"\n");
    fprintf(fout,
      "  Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
    fprintf(fout,
      "  Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
    fprintf(fout,
      "  Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
    fprintf(fout,
130
      "  Tcl_SetAssocData(interp,(char *)(\"vtk\"),NULL,reinterpret_cast<ClientData *>(info));\n");
Ken Martin's avatar
Ken Martin committed
131
    fprintf(fout,
132
      "  Tcl_CreateExitHandler(vtkCommonDeleteAssocData,reinterpret_cast<ClientData *>(info));\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
133

134
    /* create special vtkCommand command */
135
    fprintf(fout,"  Tcl_CreateCommand(interp,(char *)(\"vtkCommand\"),\n"
Andy Cedilnik's avatar
Andy Cedilnik committed
136
      "                    reinterpret_cast<vtkTclCommandType>(vtkCreateCommand),\n"
137
      "                    static_cast<ClientData *>(NULL), NULL);\n\n");
138
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
139

Ken Martin's avatar
Ken Martin committed
140
  for (i = 0; i < numCommands; i++)
141
    {
Ken Martin's avatar
Ken Martin committed
142
    fprintf(fout,"  %s_Init(interp);\n", capcommands[i]);
143
    }
Ken Martin's avatar
Ken Martin committed
144
  fprintf(fout,"\n");
145

146
  for (i = 0; i < numClasses; i++)
Ken Martin's avatar
Ken Martin committed
147
    {
148
    fprintf(fout,"  %s_TclCreate(interp);\n",classes[i]);
Ken Martin's avatar
Ken Martin committed
149
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
150

Ken Martin's avatar
Ken Martin committed
151
  fprintf(fout,"  char pkgName[]=\"%s\";\n", libName);
152 153 154 155 156 157 158 159 160 161
  if (version && *version)
    {
    fprintf(fout,"  char pkgVers[]=VTK_TCL_TO_STRING(%s);\n", version);
    }
  else
    {
    fprintf(fout,"  char pkgVers[]=VTK_TCL_TO_STRING(VTK_MAJOR_VERSION)"
            " \".\" "
            "VTK_TCL_TO_STRING(VTK_MINOR_VERSION);\n");
    }
Ken Martin's avatar
Ken Martin committed
162 163 164 165 166 167 168 169 170
  fprintf(fout,"  Tcl_PkgProvide(interp, pkgName, pkgVers);\n");
  fprintf(fout,"  return TCL_OK;\n}\n");

  for ( i = 0; i < numCommands; i++ )
    {
    free(capcommands[i]);
    }
  free(capcommands);
  free(kitName);
171
}
172 173 174 175


int main(int argc,char *argv[])
{
176
  FILE *file;
Ken Martin's avatar
Ken Martin committed
177
  FILE *fout;
178
  int numClasses = 0;
Ken Martin's avatar
Ken Martin committed
179 180 181
  int numCommands = 0;
  char libName[250];
  char tmpVal[250];
182
  char *classes[4000];
Ken Martin's avatar
Ken Martin committed
183
  char *commands[4000];
184
  char version[4000] = {'\0'};
Andy Cedilnik's avatar
Andy Cedilnik committed
185

186 187
  if (argc < 3)
    {
Ken Martin's avatar
Ken Martin committed
188 189
    fprintf(stderr,"Usage: %s input_file output_file\n",argv[0]);
    return 1;
190
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
191

Ken Martin's avatar
Ken Martin committed
192
  file = fopen(argv[1],"r");
193
  if (!file)
194
    {
Ken Martin's avatar
Ken Martin committed
195 196
    fprintf(stderr,"Input file %s could not be opened\n",argv[1]);
    return 1;
197
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
198

Ken Martin's avatar
Ken Martin committed
199 200
  fout = fopen(argv[2],"w");
  if (!fout)
201
    {
202
    fprintf(stderr,"Error opening output file\n");
203
    fclose(file);
Ken Martin's avatar
Ken Martin committed
204 205 206 207
    return 1;
    }

  /* read the info from the file */
208 209 210 211 212 213 214
  if (fscanf(file,"%s",libName) != 1)
    {
    fprintf(stderr,"Error getting libName\n");
    fclose(file);
    fclose(fout);
    return 1;
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
215

Ken Martin's avatar
Ken Martin committed
216 217 218
  /* read in the classes and commands */
  while (fscanf(file,"%s",tmpVal) != EOF)
    {
219
    if (!strcmp(tmpVal,"COMMAND"))
220
      {
221 222 223 224 225 226 227
      if (fscanf(file,"%s",tmpVal) != 1)
        {
        fprintf(stderr,"Error getting command\n");
        fclose(file);
        fclose(fout);
        return 1;
        }
Ken Martin's avatar
Ken Martin committed
228 229
      commands[numCommands] = strdup(tmpVal);
      numCommands++;
230
      }
231 232
    else if (!strcmp(tmpVal,"VERSION"))
      {
233 234 235 236 237 238 239
      if (fscanf(file,"%s",version) != 1)
        {
        fprintf(stderr,"Error getting version\n");
        fclose(file);
        fclose(fout);
        return 1;
        }
240 241 242
      }
    else
      {
243 244
      classes[numClasses] = strdup(tmpVal);
      numClasses++;
245
      }
246
    }
Ken Martin's avatar
Ken Martin committed
247 248
  /* close the file */
  fclose(file);
Andy Cedilnik's avatar
Andy Cedilnik committed
249

250
  CreateInitFile(libName, numClasses, classes, numCommands, commands, version, fout);
Ken Martin's avatar
Ken Martin committed
251
  fclose(fout);
Andy Cedilnik's avatar
Andy Cedilnik committed
252

253 254
  return 0;
}