Skip to content

Commit

Permalink
final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HossamElharmeil committed Jun 2, 2020
0 parents commit d5bebd3
Show file tree
Hide file tree
Showing 108 changed files with 3,261 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ADD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ADD.h"
#include <iostream>
ADD::ADD(){}
void ADD::er(int n,VAR* result,...){ // constructor that takes integer to represent the number of passed parameters, and a variadic number of parameters
va_list args; // list of all variadic arguments
va_start(args,result); // starting point of variadic parameters
cout<<result->get_val_real()<< endl;
double res=0; // accumulator to sum the parameters on
for(int i =0;i<n;i++){ // looping on the parameters passed to the constructor
try{ // to handle errors
double x =va_arg(args,double); // setting the passed parameter to double
res+=x; // accumulating
}catch(const exception& e){throw nonNumeric("ADD02","one or more of the parameters are non numerical","ADD");} // error of kind ADD02 , contain the error message


//std::cout<<fixed<<res<<endl;

}
va_end(args);
result->ASSGN(res); // assigning what is on the accumulator to the result
}

ADD::~ADD(){} // destructor
25 changes: 25 additions & 0 deletions ADD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef ADD_H
#define ADD_H
//#include <bits/stdc++.h>
#include <stdint.h>
#include <string>
#include <map>
#include <stdarg.h>

#include "OPERATION.h"
#include "VAR.h"
using namespace std;

class ADD : public OPERATION{ // inherited class from the operation class

public:
ADD();
virtual void er(int,VAR*,...); // variedic constructor
virtual ~ADD(); // virtual destructor

protected:

private:
};

#endif // ADD_H
20 changes: 20 additions & 0 deletions ASSIGN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "ASSIGN.h"

ASSIGN::ASSIGN(){}

void ASSIGN::er (int n,VAR* result,...){ // performing assign
va_list args; // list of all variadic arguments
va_start(args,result); // starting point of variadic parameters

string type = result->getType(); // type of the variable

if(type=="STRING")result->ASSGN(va_arg(args,string)); // checking for type and assign accordingly
else if(type=="CHAR")result->ASSGN(char(va_arg(args,string)[0]));
else if(type=="NUMERIC")result->ASSGN(stod(va_arg(args,string)));
else if(type=="REAL")result->ASSGN(stod(va_arg(args,string)));


}


ASSIGN::~ASSIGN(){}
18 changes: 18 additions & 0 deletions ASSIGN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ASSIGN_H
#define ASSIGN_H

#include "OPERATION.h"


class ASSIGN:public OPERATION{ // assigning .. inherited from operation
public:
ASSIGN();
virtual void er (int n,VAR* result,...); // variadic function to take different number of parameters according to variable
virtual ~ASSIGN();

protected:

private:
};

#endif // ASSIGN_H
27 changes: 27 additions & 0 deletions CHAR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "CHAR.h"

VAR* CHAR::clone(){ // cloner
VAR* temp =new CHAR(name,val); // creating new object
return temp; // returning the object
}


CHAR::CHAR(string nm,char data){ // constructor
val=data; // setting to data
name=nm; // setting to name
type="CHAR";
}
char CHAR::get_val_char(){ // function for getting character value
return val;
}
void CHAR::ASSGN(char data){ // assigning
val = data;
}

CHAR::operator char() const{return val;}



//virtual auto getValue()->char{return get_val_char();}

CHAR::~CHAR(){}
32 changes: 32 additions & 0 deletions CHAR.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef CHAR_H
#define CHAR_H
//#include <bits/stdc++.h>
#include <stdint.h>
#include <string>
#include <map>
#include <stdarg.h>

#include "VAR.h"

using namespace std;


class CHAR : public VAR{ // inherited class from variables class
public:
CHAR(string,char); // constructor

virtual operator char() const;
//virtual auto getValue()->char;
virtual char get_val_char(); // getting character value
virtual void ASSGN(char); // assigning
virtual VAR * clone(); // cloner
virtual ~CHAR(); // destructor

protected:

private:
char val; // char value
string name; // char name
};

#endif // CHAR_H
84 changes: 84 additions & 0 deletions ClientMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <TCPclintSocket.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h> /* calloc, exit, free */
#include <vector>
#include <unordered_map>
#include <fstream>
using namespace std;

#define MAX 1514

void writeErr(char*err,string name) {
ofstream stream;
stream.open(name);
if (!stream)cout << "cold not creat file";
for (int i = 1; i < MAX; i++)stream << err[i];
cout << "err DONE!" << endl;
}
void writeOut(char*out, string name) {
ofstream stream;
stream.open(name);
if (!stream)cout << "cold not creat file";
for (int i = 1; i < MAX; i++)stream << out[i];
stream << '\n';
cout << "err DONE!" << endl;
}
int main(int argc, char** argv){
cout << "hi there"<<endl;

char *addr=NULL;
TCPclintSocket* x=new TCPclintSocket (argv[1], 2020);
TCPSocket*getServed=x->makeConnection(MAX,MAX);
std::cout<<"File name: "<<argv[2]<< endl;
vector<string> readFile = getServed->readFromFile(argv[2]);
std::cout << readFile[0].length() << endl;
char*data;
data = (char*)calloc(MAX, sizeof(char));
cout << "data done" << endl;
std::cout << argv[1] << endl<<endl;
int i = 0;
int j = 0;


while (j < readFile.size()) {
cout << readFile[j] << endl;
for (; i < readFile[0].length(); i++)data[i] = readFile[j][i];
for (; i<MAX; i++)data[i] = ' ';
i = 0;
cout << "setting data done" << endl;
std::cout << getServed->writeToSocket(data, MAX) << endl;
cout << "write to socket done" << endl;
j++;
}
cout << "in read" << endl;

char * data2;
data2 = (char*)calloc(MAX, sizeof(char));

while (true) {
getServed->readFromSocket(data2, MAX);
if (data2[0] == 'o') {
cout << "out " << data2[1] << endl;
writeOut(data2, "try.out");
}
else if (data2[0] == 'e') {
cout << "err " << endl;
writeErr(data2,"try.err");
break;

}
else break;
}
cout << "out read" << endl;


/*b
UDPServerSocket *serve=new UDPServerSocket();
serve->initialize(addr,9000);
std::cout<<serve->readFromSocketWithBlock(data2,MAX);
//std::cout<<"ya rab "<<serve->isEnabled()<<std::endl;
for(int i=0;i<MAX;i++)std::cout<<data2[i];
*/
}
50 changes: 50 additions & 0 deletions Connection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "Connection.h"
//Constructor: Call parent Thread Constructor
Connection::Connection(TCPSocket * p_tcpSocket): Thread()
{
tcpSocket = p_tcpSocket; // Set the TCP socket
next_connection = NULL; // Set the next pointer to NULL
}

void * Connection::threadMainBody (void * arg)
{
char file_name[1024]; // A buffer for holding the file name
memset (file_name,0,1024); // Initialize the buffer
// read from socket the file name to be fetched
int read_bytes = tcpSocket->readFromSocket(file_name,1023);
if ( read_bytes > 0) // If read successfully
{
// Clean up file name
if ( file_name[strlen(file_name)-1] == '\n' || file_name[strlen(file_name)-1] == '\r')
file_name[strlen(file_name)-1] = 0;
if ( file_name[strlen(file_name)-1] == '\n' || file_name[strlen(file_name)-1] == '\r')
file_name[strlen(file_name)-1] = 0;
FILE * f = fopen(file_name,"r"); // Try to open the file
if ( f != NULL) // If opened
{
fseek (f,0,2); // Seek to the end of the file
long fsize = ftell(f); // Get current location which represents the size
// Allocate a buffer with the file size to read the content
char * buffer = (char *) calloc(fsize+1,sizeof(char));
fseek (f,0,0); // Seek the beginning of the file
fread(buffer,1,fsize,f); // Read the whole file into the buffer
tcpSocket->writeToSocket(buffer,fsize); // Write the buffer to the socket
free(buffer); // Free the buffer
fclose(f); // Close the file
}
else
{
perror("Error With File\n"); // Print an error message
tcpSocket->writeToSocket("Error\n",6);// Write error to the socket
}
}
// Shutdown the TCP Socket
tcpSocket->shutDown();
return NULL;
}
// A modifier that sets the pointer of the next connection
void Connection::setNextConnection(Connection * connection){next_connection = connection;}
// A Selectot that returned a pointer to the next connection
Connection * Connection::getNextConnection (){return next_connection;}
// Destructor: delete the TCP socket if set
Connection::~Connection(){if ( tcpSocket != NULL ) delete (tcpSocket);}
18 changes: 18 additions & 0 deletions Connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "includes.h"
#include "Thread.h"
#include "TCPSocket.h"

// A class representing a single connection form a client
class Connection : public Thread //Inherit from Thread
{
private:
TCPSocket * tcpSocket; // TCP Socket for communication with client
Connection * next_connection; // A way to build a linked list of connections for the garbage collector to be able to track them
public:
Connection(TCPSocket * p_tcpSocket); // Constructor: Set client connected TCP socket
void * threadMainBody (void * arg); // Main thread body that serves the connection
void setNextConnection(Connection * connection); // Set the next pointer: used by the Garbage Collector
Connection * getNextConnection (); // Get a pointer to the next connection
~Connection(); // Destructor
};

19 changes: 19 additions & 0 deletions DIV.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "DIV.h"
DIV::DIV(){}
void DIV::er(int n,VAR* result,...){ // constructor
va_list args; // list of all variadic arguments
va_start(args,result); // starting point of variadic parameters

try{
double x =va_arg(args,double); // setting the passed parameter to double
double y =va_arg(args,double); // setting the passed parameter to double
if(y!=0){result->ASSGN(x/y);} // handling errors
else{throw ZeroDiv();}// another error type
}
catch(const exception& e){throw nonNumeric("DIV02","one or more of the parameters are non numerical","DIV");} // error type DIV02



}

DIV::~DIV(){}
25 changes: 25 additions & 0 deletions DIV.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef DIV_H
#define DIV_H
//#include <bits/stdc++.h>
#include <stdint.h>
#include <string>
#include <map>
#include <stdarg.h>

#include "VAR.h"
#include "OPERATION.h"

using namespace std;

class DIV: public OPERATION{ // inherited class from the operation
public:
DIV();
virtual void er(int,VAR*,...); // constructor
virtual ~DIV(); // virtual destructor



private:
};

#endif // DIV_H
12 changes: 12 additions & 0 deletions DoubleJump.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "DoubleJump.h"



DoubleJump::DoubleJump() // constructor
{
}


DoubleJump::~DoubleJump() // destructor
{
}
12 changes: 12 additions & 0 deletions DoubleJump.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "Jump.h"
class DoubleJump :
public Jump
{

public:
virtual int goJump(int x, int y) = 0;
DoubleJump();
~DoubleJump();
};

Loading

0 comments on commit d5bebd3

Please sign in to comment.