TclDatabaseCommands.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.8 $
00022 // $Date: 2005/07/25 18:05:02 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/database/TclDatabaseCommands.cpp,v $
00024                                                                         
00025                                                                         
00026 // Written: fmk 
00027 // Created: 03/00
00028 // Revision: A
00029 //
00030 // Description: This file contains the function that is invoked
00031 // by the interpreter when the comand 'database' is invoked by the 
00032 // user.
00033 //
00034 // What: "@(#) commands.C, revA"
00035 
00036 #include <tcl.h>
00037 #include <tk.h>
00038 
00039 #include <OPS_Globals.h>
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 
00044 
00045 #include <Domain.h>
00046 #include <EquiSolnAlgo.h>
00047 
00048 // known databases
00049 #include <FileDatastore.h>
00050 
00051 // linked list of struct for other types of
00052 // databases that can be added dynamically
00053 
00054 #include <packages.h>
00055 
00056 typedef struct databasePackageCommand {
00057   char *funcName;
00058   int (*funcPtr)(ClientData clientData, 
00059                  Tcl_Interp *interp,  
00060                  int argc, 
00061                  TCL_Char **argv, 
00062                  Domain*, 
00063                  FEM_ObjectBroker *,
00064                  FE_Datastore **); 
00065   struct databasePackageCommand *next;
00066 } DatabasePackageCommand;
00067 
00068 
00069 // static variables
00070 static DatabasePackageCommand *theDatabasePackageCommands = NULL;
00071 static bool createdDatabaseCommands = false;
00072 
00073 
00074 int 
00075 save(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv);
00076 
00077 int 
00078 restore(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv);
00079 
00080 extern FE_Datastore *theDatabase;
00081 
00082 int
00083 TclAddDatabase(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv, 
00084                Domain &theDomain, 
00085                FEM_ObjectBroker &theBroker)
00086 {
00087   if (createdDatabaseCommands == false) {
00088 
00089     // create the commands to commit and reset
00090     Tcl_CreateCommand(interp, "save", save,
00091                       (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);    
00092     Tcl_CreateCommand(interp, "restore", restore,
00093                       (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);        
00094 
00095     createdDatabaseCommands = true;
00096   }
00097 
00098   // make sure at least one other argument to contain integrator
00099   if (argc < 2) {
00100     opserr << "WARNING need to specify a Database type; valid type File, MySQL, BerkeleyDB \n";
00101     return TCL_ERROR;
00102   }    
00103 
00104   //
00105   // check argv[1] for type of Database, parse in rest of arguments
00106   // needed for the type of Database, create the object and add to Domain
00107   //
00108 
00109   // a File Database
00110   if (strcmp(argv[1],"File") == 0) {
00111     if (argc < 3) {
00112       opserr << "WARNING database File fileName? ";
00113       return TCL_ERROR;
00114     }    
00115 
00116     // delete the old database
00117     if (theDatabase != 0)
00118       delete theDatabase;
00119 
00120     theDatabase = new FileDatastore(argv[2], theDomain, theBroker);
00121     // check we instantiated a database .. if not ran out of memory
00122     if (theDatabase == 0) {
00123       opserr << "WARNING ran out of memory - database File " << argv[2] << endln;
00124       return TCL_ERROR;
00125     } 
00126     
00127     return TCL_OK;
00128   } else {
00129 
00130     //
00131     // maybe a database package
00132     //
00133     
00134     // try existing loaded packages
00135     
00136     DatabasePackageCommand *dataCommands = theDatabasePackageCommands;
00137     bool found = false;
00138     while (dataCommands != NULL && found == false) {
00139       if (strcmp(argv[1], dataCommands->funcName) == 0) {
00140         int result = (*(dataCommands->funcPtr))(clientData, interp, argc, argv, &theDomain, &theBroker, &theDatabase);
00141         return result;
00142       } else
00143         dataCommands = dataCommands->next;
00144     }
00145     
00146     // load new package
00147 
00148     void *libHandle;
00149     int (*funcPtr)(ClientData clientData, Tcl_Interp *interp,  int argc, 
00150                    TCL_Char **argv, Domain*, FEM_ObjectBroker *, FE_Datastore **);       
00151     int databaseNameLength = strlen(argv[1]);
00152     char *tclFuncName = new char[databaseNameLength+12];
00153     strcpy(tclFuncName, "TclCommand_");
00154     strcpy(&tclFuncName[11], argv[1]);    
00155     
00156     int res = getLibraryFunction(argv[1], tclFuncName, &libHandle, (void **)&funcPtr);
00157     
00158     if (res == 0) {
00159       char *databaseName = new char[databaseNameLength+1];
00160       strcpy(databaseName, argv[1]);
00161       DatabasePackageCommand *theDataCommand = new DatabasePackageCommand;
00162       theDataCommand->funcPtr = funcPtr;
00163       theDataCommand->funcName = databaseName;  
00164       theDataCommand->next = theDatabasePackageCommands;
00165       theDatabasePackageCommands = theDataCommand;
00166       
00167       int result = (*funcPtr)(clientData, interp,
00168                               argc, 
00169                               argv,
00170                               &theDomain, 
00171                               &theBroker,
00172                               &theDatabase);    
00173       return result;
00174     }
00175   }
00176   opserr << "WARNING No database type exists ";
00177   opserr << "for database of type:" << argv[1] << "valid database type File\n";
00178 
00179   return TCL_ERROR;
00180 }    
00181 
00182 int 
00183 save(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv)
00184 {
00185 
00186   if (theDatabase == 0) {
00187     opserr << "WARNING: save - no database has been constructed\n";
00188     return TCL_OK;
00189   }
00190 
00191      // make sure at least one other argument to contain type of system
00192     if (argc < 2) {
00193       opserr << "WARNING save no commit tag - want save commitTag?";
00194       return TCL_OK;
00195     }    
00196 
00197     // check argv[1] for commitTag
00198     int commitTag;
00199     if (Tcl_GetInt(interp, argv[1], &commitTag) != TCL_OK) {
00200       opserr << "WARNING - save could not read commitTag " << argv[1] << endln;
00201       return TCL_OK;    
00202     }   
00203 
00204     if (theDatabase->commitState(commitTag) < 0) {
00205       opserr << "WARNING - database failed to commitState \n";
00206       return TCL_ERROR;
00207     }
00208     
00209     return TCL_OK;
00210 }
00211 
00212 
00213 int 
00214 restore(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv)
00215 {
00216 
00217   if (theDatabase == 0) {
00218     opserr << "WARNING: restore - no database has been constructed\n";
00219     return TCL_OK;
00220   }
00221 
00222      // make sure at least one other argument to contain type of system
00223     if (argc < 2) {
00224       opserr << "WARNING restore no commit tag - want restore commitTag?";
00225       return TCL_OK;
00226     }    
00227 
00228     // check argv[1] for commitTag
00229     int commitTag;
00230     if (Tcl_GetInt(interp, argv[1], &commitTag) != TCL_OK) {
00231       opserr << "WARNING - restore could not read commitTag " << argv[1] << endln;
00232       return TCL_OK;    
00233     }   
00234 
00235     if (theDatabase->restoreState(commitTag) < 0) {
00236       opserr << "WARNING - database failed to restoreState \n";
00237       return TCL_ERROR;
00238     }
00239     
00240     return TCL_OK;
00241 }

Generated on Mon Oct 23 15:05:00 2006 for OpenSees by doxygen 1.5.0