Skip to content

Commit

Permalink
CodeRunner now supports command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankkumar committed Nov 14, 2011
1 parent a5973eb commit d3049c5
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Logs/*
main
config.h
TestCases2/*
Execution
*_execution
7 changes: 2 additions & 5 deletions ContentParser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "includes.h"
#include "includeh.h"
#include "ContentParser.h"

int ContentParser::FetchFileInfoList(FileInfoFetchOptionsStruct* FileInfoFetchOptions){
Expand All @@ -9,15 +9,12 @@ int ContentParser::FetchFileInfoList(FileInfoFetchOptionsStruct* FileInfoFetchOp
delete CurlVar; //Clean up
return -1;
}
delete CurlVar;

FileInfoListStr = new char[strContent.size() + 1];
FileInfo = new FileInfoStruct();
strcpy(FileInfoListStr, strContent.c_str());

Logs::WriteLine(FileInfoListStr);

Ix = 0;
delete CurlVar;
return 0;
}

Expand Down
4 changes: 1 addition & 3 deletions ContentParser.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#ifndef CONTENTPARSER_H
#define CONTENTPARSER_H
#include "includes.h"
#include "includeh.h"
#include "CurlWrapper.h"
#include "Logs.h"
#include "FileInfo.h"

class ContentParser{
private:
char* FileInfoListStr;
int Ix;
FileInfoStruct* FileInfo;
FileInfoFetchOptionsStruct* FileInfoFetchOptions;
public:

int FetchFileInfoList(FileInfoFetchOptionsStruct* FileInfoFetchOptions);
Expand Down
14 changes: 9 additions & 5 deletions CurlWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ int CurlWrapper::FetchContentFromWebPage(FileInfoFetchOptionsStruct* FileInfoFet

char optstr[11];
if(FileInfoFetchOptions->f){
sprintf(optstr, "%s", FileInfoFetchOptionsS->FileInfo.FileId);
sprintf(optstr, "%d", FileInfoFetchOptions->FileInfo.FileId);
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "fileid", CURLFORM_COPYCONTENTS, optstr, CURLFORM_END);
}

if(FileInfoFetchOptions->p){
sprintf(optstr, "%s", FileInfoFetchOptionsS->FileInfo.ProblemId);
sprintf(optstr, "%s", FileInfoFetchOptions->FileInfo.ProblemId);
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "problemid", CURLFORM_COPYCONTENTS, optstr, CURLFORM_END);
}

if(FileInfoFetchOptions->l){
sprintf(optstr, "%s", FileInfoFetchOptionsS->FileInfo.lang);
sprintf(optstr, "%s", FileInfoFetchOptions->FileInfo.lang);
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "lang", CURLFORM_COPYCONTENTS, optstr, CURLFORM_END);
}

Expand Down Expand Up @@ -163,14 +163,13 @@ int CurlWrapper::FetchContentFromWebPage(FileInfoFetchOptionsStruct* FileInfoFet
sprintf(logs, "Failure to fetch File Ids. Curl Error code: %d", res);
Logs::WriteLine(logs, true);
return -1;

}
else{
sprintf(logs, "File Ids fetched succesfully.");
Logs::WriteLine(logs, true);
Logs::WriteLine(buffer.c_str());
return 0;
}

}
return -1; // Control should not reach here in normal circumstances
}
Expand All @@ -193,6 +192,9 @@ void CurlWrapper::SendResultsToWebpage(const char* fileid, const char* status, c
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "detailstatus", CURLFORM_COPYCONTENTS, detailstatus, CURLFORM_END);
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "time", CURLFORM_COPYCONTENTS, time, CURLFORM_END);
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "memory", CURLFORM_COPYCONTENTS, memory, CURLFORM_END);
if(ForcePushResult)
curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "force", CURLFORM_COPYCONTENTS, "true", CURLFORM_END);

curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
headerlist = curl_slist_append(headerlist, buf);
Expand Down Expand Up @@ -223,3 +225,5 @@ void CurlWrapper::SendResultsToWebpage(const char* fileid, const char* status, c

}
}

bool CurlWrapper::ForcePushResult=true;
26 changes: 15 additions & 11 deletions CurlWrapper.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#ifndef CURLWRAPPER
#define CURLWRAPPER
#include "includes.h"
#include "Logs.h"
#include "includeh.h"
#include "FileInfo.h"

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "config.h"

class CurlWrapper{
public:

CURL *curl;
CURLcode res;
char logs[1000];

struct FtpFile {
const char *filename;
FILE *stream;
};

public:
CurlWrapper(){
curl_global_init(CURL_GLOBAL_DEFAULT);
Logs::OpenLogFile();
Expand All @@ -18,20 +28,14 @@ class CurlWrapper{
curl_global_cleanup();
}

struct FtpFile {
const char *filename;
FILE *stream;
};

static size_t ContentInFileFTP(void *buffer, size_t size, size_t nmemb, void *stream);
static size_t ContentInVar(void* ptr, size_t size, size_t nmemb, string *stream);
static size_t ContentInFileHTTP(void *ptr, size_t size, size_t nmemb, FILE *stream);

static bool ForcePushResult;
int GetFileFromFTP(int FileId);
int GetFileFromHTTP(int FileId);
int FetchContentFromWebPage(FileInfoFetchOptionsStruct* FileInfoFetchOptions, string *content);
void SendResultsToWebpage(const char* fileid, const char* status, const char* detailstatus, const char* time, const char* memory);

};

#endif
8 changes: 5 additions & 3 deletions FileHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ int FileHandle::PrepareToExecute(){
void FileHandle::PipeExecute(){
FILE *fpipe;
if(strcmp(FileInfo->lang,"cpp")==0 || strcmp(FileInfo->lang,"c")==0){
sprintf(command, "./Execution %d %d %d %d %s", FileInfo->FileId, TestCaseId, FileInfo->TimeLimit, MemoryLimit, FileInfo->lang);
sprintf(command, "./cpp_execution %d %d %d %d %s", FileInfo->FileId, TestCaseId, FileInfo->TimeLimit, MemoryLimit, FileInfo->lang);
}
else if(strcmp(FileInfo->lang, "java")==0){
sprintf(command, "./java_Execution %d %d %d %d %s", FileInfo->FileId, TestCaseId, FileInfo->TimeLimit, FileInfo->MemoryLimit, FileInfo->lang);
sprintf(command, "./java_execution %d %d %d %d %s", FileInfo->FileId, TestCaseId, FileInfo->TimeLimit, FileInfo->MemoryLimit, FileInfo->lang);
}

char line[1024];
Expand Down Expand Up @@ -300,5 +300,7 @@ void FileHandle::Action(){
}

FileHandle::~FileHandle(){
;//CleanUp();
if(Clean) CleanUp();
}

bool FileHandle::Clean=true;
13 changes: 9 additions & 4 deletions FileHandle.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#ifndef FILEHANDLE_H
#define FILEHANDLE_H
#include "includes.h"
#include "includeh.h"
#include "CurlWrapper.h"
#include "Logs.h"
#include "FileInfo.h"

class FileHandle{
private:
int FileId, MemoryUsed, NoOfTestCases, TestCaseId, TimeLimit, MemoryLimit;
float TimeUsed;
char systemString[100], status[10], logs[10000], detailstatus[10000], str[100], *token, tmp[10], logString[100], TestCaseExecutionTime[10];
char FileAddr[10], timeused[10], memoryused[10], fileid[10], command[1000];
char systemString[100];
char status[10], logs[10000], detailstatus[10000];
char str[100], *token, tmp[10];
char logString[100];
char TestCaseExecutionTime[10];
char FileAddr[10], timeused[10], memoryused[10], fileid[10];
char command[1000];
const char* lang, *ProblemId;
string CompileOutput;
CurlWrapper FileCurl;
Expand All @@ -33,6 +37,7 @@ class FileHandle{
void pipeMatch();
void SendResults();
void CleanUp();
static bool Clean;

bool getResult();
void FileOperations();
Expand Down
4 changes: 2 additions & 2 deletions FileInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ struct FileInfoStruct{
};

struct FileInfoFetchOptionsStruct{
bool f=false, p=false, l=false, b=false;
FileInfoSturct FileInfo;
bool f, p, l, b;
FileInfoStruct FileInfo;
void Init(){
f=p=l=b=false;
}
Expand Down
2 changes: 1 addition & 1 deletion Logs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef LOGS_H
#define LOGS_H
#include "includes.h"
#include "includeh.h"

class Logs{
private:
Expand Down
19 changes: 12 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
main : main.o FileHandle.o CurlWrapper.o Logs.o ContentParser.o includes.h config.h -lcurl
VAR=includeh.h includes.h config.h Logs.h
main : main.o FileHandle.o CurlWrapper.o Logs.o ContentParser.o *_execution -lcurl
g++ -o main main.o FileHandle.o CurlWrapper.o ContentParser.o Logs.o -lcurl

main.o : main.cpp includes.h ContentParser.h FileHandle.h Logs.h config.h
main.o : main.cpp ContentParser.h FileHandle.h $(VAR)
g++ -c main.cpp

FileHandle.o : FileHandle.cpp FileHandle.h CurlWrapper.h Logs.h includes.h config.h
FileHandle.o : FileHandle.cpp FileHandle.h CurlWrapper.h $(VAR)
g++ -c FileHandle.cpp

CurlWrapper.o : CurlWrapper.cpp CurlWrapper.h Logs.h includes.h config.h
CurlWrapper.o : CurlWrapper.cpp CurlWrapper.h $(VAR)
g++ -c CurlWrapper.cpp

ContentParser.o : ContentParser.cpp ContentParser.h CurlWrapper.h includes.h config.h Logs.h
ContentParser.o : ContentParser.cpp ContentParser.h CurlWrapper.h $(VAR)
g++ -c ContentParser.cpp

Logs.o : Logs.cpp Logs.h includes.h config.h
Logs.o : Logs.cpp $(VAR)
g++ -c Logs.cpp

.PHONY: clean
clean:
rm *.o main
rm *.o main *_execution

*_execution : cpp_execution.cpp java_execution.cpp
g++ cpp_execution.cpp -o cpp_execution
g++ java_execution.cpp -o java_execution
7 changes: 4 additions & 3 deletions Execution.cpp → cpp_execution.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "includex.h"
#include "includes.h"
#include "resources.h"

pid_t cpid;
void ToPipe(const char* str){
printf("%s\n", str);
return;
}

void signalHandler(int signum){
if(cpid!=0){
killpg(cpid,SIGKILL);
}
}

int main(int args, char *argv[]){

if(args<4){
Expand Down Expand Up @@ -112,7 +114,7 @@ int main(int args, char *argv[]){
else { /* Code executed by parent */

struct rusage resourceUsage;
w = wait4 (cpid, &status, NULL, &resourceUsage);
w = wait4 (cpid, &status, 0, &resourceUsage);

gettimeofday(&finish,NULL);
t_sec = finish.tv_sec-start.tv_sec;
Expand Down Expand Up @@ -170,4 +172,3 @@ int main(int args, char *argv[]){
else if(sigalrm) printf("SIGALRM");
}
}

6 changes: 6 additions & 0 deletions includeh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef INCLUDEH_H
#define INCLUDEH_H
#include "includes.h"
#include "config.h"
#include "Logs.h"
#endif
6 changes: 0 additions & 6 deletions includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@
#include <signal.h>
#include <limits.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#include "config.h"

using namespace std;
4 changes: 2 additions & 2 deletions Java_Execution.cpp → java_execution.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "includex.h"
#include "includes.h"
#include "resources.h"

pid_t cpid;
Expand Down Expand Up @@ -113,7 +113,7 @@ int main(int args, char *argv[]){
else { /* Code executed by parent */

struct rusage resourceUsage;
w = wait4 (cpid, &status, NULL, &resourceUsage);
w = wait4 (cpid, &status, 0, &resourceUsage);

gettimeofday(&finish,NULL);
t_sec = finish.tv_sec-start.tv_sec;
Expand Down
18 changes: 10 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ You may contact the author of CodeRunner by e-mail at:
[email protected]
****************************************************************************/
#include "includes.h"
#include "includeh.h"
#include "FileInfo.h"
#include "ContentParser.h"
#include "FileHandle.h"
#include "Logs.h"

int main(int argc, char* argv[])
{
Expand All @@ -37,22 +36,25 @@ int main(int argc, char* argv[])
int opt;
FileInfoFetchOptionsStruct* FileInfoFetchOptions = new FileInfoFetchOptionsStruct();
FileInfoFetchOptions->Init();
while((opt = getopt(argc, argv, "bf:p:l:")) != -1){
while((opt = getopt(argc, argv, "cbf:p:l:")) != -1){
switch(opt){
case 'f':
FileInfoFetchOptions->f=true;
FileInfoFetchOptions->FileInfo.FileId = atoi(optarg);
break;
case 'p':
FileInfoFetchOptions->p=true
FileInfoFetchOptions->p=true;
strcpy(FileInfoFetchOptions->FileInfo.ProblemId, optarg);
break;
case 'l':
FileInfoFetchOptions->l=true
FileInfoFetchOptions->l=true;
strcpy(FileInfoFetchOptions->FileInfo.lang, optarg);
break;
case 'b':
FileInfoFetchOptions->b=true
CurlWrapper::ForcePushResult=true;
break;
case 'c':
FileHandle::Clean=true;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-f fileid | [-p problemcode] [-l language]] [-b]", argv[0]);
Expand All @@ -71,8 +73,8 @@ int main(int argc, char* argv[])
do{
Logs::OpenLogFile();
bool CurrentIteration = true;
ContentParser *ContentVar = new ContentParser(FileInfoFetchOptions);
if(ContentVar->FetchFileInfoList()==-1){
ContentParser *ContentVar = new ContentParser();
if(ContentVar->FetchFileInfoList(FileInfoFetchOptions)==-1){
CurrentIteration = false;
}

Expand Down

0 comments on commit d3049c5

Please sign in to comment.