mirror of
https://projects.vdr-developer.org/git/vdr-plugin-streamdev.git
synced 2023-10-10 17:16:51 +00:00
- transfer
This commit is contained in:
19
tools/file.c
19
tools/file.c
@@ -1,5 +1,6 @@
|
||||
#include "tools/file.h"
|
||||
|
||||
#include <vdr/tools.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@@ -13,12 +14,12 @@ cTBFile::~cTBFile() {
|
||||
Close();
|
||||
}
|
||||
|
||||
bool cTBFile::Open(const cTBString &Filename, int Mode, mode_t Attribs) {
|
||||
bool cTBFile::Open(const std::string &Filename, int Mode, mode_t Attribs) {
|
||||
int filed;
|
||||
|
||||
if (IsOpen()) Close();
|
||||
|
||||
if ((filed = ::open(Filename, Mode, Attribs)) == -1)
|
||||
if ((filed = ::open(Filename.c_str(), Mode, Attribs)) == -1)
|
||||
return false;
|
||||
|
||||
if (!cTBSource::Open(filed))
|
||||
@@ -35,7 +36,7 @@ bool cTBFile::Open(uint Fileno) {
|
||||
if (!cTBSource::Open(Fileno))
|
||||
return false;
|
||||
|
||||
m_Filename.Format("<&%d>", Fileno);
|
||||
m_Filename = (std::string)"<&" + (const char*)itoa(Fileno) + ">";
|
||||
m_Anonymous = true;
|
||||
return true;
|
||||
}
|
||||
@@ -52,12 +53,12 @@ bool cTBFile::Close(void) {
|
||||
if (!cTBSource::Close())
|
||||
ret = false;
|
||||
|
||||
m_Filename.Clear();
|
||||
m_Filename = "";
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool cTBFile::Unlink(void) const {
|
||||
if (m_Filename.IsNull())
|
||||
if (m_Filename == "")
|
||||
ERRNUL(ENOENT);
|
||||
|
||||
if (!IsOpen())
|
||||
@@ -69,8 +70,8 @@ bool cTBFile::Unlink(void) const {
|
||||
return cTBFile::Unlink(m_Filename);
|
||||
}
|
||||
|
||||
bool cTBFile::Unlink(const cTBString &Filename) {
|
||||
return (::unlink(Filename) != -1);
|
||||
bool cTBFile::Unlink(const std::string &Filename) {
|
||||
return (::unlink(Filename.c_str()) != -1);
|
||||
}
|
||||
|
||||
ssize_t cTBFile::Size(void) const {
|
||||
@@ -85,10 +86,10 @@ ssize_t cTBFile::Size(void) const {
|
||||
return buf.st_size;
|
||||
}
|
||||
|
||||
ssize_t cTBFile::Size(const cTBString &Filename) {
|
||||
ssize_t cTBFile::Size(const std::string &Filename) {
|
||||
struct stat buf;
|
||||
|
||||
if (stat(Filename, &buf) == -1)
|
||||
if (stat(Filename.c_str(), &buf) == -1)
|
||||
return -1;
|
||||
|
||||
return buf.st_size;
|
||||
|
10
tools/file.h
10
tools/file.h
@@ -3,12 +3,12 @@
|
||||
|
||||
#include "tools/tools.h"
|
||||
#include "tools/source.h"
|
||||
#include "tools/string.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
|
||||
/* cTBFile provides a cTBSource-derived interface for input and output on UNIX
|
||||
files. */
|
||||
@@ -16,7 +16,7 @@
|
||||
class cTBFile: public cTBSource {
|
||||
private:
|
||||
bool m_Anonymous;
|
||||
cTBString m_Filename;
|
||||
std::string m_Filename;
|
||||
|
||||
/* Unhide and forbid baseclass method */
|
||||
virtual bool Open(int Fd, bool IsUnixFd = false) { return false; }
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
Mode. If the file is created, it receives the attributes given by
|
||||
Attribs, defaulting to rw-------. Returns true on success and false on
|
||||
error, setting errno appropriately. */
|
||||
virtual bool Open(const cTBString &Filename, int Mode,
|
||||
virtual bool Open(const std::string &Filename, int Mode,
|
||||
mode_t Attribs = S_IRUSR + S_IWUSR);
|
||||
|
||||
/* Open() associates this file object with Fileno. Fileno must refer to a
|
||||
@@ -74,7 +74,7 @@ public:
|
||||
/* Unlink() unlinks (deletes) the file referred to by Filename from the
|
||||
underlying filesystem. Returns true on success and false otherwise,
|
||||
setting errno appropriately. */
|
||||
static bool Unlink(const cTBString &Filename);
|
||||
static bool Unlink(const std::string &Filename);
|
||||
|
||||
/* Size() returns the current size of the associated file. Returns the
|
||||
exact size of the file in bytes. Returns -1 on error, setting errno to
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
Symbolic links are followed (the size of the link-target is returned).
|
||||
Returns the exact size of the file in bytes. Returns -1 on error,
|
||||
setting errno to an appropriate value. */
|
||||
static ssize_t Size(const cTBString &Filename);
|
||||
static ssize_t Size(const std::string &Filename);
|
||||
};
|
||||
|
||||
inline ssize_t cTBFile::SysRead(void *Buffer, size_t Length) const {
|
||||
|
@@ -16,7 +16,7 @@ cTBSocket::~cTBSocket() {
|
||||
if (IsOpen()) Close();
|
||||
}
|
||||
|
||||
bool cTBSocket::Connect(const cTBString &Host, unsigned int Port) {
|
||||
bool cTBSocket::Connect(const std::string &Host, unsigned int Port) {
|
||||
socklen_t len;
|
||||
int socket;
|
||||
|
||||
@@ -34,7 +34,7 @@ bool cTBSocket::Connect(const cTBString &Host, unsigned int Port) {
|
||||
|
||||
m_RemoteAddr.sin_family = AF_INET;
|
||||
m_RemoteAddr.sin_port = htons(Port);
|
||||
m_RemoteAddr.sin_addr.s_addr = inet_addr(Host);
|
||||
m_RemoteAddr.sin_addr.s_addr = inet_addr(Host.c_str());
|
||||
if (::connect(socket, (struct sockaddr*)&m_RemoteAddr,
|
||||
sizeof(m_RemoteAddr)) == -1)
|
||||
return false;
|
||||
@@ -50,7 +50,7 @@ bool cTBSocket::Connect(const cTBString &Host, unsigned int Port) {
|
||||
return cTBSource::Open(socket);
|
||||
}
|
||||
|
||||
bool cTBSocket::Listen(const char *Ip, unsigned int Port, int BackLog) {
|
||||
bool cTBSocket::Listen(const std::string &Ip, unsigned int Port, int BackLog) {
|
||||
int val;
|
||||
socklen_t len;
|
||||
int socket;
|
||||
@@ -66,7 +66,7 @@ bool cTBSocket::Listen(const char *Ip, unsigned int Port, int BackLog) {
|
||||
|
||||
m_LocalAddr.sin_family = AF_INET;
|
||||
m_LocalAddr.sin_port = htons(Port);
|
||||
m_LocalAddr.sin_addr.s_addr = inet_addr(Ip);
|
||||
m_LocalAddr.sin_addr.s_addr = inet_addr(Ip.c_str());
|
||||
if (::bind(socket, (struct sockaddr*)&m_LocalAddr, sizeof(m_LocalAddr))
|
||||
== -1)
|
||||
return false;
|
||||
|
@@ -3,11 +3,11 @@
|
||||
|
||||
#include "tools/tools.h"
|
||||
#include "tools/source.h"
|
||||
#include "tools/string.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string>
|
||||
|
||||
/* cTBSocket provides a cTBSource-derived interface for input and output on
|
||||
TCP/IPv4-sockets. */
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
by Port of the target host given by Host in numbers-and-dots notation
|
||||
(i.e. "212.43.45.21"). Returns true if the connection attempt was
|
||||
successful and false otherwise, setting errno appropriately. */
|
||||
virtual bool Connect(const cTBString &Host, uint Port);
|
||||
virtual bool Connect(const std::string &Host, uint Port);
|
||||
|
||||
/* Shutdown() shuts down one or both ends of a socket. If called with How
|
||||
set to SHUT_RD, further reads on this socket will be denied. If called
|
||||
@@ -54,7 +54,7 @@ public:
|
||||
BackLog parameter defines the maximum length the queue of pending
|
||||
connections may grow to. Returns true if the object is listening on
|
||||
the specified port and false otherwise, setting errno appropriately. */
|
||||
virtual bool Listen(const char *Ip, uint Port, int BackLog);
|
||||
virtual bool Listen(const std::string &Ip, uint Port, int BackLog);
|
||||
|
||||
/* Accept() returns a newly created cTBSocket, which is connected to the
|
||||
first connection request on the queue of pending connections of a
|
||||
@@ -80,12 +80,12 @@ public:
|
||||
the interface this socket is connected to locally. This can be
|
||||
"0.0.0.0" for a listening socket listening to all interfaces. If the
|
||||
socket is in its closed state, the result is undefined. */
|
||||
cTBString LocalIp(void) const { return inet_ntoa(m_LocalAddr.sin_addr); }
|
||||
std::string LocalIp(void) const { return inet_ntoa(m_LocalAddr.sin_addr); }
|
||||
|
||||
/* RemoteIp() returns the internet address in numbers-and-dots notation of
|
||||
the interface this socket is connected to on the remote side. If the
|
||||
socket is in its closed state, the result is undefined. */
|
||||
cTBString RemoteIp(void) const { return inet_ntoa(m_RemoteAddr.sin_addr); }
|
||||
std::string RemoteIp(void) const { return inet_ntoa(m_RemoteAddr.sin_addr); }
|
||||
|
||||
in_addr_t LocalIpAddr(void) const { return m_LocalAddr.sin_addr.s_addr; }
|
||||
in_addr_t RemoteIpAddr(void) const { return m_RemoteAddr.sin_addr.s_addr; }
|
||||
|
@@ -87,30 +87,25 @@ bool cTBSource::TimedWrite(const void *Buffer, size_t Length, uint TimeoutMs) {
|
||||
|
||||
ssize_t cTBSource::ReadUntil(void *Buffer, size_t Length, const char *Seq,
|
||||
uint TimeoutMs) {
|
||||
char *offs;
|
||||
int seqlen, ms;
|
||||
size_t olen;
|
||||
size_t len;
|
||||
cTBSelect sel;
|
||||
|
||||
seqlen = strlen(Seq);
|
||||
if ((offs = (char*)memmem(m_LineBuffer, m_LineBuffer.Length(), Seq, seqlen))){
|
||||
olen = offs - m_LineBuffer;
|
||||
if (olen >= Length) {
|
||||
if ((len = m_LineBuffer.find(Seq)) != (size_t)-1) {
|
||||
if (len > Length) {
|
||||
errno = ENOBUFS;
|
||||
return -1;
|
||||
}
|
||||
memcpy(Buffer, m_LineBuffer, olen);
|
||||
m_LineBuffer = m_LineBuffer.Mid(olen + seqlen);
|
||||
Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", olen, olen - 1,
|
||||
memcpy(Buffer, m_LineBuffer.data(), len);
|
||||
m_LineBuffer.erase(0, len + strlen(Seq));
|
||||
Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", len, len - 1,
|
||||
(char*)Buffer);
|
||||
return olen;
|
||||
return len;
|
||||
}
|
||||
|
||||
cTimeMs starttime;
|
||||
ms = TimeoutMs;
|
||||
while (m_LineBuffer.Length() < BUFSIZ) {
|
||||
int b;
|
||||
|
||||
while (m_LineBuffer.size() < BUFSIZ) {
|
||||
sel.Clear();
|
||||
sel.Add(m_Filed, false);
|
||||
|
||||
@@ -118,25 +113,24 @@ ssize_t cTBSource::ReadUntil(void *Buffer, size_t Length, const char *Seq,
|
||||
return -1;
|
||||
|
||||
if (sel.CanRead(m_Filed)) {
|
||||
offs = m_LineBuffer.Buffer(BUFSIZ);
|
||||
if ((b = Read(offs + m_LineBuffer.Length(), BUFSIZ
|
||||
- m_LineBuffer.Length())) == -1)
|
||||
return -1;
|
||||
int b;
|
||||
|
||||
m_LineBuffer.Release(m_LineBuffer.Length() + b);
|
||||
if ((offs = (char*)memmem(m_LineBuffer, m_LineBuffer.Length(), Seq,
|
||||
seqlen))) {
|
||||
olen = offs - m_LineBuffer;
|
||||
if (olen >= Length) {
|
||||
len = m_LineBuffer.size();
|
||||
m_LineBuffer.resize(BUFSIZ);
|
||||
if ((b = Read((char*)m_LineBuffer.data() + len, BUFSIZ - len)) == -1)
|
||||
return -1;
|
||||
m_LineBuffer.resize(len + b);
|
||||
|
||||
if ((len = m_LineBuffer.find(Seq)) != (size_t)-1) {
|
||||
if (len > Length) {
|
||||
errno = ENOBUFS;
|
||||
return -1;
|
||||
}
|
||||
memcpy(Buffer, m_LineBuffer, olen);
|
||||
m_LineBuffer = m_LineBuffer.Mid(olen + seqlen, m_LineBuffer.Length()
|
||||
- olen - seqlen);
|
||||
Dprintf("ReadUntil: Served after Read: %d, |%.*s|\n", olen, olen-1,
|
||||
memcpy(Buffer, m_LineBuffer.data(), len);
|
||||
m_LineBuffer.erase(0, len + strlen(Seq));
|
||||
Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", len, len - 1,
|
||||
(char*)Buffer);
|
||||
return olen;
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,46 +142,5 @@ ssize_t cTBSource::ReadUntil(void *Buffer, size_t Length, const char *Seq,
|
||||
}
|
||||
errno = ENOBUFS;
|
||||
return -1;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
cTBSelect sel;
|
||||
time_t st, et;
|
||||
int ms, seqlen, offs;
|
||||
|
||||
seqlen = strlen(Seq);
|
||||
st = time_ms();
|
||||
ms = TimeoutMs;
|
||||
offs = 0;
|
||||
while (Length > 0) {
|
||||
int b;
|
||||
|
||||
sel.Clear();
|
||||
sel.Add(m_Filed, false);
|
||||
if (sel.Select(ms) == -1)
|
||||
return -1;
|
||||
|
||||
if (sel.CanRead(m_Filed)) {
|
||||
if ((b = Read((char*)Buffer + offs, Length)) == -1)
|
||||
return -1;
|
||||
|
||||
offs += b;
|
||||
Length -= b;
|
||||
|
||||
if (memmem(Buffer, offs, Seq, seqlen) != NULL)
|
||||
return offs;
|
||||
}
|
||||
|
||||
et = time_ms();
|
||||
ms -= et - st;
|
||||
if (ms <= 0) {
|
||||
errno = ETIMEDOUT;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
errno = ENOBUFS;
|
||||
return -1;
|
||||
*/
|
||||
}
|
||||
|
||||
|
@@ -2,9 +2,9 @@
|
||||
#define TOOLBOX_SOURCE_H
|
||||
|
||||
#include "tools/tools.h"
|
||||
#include "tools/string.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string>
|
||||
|
||||
/* cTBSource provides an abstract interface for input and output. It can
|
||||
be used to have common access to different types of UNIX-files. */
|
||||
@@ -16,7 +16,7 @@ private:
|
||||
size_t m_BytesRead;
|
||||
size_t m_BytesWritten;
|
||||
|
||||
cTBString m_LineBuffer;
|
||||
std::string m_LineBuffer;
|
||||
|
||||
public:
|
||||
cTBSource(void);
|
||||
|
Reference in New Issue
Block a user