From 2b354dad5a518e64f8160a2385194a1e6c99c4ce Mon Sep 17 00:00:00 2001
From: "Israel B. Sant'Anna" <ibsa14@inf.ufpr.br>
Date: Tue, 24 Nov 2015 15:50:04 -0200
Subject: [PATCH] Implemented local cd and ls. Created Protocol class.

Signed-off-by: Israel B. Sant'Anna <ibsa14@inf.ufpr.br>
---
 .gitignore       |  3 +++
 Makefile         | 14 ++++++++++++++
 Protocol.cpp     | 14 ++++++++++++++
 Protocol.h       | 17 +++++++++++++++++
 client.cpp       | 29 +++++++++++++++++++++++++++++
 definitions.h    | 18 ++++++++++++++++++
 dirFunctions.cpp | 28 ++++++++++++++++++++++++++++
 dirFunctions.h   | 12 ++++++++++++
 8 files changed, 135 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 Protocol.cpp
 create mode 100644 Protocol.h
 create mode 100644 client.cpp
 create mode 100644 definitions.h
 create mode 100644 dirFunctions.cpp
 create mode 100644 dirFunctions.h

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1c6283f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.o
+cacoclient
+cacoserver
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..15d2058
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+CFLAGS= -g -Wall -O2
+CC=g++
+
+OBJ=Protocol.o
+
+
+%.o: %.cpp
+	$(CC) $(CFLAGS) $^ -c -o $@
+cacoclient: client.cpp $(OBJ)
+	$(CC) $(CFLAGS) $^ -o $@
+# cacoserver: $(OBJ) client.cpp
+# 	$(CC) $(CFLAGS) $^ -o $@
+clean:
+	rm -f *.o caco*
diff --git a/Protocol.cpp b/Protocol.cpp
new file mode 100644
index 0000000..146b712
--- /dev/null
+++ b/Protocol.cpp
@@ -0,0 +1,14 @@
+#include "Protocol.h"
+#include "definitions.h"
+
+Message Protocol::getMessage(){
+    return message;
+}
+
+void Protocol::setMessage(Message message){
+    this->message = message;
+}
+
+Protocol::Protocol(){
+    message.begin = 0x7E;
+}
\ No newline at end of file
diff --git a/Protocol.h b/Protocol.h
new file mode 100644
index 0000000..e361922
--- /dev/null
+++ b/Protocol.h
@@ -0,0 +1,17 @@
+#ifndef __PROTOCOL__
+#define __PROTOCOL__
+#include "definitions.h"
+
+class Protocol{
+
+private:
+    Message message;
+    vector<char> data;
+public:
+
+    Message getMessage();
+    void setMessage(Message message);
+
+    Protocol();
+};
+#endif
\ No newline at end of file
diff --git a/client.cpp b/client.cpp
new file mode 100644
index 0000000..7b9d490
--- /dev/null
+++ b/client.cpp
@@ -0,0 +1,29 @@
+#include "definitions.h"
+#include "dirFunctions.h"
+
+int main(){
+    
+    while(true){
+        string command, path;
+        cout << endl << "Entre com o comando:" << endl;
+        cin >> command;
+        if(command == "quit"){
+            break;
+        }
+        cin >> path;
+        if(command == "cd"){
+            cd(path);
+        }else if(command == "ls"){
+            ls(path);
+        }else if(command == "cdr"){
+            //TODO
+        }else if(command == "lsr"){
+            //TODO
+        }else if(command == "put"){
+            //TODO
+        }else if(command == "get"){
+            //TODO
+        }
+    }
+    return 0;
+}
\ No newline at end of file
diff --git a/definitions.h b/definitions.h
new file mode 100644
index 0000000..6bfad53
--- /dev/null
+++ b/definitions.h
@@ -0,0 +1,18 @@
+#ifndef __DEFINITIONS__
+#define __DEFINITIONS__
+
+#include <iostream>
+#include <string.h>
+#include <vector>
+
+using namespace std;
+
+typedef struct {
+    int begin   : 8,
+        size    : 6,
+        sequence: 6,
+        type    : 4,
+        parity  : 8;
+}Message;
+
+#endif
\ No newline at end of file
diff --git a/dirFunctions.cpp b/dirFunctions.cpp
new file mode 100644
index 0000000..078e39c
--- /dev/null
+++ b/dirFunctions.cpp
@@ -0,0 +1,28 @@
+#include <iostream>
+#include <errno.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <string.h>
+
+using namespace std;
+
+void cd(string path){
+    if(chdir(path.c_str()) != 0){
+        cout << "Error: could not change directory." << endl;
+        cerr << strerror(errno) << endl;
+    }
+}
+
+void ls(string path){
+    //TODO: #1
+    struct dirent *entry;
+    DIR *dir = opendir(path.c_str());
+    if(dir != NULL){
+        while((entry=readdir(dir)) != NULL){
+           cout << entry->d_name << endl;
+        }
+        closedir(dir);
+    }else{
+        cout<<"Error: could not open directory."<<endl;
+    }
+}
\ No newline at end of file
diff --git a/dirFunctions.h b/dirFunctions.h
new file mode 100644
index 0000000..1cb2242
--- /dev/null
+++ b/dirFunctions.h
@@ -0,0 +1,12 @@
+#ifndef __DIRFUNCTIONS__
+#define __DIRFUNCTIONS__
+#include "dirFunctions.cpp"
+#include <string.h>
+
+using namespace std;
+
+void cd(string path);
+
+void ls(string path);
+
+#endif
\ No newline at end of file
-- 
GitLab