Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "CFormatter.h"
- #include "CServer.h"
- #include <cstring> // for strncmp()
- #include <string>
- #include <cstdlib> // for atoi and exit()
- using namespace std;
- /**
- * Runs server on port specified.
- *
- * @param port Server port.
- */
- void runServer ( int port = 80 );
- /**
- * Runs formatter locally.
- *
- * @param inFile Input file.
- * @param outFile Output File.
- */
- void runLocal ( const string & inFile, const string & outFile = "" );
- /**
- * Prints help for user on std out.
- */
- void help ();
- /**
- * @mainpage Java Formatter
- *
- * @section intro Introduction
- *
- * This is java formatter program. It was written as seminar project on subject BI-PA2 on Czech technical university in Prague,
- * Faculty of information technology. It means there will be no future development but feel free to use it as you want.
- *
- * @section usage Usage
- *
- * @subsection server Server
- *
- * ./program_name
- *
- * Runs server on port 80 (make sure you have rights to do this).
- *
- * ./program_name -s
- *
- * Same as above.
- *
- * ./program_name -s -p 80
- *
- * Runs server on port specified after -p
- *
- * @subsection local Local
- *
- * ./program_name -l "filename"
- *
- * Works locally with file given after -l and prints result on std out.
- *
- * ./program_name -l "filename" -o "filename"
- *
- * Works locally with file given after -l and prints result to file given after -o
- */
- int main ( int argc, char * argv[] )
- {
- switch ( argc )
- {
- case 1:
- case 2:
- if ( ! argv[1] || strncmp ( argv[1], "-s", 2 ) == 0 )
- runServer ( 80 );
- else
- help();
- break;
- case 3:
- if ( strncmp ( argv[1], "-l", 2 ) == 0 )
- runLocal ( argv[2] );
- else
- help();
- break;
- case 4:
- if ( strncmp ( argv[1], "-s", 2 ) == 0 && strncmp ( argv[2], "-p", 2 ) == 0 )
- runServer ( atoi ( argv[3] ) );
- else
- help();
- break;
- case 5:
- if ( strncmp ( argv[1], "-l", 2 ) == 0 && strncmp ( argv[3], "-o", 2 ) == 0 )
- runLocal ( argv[2], argv[4] );
- else
- help();
- break;
- default:
- help();
- break;
- }
- return 0;
- }
- void runServer ( int port )
- {
- if ( port < 1 || port > 65000 )
- {
- cout << "Can not run on port: " << port << endl;
- exit ( 1 );
- }
- CServer server;
- cout << "Starting server on port: " << port << endl;
- server . run( port );
- }
- void runLocal ( const string & inFile, const string & outFile )
- {
- try
- {
- CFormatter f ( inFile );
- f . read();
- if ( ! outFile . empty() )
- {
- fstream fs;
- fs . open ( outFile.c_str(), std::fstream::out );
- f . print ( fs );
- }
- else
- f . print ( cout );
- }
- catch ( CBadFileNameException e )
- {
- cerr << e.what() << endl;
- }
- }
- void help ()
- {
- cout << "\n\
- Usage:\n\
- =====================================================================\n\n\
- Server behavioral:\n\
- ---------------------------------------------------------------------\n\
- ./program_name\n\
- \tRuns server on port 80 (make sure you have rights to do this).\n\
- ./program_name -s\n\
- \tSame as above.\n\
- ./program_name -s -p 80\n\
- \tRuns server on port specified after -p\n\n\
- Local behavioral\n\
- ---------------------------------------------------------------------\n\
- ./program_name -l \"filename\"\n\
- \tWorks locally with file given after -l and prints result on std out.\n\
- ./program_name -l \"filename\" -o \"filename\"\n\
- \tWorks locally with file given after -l and prints result to file given after -o\n\n";
- exit ( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment