/** * * ADTF File Access example * * @file * Copyright © Audi Electronics Venture GmbH. All rights reserved * * $Author: voigtlpi $ * $Date: 2014-10-31 14:01:50 +0100 (Fr, 31 Okt 2014) $ * $Revision: 30555 $ * * @remarks * */ #include #include #include #include #include void query_file_info(const std::string& filename) { using namespace adtf_file; // open file adtf_file::Reader reader(filename, standard_type_factories<>(), standard_sample_factories<>()); std::cout << "File: " << filename << std::endl; uint32_t version = reader.GetFileVersion(); std::string strADTFVersion("ADTF 3 and higher"); if (version < ifhd::v400::g_nVersionId) { strADTFVersion = "below ADTF 3"; } std::cout << std::endl << "File Header" << std::endl; std::cout << "------------------------------------------------------------------------------" << std::endl; std::cout << "File version : " << reader.GetFileVersion() << " - " << strADTFVersion << std::endl; std::cout << "Date : " << reader.GetDateTime().Format("%d.%m.%y - %H:%M:%S") << std::endl; std::cout << "Duration : " << reader.GetDuration().count() << std::endl; std::cout << "Short description : " << get_short_description(reader.GetDescription()) << std::endl; std::cout << "Long description : " << get_long_description(reader.GetDescription()) << std::endl; std::cout << "Chunk count : " << reader.GetItemCount() << std::endl; std::cout << "Extension count : " << reader.GetExtensions().size() << std::endl; std::cout << "Stream count : " << reader.GetStreams().size() << std::endl; std::cout << std::endl << "Streams" << std::endl; std::cout << "------------------------------------------------------------------------------" << std::endl; std::cout << " ------ getStreamItem -------- \n" << std::endl; auto fileItem_ = reader.GetNextItem(); std::cout << "Chunk count : " << fileItem_.stream_id << std::endl; std::cout << "Extension count : " << fileItem_.stream_item.get() << std::endl; std::cout << "Stream count : " << fileItem_.time_stamp.count() << std::endl; auto type = std::dynamic_pointer_cast< const ::adtf_file::StreamType >(fileItem_.stream_item); auto data = std::dynamic_pointer_cast< const ::adtf_file::Sample >(fileItem_.stream_item); auto trigger = std::dynamic_pointer_cast< const ::adtf_file::Trigger >(fileItem_.stream_item); auto sample_data = std::dynamic_pointer_cast< const ::adtf_file::DefaultSample >(data); int a = 0; auto timee = sample_data->GetTimeStamp().count(); int b = 0; std::cout << " ------ getStreamItem -------- \n" << std::endl; auto streamExtend = reader.GetExtensions(); for (const auto& currentExtend : streamExtend) { std::cout << "------------------------------------------------------------------------------ :" << std::endl; std::cout << "New Extension :" << std::endl; std::cout << "name :" << currentExtend.name << std::endl; std::cout << "data_size :" << currentExtend.data_size << std::endl; std::cout << "stream_id :" << currentExtend.stream_id << std::endl; std::cout << "type_id :" << currentExtend.type_id << std::endl; std::cout << "user_id :" << currentExtend.user_id << std::endl; std::cout << "version_id :" << currentExtend.version_id << std::endl; } auto streams = reader.GetStreams(); for (const auto& current_stream : streams) { auto property_stream_type = std::dynamic_pointer_cast(current_stream.initial_type); if (property_stream_type) { std::string stream_meta_type = property_stream_type->GetMetaType(); std::cout << "Stream #" << current_stream.stream_id << " : " << current_stream.name << std::endl; std::cout << " MetaType : " << stream_meta_type << std::endl; property_stream_type->IterateProperties( [&](const char* name, const char* type, const char* value) -> void { std::cout << " " << name << " - " << value << std::endl; }); } } } class StreamsInfo { typedef std::map LastTimesMap; typedef std::map StreamNameMap; public: StreamsInfo(adtf_file::Reader& reader) { auto streams = reader.GetStreams(); for (auto current_stream : streams) { m_mapStreamName[current_stream.stream_id] = current_stream.name; UpdateType(current_stream.stream_id, current_stream.initial_type); } } ~StreamsInfo() = default; std::string GetDiffToLastChunkTime(const uint16_t& stream_id, const std::chrono::microseconds& tmCurrentTime) { return GetLastTimeStamp(m_mapLastChunkTime, stream_id, tmCurrentTime); } std::string GetDiffToLastSampleStreamTime(const uint16_t& stream_id, const std::chrono::microseconds& tmCurrentTime) { return GetLastTimeStamp(m_mapLastStreamTime, stream_id, tmCurrentTime); } std::string GetStreamName(const uint16_t& stream_id) { return m_mapStreamName[stream_id]; } void UpdateType(const uint16_t& stream_id, const std::shared_ptr& type) { auto property_stream_type = std::dynamic_pointer_cast(type); if (property_stream_type) { m_mapStreamMetaType[stream_id] = property_stream_type->GetMetaType(); } } std::string GetLastStreamMetaType(const uint16_t& stream_id) { return m_mapStreamMetaType[stream_id]; } private: std::string GetLastTimeStamp(LastTimesMap& mapLastTimes, const uint16_t& stream_id, const std::chrono::microseconds& tmCurrentTime) { std::chrono::microseconds tmResult(-1); LastTimesMap::iterator it = mapLastTimes.find(stream_id); if (it != mapLastTimes.end()) { tmResult = tmCurrentTime - it->second; it->second = tmCurrentTime; } else { if (tmCurrentTime.count() != -1) { mapLastTimes[stream_id] = tmCurrentTime; } } if (tmResult.count() >= 0) { return a_util::strings::format("%lld", tmResult.count()); } else { return ""; } } LastTimesMap m_mapLastChunkTime; LastTimesMap m_mapLastStreamTime; StreamNameMap m_mapStreamName; StreamNameMap m_mapStreamMetaType; }; void access_file_data(const std::string& filename, const std::string& csv_file_path) { using namespace adtf_file; StandardReader reader (filename); StreamsInfo stream_info(reader); std::cout << std::endl << "File data" << std::endl; std::cout << "------------------------------------------------------------------------------" << std::endl; utils5ext::cFile csv_file; csv_file.Open(csv_file_path, utils5ext::cFile::OM_Append | utils5ext::cFile::OM_Write); // set the labels csv_file.WriteLine("stream;stream_name;chunk_type;stream_type;chunk_time;samplestream_time;chunk_time_delta_to_lastofstream;samplestream_time_delta_to_lastofstream"); size_t item_count = 0; for (;; ++item_count) { try { auto item = reader.GetNextItem(); std::chrono::microseconds chunk_time = item.time_stamp; std::string chunk_type; auto type = std::dynamic_pointer_cast(item.stream_item); auto data = std::dynamic_pointer_cast(item.stream_item); auto trigger = std::dynamic_pointer_cast(item.stream_item); std::chrono::microseconds sample_time(-1); std::string sample_time_string(""); if (type) { //the type change is part of the chunk_type = "stream_type"; stream_info.UpdateType(item.stream_id, type); } else if (data) { chunk_type = "sample"; auto sample_data = std::dynamic_pointer_cast(data); if (sample_data) { sample_time = sample_data->GetTimeStamp(); sample_time_string = a_util::strings::format("%lld", sample_time.count()); } } else if (trigger) { chunk_type = "trigger"; } csv_file.WriteLine(a_util::strings::format("%d;%s;%s;%s;%lld;%s;%s;%s", static_cast(item.stream_id), stream_info.GetStreamName(item.stream_id).c_str(), chunk_type.c_str(), stream_info.GetLastStreamMetaType(item.stream_id).c_str(), chunk_time.count(), sample_time_string.c_str(), stream_info.GetDiffToLastChunkTime(item.stream_id, chunk_time).c_str(), stream_info.GetDiffToLastSampleStreamTime(item.stream_id, sample_time).c_str() )); } catch (const exceptions::end_of_file&) { break; } } csv_file.Close(); } int main(int argc, char* argv[]) { if (argc < 3 || argv[1] == NULL || argv[2] == NULL) { std::cout << "usage: " << argv[0] << " DAT-File CSV-File" << std::endl; return -1; } std::string strWorkingFile = argv[1]; try { query_file_info(strWorkingFile); } catch (const std::exception& ex) { std::cout << "could not query info of file " << argv[1] << std::endl; std::cout << ex.what() << std::endl; return -2; } try { access_file_data(strWorkingFile, argv[2]); } catch (const std::exception& ex) { std::cout << "could not access data of file " << argv[1] << std::endl; std::cout << ex.what() << std::endl; return -2; } return 0; }