Commit ab0dbb19 authored by louiz’'s avatar louiz’

Use boost::filesystem and some c++14 features to simplify the filenames usage

fix #2396
parent f611cbbc
......@@ -44,7 +44,7 @@ option(BUILD_CLIENT "build the server" ${BUILD_CLIENT})
# Libraries look up
##########################
find_package(Boost 1.48.0 COMPONENTS serialization system date_time unit_test_framework iostreams locale REQUIRED)
find_package(Boost 1.48.0 COMPONENTS filesystem system date_time iostreams locale serialization REQUIRED)
find_package(Cryptopp REQUIRED)
find_package(YamlCpp REQUIRED)
......
......@@ -60,7 +60,7 @@ class nullstream: public std::ostream
{
public:
nullstream():
std::ostream(0)
std::ostream(nullptr)
{ }
};
......
......@@ -2,20 +2,22 @@
#include <network/transfer_receiver.hpp>
#include <network/client.hpp>
using namespace std::string_literals;
static const fs::path FILES_TO_RECEIVE_DIRECTORY("./receive/");
TransferReceiver::TransferReceiver(Client* client,
const std::string& sid,
const std::string& filename,
int length):
const std::string& sid,
const std::string& filename,
int length):
client(client),
id(sid),
filename(FILES_TO_RECEIVE_DIRECTORY + filename),
filename(FILES_TO_RECEIVE_DIRECTORY / filename),
length(length),
received_length(0)
{
std::string command_name("TRANSFER_");
command_name += sid;
this->client->install_callback(command_name, std::bind(&TransferReceiver::get_next_chunk, this, std::placeholders::_1));
this->file.open(this->filename.data(), std::ofstream::binary);
this->client->install_callback("TRANSFER_"s + sid, std::bind(&TransferReceiver::get_next_chunk, this, std::placeholders::_1));
this->file.open(this->filename, std::ofstream::binary);
}
TransferReceiver::~TransferReceiver()
......@@ -40,10 +42,6 @@ void TransferReceiver::get_next_chunk(Command* received_command)
void TransferReceiver::stop()
{
std::ostringstream sid;
sid << this->id;
std::string command_name("TRANSFER_");
command_name += sid.str();
this->client->remove_callback(command_name);
this->client->remove_callback("TRANSFER_"s + this->id);
this->client->on_transfer_ended(this);
}
......@@ -7,14 +7,14 @@
* @class TransferReceiver
*/
#include <iostream>
#include <fstream>
#include <string>
#ifndef __TRANSFER_RECEIVER_HPP__
# define __TRANSFER_RECEIVER_HPP__
#define FILES_TO_RECEIVE_DIRECTORY std::string("./receive/")
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
#include <network/command.hpp>
......@@ -34,10 +34,10 @@ private:
void get_next_chunk(Command*);
Client* client;
std::string id;
const std::string filename;
fs::path filename;
int length;
int received_length;
std::ofstream file;
fs::ofstream file;
};
#endif // __TRANSFER_RECEIVER_HPP__
......
#include <iostream>
#include <logging/logging.hpp>
#include <network/transfer_sender.hpp>
#include <network/remote_client.hpp>
#define CHUNK_SIZE 262144
using namespace std::string_literals;
static constexpr std::size_t CHUNK_SIZE = 262144u;
unsigned long int TransferSender::current_id = 0;
......@@ -10,11 +13,10 @@ TransferSender::TransferSender(RemoteClient* client, const std::string& filename
client(client),
filename(filename)
{
this->file.open(std::string(FILES_TO_SEND_DIRECTORY + this->filename).data(), std::ofstream::binary);
fs::path file_name = FILES_TO_SEND_DIRECTORY / this->filename;
this->file.open(file_name, std::ofstream::binary);
std::ostringstream sid;
sid << TransferSender::current_id++;
this->id = std::string(sid.str());
this->id = std::to_string(TransferSender::current_id++);
}
TransferSender::~TransferSender()
......
......@@ -14,15 +14,16 @@
* @class TransferSender
*/
#include <iostream>
#include <fstream>
#include <string>
#include <boost/asio.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#ifndef __TRANSFER_SENDER_HPP__
# define __TRANSFER_SENDER_HPP__
#define FILES_TO_SEND_DIRECTORY std::string("./send/")
static const fs::path FILES_TO_SEND_DIRECTORY("./send/");
class RemoteClient;
......@@ -47,7 +48,7 @@ private:
RemoteClient* client;
const std::string filename;
std::ifstream file;
fs::ifstream file;
int length;
std::string id;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment