ADMB Documentation  11.1.1649
 All Classes Files Functions Variables Typedefs Friends Defines
spcomm.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: spcomm.cpp 1108 2013-07-11 23:46:23Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <windows.h>
00008 #include <windows.h>
00009 #include <winnt.h>
00010 #include <ddeml.h>
00011 
00012 #include <math.h>
00013 #include <stdlib.h>
00014 #include <spcomm.h>
00015 #include <fvar.hpp>
00016 //#include <admoddde.h>
00017 //#include <ddesp.h>
00018 
00019 
00020 DWORD DDEspclient::idInst=0;
00021 DWORD DDEspclient::sleeptime=0;
00022 HCONV DDEspclient::cstatus=NULL;
00023 UINT  DDEspclient::status=0;
00024 
00025 void check_when_finished(void);
00026 void open_the_file(ifstream*& pifs, const int& waitflag, const int& numwaits);
00027 void open_the_file_when_finished(ifstream*& pifs, const int& waitflag, const int& numwaits);
00028 
00029 # if defined(STUPID_DDL_FUNCTIONS)
00030 HDDEDATA CALLBACK mycallback(       //DdeCallback(
00031          UINT  uType,   // transaction type
00032          UINT  uFmt,    // clipboard data format
00033    HCONV  hconv,  // handle of the conversation
00034    HSZ  hsz1,     // handle of a string
00035    HSZ  hsz2,     // handle of a string
00036    HDDEDATA  hdata, // handle of a global memory object
00037    DWORD  dwData1,  // transaction-specific data
00038    DWORD  dwData2   // transaction-specific data
00039 );
00040 
00041 
00042 DDEspclient::DDEspclient(DWORD _sleeptime)
00043 {
00044   status=DdeInitialize(&idInst, mycallback,CBF_FAIL_EXECUTES |
00045     CBF_SKIP_ALLNOTIFICATIONS,0);
00046   HSZ  hszService=DdeCreateStringHandle(idInst,"SPLUS", CP_WINANSI);
00047   HSZ  hszTopic=DdeCreateStringHandle(idInst,"SCommand", CP_WINANSI);
00048 
00049   if (status !=DMLERR_NO_ERROR)
00050   {
00051     printInitializestatus(status);
00052     cstatus=NULL;
00053   }
00054   else
00055   {
00056     cstatus=DdeConnect(idInst,hszService,hszTopic, NULL);
00057   }
00058 }
00059 
00060 
00061 void SPsend(const double& x, const adstring& name)
00062 {
00063   ofstream ofs("c:\\tmp.spl");
00064 
00065   adstring command = name + adstring(" <- scan(\"c:\\\\tmp.spl\")");
00066 
00067   ofs << x;
00068   ofs << endl;
00069   ofs.close();
00070   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00071   check_when_finished();
00072 }
00073 
00074 void SPsend(const dvector& x, const adstring& name)
00075 {
00076   ofstream ofs("c:\\tmp.spl");
00077 
00078   adstring command = name + adstring(" <- scan(\"c:\\\\tmp.spl\")");
00079 
00080   ofs << x;
00081   ofs << endl;
00082   ofs.close();
00083   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00084   check_when_finished();
00085 }
00086 
00087 void SPsend(const named_dmatrix& _x)
00088 {
00089   ADUNCONST(named_dmatrix,x)
00090   SPsend(x,adstring(x.label().mychar()));
00091 }
00092 
00093 
00094 void SPsend(const named_dvar_matrix& _x)
00095 {
00096   ADUNCONST(named_dvar_matrix,x)
00097   SPsend(value(x),adstring(x.label().mychar()));
00098 }
00099 
00100 void SPsend(const named_dvariable& _x)
00101 {
00102   ADUNCONST(named_dvariable,x)
00103   SPsend(value(x),adstring(x.label().mychar()));
00104 }
00105 
00106 
00107 void SPsend(const dmatrix& x, const adstring& name)
00108 {
00109   int i1=x.rowmin();
00110   int ncol=x(i1).indexmax()-x(i1).indexmin()+1;
00111   ofstream ofs("c:\\tmp.spl");
00112 
00113   adstring command = name + adstring(" <- matrix(scan(\"c:\\\\tmp.spl\")"
00114    ",byrow=T,ncol=") + str(ncol) + adstring(")");
00115 
00116   ofs << x;
00117   ofs << endl;
00118   ofs.close();
00119   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00120   check_when_finished();
00121 }
00122 
00123 void SPreceive(const dvector& _x, const adstring& name)
00124 {
00125   ADUNCONST(dvector,x)
00126 
00127   //adstring command = name + adstring(" <- scan(\"c:\\\\tmp.spl\")");
00128   // tell splus to send the size of the vector
00129   adstring command = adstring(" write(length(") + name
00130     + adstring("),\"c:\\\\tmp.spl\")");
00131   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00132   Sleep(200);
00133   // now send the vector
00134   command = adstring(" write(") + name
00135     + adstring(",\"c:\\\\tmp.spl\",append=T)");
00136   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00137   Sleep(200);
00138 
00139 
00140   ifstream * pifs=NULL;
00141   int waitflag=0;
00142   int numwaits=0;
00143   do
00144   {
00145     open_the_file_when_finished(pifs,waitflag,numwaits);
00146   }
00147   while (waitflag && numwaits<5);
00148 
00149   if (pifs && !(!(*pifs)) )
00150   {
00151     int n;
00152     (*pifs) >> n;
00153 
00154     // check to see if this object is allocated;
00155     if (!x)
00156     {
00157       x.allocate(1,n);
00158       (*pifs) >> x;
00159     }
00160     else
00161     {
00162       if ( (x.indexmax()-x.indexmin()+1) != n)
00163       {
00164         cerr << "Error allocated dvector of the wrong size in"
00165            "SPreceive(const dvector& x, const adstring& name)";
00166       }
00167       else
00168       {
00169         (*pifs) >> x;
00170       }
00171     }
00172   }
00173   else
00174   {
00175     cerr << " couldn't read from splus temporary file" << endl;
00176   }
00177   // now delete the temporary file
00178   if (pifs)
00179   {
00180     delete pifs;
00181     pifs=NULL;
00182   }
00183   //DeleteFile("c:\\tmp.spl");
00184 }
00185 
00186 void SPreceive(const named_dvector& _x)
00187 {
00188   ADUNCONST(named_dvector,x)
00189   SPreceive(x,adstring(x.label().mychar()));
00190 }
00191 
00192 void SPreceive(const named_dvar_vector& _x)
00193 {
00194   ADUNCONST(named_dvar_vector,x)
00195   SPreceive(x,adstring(x.label().mychar()));
00196 }
00197 
00198 void SPreceive(const dvar_vector& _x,const adstring& name)
00199 {
00200   ADUNCONST(dvar_vector,x)
00201 
00202   //adstring command = name + adstring(" <- scan(\"c:\\\\tmp.spl\")");
00203   // tell splus to send the size of the vector
00204   adstring command = adstring(" write(length(") + name
00205     + adstring("),\"c:\\\\tmp.spl\")");
00206   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00207   Sleep(200);
00208   // now send the vector
00209   command = adstring(" write(") + name
00210     + adstring(",\"c:\\\\tmp.spl\",append=T)");
00211   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00212   Sleep(200);
00213 
00214 
00215   ifstream * pifs=NULL;
00216   int waitflag=0;
00217   int numwaits=0;
00218   do
00219   {
00220     open_the_file_when_finished(pifs,waitflag,numwaits);
00221   }
00222   while (waitflag && numwaits<5);
00223 
00224   if (pifs && !(!(*pifs)) )
00225   {
00226     int n;
00227     (*pifs) >> n;
00228 
00229     // check to see if this object is allocated;
00230     if (!x)
00231     {
00232       x.allocate(1,n);
00233       (*pifs) >> x;
00234     }
00235     else
00236     {
00237       if ( (x.indexmax()-x.indexmin()+1) != n)
00238       {
00239         cerr << "Error allocated dvector of the wrong size in"
00240            "SPreceive(const dvector& x,const adstring& name)";
00241       }
00242       else
00243       {
00244         (*pifs) >> x;
00245       }
00246     }
00247   }
00248   else
00249   {
00250     cerr << " couldn't read from splus temporary file" << endl;
00251   }
00252   // now delete the temporary file
00253   if (pifs)
00254   {
00255     delete pifs;
00256     pifs=NULL;
00257   }
00258   //DeleteFile("c:\\tmp.spl");
00259 }
00260 
00261 void SPreceive(const dmatrix& _x, const adstring& name);
00262 void SPreceive(const named_dmatrix& _x)
00263 {
00264   ADUNCONST(named_dmatrix,x)
00265   SPreceive(x,adstring(x.label().mychar()));
00266 }
00267 
00268 void SPreceive(const dmatrix& _x, const adstring& name)
00269 {
00270   ADUNCONST(dmatrix,x)
00271 
00272   // tell splus to send the number of rows in the matrix
00273   adstring command = adstring(" write(nrow(") + name
00274     + adstring("),\"c:\\\\tmp.spl\")");
00275 
00276   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00277   Sleep(200);
00278 
00279   // tell splus to send the number of columns in the matrix
00280   command = adstring(" write(ncol(") + name
00281     + adstring("),\"c:\\\\tmp.spl\",append=T)");
00282 
00283   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00284   Sleep(200);
00285 
00286   // now send the matrix
00287   command = adstring(" write(t(") + name
00288     + adstring("),\"c:\\\\tmp.spl\", append=T)");
00289 
00290   //command = adstring(" write(t(") + name
00291   //  + adstring("),\"c:\\\\tmp.spl\",ncol=ncol(") + name
00292   //  + adstring("),append=T)");
00293   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00294   Sleep(200);
00295 
00296   ifstream * pifs=NULL;
00297   int waitflag=0;
00298   int numwaits=0;
00299   do
00300   {
00301     open_the_file_when_finished(pifs,waitflag,numwaits);
00302   }
00303   while (waitflag && numwaits<5);
00304 
00305   if (pifs && !(!(*pifs)) )
00306   {
00307     int nrows;
00308     int ncols;
00309     (*pifs) >> nrows;
00310     (*pifs) >> ncols;
00311 
00312     // check to see if this object is allocated;
00313     if (!x)
00314     {
00315       x.allocate(1,nrows,1,ncols);
00316       if (!(*pifs))
00317       {
00318         cerr << "Error state before reading dmatrix from temporary file"
00319              << endl;
00320       }
00321       (*pifs) >> x;
00322     }
00323     else
00324     {
00325       int r1=x.rowmin();
00326       if ( ((x.rowmax()-x.rowmin()+1) != nrows) ||
00327          ( (x(r1).indexmax()-x(r1).indexmin()+1) != ncols) )
00328       {
00329         cerr << "Error --  allocated dimatrix is  the wrong size in"
00330            "SPreceive(const dvector& x, const adstring& name)";
00331       }
00332       else
00333       {
00334         (*pifs) >> x;
00335       }
00336     }
00337   }
00338   else
00339   {
00340     cerr << " couldn't read from splus temporary file" << endl;
00341   }
00342   // now delete the temporary file
00343   if (pifs)
00344   {
00345     delete pifs;
00346     pifs=NULL;
00347   }
00348   DeleteFile("c:\\tmp.spl");
00349 }
00350 
00351 void SPreceive(const dvar_matrix& x, const adstring& name);
00352 
00353 void SPreceive(const named_dvar_matrix& _x)
00354 {
00355   ADUNCONST(named_dvar_matrix,x)
00356   SPreceive(x,adstring(x.label().mychar()));
00357 }
00358 
00359 void SPreceive(const dvar_matrix& _x, const adstring& name)
00360 {
00361   ADUNCONST(dvar_matrix,x)
00362 
00363   // tell splus to send the number of rows in the matrix
00364   adstring command = adstring(" write(nrow(") + name
00365     + adstring("),\"c:\\\\tmp.spl\")");
00366 
00367   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00368   Sleep(200);
00369 
00370   // tell splus to send the number of columns in the matrix
00371   command = adstring(" write(ncol(") + name
00372     + adstring("),\"c:\\\\tmp.spl\",append=T)");
00373 
00374   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00375   Sleep(200);
00376 
00377   // now send the matrix
00378   command = adstring(" write(t(") + name
00379     + adstring("),\"c:\\\\tmp.spl\", append=T)");
00380 
00381   //command = adstring(" write(t(") + name
00382   //  + adstring("),\"c:\\\\tmp.spl\",ncol=ncol(") + name
00383   //  + adstring("),append=T)");
00384   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00385   Sleep(200);
00386 
00387   ifstream * pifs=NULL;
00388   int waitflag=0;
00389   int numwaits=0;
00390   do
00391   {
00392     open_the_file_when_finished(pifs,waitflag,numwaits);
00393   }
00394   while (waitflag && numwaits<5);
00395 
00396   if (pifs && !(!(*pifs)) )
00397   {
00398     int nrows;
00399     int ncols;
00400     (*pifs) >> nrows;
00401     (*pifs) >> ncols;
00402 
00403     // check to see if this object is allocated;
00404     if (!x)
00405     {
00406       x.allocate(1,nrows,1,ncols);
00407       if (!(*pifs))
00408       {
00409         cerr << "Error state before reading dmatrix from temporary file"
00410              << endl;
00411       }
00412       (*pifs) >> x;
00413     }
00414     else
00415     {
00416       int r1=x.rowmin();
00417       if ( ((x.rowmax()-x.rowmin()+1) != nrows) ||
00418          ( (x(r1).indexmax()-x(r1).indexmin()+1) != ncols) )
00419       {
00420         cerr << "Error --  allocated dimatrix is  the wrong size in"
00421            "SPreceive(const dvector& x, const adstring& name)";
00422       }
00423       else
00424       {
00425         (*pifs) >> x;
00426       }
00427     }
00428   }
00429   else
00430   {
00431     cerr << " couldn't read from splus temporary file" << endl;
00432   }
00433   // now delete the temporary file
00434   if (pifs)
00435   {
00436     delete pifs;
00437     pifs=NULL;
00438   }
00439   DeleteFile("c:\\tmp.spl");
00440 }
00441 
00442 void SPreceive(const named_dvariable& _x)
00443 {
00444   ADUNCONST(named_dvariable,x)
00445   SPreceive(value(x),adstring(x.label().mychar()));
00446 }
00447 
00448 void SPreceive(const data_number& _x)
00449 {
00450   ADUNCONST(data_number,x)
00451   SPreceive(x.value(),adstring(x.label().mychar()));
00452 }
00453 
00454 void SPreceive(const dvariable& x, const adstring& name)
00455 {
00456   SPreceive(value(x),name);
00457 }
00458 
00459 void SPreceive(const double& x, const adstring& name)
00460 {
00461 
00462   //adstring command = name + adstring(" <- scan(\"c:\\\\tmp.spl\")");
00463   // tell splus to send the size of the vector
00464   adstring command = adstring(" write(") + name
00465     + adstring(",\"c:\\\\tmp.spl\")");
00466 
00467   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00468   Sleep(500);
00469   // now send the vector
00470   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00471   Sleep(500);
00472 
00473   ifstream * pifs=NULL;
00474   int waitflag=0;
00475   int numwaits=0;
00476   do
00477   {
00478     open_the_file_when_finished(pifs,waitflag,numwaits);
00479   }
00480   while (waitflag && numwaits<5);
00481 
00482   if (pifs && !(!(*pifs)) )
00483   {
00484     (*pifs) >> (double&)x;
00485   }
00486   else
00487   {
00488     cerr << " couldn't read from splus temporary file" << endl;
00489   }
00490   // now delete the temporary file
00491   if (pifs)
00492   {
00493     delete pifs;
00494     pifs=NULL;
00495   }
00496   DeleteFile("c:\\tmp.spl");
00497 }
00498 
00499 void SPsend(const named_dvector& _x)
00500 {
00501   ADUNCONST(named_dvector,x)
00502   SPsend(x,adstring(x.label().mychar()));
00503 }
00504 
00505 void SPsend(const named_dvar_vector& _x)
00506 {
00507   ADUNCONST(named_dvar_vector,x)
00508    SPsend(value(x),adstring(x.label().mychar()));
00509 }
00510 
00511 void SPplot(const dvector& x, const adstring& name)
00512 {
00513 
00514   ofstream ofs("c:\\tmp.spl");
00515 
00516   adstring command = name + adstring(" <- scan(\"c:\\\\tmp.spl\")");
00517 
00518   ofs << x;
00519   ofs << endl;
00520   ofs.close();
00521   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00522   Sleep(500);
00523   command = adstring("win3(\"spadcln\",multi=T,minimized=T)");
00524   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00525   Sleep(500);
00526   command = adstring("plot(") + name + adstring(")");
00527   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00528   Sleep(500);
00529 }
00530 
00531 void SPplot(const named_dvector& _x)
00532 {
00533   ADUNCONST(named_dvector,x)
00534   SPplot(x,adstring(x.label().mychar()));
00535 }
00536 
00537 void printInitializestatus(int status)
00538 {
00539   char str1[]=" Error in DdeInitialize() error type is ";
00540   switch(status)
00541   {
00542   case DMLERR_NO_ERROR:
00543     cerr << " DdeInitialize() was successful" << endl;
00544     break;
00545   case DMLERR_DLL_USAGE:
00546     cerr << str1 << "DMLERR_DLL_USAGE" << endl;
00547     break;
00548   case DMLERR_INVALIDPARAMETER:
00549     cerr << str1 << "DMLERR_INVALIDPARAMETER" << endl;
00550     break;
00551   case DMLERR_SYS_ERROR:
00552     cerr << str1 << "DMLERR_SYS_ERROR"  << endl;
00553     break;
00554   default:
00555     cerr << str1 << "Error is unidentified" << endl;
00556     break;
00557   }
00558 }
00559 
00560 void SPexecute( LPBYTE  cbuff, // address of data to pass to server
00561   HCONV   cstatus  /* handle of conversation */ )
00562 {
00563 
00564   DWORD  dwResult=0; // address of transaction result
00565 
00566   DdeClientTransaction(
00567    cbuff, // address of data to pass to server
00568    strlen((const char *)cbuff)+1, // length of data
00569    cstatus, // handle of conversation
00570    NULL,  // handle of item name string
00571    0, // clipboard data format
00572    XTYP_EXECUTE,  // transaction type
00573    1000, //TIMEOUT_ASYNC, // time-out duration
00574    &dwResult  // address of transaction result
00575   );
00576 }
00577 
00578 
00579 HDDEDATA CALLBACK mycallback(       //DdeCallback(
00580    UINT  uType, // transaction type
00581    UINT  uFmt,  // clipboard data format
00582    HCONV  hconv,  // handle of the conversation
00583    HSZ  hsz1, // handle of a string
00584    HSZ  hsz2, // handle of a string
00585    HDDEDATA  hdata, // handle of a global memory object
00586    DWORD  dwData1,  // transaction-specific data
00587    DWORD  dwData2   // transaction-specific data
00588   )
00589 
00590 {
00591   return (HDDEDATA) NULL;
00592 }
00593 
00594 #endif // # if defined(STUPID_DDL_FUNCTIONS)
00595 
00596 void check_when_finished(void)
00597 {
00598   // tell us when splus is finished writing the matrix
00599   DeleteFile("c:\\tmp1.spl");
00600 
00601   adstring command = adstring(" write(1,\"c:\\\\tmp1.spl\")");
00602 
00603   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00604   int nn=0;
00605   int numwaits=0;
00606   int waitflag=0;
00607   ifstream * pifs=NULL;
00608   do
00609   {
00610     pifs=new ifstream("c:\\tmp1.spl");
00611     if ((!pifs) || (!(*pifs)) )
00612     {
00613       //cout << "Error opening semaphore file ... will retry " << endl;
00614       waitflag=1;
00615       numwaits++;
00616       Sleep(500);
00617       if (pifs)
00618       {
00619         delete pifs;
00620         pifs=NULL;
00621       }
00622     }
00623     else
00624     {
00625       waitflag=0;
00626     }
00627   }
00628   while (waitflag==1);
00629   if (pifs)
00630   {
00631     delete pifs;
00632     pifs=NULL;
00633   }
00634   DeleteFile("c:\\tmp1.spl");
00635 }
00636 
00637 void open_the_file_when_finished(ifstream*& pifs, const int& _waitflag, const int& _numwaits)
00638 {
00639   int& waitflag=(int&) _waitflag;
00640   int& numwaits=(int&) _numwaits;
00641   // tell us when splus is finished writing the matrix
00642   DeleteFile("c:\\tmp1.spl");
00643   adstring command = adstring(" write(1,\"c:\\\\tmp1.spl\")");
00644   SPexecute((unsigned char*)command,DDEspclient::get_cstatus());
00645   int nn=0;
00646   do
00647   {
00648     pifs=new ifstream("c:\\tmp1.spl");
00649     //(*pifs) >> nn;
00650     if ((!pifs) || (!(*pifs)) )
00651     {
00652       //cout << "Error opening semaphore file ... will retry " << endl;
00653       waitflag=1;
00654       numwaits++;
00655       Sleep(1000*numwaits);
00656       if (pifs)
00657       {
00658         delete pifs;
00659         pifs=NULL;
00660       }
00661     }
00662     else
00663     {
00664       waitflag=0;
00665     }
00666   }
00667   while (waitflag==1);
00668   if (pifs)
00669   {
00670     delete pifs;
00671     pifs=NULL;
00672   }
00673   DeleteFile("c:\\tmp1.spl");
00674   pifs=new ifstream("c:\\tmp.spl");
00675   if ((!pifs) || (!(*pifs)) )
00676   {
00677     cout << "Eror opening file ... will retry " << endl;
00678     waitflag=1;
00679     numwaits++;
00680     Sleep(500);
00681     if (pifs)
00682     {
00683       delete pifs;
00684       pifs=NULL;
00685     }
00686   }
00687   else
00688   {
00689     waitflag=0;
00690   }
00691 }
00692 
00693 void open_the_file(ifstream*& pifs, const int& _waitflag, const int& _numwaits)
00694 {
00695   int& waitflag=(int&) _waitflag;
00696   int& numwaits=(int&) _numwaits;
00697   int nn=0;
00698   do
00699   {
00700     pifs=new ifstream("c:\\tmp1.spl");
00701     //(*pifs) >> nn;
00702     if ((!pifs) || (!(*pifs)) )
00703     {
00704       //cout << "Error opening semaphore file ... will retry " << endl;
00705       waitflag=1;
00706       numwaits++;
00707       Sleep(1000*numwaits);
00708       if (pifs)
00709       {
00710         delete pifs;
00711         pifs=NULL;
00712       }
00713     }
00714     else
00715     {
00716       waitflag=0;
00717     }
00718   }
00719   while (waitflag==1);
00720   if (pifs)
00721   {
00722     delete pifs;
00723     pifs=NULL;
00724   }
00725   DeleteFile("c:\\tmp1.spl");
00726   pifs=new ifstream("c:\\tmp.spl");
00727   if ((!pifs) || (!(*pifs)) )
00728   {
00729     cout << "Eror opening file ... will retry " << endl;
00730     waitflag=1;
00731     numwaits++;
00732     Sleep(500);
00733     if (pifs)
00734     {
00735       delete pifs;
00736       pifs=NULL;
00737     }
00738   }
00739   else
00740   {
00741     waitflag=0;
00742   }
00743 }