2016-05-31 03:58:58 +00:00
|
|
|
#pragma once
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
namespace io
|
|
|
|
{
|
|
|
|
namespace socket
|
|
|
|
{
|
|
|
|
int open(const std::string& path);
|
|
|
|
int send(int fd, const std::string& data, int flags = 0);
|
|
|
|
int recv(int fd, char *buffer, int recv_bytes, int flags = 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace file
|
|
|
|
{
|
|
|
|
class FilePtr
|
|
|
|
{
|
|
|
|
FILE *fptr = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string path;
|
|
|
|
std::string mode;
|
|
|
|
|
|
|
|
FilePtr(const std::string& path, const std::string& mode = "a+")
|
2016-05-31 03:58:58 +00:00
|
|
|
: path(std::string(path)), mode(std::string(mode))
|
2016-05-19 14:41:06 +00:00
|
|
|
{
|
|
|
|
this->fptr = fopen(this->path.c_str(), this->mode.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
~FilePtr()
|
|
|
|
{
|
|
|
|
if (this->fptr != nullptr)
|
|
|
|
fclose(this->fptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() {
|
|
|
|
return this->fptr != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *operator()() {
|
|
|
|
return this->fptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool exists(const std::string& fname);
|
|
|
|
std::string get_contents(const std::string& fname);
|
|
|
|
bool is_fifo(const std::string& fname);
|
|
|
|
std::size_t write(FilePtr *fptr, const std::string& data);
|
|
|
|
std::size_t write(const std::string& fpath, const std::string& data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string read(int read_fd, int bytes_to_read = -1);
|
|
|
|
std::string read(int read_fd, int bytes_to_read, int &bytes_read_loc, int &status_loc);
|
2016-06-14 10:14:29 +00:00
|
|
|
std::string readline(int read_fd, int &bytes_read);
|
2016-05-19 14:41:06 +00:00
|
|
|
std::string readline(int read_fd);
|
|
|
|
|
|
|
|
int write(int write_fd, const std::string& data);
|
|
|
|
int writeline(int write_fd, const std::string& data);
|
|
|
|
|
|
|
|
void tail(int read_fd, std::function<void(std::string)> callback);
|
|
|
|
void tail(int read_fd, int writeback_fd);
|
|
|
|
|
|
|
|
bool poll_read(int fd, int timeout_ms = 1);
|
2016-05-31 03:58:58 +00:00
|
|
|
// bool poll_write(int fd, int timeout_ms = 1);
|
2016-05-19 14:41:06 +00:00
|
|
|
bool poll(int fd, short int events, int timeout_ms = 1);
|
|
|
|
|
2016-05-31 03:58:58 +00:00
|
|
|
// int get_flags(int fd);
|
|
|
|
// int set_blocking(int fd);
|
|
|
|
// int set_non_blocking(int fd);
|
2016-05-19 14:41:06 +00:00
|
|
|
}
|