Skip to content

Commit

Permalink
Added Log class for logging purpose. Only definitions add for now. Mo…
Browse files Browse the repository at this point in the history
…dified way of calling file operations through Action method in FileHandle class. Added method for checking mime type of file. Added some IE codes.
  • Loading branch information
shashankkumar committed May 4, 2011
1 parent ee97497 commit 5cae90d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 49 deletions.
138 changes: 98 additions & 40 deletions FileHandle.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "FileHandle.h"

FileHandle::FileHandle(int fid, const char* pid, const char* language){
FileHandle::FileHandle(int fid, const char* pid, int tl, int ml, const char* language){
FileId = fid;
TimeLimit = tl;
MemoryLimit = ml;
TimeUsed=0.0;
MemoryUsed=0;
ProblemId = pid;
Expand All @@ -19,30 +21,59 @@ int FileHandle::FetchFile(){
int res = FileCurl.GetFileFromHTTP(FileId);
if(res==-1) return -1;
}
else {
strcpy(status, "IE");
strcpy(detailstatus, "Cannot download file. No method specified for downloading!!!");
res = -1;
return -1;
}
return 0;
}

int FileHandle::MakeDir(){
int ErrNo;
char dirString[100];
sprintf(dirString, "%s%d",FILEPATH, FileId);
ToLogs("Creating directory with File Id");
if( mkdir(dirString, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH | S_IWOTH)==-1){
ErrNo=errno;
if(ErrNo==17) ToLogs("Directory already created.. Continuing");
else {
sprintf(dirString, "IE ERROR Failure to create directory. Error Number: %d", errno);
ToLogs(dirString);
return -1;
int FileHandle::CheckMime(){
FILE *fpipe;
char line[256];
sprintf(command, "file -b --mime-type %s%d.%s", FILEPATH, FileId, lang);

if ( !(fpipe = (FILE*)popen(command,"r")) ){
// If fpipe is NULL
perror("Problems with pipe");
ToLogs("Problems with pipe");
return -1;
}
else{
if ( fgets( line, sizeof line, fpipe)){
if(strncmp(line, "text", 4) != 0){
result = false;
strcpy(status, "CE");
strcpy(detailstatus, "Not a text file.");
}
return 0;
}
sprintf(systemString, "cp %s%d.txt %s%d/main.%s", FILEPATH, FileId, FILEPATH, FileId, lang);
ToLogs(systemString);
if(system(systemString)==-1){
ToLogs("Error in copying dowloaded file.");
}
pclose(fpipe);
}
int FileHandle::MakeDir(){
int ErrNo;
char dirString[100];
sprintf(dirString, "%s%d",FILEPATH, FileId);
ToLogs("Creating directory with File Id");
if( mkdir(dirString, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH | S_IWOTH)==-1){
ErrNo=errno;
if(ErrNo==17) ToLogs("Directory already created.. Continuing");
else {
sprintf(dirString, "IE ERROR Failure to create directory. Error Number: %d", errno);
ToLogs(dirString);
return -1;
}
return 0;
}
sprintf(systemString, "cp %s%d.txt %s%d/main.%s", FILEPATH, FileId, FILEPATH, FileId, lang);
ToLogs(systemString);
if(system(systemString)==-1){
ToLogs("Error in copying dowloaded file.");
return -1;
}
return 0;
}

void FileHandle::Compile(){
Expand Down Expand Up @@ -138,30 +169,30 @@ void FileHandle::PipeExecute(){
}

void FileHandle::Execution(){
for(TestCaseId=1; TestCaseId<=NoOfInputFiles; TestCaseId++){
PipeExecute();
strcpy(str, ExecutionStr.c_str()); ToLogs(str);
token = strtok(str, " \n");
strcpy(status, token);
if(strcmp(token, "AC")!=0) result=false;
if(strcmp(token, "RE")==0 || strcmp(token, "IE")==0 ){
token = strtok(NULL, "\n");
strcpy(detailstatus, token);
}
token = strtok(NULL, " \n"); sprintf(tmp, "%s", token);
TimeUsed+=(float)atof(tmp);
if(TimeUsed>(float)TimeLimit){
result = false;
sprintf(status, "TLE");
sprintf(detailstatus, "\0");
}
if(result==false) break;
for(TestCaseId=1; TestCaseId<=NoOfInputFiles; TestCaseId++){
PipeExecute();
strcpy(str, ExecutionStr.c_str()); ToLogs(str);
token = strtok(str, " \n");
strcpy(status, token);
if(strcmp(token, "AC")!=0) result=false;
if(strcmp(token, "RE")==0 || strcmp(token, "IE")==0 ){
token = strtok(NULL, "\n");
strcpy(detailstatus, token);
}

for(TestCaseId=1; (result==true && TestCaseId<=NoOfInputFiles); TestCaseId++){
MatchOutput();
if(result==false) strcpy(status,"WA");
token = strtok(NULL, " \n"); sprintf(tmp, "%s", token);
TimeUsed+=(float)atof(tmp);
if(TimeUsed>(float)TimeLimit){
result = false;
sprintf(status, "TLE");
sprintf(detailstatus, "\0");
}
if(result==false) break;
}

for(TestCaseId=1; (result==true && TestCaseId<=NoOfInputFiles); TestCaseId++){
MatchOutput();
if(result==false) strcpy(status,"WA");
}
}

void FileHandle::pipeMatch(){
Expand Down Expand Up @@ -205,3 +236,30 @@ void FileHandle::CleanUp(){
sprintf(systemString, "rm -rf %s%d", FILEPATH, FileId);
system(systemString);
}

void FileHandle::Action(){

if(FetchFile() == -1) return;
if(CheckMIME() == -1) return;
if(result==false) return;
if(MakeDir()==-1) return;

Compile();

if(result==false) return;

if(PrepareToExecute()==-1){
CleanUp();
return;
}

Execution(TimeLimit, MemoryLimit);

F->SendResults();
//CleanUp();

}

FileHandle::~FileHandle(){
//CleanUp();
}
12 changes: 8 additions & 4 deletions FileHandle.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef FILEHANDLE
#define FILEHANDLE
#ifndef FILEHANDLE_H
#define FILEHANDLE_H
#include "includes.h"
#include "CurlWrapper.h"
class FileHandle{
Expand All @@ -12,9 +12,12 @@ class FileHandle{
string CompileOutput;
CurlWrapper FileCurl;
string ExecutionStr;
bool result;
public:
FileHandle(int fileid, const char* pid, const char* l);
FileHandle(int fileid, const char* pid, int tl, int ml, const char* l);
~FileHandle();
int FetchFile();
int CheckMIME();
int MakeDir();
void Compile();
void pipeCompile();
Expand All @@ -26,7 +29,8 @@ class FileHandle{
void pipeMatch();
void SendResults();
void CleanUp();
bool result;

void Action();

};
#endif
1 change: 0 additions & 1 deletion FileWrapper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
void FileThread(int FileId, const char* ProblemId, int TimeLimit, int MemoryLimit, const char* lang){

FileHandle *F = new FileHandle(FileId, ProblemId, lang);
int TestCaseId; int ErrNo;

if(F->FetchFile()==-1) return;
if(F->MakeDir()==-1) return;
Expand Down
14 changes: 14 additions & 0 deletions Logs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LOGS_H
#define LOGS_H
#include "includes.h"

class Logs{
private:
public:
Logs();
Logs(int FileId);
ToLogs(const char* logs);
ToLogs(char* logs);
~Logs();
};
#endif
5 changes: 3 additions & 2 deletions config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CONFIG
#define CONFIG
#ifndef CONFIG_H
#define CONFIG_H
#define USERNAME ""
#define PASSWORD ""
#define FTPUSERNAME ""
Expand All @@ -13,6 +13,7 @@
#define TESTCASESPATH "./TestCases/"
#define URLToSendResults "http://192.168.208.208/codevillage/update_result2.php"
#define FetchFileFromFTP true
#define LOGFILEPATH "./Logs/logs.txt"

FILE * logFile;

Expand Down
6 changes: 4 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ int main(){
printf("FileId: %d ProblemId: %s TimeLimit: %d MemoryLimit: %d lang: %s\n", FileId, ProblemId, TimeLimit, MemoryLimit, lang);

// FileThread to carry out grading of the file
FileThread(FileId, ProblemId, TimeLimit, MemoryLimit, lang);

//FileThread(FileId, ProblemId, TimeLimit, MemoryLimit, lang);
FileHandle *F = new FileHandle(FileId, ProblemId, TimeLimit, MemoryLimit, lang);
F->Action();
delete F;
}
delete C; //Clean up
ToLogs("Going to sleep for 5 seconds");
Expand Down

0 comments on commit 5cae90d

Please sign in to comment.