From 6ba21fdca02971244d90a95f88117a9c5eb0a64f Mon Sep 17 00:00:00 2001
From: Andrea Zoli <zoli@iasfbo.inaf.it>
Date: Fri, 18 Jul 2014 15:06:31 +0200
Subject: [PATCH] Add openDevice() for Input and Output classes.

---
 include/Input.h              |  4 ++++
 include/InputFile.h          |  5 ++++-
 include/InputSerial.h        |  4 +++-
 include/InputSocketServer.h  |  2 ++
 include/Output.h             |  4 ++++
 include/OutputFile.h         |  6 +++++-
 include/OutputSerial.h       |  4 +++-
 include/OutputSocketClient.h |  4 +++-
 src/InputFile.cpp            |  7 +++++++
 src/InputSerial.cpp          | 18 ++++++++++++++++--
 src/InputSocketServer.cpp    | 11 +++++++++++
 src/OutputFile.cpp           | 12 ++++++++++++
 src/OutputSerial.cpp         | 12 ++++++++++--
 src/OutputSocketClient.cpp   | 14 ++++++++++----
 14 files changed, 94 insertions(+), 13 deletions(-)

diff --git a/include/Input.h b/include/Input.h
index 031d799..a33f8f4 100644
--- a/include/Input.h
+++ b/include/Input.h
@@ -20,6 +20,8 @@
 
 #include "PacketExceptionIO.h"
 #include "ByteStream.h"
+#include <vector>
+#include <string>
 
 namespace PacketLib
 {
@@ -35,6 +37,8 @@ public:
 
     virtual bool open( char** parameters ) throw(PacketExceptionIO*) = 0;
 
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*) = 0;
+
     virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*) = 0;
 
     virtual char* readString() throw(PacketExceptionIO*) = 0;
diff --git a/include/InputFile.h b/include/InputFile.h
index 8b04847..596e1a5 100644
--- a/include/InputFile.h
+++ b/include/InputFile.h
@@ -35,6 +35,9 @@ public:
 
     virtual bool open( char** parameters ) throw(PacketExceptionIO*);
 
+	/// parameters[0] = filename
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*);
+
     virtual void close() throw(PacketExceptionIO*);
 
     virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*);
@@ -52,7 +55,7 @@ protected:
 
     File* file;
 
-    char* filename;
+    std::string filename;
 
 };
 }
diff --git a/include/InputSerial.h b/include/InputSerial.h
index e00b525..40836f4 100644
--- a/include/InputSerial.h
+++ b/include/InputSerial.h
@@ -35,6 +35,8 @@ public:
 
     virtual bool open( char** parameters ) throw(PacketExceptionIO*);
 
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*);
+
     virtual void close() throw(PacketExceptionIO*);
 
     virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*);
@@ -50,7 +52,7 @@ protected:
 
     Serial* serial;
 
-    char*  device;
+    std::string device;
     int  flag;
 
 };
diff --git a/include/InputSocketServer.h b/include/InputSocketServer.h
index 30ac632..715ea17 100644
--- a/include/InputSocketServer.h
+++ b/include/InputSocketServer.h
@@ -35,6 +35,8 @@ public:
 
     virtual bool open( char** parameters ) throw(PacketExceptionIO*);
 
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*);
+
     virtual void close() throw(PacketExceptionIO*);
 
     virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*);
diff --git a/include/Output.h b/include/Output.h
index 980dbf5..a82f4ec 100644
--- a/include/Output.h
+++ b/include/Output.h
@@ -21,6 +21,8 @@
 #include "PacketLibDefinition.h"
 #include "ByteStream.h"
 #include "PacketExceptionIO.h"
+#include <vector>
+#include <string>
 
 namespace PacketLib
 {
@@ -35,6 +37,8 @@ public:
 
     virtual bool open(char** parameters) throw(PacketExceptionIO*) = 0;
 
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*) = 0;
+
     virtual bool isClosed();
 
     virtual bool isBigendian();
diff --git a/include/OutputFile.h b/include/OutputFile.h
index d53d425..5b18637 100644
--- a/include/OutputFile.h
+++ b/include/OutputFile.h
@@ -40,6 +40,10 @@ public:
 	///second parameter: fopen modes: w, r, a (optional)
     virtual bool open(char** parameters) throw(PacketExceptionIO*);
 
+	/// parameters[0] = filename
+	/// parameters[1] = modes: w, a (default w)
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*);
+
     virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*);
@@ -53,7 +57,7 @@ protected:
 
     File* file;
 
-    char* filename;
+    std::string filename;
 
 };
 
diff --git a/include/OutputSerial.h b/include/OutputSerial.h
index 0a316a0..f2cdcfe 100644
--- a/include/OutputSerial.h
+++ b/include/OutputSerial.h
@@ -38,6 +38,8 @@ public:
 
     virtual bool open(char** parameters) throw(PacketExceptionIO*);
 
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*);
+
     virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*);
@@ -50,7 +52,7 @@ public:
 protected:
 
 
-    char* device;
+    std::string device;
     Serial* serial;
     int	flag;
 
diff --git a/include/OutputSocketClient.h b/include/OutputSocketClient.h
index 1ad7838..c574009 100644
--- a/include/OutputSocketClient.h
+++ b/include/OutputSocketClient.h
@@ -37,6 +37,8 @@ public:
 
     virtual bool open(char** parameters) throw(PacketExceptionIO*);
 
+    virtual void openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*);
+
     virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*);
@@ -50,7 +52,7 @@ protected:
 
     SocketClient* socketclient;
 
-    char* host;
+    std::string host;
 
     int port;
 };
diff --git a/src/InputFile.cpp b/src/InputFile.cpp
index bb57767..31ff2d8 100644
--- a/src/InputFile.cpp
+++ b/src/InputFile.cpp
@@ -41,6 +41,13 @@ bool InputFile::open( char** parameters ) throw(PacketExceptionIO*)
     return true;
 }
 
+void InputFile::openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*)
+{
+    file->open(parameters[0].c_str(), (char*) "r");
+    filename = parameters[0];
+    closed = false;
+    eof = file->isEOF();
+}
 
 void InputFile::close()  throw(PacketExceptionIO*)
 {
diff --git a/src/InputSerial.cpp b/src/InputSerial.cpp
index b2b3d26..416a0b2 100644
--- a/src/InputSerial.cpp
+++ b/src/InputSerial.cpp
@@ -16,6 +16,7 @@
  ***************************************************************************/
 
 #include "InputSerial.h"
+#include <sstream>
 
 using namespace PacketLib;
 
@@ -38,14 +39,27 @@ bool InputSerial::open( char** parameters ) throw(PacketExceptionIO*)
     flag   = atoi( parameters[1] );
     device = parameters[0];
     //cout << "SSSSS: " << O_NONBLOCK << endl;
-    serial->open( device, O_NONBLOCK);
+    serial->open((char*)device.c_str(), O_NONBLOCK);
     serial->dump();
     serial->close();
-    serial->open( device, flag );
+    serial->open((char*)device.c_str(), flag);
     closed = false;
     return true;
 }
 
+void InputSerial::openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*)
+{
+	std::istringstream ss(parameters[1]);
+	ss >> flag;
+    device = parameters[0];
+
+    serial->open((char*)device.c_str(), O_NONBLOCK);
+    serial->dump();
+    serial->close();
+    serial->open((char*)device.c_str(), flag);
+
+    closed = false;
+}
 
 
 void InputSerial::close()  throw(PacketExceptionIO*)
diff --git a/src/InputSocketServer.cpp b/src/InputSocketServer.cpp
index 7cb5853..41d09c4 100644
--- a/src/InputSocketServer.cpp
+++ b/src/InputSocketServer.cpp
@@ -16,6 +16,7 @@
  ***************************************************************************/
 
 #include "InputSocketServer.h"
+#include <sstream>
 
 using namespace PacketLib;
 
@@ -46,7 +47,17 @@ bool InputSocketServer::open( char** parameters ) throw(PacketExceptionIO*)
     return true;
 }
 
+void InputSocketServer::openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*)
+{
+	std::istringstream ss(parameters[0]);
+	ss >> port;
+    socketserver = new SocketServer(bigendian, port);
 
+    new_sock = new SocketServer(bigendian);
+    accepted = false;
+
+    closed = false;
+}
 
 void InputSocketServer::close() throw(PacketExceptionIO*)
 {
diff --git a/src/OutputFile.cpp b/src/OutputFile.cpp
index 8725e48..9343b13 100644
--- a/src/OutputFile.cpp
+++ b/src/OutputFile.cpp
@@ -55,6 +55,18 @@ bool OutputFile::open(char** parameters) throw(PacketExceptionIO*)
     return true;
 }
 
+void OutputFile::openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*)
+{
+	std::string mode = "w";
+
+	if(parameters.size() > 1)
+		mode = parameters[2];
+
+	file->open(parameters[0].c_str(), mode.c_str());
+
+    filename = parameters[0];
+    isclosed = false;
+}
 
 
 bool OutputFile::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
diff --git a/src/OutputSerial.cpp b/src/OutputSerial.cpp
index c594ff2..8ae2157 100644
--- a/src/OutputSerial.cpp
+++ b/src/OutputSerial.cpp
@@ -16,6 +16,7 @@
  ***************************************************************************/
 
 #include "OutputSerial.h"
+#include <sstream>
 
 using namespace PacketLib;
 
@@ -46,12 +47,19 @@ bool OutputSerial::open(char** parameters) throw(PacketExceptionIO*)
 {
     flag   = atoi( parameters[1] );
     device = parameters[0];
-    serial->open( device, flag );
+    serial->open((char*)device.c_str(), flag);
     isclosed = false;
     return true;
 }
 
-
+void OutputSerial::openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*)
+{
+    device = parameters[0];
+	std::istringstream ss(parameters[1]);
+	ss >> flag;
+    serial->open((char*)device.c_str(), flag);
+    isclosed = false;
+}
 
 bool OutputSerial::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
 {
diff --git a/src/OutputSocketClient.cpp b/src/OutputSocketClient.cpp
index 1a0f707..3195104 100644
--- a/src/OutputSocketClient.cpp
+++ b/src/OutputSocketClient.cpp
@@ -16,14 +16,14 @@
  ***************************************************************************/
 
 #include "OutputSocketClient.h"
+#include <sstream>
 
 using namespace PacketLib;
 
 
-OutputSocketClient::OutputSocketClient(bool bigendian) : Output(bigendian)
+OutputSocketClient::OutputSocketClient(bool bigendian) : Output(bigendian), host("")
 {
     socketclient = 0;
-    host = 0;
 }
 
 
@@ -31,7 +31,6 @@ OutputSocketClient::OutputSocketClient(bool bigendian) : Output(bigendian)
 OutputSocketClient::~OutputSocketClient()
 {
     delete socketclient;
-    delete host;
 }
 
 
@@ -50,7 +49,14 @@ bool OutputSocketClient::open(char** argv) throw(PacketExceptionIO*)
     return true;
 }
 
-
+void OutputSocketClient::openDevice(const std::vector<std::string>& parameters) throw(PacketExceptionIO*)
+{
+    host = parameters[0];
+	std::istringstream ss(parameters[1]);
+	ss >> port;
+    socketclient = new SocketClient(bigendian, host, port);
+    isclosed = false;
+}
 
 bool OutputSocketClient::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
 {
-- 
GitLab