polybar-dwm/include/utils/io.hpp

78 lines
1.9 KiB
C++
Raw Normal View History

#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
{
2016-06-21 01:59:43 +00:00
int open(std::string path);
int send(int fd, std::string data, int flags = 0);
2016-05-19 14:41:06 +00:00
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;
2016-06-21 01:59:43 +00:00
FilePtr(std::string path, std::string mode = "a+")
: 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;
}
};
2016-06-21 01:59:43 +00:00
bool exists(std::string fname);
std::string get_contents(std::string fname);
bool is_fifo(std::string fname);
std::size_t write(FilePtr *fptr, std::string data);
std::size_t write(std::string fpath, std::string data);
2016-05-19 14:41:06 +00:00
}
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);
std::string readline(int read_fd, int &bytes_read);
2016-05-19 14:41:06 +00:00
std::string readline(int read_fd);
2016-06-21 01:59:43 +00:00
int write(int write_fd, std::string data);
int writeline(int write_fd, std::string data);
2016-05-19 14:41:06 +00:00
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);
// 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);
// int get_flags(int fd);
// int set_blocking(int fd);
// int set_non_blocking(int fd);
2016-05-19 14:41:06 +00:00
}