Project

General

Profile

Support Request #1257 » fileaccess.cpp

hidden, 2017-10-24 10:00

 
1
/**
2
 *
3
 * ADTF File Access example
4
 *
5
 * @file
6
 * Copyright © Audi Electronics Venture GmbH. All rights reserved
7
 *
8
 * $Author: voigtlpi $
9
 * $Date: 2014-10-31 14:01:50 +0100 (Fr, 31 Okt 2014) $
10
 * $Revision: 30555 $
11
 *
12
 * @remarks
13
 *
14
 */
15
#include <standard_adtf_file_reader.h>
16

    
17
#include <stdio.h>
18
#include <iostream>
19
#include <sstream>
20
#include <map>
21

    
22
void query_file_info(const std::string& filename)
23
{
24
    using namespace adtf_file;
25
    // open file
26
    adtf_file::Reader reader(filename,
27
                             standard_type_factories<>(),
28
                             standard_sample_factories<>());
29

    
30

    
31
    std::cout << "File: " << filename << std::endl;
32
    uint32_t version = reader.GetFileVersion();
33
    std::string strADTFVersion("ADTF 3 and higher");
34
    if (version < ifhd::v400::g_nVersionId)
35
    {
36
        strADTFVersion = "below ADTF 3";
37
    }
38
    
39
    std::cout << std::endl << "File Header" << std::endl;
40
    std::cout << "------------------------------------------------------------------------------" << std::endl;
41
    std::cout << "File version      : " << reader.GetFileVersion() << " - " << strADTFVersion << std::endl;
42
    std::cout << "Date              : " << reader.GetDateTime().Format("%d.%m.%y - %H:%M:%S") << std::endl;
43
    std::cout << "Duration          : " << reader.GetDuration().count() << std::endl;
44
    std::cout << "Short description : " << get_short_description(reader.GetDescription()) << std::endl;
45
    std::cout << "Long description  : " << get_long_description(reader.GetDescription()) << std::endl;
46
    std::cout << "Chunk count       : " << reader.GetItemCount() << std::endl;
47
    std::cout << "Extension count   : " << reader.GetExtensions().size() << std::endl;
48
    std::cout << "Stream count      : " << reader.GetStreams().size() << std::endl;
49
    std::cout << std::endl << "Streams" << std::endl;
50
    std::cout << "------------------------------------------------------------------------------" << std::endl;
51

    
52

    
53
	std::cout << " ------ getStreamItem -------- \n" << std::endl;
54

    
55
	auto fileItem_ = reader.GetNextItem();
56
	std::cout << "Chunk count       : " << fileItem_.stream_id   << std::endl;
57
	std::cout << "Extension count   : " << fileItem_.stream_item.get() << std::endl;
58
	std::cout << "Stream count      : " << fileItem_.time_stamp.count()  << std::endl;
59
	
60
	auto type = std::dynamic_pointer_cast< const ::adtf_file::StreamType >(fileItem_.stream_item);
61
	auto data = std::dynamic_pointer_cast< const ::adtf_file::Sample >(fileItem_.stream_item);
62
	auto trigger = std::dynamic_pointer_cast< const ::adtf_file::Trigger >(fileItem_.stream_item);
63
	auto sample_data = std::dynamic_pointer_cast< const ::adtf_file::DefaultSample >(data);
64
	int a = 0;
65
	auto timee = sample_data->GetTimeStamp().count();
66
	int b = 0;
67
	std::cout << " ------ getStreamItem -------- \n" << std::endl;
68

    
69
	auto streamExtend = reader.GetExtensions();
70

    
71
    for (const auto& currentExtend : streamExtend)
72
	{
73
		std::cout << "------------------------------------------------------------------------------ :" << std::endl;
74
		std::cout << "New Extension :" << std::endl;
75
		std::cout << "name       :" << currentExtend.name << std::endl;
76
		std::cout << "data_size  :"  << currentExtend.data_size << std::endl;		
77
		std::cout << "stream_id  :"  << currentExtend.stream_id << std::endl;
78
		std::cout << "type_id    :"  << currentExtend.type_id << std::endl;
79
		std::cout << "user_id    :"  << currentExtend.user_id << std::endl;
80
		std::cout << "version_id :"  << currentExtend.version_id << std::endl;
81
		
82
       
83
	}
84

    
85
    auto streams = reader.GetStreams();
86

    
87
    for (const auto& current_stream : streams)
88
    {
89
        auto property_stream_type = std::dynamic_pointer_cast<const PropertyStreamType>(current_stream.initial_type);
90
        if (property_stream_type)
91
        {
92
            std::string stream_meta_type = property_stream_type->GetMetaType();      
93
            std::cout << "Stream #" << current_stream.stream_id << " : " << current_stream.name << std::endl;
94
            std::cout << "    MetaType       : " << stream_meta_type << std::endl;
95

    
96
            property_stream_type->IterateProperties(
97
                [&](const char* name, 
98
                const char* type,
99
                const char* value) -> void
100
                {
101
                    std::cout << "        " << name << " - " << value << std::endl;
102
                });
103
        }
104
    }
105
}
106

    
107

    
108

    
109
class StreamsInfo 
110
{
111
    typedef std::map<uint16_t, std::chrono::microseconds> LastTimesMap;
112
    typedef std::map<uint16_t, std::string> StreamNameMap;
113

    
114
    public:
115
        StreamsInfo(adtf_file::Reader& reader)
116
        {
117
            auto streams = reader.GetStreams();
118
            for (auto current_stream : streams)
119
            {
120
                m_mapStreamName[current_stream.stream_id] = current_stream.name;
121
                UpdateType(current_stream.stream_id, current_stream.initial_type);
122
            }
123
        }
124
        ~StreamsInfo() = default;
125
       std::string GetDiffToLastChunkTime(const uint16_t& stream_id, const std::chrono::microseconds& tmCurrentTime)
126
        {
127
            return GetLastTimeStamp(m_mapLastChunkTime, stream_id, tmCurrentTime);
128
        }
129
        std::string GetDiffToLastSampleStreamTime(const uint16_t& stream_id, const std::chrono::microseconds& tmCurrentTime)
130
        {
131
            return GetLastTimeStamp(m_mapLastStreamTime, stream_id, tmCurrentTime);
132
        }
133
        std::string GetStreamName(const uint16_t& stream_id)
134
        {
135
            return m_mapStreamName[stream_id];
136
        }
137

    
138
        void UpdateType(const uint16_t& stream_id, const std::shared_ptr<const adtf_file::StreamType>& type)
139
        {
140
            auto property_stream_type = std::dynamic_pointer_cast<const adtf_file::PropertyStreamType>(type);
141
            if (property_stream_type)
142
            {
143
                 m_mapStreamMetaType[stream_id] = property_stream_type->GetMetaType();
144
            }
145
        }
146
        std::string GetLastStreamMetaType(const uint16_t& stream_id)
147
        {
148
            return m_mapStreamMetaType[stream_id];
149
        }
150
        
151
    private:
152
        std::string GetLastTimeStamp(LastTimesMap& mapLastTimes,
153
                                                   const uint16_t& stream_id,
154
                                                   const std::chrono::microseconds& tmCurrentTime)
155
        {
156
            std::chrono::microseconds tmResult(-1);
157
            LastTimesMap::iterator it = mapLastTimes.find(stream_id);
158
            if (it != mapLastTimes.end())
159
            {
160
                tmResult = tmCurrentTime - it->second;
161
                it->second = tmCurrentTime;
162
            }
163
            else
164
            {
165
                if (tmCurrentTime.count() != -1)
166
                {
167
                    mapLastTimes[stream_id] = tmCurrentTime;
168
                }
169
            }
170
            if (tmResult.count() >= 0)
171
            {
172
                return a_util::strings::format("%lld", tmResult.count());
173
            }
174
            else
175
            {
176
                return "";
177
            }
178
        }
179
        LastTimesMap m_mapLastChunkTime;
180
        LastTimesMap m_mapLastStreamTime;
181
        StreamNameMap m_mapStreamName;
182
        StreamNameMap m_mapStreamMetaType;
183
};
184

    
185

    
186
void access_file_data(const std::string& filename, const std::string& csv_file_path)
187
{
188
    using namespace adtf_file;
189
    StandardReader reader (filename);
190
    StreamsInfo stream_info(reader);
191

    
192
    std::cout << std::endl << "File data" << std::endl;
193
    std::cout << "------------------------------------------------------------------------------" << std::endl;
194

    
195

    
196
    utils5ext::cFile csv_file;
197
    csv_file.Open(csv_file_path, utils5ext::cFile::OM_Append | utils5ext::cFile::OM_Write);
198

    
199
    // set the labels
200
    csv_file.WriteLine("stream;stream_name;chunk_type;stream_type;chunk_time;samplestream_time;chunk_time_delta_to_lastofstream;samplestream_time_delta_to_lastofstream");
201

    
202
    size_t item_count = 0;
203
    for (;; ++item_count)
204
    {
205
        try
206
        {
207
            auto item = reader.GetNextItem();
208
            std::chrono::microseconds chunk_time = item.time_stamp;
209
            
210
            std::string chunk_type;
211
            auto type = std::dynamic_pointer_cast<const StreamType>(item.stream_item);
212
            auto data = std::dynamic_pointer_cast<const Sample>(item.stream_item);
213
            auto trigger = std::dynamic_pointer_cast<const Trigger>(item.stream_item);
214
            std::chrono::microseconds sample_time(-1);
215
            std::string sample_time_string("");
216
            if (type)
217
            {
218
                //the type change is part of the 
219
                chunk_type = "stream_type";
220
                stream_info.UpdateType(item.stream_id,
221
                                       type);
222
            }
223
            else if (data)
224
            {
225
                chunk_type = "sample";
226
                auto sample_data = std::dynamic_pointer_cast<const DefaultSample>(data);
227
                if (sample_data)
228
                {
229
                    sample_time = sample_data->GetTimeStamp();
230
                    sample_time_string = a_util::strings::format("%lld", sample_time.count());
231
                }
232
            }
233
            else if (trigger)
234
            {
235
                chunk_type = "trigger";
236
            }
237
            
238
            csv_file.WriteLine(a_util::strings::format("%d;%s;%s;%s;%lld;%s;%s;%s",
239
                static_cast<int>(item.stream_id),
240
                stream_info.GetStreamName(item.stream_id).c_str(),
241
                chunk_type.c_str(),
242
                stream_info.GetLastStreamMetaType(item.stream_id).c_str(),
243
                chunk_time.count(),
244
                sample_time_string.c_str(),
245
                stream_info.GetDiffToLastChunkTime(item.stream_id, chunk_time).c_str(),
246
                stream_info.GetDiffToLastSampleStreamTime(item.stream_id, sample_time).c_str()
247
                ));
248
        }
249
        catch (const exceptions::end_of_file&)
250
        {
251
            break;
252
        }
253
    }
254

    
255
    csv_file.Close();
256
}
257

    
258
int main(int argc, char* argv[])
259
{
260
    if (argc < 3 || argv[1] == NULL || argv[2] == NULL)
261
    {
262
        std::cout << "usage: " << argv[0] << " DAT-File CSV-File"  << std::endl;
263
        return -1;
264
    }
265

    
266
    std::string strWorkingFile = argv[1];
267

    
268
    try
269
    {
270
        query_file_info(strWorkingFile);
271
    }
272
    catch (const std::exception& ex)
273
    {
274
        std::cout << "could not query info of file " << argv[1] << std::endl;
275
        std::cout << ex.what() << std::endl;
276
        return -2;
277
    }
278

    
279
    try
280
    {
281
        access_file_data(strWorkingFile, argv[2]);
282
    }
283
    catch (const std::exception& ex)
284
    {
285
        std::cout << "could not access data of file " << argv[1] << std::endl;
286
        std::cout << ex.what() << std::endl;
287
        return -2;
288
    }
289
    return 0;
290
}
(1-1/2)