home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / pmics.zip / pmics.cc < prev    next >
C/C++ Source or Header  |  1994-12-10  |  6KB  |  188 lines

  1. /*
  2.     PMICS -- PM interface for playing chess on internet chess server
  3.     Copyright (C) 1994  Kevin Nomura
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Author can be reached at email: chow@netcom.com
  20. */
  21. #include <iapp.hpp>
  22. #include <iaccel.hpp>
  23. #include "pmics.hh"
  24. #include "wpmics.hh"
  25. #include "session.hh"
  26. #include "engcom.hh"
  27. #include <imsgbox.hpp>
  28. #include <iexcbase.hpp>        // IException
  29.  
  30. #include <ithread.hpp>
  31. #include <stdio.h>
  32.  
  33. IWindow     *mainWin;
  34. PmicsWindow *mainWindow = NULL;
  35. ASession    *aSession = NULL;
  36. EngineComm  *aEngineComm = NULL;
  37.  
  38. int intOption(IString s);
  39. IString stringOption(IString s);
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.   IFUNCTRACE_DEVELOP();
  44.  
  45.   try {
  46.     IApplication::current().setArgs(argc, argv);
  47.     IThread::current().initializePM(100);
  48.     ITRACE_DEVELOP("QueueSize now " + IString(IThread::current().queueSize()));
  49.     mainWin = mainWindow = new PmicsWindow(WND_PMICS);
  50.  
  51.     // start communication session
  52.     if (intOption("dos")) {
  53.       aSession = new ADosSession();
  54.     }
  55. #ifdef TCP_SESSION
  56.     else if (intOption("tcp")) {
  57.       aSession = new ATcpSession();
  58.     }
  59. #endif
  60.     else {
  61.       aSession = new AComSession(); 
  62.     }
  63.     
  64.     // open engine pipe
  65.     aEngineComm = new EngineComm;
  66.  
  67.     IApplication::current().run();
  68.   }
  69.   /****************************************************
  70.    * catchall for exceptions, both pmics and ICLUI
  71.    ****************************************************/
  72.   catch (IException &xcp) {
  73.     IMessageBox boom(IWindow::desktopWindow());
  74.     ITRACE_DEVELOP(IString("exception caught: ") + xcp.text());
  75.     boom.setTitle("PMICS error");
  76.     boom.show(xcp.text(),
  77.           IMessageBox::errorIcon |
  78.           IMessageBox::okButton |
  79.           IMessageBox::applicationModal |
  80.           IMessageBox::moveable         );
  81.   }
  82.   
  83.   if (aSession) delete aSession;
  84.   if (mainWindow) delete mainWindow;
  85. }
  86.  
  87.  
  88. /**************************************************************************
  89.  * FUNCTION:    intOption
  90.  *
  91.  * DESCRIPTION: look for a command-line option of the form -xxx
  92.  *              if -xxx is present then return 1;
  93.  *              if -xxx=n is present where n is an integer, return n
  94.  *              else return 0 (ambiguous with -xxx=0)
  95.  *               
  96.  **************************************************************************/
  97. int intOption(IString s)
  98. {
  99.   IString arg;
  100.   int result = 0;
  101.   char c;
  102.  
  103.   for (int n=1; n<IApplication::current().argc(); n++) {
  104.     c = IApplication::current().argv(n)[1];
  105.     if (c != '-' && c != '/')
  106.       break;            // switch must be introduced by - or /
  107.     arg = IApplication::current().argv(n).subString(2);
  108.  
  109.     if (arg.subString(1, s.length()) == s) {
  110.       if (arg.length() == s.length()) {
  111.     result = 1;
  112.     break;
  113.       }
  114.       if (arg[s.length()+1] == '=') {
  115.     result = arg.subString(s.length()+2).asInt();
  116.     break;
  117.       }
  118.     }
  119.   }
  120.   ITRACE_DEVELOP("intOption: " + s + " = <" + IString(result) + ">");
  121.   return result;
  122. }
  123.  
  124. /**************************************************************************
  125.  * FUNCTION:    stringOption
  126.  *
  127.  * DESCRIPTION: look for a command-line option of the form -xxx
  128.  *              if -xxx is present then return IString("y") (eqv to -xxx=y)
  129.  *              if -xxx=s is present then return IString(s)
  130.  *              else return IString("")
  131.  *               
  132.  **************************************************************************/
  133. IString stringOption(IString xxx)
  134. {
  135.   IString arg;
  136.   IString result;
  137.   char c;
  138.  
  139.   for (int n=1; n<IApplication::current().argc(); n++) {
  140.     c = IApplication::current().argv(n)[1];
  141.     if (c != '-' && c != '/')
  142.       break;            // switch must be introduced by - or /
  143.     arg = IApplication::current().argv(n).subString(2);
  144.  
  145.     if (arg.subString(1, xxx.length()) == xxx) {
  146.       if (arg.length() == xxx.length())    { // no argument
  147.     result = "y";
  148.     break;
  149.       }
  150.       if (arg[xxx.length()+1] == '=') {    // has an argument
  151.     if (arg[xxx.length()+2] != '"') {
  152.       result = arg.subString(xxx.length()+2);
  153.       break;
  154.     }
  155.     // string is quoted.  consume this and following argv's until
  156.     // termination quote is reached.
  157.     else {
  158.       result = arg.subString(xxx.length()+3);
  159.       for (n++; n<IApplication::current().argc(); n++) {
  160.         if (arg[arg.length()] == '"')
  161.           break;
  162.         arg = IApplication::current().argv(n);
  163.         result += arg;
  164.       }
  165.       result = result.stripTrailing('"');
  166.     }
  167.       }
  168.     }
  169.   }
  170.   ITRACE_DEVELOP("stringOption: " + xxx + " = <" + result + ">");
  171.   return result;
  172. }
  173.  
  174. int getIntOption(IString s, int def)
  175. {
  176.   int opt;
  177.   opt = intOption(s);
  178.   return (opt) ? opt : def;
  179. }
  180.  
  181. IString getStringOption(IString s, IString def)
  182. {
  183.   IString opt;
  184.   opt = stringOption(s);
  185.   return (opt != IString("")) ? opt : def;
  186. }
  187.  
  188.