Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround for std::tmpfile() returning nullptr o Windows #45

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ build/
*.user
*.userosscache
*.sln.docstates
TestResults/
TestResults/ASAM MDF - Wiki.pdf
Licensing_Terms_for_Grant_of_Rights_to_Use_ASAM_Products_DE.pdf
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ project(
DESCRIPTION "Interface against MDF 3/4 files"
LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_DEBUG_POSTFIX d)

add_subdirectory(mdflib)
Expand Down
2 changes: 1 addition & 1 deletion include/mdf/iblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IBlock {
/** \brief Returns the block type.
*
* The block type is 2 character string for example 'AT' and 'DT'. In MDF 4,
* the block type is actual 4 character but this function removes
* the block type is actually 4 characters, but this function removes
* the two first characters.
* @return Block type (2 characters length).
*/
Expand Down
12 changes: 4 additions & 8 deletions include/mdf/ichannelconversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ class IChannelConversion : public IBlock {
};

template <typename T, typename V>
inline bool IChannelConversion::Convert(const T& channel_value,
V& eng_value) const {
inline bool IChannelConversion::Convert(const T& channel_value, V& eng_value) const {
bool valid = false;
double value = 0.0;
switch (Type()) {
Expand All @@ -345,8 +344,7 @@ inline bool IChannelConversion::Convert(const T& channel_value,
}

case ConversionType::ValueToValueInterpolation: {
valid = ConvertValueToValueInterpolate(
static_cast<double>(channel_value), value);
valid = ConvertValueToValueInterpolate(static_cast<double>(channel_value), value);
eng_value = static_cast<V>(value);
break;
}
Expand All @@ -358,8 +356,7 @@ inline bool IChannelConversion::Convert(const T& channel_value,
}

case ConversionType::ValueRangeToValue: {
valid =
ConvertValueRangeToValue(static_cast<double>(channel_value), value);
valid = ConvertValueRangeToValue(static_cast<double>(channel_value), value);
eng_value = static_cast<V>(value);
break;
}
Expand All @@ -374,8 +371,7 @@ inline bool IChannelConversion::Convert(const T& channel_value,

case ConversionType::ValueRangeToText: {
std::string text;
valid =
ConvertValueRangeToText(static_cast<double>(channel_value), text);
valid = ConvertValueRangeToText(static_cast<double>(channel_value), text);
std::istringstream s(text);
s >> eng_value;
break;
Expand Down
10 changes: 5 additions & 5 deletions include/mdf/mdfreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ void CreateChannelObserverForChannelGroup(const IDataGroup& data_group,
*/
class MdfReader {
public:
explicit MdfReader(
const std::string& filename); ///< Constructor that opens the file and
///< read ID and HD block.
///< Constructor that opens the file and
///< read ID and HD block.
explicit MdfReader(const std::string& filename);
virtual ~MdfReader(); ///< Destructor that close any open file and destructs.

MdfReader() = delete;
Expand Down Expand Up @@ -104,8 +104,8 @@ class MdfReader {
bool ExportAttachmentData(const IAttachment& attachment,
const std::string& dest_file);

bool ReadData(const IDataGroup& data_group); ///< Reads the sample data. See
///< sample observer.
/// Reads the sample data. See sample observer.
bool ReadData(const IDataGroup& data_group);

private:
std::FILE* file_ = nullptr; ///< Pointer to the file stream.
Expand Down
4 changes: 2 additions & 2 deletions mdflib/src/at4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

bool FileToBuffer(const std::string& filename, mdf::ByteArray& dest) {
try {
path fullname = u8path(filename);

Check warning on line 66 in mdflib/src/at4block.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
const auto size = file_size(fullname);
if (size > 0) {
dest.resize(size, 0);
Expand Down Expand Up @@ -114,7 +114,7 @@
std::string name;
if (Link(kIndexFilename) > 0) {
try {
std::filesystem::path p = std::filesystem::u8path(filename_);

Check warning on line 117 in mdflib/src/at4block.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
const auto& u8str = p.filename().u8string();
name = std::string(u8str.begin(), u8str.end());
} catch (const std::exception&) {
Expand Down Expand Up @@ -164,7 +164,7 @@
}
ByteArray data_buffer;
try {
path filename = u8path(filename_);

Check warning on line 167 in mdflib/src/at4block.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
if (!std::filesystem::exists(filename)) {
MDF_ERROR() << "Attachment File doesn't exist. File: " << filename_;
return 0;
Expand Down Expand Up @@ -244,8 +244,8 @@
}
} else {
// Need to copy the source file
std::filesystem::path s = std::filesystem::u8path(filename_);

Check warning on line 247 in mdflib/src/at4block.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
std::filesystem::path d = std::filesystem::u8path(dest_file);

Check warning on line 248 in mdflib/src/at4block.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
if (s != d) {
std::filesystem::copy_file(
s, d, std::filesystem::copy_options::overwrite_existing);
Expand All @@ -255,7 +255,7 @@
std::vector<uint8_t> md5(16, 0xFF);
CreateMd5FileChecksum(dest_file, md5);
if (md5 != md5_) {
throw std::runtime_error("Mismatching MD5 checksums");
throw std::runtime_error("Mismatched MD5 checksums");
}
}
}
Expand Down Expand Up @@ -295,4 +295,4 @@
void At4Block::CreatorIndex(uint16_t creator) { creator_index_ = creator; }
uint16_t At4Block::CreatorIndex() const { return creator_index_; }

} // namespace mdf::detail
} // namespace mdf::detail
18 changes: 17 additions & 1 deletion mdflib/src/dg4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "dg4block.h"

#include <filesystem>
#include <stdexcept>
#include <algorithm>

Expand Down Expand Up @@ -192,6 +193,12 @@ void Dg4Block::ReadData(std::FILE* file) const {

bool close_data_file = false;
std::FILE* data_file = nullptr;
#ifdef _WIN32
const auto now = std::chrono::system_clock::now();
std::wstring tmppath = std::filesystem::temp_directory_path()
.append("mdflib-" + std::to_string(now.time_since_epoch().count()));
#endif

size_t data_size = 0;
if (block_list.size() == 1 && block_list[0] &&
block_list[0]->BlockType() == "DT") { // If DT read from file directly
Expand All @@ -204,6 +211,12 @@ void Dg4Block::ReadData(std::FILE* file) const {
} else {
close_data_file = true;
data_file = std::tmpfile();
#ifdef _WIN32
// std::tmpfile() sometimes returns nullpointer on windows, workaround:
if (data_file == nullptr) {
data_file = _wfopen(tmppath.c_str(), L"wb+");
}
#endif
data_size = CopyDataToFile(block_list, file, data_file);
std::rewind(data_file); // SetFilePosition(data_file,0);
}
Expand All @@ -213,6 +226,9 @@ void Dg4Block::ReadData(std::FILE* file) const {
ParseDataRecords(data_file, data_size);
if (data_file != nullptr && close_data_file) {
fclose(data_file);
#ifdef _WIN32
_wremove(tmppath.c_str());
#endif
}

for (const auto& cg : cg_list_) {
Expand Down Expand Up @@ -446,4 +462,4 @@ const IChannelGroup* Dg4Block::FindParentChannelGroup(const IChannel&
return itr != cg_list.cend() ? itr->get() : nullptr;
}

} // namespace mdf::detail
} // namespace mdf::detail
4 changes: 2 additions & 2 deletions mdflib/src/ichannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void IChannel::CopyToDataBuffer(const std::vector<uint8_t> &record_buffer,
for (auto i = first_byte; i <= last_byte; ++i) {
value <<= 8;
value |= record_buffer[i];
}
}

// Apply offset
value >>= BitOffset();
Expand Down Expand Up @@ -990,4 +990,4 @@ IChannel *IChannel::CreateChannelComposition(const std::string_view &name) {
return new_channel;
}

} // end namespace mdf
} // end namespace mdf
2 changes: 1 addition & 1 deletion mdflib/src/ichannelconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool IChannelConversion::ConvertRational(double channel_value,

eng_value = (Parameter(0) * std::pow(channel_value, 2)) +
(Parameter(1) * channel_value) + Parameter(2);
const double div = (Parameter(3) * std::pow(channel_value, 2)) +
const double div = (Parameter(3) * std::pow(channel_value, 2.)) +
(Parameter(4) * channel_value) + Parameter(5);
if (div == 0.0) {
return false;
Expand Down
3 changes: 1 addition & 2 deletions mdflib/src/mdfblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*/
#include "mdfblock.h"

#include <stdio.h>

#include <cstdio>
#include <chrono>
#include <ios>
#include <sstream>
Expand Down
4 changes: 3 additions & 1 deletion mdflib/src/mdfreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
// Need to create MDF3 of MDF4 file
bool bExist = false;
try {
std::filesystem::path p = std::filesystem::u8path(filename_);

Check warning on line 189 in mdflib/src/mdfreader.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
if (std::filesystem::exists(p)) {
bExist = true;
}
Expand Down Expand Up @@ -236,14 +236,16 @@

std::string MdfReader::ShortName() const {
try {
std::filesystem::path file = std::filesystem::u8path(filename_);

Check warning on line 239 in mdflib/src/mdfreader.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
return file.stem().string();
} catch (const std::exception &) {
}
return {};
}

bool MdfReader::Open() { return detail::OpenMdfFile(file_, filename_, "rb"); }
bool MdfReader::Open() {
return detail::OpenMdfFile(file_, filename_, "rb");
}

void MdfReader::Close() {
if (file_ != nullptr) {
Expand Down
8 changes: 4 additions & 4 deletions mdflib/src/mdfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@

std::string MdfWriter::Name() const {
try {
path filename = u8path(filename_);

Check warning on line 468 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

'u8path<std::string>' is deprecated [-Wdeprecated-declarations]
return filename.stem().string();
} catch (...) {
}
Expand Down Expand Up @@ -626,10 +626,10 @@
cc_length->Type(ConversionType::ValueToValue);
size_t index = 0;
for (size_t key = 0; key < 16; ++key) {
cc_length->Parameter(index++,key);

Check failure on line 629 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

call to member function 'Parameter' is ambiguous
cc_length->Parameter(index++,CanMessage::DlcToLength(key));

Check failure on line 630 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

call to member function 'Parameter' is ambiguous
}
cc_length->Parameter(index, 0ULL);
cc_length->Parameter(index, static_cast<uint64_t>(0));
}
}
}
Expand Down Expand Up @@ -736,10 +736,10 @@
cc_length->Type(ConversionType::ValueToValue);
size_t index = 0;
for (size_t key = 0; key < 16; ++key) {
cc_length->Parameter(index++,key);

Check failure on line 739 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

call to member function 'Parameter' is ambiguous
cc_length->Parameter(index++,CanMessage::DlcToLength(key));

Check failure on line 740 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

call to member function 'Parameter' is ambiguous
}
cc_length->Parameter(index, 0ULL);
cc_length->Parameter(index, static_cast<uint64_t>(0));
}
}
}
Expand Down Expand Up @@ -818,10 +818,10 @@
cc_length->Type(ConversionType::ValueToValue);
size_t index = 0;
for (size_t key = 0; key < 16; ++key) {
cc_length->Parameter(index++,key);

Check failure on line 821 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

call to member function 'Parameter' is ambiguous
cc_length->Parameter(index++,CanMessage::DlcToLength(key));

Check failure on line 822 in mdflib/src/mdfwriter.cpp

View workflow job for this annotation

GitHub Actions / run on macos-latest(x64)

call to member function 'Parameter' is ambiguous
}
cc_length->Parameter(index, 0ULL);
cc_length->Parameter(index, static_cast<uint64_t>(0));
}
}
}
Expand Down Expand Up @@ -915,4 +915,4 @@

}

} // namespace mdf
} // namespace mdf
2 changes: 2 additions & 0 deletions mdflib/src/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ void strerror(int __errnum, char *__buf, size_t __buflen) {
}
#elif (__CYGWIN__)
strerror_r(__errnum, __buf, __buflen);
#elif (__EMSCRIPTEN__)
strerror_r(__errnum, __buf, __buflen);
#else
auto* dummy = strerror_r(__errnum, __buf, __buflen);
if (dummy != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion mdflib_test/TestMDFCMake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(mdfTestReadExample
DESCRIPTION "Test find_package mdf"
LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)

find_package(MdfLib REQUIRED)

Expand Down
4 changes: 4 additions & 0 deletions mdflibrary/src/MdfExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ using namespace mdf;
// WINDOWS
#define EXPORT(ReturnType, ClassName, FuncName, ...) \
__declspec(dllexport) ReturnType ClassName##FuncName(__VA_ARGS__)
#elif defined(__EMSCRIPTEN__)
// Web Assembly
#define EXPORT(ReturnType, ClassName, FuncName, ...) \
ReturnType ClassName##FuncName(__VA_ARGS__)
#elif defined(__linux__)
// LINUX
#define EXPORT(ReturnType, ClassName, FuncName, ...) \
Expand Down
2 changes: 1 addition & 1 deletion mdflibrary_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ project(
DESCRIPTION "MdfLibraryTest"
LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)

add_executable(mdflibraryexample src/test.cpp)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Expand Down
102 changes: 59 additions & 43 deletions mdfviewer/src/channelobserverlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,75 @@

namespace mdf::viewer {

ChannelObserverListView::ChannelObserverListView(wxWindow *parent, wxWindowID win_id, const wxPoint &pos,
const wxSize &size, long style)
: wxListView(parent,win_id,pos,size,style) {
ChannelObserverListView::ChannelObserverListView(wxWindow *parent,
wxWindowID win_id,
const wxPoint &pos,
const wxSize &size,
long style)
: wxListView(parent,win_id,pos,size,style) {

}

ChannelObserverListView::~ChannelObserverListView() {
if (observer_list_) {
observer_list_->clear();
}
observer_list_.reset();
ChannelObserverListView::~ChannelObserverListView()
{
if (observer_list_)
{
observer_list_->clear();
}
observer_list_.reset();
}

wxString ChannelObserverListView::OnGetItemText(long item, long column) const {
if (item < 0 || !observer_list_) {
return "?";
}
auto sample = static_cast<size_t>(item);
std::ostringstream s;
if (column > 0) {
auto index = static_cast<size_t>(column - 1);
if (index < observer_list_->size()) {
const auto& observer = observer_list_->at(index);
wxString ChannelObserverListView::OnGetItemText(long item, long column) const
{
if (item < 0 || !observer_list_)
{
return "?";
}
auto sample = static_cast<size_t>(item);
std::ostringstream s;
if (column > 0)
{
auto index = static_cast<size_t>(column - 1);
if (index < observer_list_->size())
{
const auto& observer = observer_list_->at(index);

if (observer) {
std::string value;
bool valid = observer->GetEngValue(sample, value);
s << (valid ? value : "*");
const auto& channel = observer->Channel();
const auto* cc = channel.ChannelConversion();
if (observer)
{
std::string value;
bool valid = observer->GetEngValue(sample, value);
s << (valid ? value : "*");
const auto& channel = observer->Channel();
const auto* cc = channel.ChannelConversion();

if (cc != nullptr) {
switch(cc->Type()) {
case ConversionType::NoConversion:
case ConversionType::DateConversion:
case ConversionType::TimeConversion:
break;
if (cc != nullptr)
{
switch(cc->Type())
{
case ConversionType::NoConversion:
case ConversionType::DateConversion:
case ConversionType::TimeConversion:
break;

default: // Show channel value
valid = observer->GetChannelValue(sample, value);
s << " (" << (valid ? value : "*") << ")";
break;
}
default: // Show channel value
valid = observer->GetChannelValue(sample, value);
s << " (" << (valid ? value : "*") << ")";
break;
}
}
}
else
{
s << "?";
}
}
} else {
s << "?";
}
}
} else if (column == 0) {
s << sample;
}
else if (column == 0)
{
s << sample;
}

return wxString::FromUTF8(s.str());
return wxString::FromUTF8(s.str());
}

} // namespace mdf::viewer
} // namespace mdf::viewer
Loading