michael_hartman_cz

PA2 Semestrálka main.cpp

Jun 2nd, 2015
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. #include <iostream>
  2. #include "CFormatter.h"
  3. #include "CServer.h"
  4. #include <cstring>    // for strncmp()
  5. #include <string>
  6. #include <cstdlib>    // for atoi and exit()
  7. using namespace std;
  8.  
  9. /**
  10.  * Runs server on port specified.
  11.  *
  12.  * @param port Server port.
  13.  */
  14. void runServer ( int port = 80 );
  15.  
  16. /**
  17.  * Runs formatter locally.
  18.  *
  19.  * @param inFile Input file.
  20.  * @param outFile Output File.
  21.  */
  22. void runLocal  ( const string & inFile, const string & outFile = "" );
  23.  
  24. /**
  25.  * Prints help for user on std out.
  26.  */
  27. void help      ();
  28.  
  29. /**
  30.  * @mainpage Java Formatter
  31.  *
  32.  * @section intro Introduction
  33.  *
  34.  * This is java formatter program. It was written as seminar project on subject BI-PA2 on Czech technical university in Prague,
  35.  * Faculty of information technology. It means there will be no future development but feel free to use it as you want.
  36.  *
  37.  * @section usage Usage
  38.  *
  39.  * @subsection server Server
  40.  *
  41.  * ./program_name
  42.  *
  43.  * Runs server on port 80 (make sure you have rights to do this).
  44.  *
  45.  * ./program_name -s
  46.  *
  47.  * Same as above.
  48.  *
  49.  * ./program_name -s -p 80
  50.  *
  51.  * Runs server on port specified after -p
  52.  *
  53.  * @subsection local Local
  54.  *
  55.  * ./program_name -l "filename"
  56.  *
  57.  * Works locally with file given after -l and prints result on std out.
  58.  *
  59.  * ./program_name -l "filename" -o "filename"
  60.  *
  61.  * Works locally with file given after -l and prints result to file given after -o
  62.  */
  63.  
  64. int main ( int argc, char * argv[] )
  65.  {    
  66.     switch ( argc )
  67.      {
  68.         case 1:
  69.         case 2:
  70.             if ( ! argv[1] || strncmp ( argv[1], "-s", 2 ) == 0 )
  71.                 runServer ( 80 );
  72.             else
  73.                 help();
  74.             break;
  75.         case 3:
  76.             if ( strncmp ( argv[1], "-l", 2 ) == 0 )
  77.                 runLocal ( argv[2] );
  78.             else
  79.                 help();
  80.             break;
  81.         case 4:
  82.             if ( strncmp ( argv[1], "-s", 2 ) == 0 && strncmp ( argv[2], "-p", 2 ) == 0 )
  83.                 runServer ( atoi ( argv[3] ) );
  84.             else
  85.                 help();
  86.             break;
  87.         case 5:
  88.             if ( strncmp ( argv[1], "-l", 2 ) == 0 && strncmp ( argv[3], "-o", 2 ) == 0 )
  89.                 runLocal ( argv[2], argv[4] );
  90.             else
  91.                 help();
  92.             break;
  93.         default:
  94.             help();
  95.             break;
  96.      }
  97.     return 0;
  98.  }
  99.  
  100. void runServer ( int port )
  101.  {
  102.     if ( port < 1 || port > 65000 )
  103.      {
  104.         cout << "Can not run on port: " << port << endl;
  105.         exit ( 1 );
  106.      }
  107.     CServer server;
  108.     cout << "Starting server on port: " << port << endl;
  109.     server . run( port );
  110.  }
  111.  
  112. void runLocal ( const string & inFile, const string & outFile )
  113.  {
  114.     try
  115.      {
  116.         CFormatter f ( inFile );
  117.         f . read();
  118.         if ( ! outFile . empty() )
  119.          {
  120.             fstream fs;
  121.             fs . open ( outFile.c_str(), std::fstream::out );
  122.             f . print ( fs );
  123.          }
  124.         else
  125.             f . print ( cout );
  126.      }
  127.     catch ( CBadFileNameException e )
  128.      {
  129.         cerr << e.what() << endl;
  130.      }
  131.  }
  132.  
  133. void help ()
  134.  {
  135.     cout << "\n\
  136.    Usage:\n\
  137.    =====================================================================\n\n\
  138.    Server behavioral:\n\
  139.    ---------------------------------------------------------------------\n\
  140.    ./program_name\n\
  141.    \tRuns server on port 80 (make sure you have rights to do this).\n\
  142.    ./program_name -s\n\
  143.    \tSame as above.\n\
  144.    ./program_name -s -p 80\n\
  145.    \tRuns server on port specified after -p\n\n\
  146.    Local behavioral\n\
  147.    ---------------------------------------------------------------------\n\
  148.    ./program_name -l \"filename\"\n\
  149.    \tWorks locally with file given after -l and prints result on std out.\n\
  150.    ./program_name -l \"filename\" -o \"filename\"\n\
  151.    \tWorks locally with file given after -l and prints result to file given after -o\n\n";
  152.                
  153.     exit ( 0 );
  154.  }
Advertisement
Add Comment
Please, Sign In to add comment