processing of <usi:include> blocks complete

This commit is contained in:
Mario Fink 2021-01-19 18:52:44 +01:00
parent 628ae02531
commit c0f5346f58
5 changed files with 142 additions and 22 deletions

View File

@ -15,6 +15,39 @@
#include <chrono> #include <chrono>
#include <sstream> #include <sstream>
// -------------------------------------------------------------------------- //
// block of data
struct block {
std::string id_;
unsigned long int byte_offset_;
unsigned long int length_;
unsigned long int block_offset_, block_size_;
std::string value_type_;
block () {
id_ = std::string("");
byte_offset_ = 0;
length_ = 0;
block_offset_ = 0;
block_size_ = 0;
value_type_ = std::string("");
}
const std::string get_info(int width = 20)
{
std::stringstream ss;
ss<<std::setw(width)<<std::left<<"id:"<<id_<<"\n"
<<std::setw(width)<<std::left<<"byteOffset:"<<std::to_string(byte_offset_)<<"\n"
<<std::setw(width)<<std::left<<"length:"<<std::to_string(length_)<<"\n"
<<std::setw(width)<<std::left<<"blockOffset:"<<std::to_string(block_offset_)<<"\n"
<<std::setw(width)<<std::left<<"blockSize:"<<std::to_string(block_size_)<<"\n"
<<std::setw(width)<<std::left<<"valueType:"<<value_type_<<"\n";
return ss.str();
}
};
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// tdm datatypes // tdm datatypes

View File

@ -4,7 +4,7 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
tdm_ripper::tdm_ripper(std::string tdmfile, std::string tdxfile, bool showlog): tdm_reaper::tdm_reaper(std::string tdmfile, std::string tdxfile, bool showlog):
tdmfile_(tdmfile), tdxfile_(tdxfile) tdmfile_(tdmfile), tdxfile_(tdxfile)
{ {
// check both tdm, tdx files // check both tdm, tdx files
@ -18,37 +18,114 @@ tdm_ripper::tdm_ripper(std::string tdmfile, std::string tdxfile, bool showlog):
throw std::runtime_error(std::string("*.tdx file ") + tdxfile_ + " does not exist!"); throw std::runtime_error(std::string("*.tdx file ") + tdxfile_ + " does not exist!");
} }
// set up xml-parser and load tdm file // set up xml-parser and load tdm-file
try { try {
xml_result_ = xml_doc_.load_file(tdmfile_.c_str()); xml_result_ = xml_doc_.load_file(tdmfile_.c_str());
if ( !showlog ) if ( showlog )
{ {
std::cout<<"loading and parsing file: "<<xml_result_.description()<<"\n"; std::cout<<"\nloading "<<tdmfile_<<": "<<xml_result_.description()<<"\n\n";
std::cout<<"encoding: "<<(pugi::xml_encoding)xml_result_.encoding<<"\n"; std::cout<<"encoding: "<<(pugi::xml_encoding)xml_result_.encoding<<"\n";
pugi::xml_node tdmdocu = xml_doc_.child("usi:tdm").child("usi:documentation");
std::cout<<tdmdocu.child_value("usi:exporter")<<"\n"
<<tdmdocu.child_value("usi:exporterVersion")<<"\n";
pugi::xml_node tdmmodel = xml_doc_.child("usi:tdm").child("usi:model");
std::cout<<tdmmodel.attribute("modelName").value()<<"\n";
std::cout<<tdmmodel.child("usi:include").attribute("nsUri").value()
<<tdmmodel.child("usi:include").attribute("modelVersion").value()<<"\n";
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
throw std::runtime_error(std::string("failed to load tdm file: ") + e.what()); throw std::runtime_error(std::string("failed to load tdm file: ") + e.what());
} }
// process elements of XML
this->process_include(showlog);
}
void tdm_reaper::process_include(bool showlog)
{
// get XML node
pugi::xml_node tdmincl = xml_doc_.child("usi:tdm").child("usi:include");
// check endianess
std::string endianness(tdmincl.child("file").attribute("byteOrder").value());
endianness_ = endianness.compare("littleEndian") == 0 ? true : false;
// obtain machine's endianess
int num = 1;
machine_endianness_ = ( *(char*)&num == 1 );
if ( machine_endianness_ != endianness_ ) throw std::runtime_error("endianess mismatch");
if ( showlog )
{
std::cout<<"\n";
std::cout<<"endianess: "<<(endianness_?"little":"big")<<"\n"
<<"machine endianness: "<<(machine_endianness_?"little":"big")<<"\n\n";
}
// check for existence of attributes before using
// pugi::xml_attribute attr;
// list block of massdata
for (pugi::xml_node anode: tdmincl.child("file").children())
{
// declare new block
block tdxblock;
if ( anode.attribute("id") )
{
tdxblock.id_ = anode.attribute("id").value();
}
if ( anode.attribute("byteOffset") )
{
tdxblock.byte_offset_ = std::stoul(anode.attribute("byteOffset").value());
}
if ( anode.attribute("length") )
{
tdxblock.length_ = std::stoul(anode.attribute("length").value());
}
if ( anode.attribute("blockOffset") )
{
tdxblock.block_offset_ = std::stoul(anode.attribute("blockOffset").value());
}
if ( anode.attribute("blockSize") )
{
tdxblock.block_size_ = std::stoul(anode.attribute("blockSize").value());
}
if ( anode.attribute("valueType") )
{
tdxblock.value_type_ = anode.attribute("valueType").value();
}
// add block to map
tdx_blocks_.insert(std::pair<std::string,block>(tdxblock.id_,tdxblock));
if ( showlog ) std::cout<<tdxblock.get_info()<<"\n";
}
if ( showlog ) std::cout<<"number of blocks: "<<tdx_blocks_.size()<<"\n";
}
// pugi::xml_node xmlusiincl = xml_doc_.child("usi:tdm").child("usi:include"); // pugi::xml_node xmlusiincl = xml_doc_.child("usi:tdm").child("usi:include");
// pugi::xml_node xmlusidata = xml_doc_.child("usi:tdm").child("usi:data"); // pugi::xml_node xmlusidata = xml_doc_.child("usi:tdm").child("usi:data");
// pugi::xml_node xmltdmroot = xml_doc_.child("usi:tdm").child("usi:data").child("tdm_root"); // pugi::xml_node xmltdmroot = xml_doc_.child("usi:tdm").child("usi:data").child("tdm_root");
} void tdm_reaper::print_channel(int idx, char const* name, int width)
void tdm_ripper::print_channel(int idx, char const* name, int width)
{ {
} }
void tdm_ripper::list_groups(std::ostream& out, int g, int c) void tdm_reaper::list_groups(std::ostream& out, int g, int c)
{ {
} }
void tdm_ripper::list_channels(std::ostream& out, int g, int c) void tdm_reaper::list_channels(std::ostream& out, int g, int c)
{ {
} }

View File

@ -22,18 +22,25 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
class tdm_ripper class tdm_reaper
{ {
// .tdm and .tdx paths/filenames // .tdm and .tdx paths/filenames
std::string tdmfile_; std::string tdmfile_;
std::string tdxfile_; std::string tdxfile_;
// set of .csv files // set of .csv files (encoding mode)
std::vector<std::string> csvfile_; std::vector<std::string> csvfile_;
// XML parser
pugi::xml_document xml_doc_;
pugi::xml_parse_result xml_result_;
// endianness (true = little, false = big) // endianness (true = little, false = big)
bool endianness_, machine_endianness_; bool endianness_, machine_endianness_;
// blocks of data in .tdx file
std::map<std::string,block> tdx_blocks_;
// tdm root // tdm root
tdm_root tdmroot_; tdm_root tdmroot_;
@ -65,10 +72,6 @@ class tdm_ripper
// NI datatypes ( ) // NI datatypes ( )
std::map<std::string, int> datatypes_; std::map<std::string, int> datatypes_;
// xml parser
pugi::xml_document xml_doc_;
pugi::xml_parse_result xml_result_;
// .tdm-file eventually contains some meta information (about measurement) // .tdm-file eventually contains some meta information (about measurement)
std::map<std::string,std::string> root_info_; std::map<std::string,std::string> root_info_;
std::map<std::string,std::string> meta_info_; std::map<std::string,std::string> meta_info_;
@ -78,7 +81,14 @@ class tdm_ripper
public: public:
tdm_ripper(std::string tdmfile, std::string tdxfile = std::string(""), bool showlog = false); // decoding
tdm_reaper(std::string tdmfile, std::string tdxfile = std::string(""), bool showlog = false);
// encoding
tdm_reaper(std::vector<std::string> csvfile);
// process <usi:include> element
void process_include(bool showlog);
void parse_structure(); void parse_structure();

View File

@ -42,7 +42,7 @@ main.o : src/main.cpp
$(CC) -c $(OPT) -I $(LIB) -I lib/ $<.cpp -o $@ $(CC) -c $(OPT) -I $(LIB) -I lib/ $<.cpp -o $@
@rm $<.cpp @rm $<.cpp
$(SRC).o : lib/$(SRC).cpp lib/$(SRC).hpp $(SRC).o : lib/$(SRC).cpp lib/$(SRC).hpp lib/tdm_datamodel.hpp
$(CC) -c $(OPT) -I $(LIB) $< -o $@ $(CC) -c $(OPT) -I $(LIB) $< -o $@
clean : clean :

View File

@ -171,15 +171,15 @@ int main(int argc, char* argv[])
: std::string(","); : std::string(",");
std::string files = cfgopts.count("filenames") == 1 ? cfgopts.at("filenames") std::string files = cfgopts.count("filenames") == 1 ? cfgopts.at("filenames")
: std::string("channel_%G_%C.csv"); : std::string("channel_%G_%C.csv");
bool listgroups = cfgopts.count("listgroups") == 1 ? true : false; // bool listgroups = cfgopts.count("listgroups") == 1 ? true : false;
bool listchannels = cfgopts.count("listchannels") == 1 ? true : false; // bool listchannels = cfgopts.count("listchannels") == 1 ? true : false;
// declare and initialize tdm_ripper instance // declare and initialize tdm_ripper instance
tdm_ripper jack(cfgopts.at("tdm"),cfgopts.at("tdx")); tdm_reaper jack(cfgopts.at("tdm"),cfgopts.at("tdx"),true);
// print list of groups or channels to stdout // print list of groups or channels to stdout
if ( listgroups ) jack.list_groups(); // if ( listgroups ) jack.list_groups();
if ( listchannels ) jack.list_channels(); // if ( listchannels ) jack.list_channels();
// // write data to filesystem // // write data to filesystem
// if ( !listgroups && !listchannels ) // if ( !listgroups && !listchannels )