Updated DTSC::File, added appending new packets, added copy constructor in order to keep valid file descriptors open.
This commit is contained in:
parent
59d03dcf48
commit
c4fecd7e3d
2 changed files with 84 additions and 7 deletions
81
lib/dtsc.cpp
81
lib/dtsc.cpp
|
@ -507,6 +507,35 @@ DTSC::Stream::~Stream(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DTSC::File::File(){
|
||||||
|
F = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DTSC::File::File(const File & rhs){
|
||||||
|
*this = rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
DTSC::File & DTSC::File::operator =(const File & rhs){
|
||||||
|
created = rhs.created;
|
||||||
|
if (rhs.F){
|
||||||
|
int tmpFd = fileno(rhs.F);
|
||||||
|
int newFd = dup(tmpFd);
|
||||||
|
F = fdopen( newFd, (created ? "w+b": "r+b"));
|
||||||
|
}else{
|
||||||
|
F = 0;
|
||||||
|
}
|
||||||
|
strbuffer = rhs.strbuffer;
|
||||||
|
jsonbuffer = rhs.jsonbuffer;
|
||||||
|
metadata = rhs.metadata;
|
||||||
|
firstmetadata = rhs.firstmetadata;
|
||||||
|
frames = rhs.frames;
|
||||||
|
msframes = rhs.msframes;
|
||||||
|
currtime = rhs.currtime;
|
||||||
|
lastreadpos = rhs.lastreadpos;
|
||||||
|
headerSize = rhs.headerSize;
|
||||||
|
memcpy(buffer, rhs.buffer, 4);
|
||||||
|
}
|
||||||
|
|
||||||
/// Open a filename for DTSC reading/writing.
|
/// Open a filename for DTSC reading/writing.
|
||||||
/// If create is true and file does not exist, attempt to create.
|
/// If create is true and file does not exist, attempt to create.
|
||||||
DTSC::File::File(std::string filename, bool create){
|
DTSC::File::File(std::string filename, bool create){
|
||||||
|
@ -521,6 +550,7 @@ DTSC::File::File(std::string filename, bool create){
|
||||||
}else{
|
}else{
|
||||||
F = fopen(filename.c_str(), "r+b");
|
F = fopen(filename.c_str(), "r+b");
|
||||||
}
|
}
|
||||||
|
created = create;
|
||||||
if ( !F){
|
if ( !F){
|
||||||
fprintf(stderr, "Could not open file %s\n", filename.c_str());
|
fprintf(stderr, "Could not open file %s\n", filename.c_str());
|
||||||
return;
|
return;
|
||||||
|
@ -562,6 +592,12 @@ bool DTSC::File::writeHeader(std::string & header, bool force){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
headerSize = header.size();
|
headerSize = header.size();
|
||||||
|
int pSize = htonl(header.size());
|
||||||
|
fseek(F, 4, SEEK_SET);
|
||||||
|
int tmpret = fwrite((void*)( &pSize), 4, 1, F);
|
||||||
|
if (tmpret != 1){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
fseek(F, 8, SEEK_SET);
|
fseek(F, 8, SEEK_SET);
|
||||||
int ret = fwrite(header.c_str(), headerSize, 1, F);
|
int ret = fwrite(header.c_str(), headerSize, 1, F);
|
||||||
fseek(F, 8 + headerSize, SEEK_SET);
|
fseek(F, 8 + headerSize, SEEK_SET);
|
||||||
|
@ -621,13 +657,15 @@ void DTSC::File::readHeader(int pos){
|
||||||
uint32_t * ubuffer = (uint32_t *)buffer;
|
uint32_t * ubuffer = (uint32_t *)buffer;
|
||||||
long packSize = ntohl(ubuffer[0]);
|
long packSize = ntohl(ubuffer[0]);
|
||||||
strbuffer.resize(packSize);
|
strbuffer.resize(packSize);
|
||||||
if (fread((void*)strbuffer.c_str(), packSize, 1, F) != 1){
|
if (packSize){
|
||||||
fprintf(stderr, "Could not read packet (H%i)\n", pos);
|
if (fread((void*)strbuffer.c_str(), packSize, 1, F) != 1){
|
||||||
strbuffer = "";
|
fprintf(stderr, "Could not read packet (H%i)\n", pos);
|
||||||
metadata.null();
|
strbuffer = "";
|
||||||
return;
|
metadata.null();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
metadata = JSON::fromDTMI(strbuffer);
|
||||||
}
|
}
|
||||||
metadata = JSON::fromDTMI(strbuffer);
|
|
||||||
if (pos == 0){
|
if (pos == 0){
|
||||||
firstmetadata = metadata;
|
firstmetadata = metadata;
|
||||||
}
|
}
|
||||||
|
@ -652,6 +690,19 @@ void DTSC::File::readHeader(int pos){
|
||||||
metadata.netPrepare();
|
metadata.netPrepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long int DTSC::File::getBytePosEOF(){
|
||||||
|
fseek(F, 0, SEEK_END);
|
||||||
|
return ftell(F);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int DTSC::File::getBytePos(){
|
||||||
|
return ftell(F);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DTSC::File::reachedEOF(){
|
||||||
|
return feof(F);
|
||||||
|
}
|
||||||
|
|
||||||
/// Reads the packet available at the current file position.
|
/// Reads the packet available at the current file position.
|
||||||
/// If the packet could not be read for any reason, the reason is printed to stderr.
|
/// If the packet could not be read for any reason, the reason is printed to stderr.
|
||||||
/// Reading the packet means the file position is increased to the next packet.
|
/// Reading the packet means the file position is increased to the next packet.
|
||||||
|
@ -682,7 +733,7 @@ void DTSC::File::seekNext(){
|
||||||
version = 2;
|
version = 2;
|
||||||
}
|
}
|
||||||
if (version == 0){
|
if (version == 0){
|
||||||
fprintf(stderr, "Invalid header - %.4s != %.4s\n", buffer, DTSC::Magic_Packet2);
|
fprintf(stderr, "Invalid packet header @ %#x - %.4s != %.4s\n", getBytePos(), buffer, DTSC::Magic_Packet2);
|
||||||
strbuffer = "";
|
strbuffer = "";
|
||||||
jsonbuffer.null();
|
jsonbuffer.null();
|
||||||
return;
|
return;
|
||||||
|
@ -807,6 +858,22 @@ bool DTSC::File::seek_time(int ms){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DTSC::File::seek_bpos(int bpos){
|
||||||
|
if (fseek(F, bpos, SEEK_SET) == 0){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DTSC::File::writePacket(std::string & newPacket){
|
||||||
|
fseek(F, 0, SEEK_END);
|
||||||
|
fwrite(newPacket.c_str(), newPacket.size(), 1, F); //write contents
|
||||||
|
}
|
||||||
|
|
||||||
|
void DTSC::File::writePacket(JSON::Value & newPacket){
|
||||||
|
writePacket(newPacket.toNetPacked());
|
||||||
|
}
|
||||||
|
|
||||||
/// Close the file if open
|
/// Close the file if open
|
||||||
DTSC::File::~File(){
|
DTSC::File::~File(){
|
||||||
if (F){
|
if (F){
|
||||||
|
|
10
lib/dtsc.h
10
lib/dtsc.h
|
@ -67,18 +67,27 @@ namespace DTSC {
|
||||||
/// A simple wrapper class that will open a file and allow easy reading/writing of DTSC data from/to it.
|
/// A simple wrapper class that will open a file and allow easy reading/writing of DTSC data from/to it.
|
||||||
class File{
|
class File{
|
||||||
public:
|
public:
|
||||||
|
File();
|
||||||
|
File(const File & rhs);
|
||||||
File(std::string filename, bool create = false);
|
File(std::string filename, bool create = false);
|
||||||
|
File & operator = (const File & rhs);
|
||||||
~File();
|
~File();
|
||||||
JSON::Value & getMeta();
|
JSON::Value & getMeta();
|
||||||
JSON::Value & getFirstMeta();
|
JSON::Value & getFirstMeta();
|
||||||
long long int getLastReadPos();
|
long long int getLastReadPos();
|
||||||
bool writeHeader(std::string & header, bool force = false);
|
bool writeHeader(std::string & header, bool force = false);
|
||||||
long long int addHeader(std::string & header);
|
long long int addHeader(std::string & header);
|
||||||
|
long int getBytePosEOF();
|
||||||
|
long int getBytePos();
|
||||||
|
bool reachedEOF();
|
||||||
void seekNext();
|
void seekNext();
|
||||||
std::string & getPacket();
|
std::string & getPacket();
|
||||||
JSON::Value & getJSON();
|
JSON::Value & getJSON();
|
||||||
bool seek_frame(int frameno);
|
bool seek_frame(int frameno);
|
||||||
bool seek_time(int seconds);
|
bool seek_time(int seconds);
|
||||||
|
bool seek_bpos(int bpos);
|
||||||
|
void writePacket(std::string & newPacket);
|
||||||
|
void writePacket(JSON::Value & newPacket);
|
||||||
private:
|
private:
|
||||||
void readHeader(int pos);
|
void readHeader(int pos);
|
||||||
std::string strbuffer;
|
std::string strbuffer;
|
||||||
|
@ -93,6 +102,7 @@ namespace DTSC {
|
||||||
FILE * F;
|
FILE * F;
|
||||||
unsigned long headerSize;
|
unsigned long headerSize;
|
||||||
char buffer[4];
|
char buffer[4];
|
||||||
|
bool created;
|
||||||
};
|
};
|
||||||
//FileWriter
|
//FileWriter
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue