diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ffcfd33e2..875ac2c6c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,6 +247,7 @@ set(eCAL_config ${eCAL_config_dir}/eCALConfig.cmake) set(eCAL_config_version ${eCAL_config_dir}/eCALConfigVersion.cmake) if(WIN32) #set(eCAL_install_config_dir $ENV{PROGRAMDATA}/eCAL) + set(eCAL_install_samples_dir samples/bin/) set(eCAL_install_samples_src_dir samples/src/) set(eCAL_install_tests_dir tests) diff --git a/README.md b/README.md index a4a9147e06..99e8637a10 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ setup the correct ip address - here for adapter eth0, ip address 192.168.0.1 sudo ifconfig eth0 192.168.0.1 netmask 255.255.255.0 ``` -Restart the ethernet interface or the whole to apply changes, check if you can ping to each other. +Restart the ethernet interface or the whole machine to apply changes, check if you can ping to each other. After the ipc configuration setup the multicast route for udp multicasting diff --git a/app/mon/eCALMon/CMakeLists.txt b/app/mon/eCALMon/CMakeLists.txt index 63eafa6819..c12e2a7b93 100644 --- a/app/mon/eCALMon/CMakeLists.txt +++ b/app/mon/eCALMon/CMakeLists.txt @@ -127,6 +127,7 @@ set(source_files src/Widgets/TreeItemViewWidget/TreeItemViewWidget.cpp src/Widgets/TreeItemViewWidget/TreeItemViewWidget.h ) + set(qt_resource_files ../../iconset/ecalicons.qrc resources/resources.qrc @@ -151,6 +152,18 @@ set(ui_files src/Widgets/VisualisationWidget/VisualisationWidget.ui ) +if(ECAL_NPCAP_SUPPORT) + set(source_files + ${source_files} + src/Widgets/NpcapStatusDialog/NpcapStatusDialog.cpp + src/Widgets/NpcapStatusDialog/NpcapStatusDialog.h + ) + set(ui_files + ${ui_files} + src/Widgets/NpcapStatusDialog/NpcapStatusDialog.ui + ) +endif(ECAL_NPCAP_SUPPORT) + # compile qt resource files and ui files qt5_add_resources(autogen_resources ${qt_resource_files}) qt5_wrap_ui (autogen_ui ${ui_files}) @@ -176,6 +189,14 @@ target_link_libraries (${PROJECT_NAME} Qt5::Widgets CustomQt ) + +if(ECAL_NPCAP_SUPPORT) + add_definitions(-DECAL_NPCAP_SUPPORT) + target_link_libraries(${PROJECT_NAME} + udpcap + ) +endif(ECAL_NPCAP_SUPPORT) + if(WIN32) set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") diff --git a/app/mon/eCALMon/src/Ecalmon.cpp b/app/mon/eCALMon/src/Ecalmon.cpp index ba6d9789d8..9bfd999159 100644 --- a/app/mon/eCALMon/src/Ecalmon.cpp +++ b/app/mon/eCALMon/src/Ecalmon.cpp @@ -25,6 +25,10 @@ #include "Widgets/LicenseDialog/LicenseDialog.h" #include "Widgets/PluginSettingsDialog/PluginSettingsDialog.h" +#ifdef ECAL_NPCAP_SUPPORT +#include "Widgets/NpcapStatusDialog/NpcapStatusDialog.h" +#endif //ECAL_NPCAP_SUPPORT + #include "EcalmonGlobals.h" #include "PluginLoader.h" @@ -157,6 +161,17 @@ Ecalmon::Ecalmon(QWidget *parent) // Parse Time connect(ui_.action_show_parsed_times, &QAction::toggled, this, &Ecalmon::setParseTimeEnabled); +#ifdef ECAL_NPCAP_SUPPORT + connect(ui_.action_npcap_status, &QAction::triggered, this, + [this]() + { + NpcapStatusDialog npcap_status_dialog(this); + npcap_status_dialog.exec(); + }); +#else + ui_.action_npcap_status->setVisible(false); +#endif // ECAL_NPCAP_SUPPORT + // Reset layout connect(ui_.action_reset_layout, &QAction::triggered, this, &Ecalmon::resetLayout); diff --git a/app/mon/eCALMon/src/MainWindow.ui b/app/mon/eCALMon/src/MainWindow.ui index 7d6854e9ff..cb85c0e04c 100644 --- a/app/mon/eCALMon/src/MainWindow.ui +++ b/app/mon/eCALMon/src/MainWindow.ui @@ -27,7 +27,7 @@ 0 0 1024 - 26 + 21 @@ -79,6 +79,8 @@ + + @@ -534,6 +536,11 @@ Plugin settings... + + + Npcap status... + + diff --git a/app/mon/eCALMon/src/Widgets/EcalmonTreeWidget/TopicWidget.cpp b/app/mon/eCALMon/src/Widgets/EcalmonTreeWidget/TopicWidget.cpp index fce0b1f476..9961c8cbc6 100644 --- a/app/mon/eCALMon/src/Widgets/EcalmonTreeWidget/TopicWidget.cpp +++ b/app/mon/eCALMon/src/Widgets/EcalmonTreeWidget/TopicWidget.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include @@ -386,4 +386,4 @@ void TopicWidget::setParseTimeEnabled(bool enabled) { visualisation_window->setParseTimeEnabled(parse_time_); } -} \ No newline at end of file +} diff --git a/app/mon/eCALMon/src/Widgets/Models/ProcessTreeItem.cpp b/app/mon/eCALMon/src/Widgets/Models/ProcessTreeItem.cpp index 4e03b2a585..c0c417203c 100644 --- a/app/mon/eCALMon/src/Widgets/Models/ProcessTreeItem.cpp +++ b/app/mon/eCALMon/src/Widgets/Models/ProcessTreeItem.cpp @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include "ItemDataRoles.h" diff --git a/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.cpp b/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.cpp new file mode 100644 index 0000000000..be2c9c6dbf --- /dev/null +++ b/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.cpp @@ -0,0 +1,35 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2019 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include "NpcapStatusDialog.h" + +#include +#include + +NpcapStatusDialog::NpcapStatusDialog(QWidget *parent) + : QDialog(parent) +{ + ui_.setupUi(this); + Udpcap::Initialize(); + ui_.status_textedit->setPlainText(QString::fromStdString(Udpcap::GetHumanReadibleErrorText())); + connect(ui_.button_box->button(QDialogButtonBox::StandardButton::Close), SIGNAL(clicked()), this, SLOT(close())); +} + +NpcapStatusDialog::~NpcapStatusDialog() +{} \ No newline at end of file diff --git a/ecal/core/src/simpleini_template.h b/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.h similarity index 62% rename from ecal/core/src/simpleini_template.h rename to app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.h index 1cc19e2a0b..e3aa580bc2 100644 --- a/ecal/core/src/simpleini_template.h +++ b/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.h @@ -17,17 +17,25 @@ * ========================= eCAL LICENSE ================================= */ -#include +#pragma once + +#include +#include + +#include "ui_NpcapStatusDialog.h" + #include -#include -#include +#include -/* -* This file provides two methods to access the Simple Inifile Setter/Getters on a template basis. -*/ -template -T GetValue(const CSimpleIni& ini_, const std::string& section_, const std::string& key_, const T& default_); -template -SI_Error SetValue(CSimpleIni& ini_, const std::string& section_, const std::string& key_, const T& value_, const std::string& comment_, bool force_overwrite_); +class NpcapStatusDialog : public QDialog +{ + Q_OBJECT + +public: + NpcapStatusDialog(QWidget *parent = Q_NULLPTR); + ~NpcapStatusDialog(); +private: + Ui::NpcapStatusDialog ui_; +}; \ No newline at end of file diff --git a/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.ui b/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.ui new file mode 100644 index 0000000000..171e0f8aae --- /dev/null +++ b/app/mon/eCALMon/src/Widgets/NpcapStatusDialog/NpcapStatusDialog.ui @@ -0,0 +1,49 @@ + + + NpcapStatusDialog + + + + 0 + 0 + 326 + 228 + + + + Npcap Status + + + false + + + false + + + + + + + Courier New + + + + true + + + + + + + QDialogButtonBox::Close + + + true + + + + + + + + diff --git a/cmake/helper_functions/ecal_install_functions.cmake b/cmake/helper_functions/ecal_install_functions.cmake index c4b1bb7756..bed3fae552 100644 --- a/cmake/helper_functions/ecal_install_functions.cmake +++ b/cmake/helper_functions/ecal_install_functions.cmake @@ -100,6 +100,8 @@ function(ecal_install_app TARGET_NAME) install(TARGETS ${TARGET_NAME} RUNTIME DESTINATION "${eCAL_install_app_dir}" COMPONENT app ) + set_property(INSTALL "${eCAL_install_app_dir}/$" PROPERTY CPACK_START_MENU_SHORTCUTS "${TARGET_NAME}") + endfunction() function(ecal_install_gtest TARGET_NAME) diff --git a/contrib/ecalproto/include/ecal/protobuf/ecal_proto_dyn.h b/contrib/ecalproto/include/ecal/protobuf/ecal_proto_dyn.h index 720794f7b1..b03656462a 100644 --- a/contrib/ecalproto/include/ecal/protobuf/ecal_proto_dyn.h +++ b/contrib/ecalproto/include/ecal/protobuf/ecal_proto_dyn.h @@ -29,6 +29,10 @@ #pragma warning(push) #pragma warning(disable: 4100 4146 4800) #endif +#if defined(__linux__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif #include #include #include @@ -38,6 +42,9 @@ #ifdef _MSC_VER #pragma warning(pop) #endif +#if defined(__linux__) +#pragma GCC diagnostic pop +#endif #include @@ -135,4 +142,4 @@ namespace protobuf google::protobuf::DynamicMessageFactory m_message_factory; }; } -} \ No newline at end of file +} diff --git a/contrib/ecaltime/linuxptp/src/clock.h b/contrib/ecaltime/linuxptp/src/clock.h index d477c9c5db..13e8debff9 100644 --- a/contrib/ecaltime/linuxptp/src/clock.h +++ b/contrib/ecaltime/linuxptp/src/clock.h @@ -23,8 +23,8 @@ #include #define CLOCKFD 3 -#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD) -#define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3)) +#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD) +#define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3)) namespace linuxptp{ /** diff --git a/contrib/ecaltime/linuxptp/src/ecal_time_linuxptp.h b/contrib/ecaltime/linuxptp/src/ecal_time_linuxptp.h index 98cb901543..acb12babd0 100644 --- a/contrib/ecaltime/linuxptp/src/ecal_time_linuxptp.h +++ b/contrib/ecaltime/linuxptp/src/ecal_time_linuxptp.h @@ -23,8 +23,8 @@ #include #define CLOCKFD 3 -#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD) -#define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3)) +#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD) +#define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3)) class Linuxptp { diff --git a/cpack/cpack_variables.cmake b/cpack/cpack_variables.cmake index 2fa5b745a1..86d164fd19 100644 --- a/cpack/cpack_variables.cmake +++ b/cpack/cpack_variables.cmake @@ -12,6 +12,8 @@ set(CPACK_PACKAGE_VERSION_PATCH ${GIT_REVISION_PATCH}) set(CPACK_PACKAGE_INSTALL_DIRECTORY ${PROJECT_NAME}) set(CPACK_PACKAGE_CONTACT "rex.schilasky@continental-corporation.com") set(CPACK_SOURCE_STRIP_FILES "") +set(CPACK_WIX_PATCH_FILE ${CMAKE_SOURCE_DIR}/cpack/ecal_path.xml) +#set(CPACK_WIX_EXTRA_SOURCES ${CMAKE_SOURCE_DIR}/cpack/settings.wxs) if(WIN32) set(CPACK_GENERATOR "WIX") diff --git a/cpack/ecal_path.xml b/cpack/ecal_path.xml new file mode 100644 index 0000000000..f2645fdb06 --- /dev/null +++ b/cpack/ecal_path.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ecal/core/include/ecal/ecal_publisher.h b/ecal/core/include/ecal/ecal_publisher.h index cab51501f8..f17c3a17f9 100644 --- a/ecal/core/include/ecal/ecal_publisher.h +++ b/ecal/core/include/ecal/ecal_publisher.h @@ -440,13 +440,13 @@ namespace eCAL bool ShareType(bool state_ = true) { if (!m_publisher) return(false); - return(eCAL_Pub_ShareType(m_publisher, static_cast(state_))); + return(eCAL_Pub_ShareType(m_publisher, static_cast(state_)) != 0); } bool ShareDescription(bool state_ = true) { if (!m_publisher) return(false); - return(eCAL_Pub_ShareDescription(m_publisher, static_cast(state_))); + return(eCAL_Pub_ShareDescription(m_publisher, static_cast(state_)) != 0); } bool SetQOS(const QOS::SWriterQOS& qos_) diff --git a/ecal/core/src/CMakeLists.txt b/ecal/core/src/CMakeLists.txt index 3a146a8024..d1cc83a17c 100644 --- a/ecal/core/src/CMakeLists.txt +++ b/ecal/core/src/CMakeLists.txt @@ -18,9 +18,10 @@ project(core VERSION ${eCAL_VERSION_STRING}) -find_package(Threads REQUIRED) -find_package(asio REQUIRED) -find_package(tclap REQUIRED) +find_package(Threads REQUIRED) +find_package(asio REQUIRED) +find_package(tclap REQUIRED) +find_package(simpleini REQUIRED) if (ECAL_JOIN_MULTICAST_TWICE) message(STATUS "eCAL ${PROJECT_NAME}: Enabling Specific Multicast Network Bug Workaround") @@ -122,7 +123,6 @@ set(ecal_cmn_cpp_src ecal_timer.cpp ecal_util.cpp ecalc.cpp - simpleini_template.cpp sys_usage.cpp ) @@ -212,8 +212,6 @@ set(ecal_cmn_header_src ecal_win_main.h ecal_win_socket.h getenvvar.h - simpleini.h - simpleini_template.h sys_usage.h topic2mcast.h ) @@ -323,7 +321,7 @@ ecal_add_ecal_shared_library(${PROJECT_NAME} ${ecal_readwrite_rtps_msg_header_src} ${ecal_readwrite_rtps_header_src} ${ecal_service_header_src} - + ${ecal_header_base} ${ecal_header_cimpl} ${ecal_header_msg} @@ -364,7 +362,7 @@ if(ECAL_NPCAP_SUPPORT) add_definitions(-DECAL_NPCAP_SUPPORT) target_link_libraries(${PROJECT_NAME} PRIVATE - udpcap + udpcap ) endif(ECAL_NPCAP_SUPPORT) @@ -391,14 +389,15 @@ target_link_libraries(${PROJECT_NAME} $<$:wsock32> asio::asio tclap::tclap + simpleini::simpleini eCAL::pb Threads::Threads eCAL::apps EcalUtils ) -set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ecal) -set_property(TARGET ${PROJECT_NAME_c} PROPERTY FOLDER ecal) +set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ecal/core) +set_property(TARGET ${PROJECT_NAME}_c PROPERTY FOLDER ecal/core) ecal_install_ecal_shared_library(${PROJECT_NAME}_c) ecal_install_ecal_shared_library(${PROJECT_NAME}) diff --git a/ecal/core/src/ecal_config.cpp b/ecal/core/src/ecal_config.cpp index a297b9ba3e..2da1fa8d8d 100644 --- a/ecal/core/src/ecal_config.cpp +++ b/ecal/core/src/ecal_config.cpp @@ -42,7 +42,7 @@ #include #endif -#include "simpleini.h" +#include namespace { diff --git a/ecal/core/src/ecal_event.cpp b/ecal/core/src/ecal_event.cpp index 5d7f67979a..eb7d086a01 100644 --- a/ecal/core/src/ecal_event.cpp +++ b/ecal/core/src/ecal_event.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef ECAL_OS_WINDOWS @@ -95,78 +96,188 @@ namespace eCAL #include #include #include +#include +#include +#include #include +#include +#include #include +#include +#include + +namespace +{ + struct named_event + { + pthread_mutex_t mtx; + pthread_cond_t cvar; + }; + typedef struct named_event named_event_t; + + named_event_t* named_event_create(const char* event_name_) + { + int fd = ::shm_open(event_name_, O_RDWR | O_CREAT | O_EXCL, 0666); + if (fd < 0) return nullptr; + + if(ftruncate(fd, sizeof(named_event_t)) == -1) + { + ::close(fd); + return nullptr; + } + + pthread_mutexattr_t shmtx; + pthread_mutexattr_init(&shmtx); + pthread_mutexattr_setpshared(&shmtx, PTHREAD_PROCESS_SHARED); + + pthread_condattr_t shattr; + pthread_condattr_init(&shattr); + pthread_condattr_setpshared(&shattr, PTHREAD_PROCESS_SHARED); + pthread_condattr_setclock(&shattr, CLOCK_MONOTONIC); + named_event_t* evt = static_cast(mmap(nullptr, sizeof(named_event_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + ::close(fd); + + pthread_mutex_init(&evt->mtx, &shmtx); + pthread_cond_init(&evt->cvar, &shattr); + + return evt; + } + + named_event_t* named_event_open(const char* event_name_) + { + int fd = ::shm_open(event_name_, O_RDWR, 0666); + if (fd < 0) return nullptr; + + named_event_t* evt = static_cast(mmap(nullptr, sizeof(named_event_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + ::close(fd); + + return evt; + } + + void named_event_set(named_event_t* evt_) + { + pthread_mutex_lock(&evt_->mtx); + pthread_cond_signal(&evt_->cvar); + pthread_mutex_unlock(&evt_->mtx); + } + + int named_event_wait(named_event_t *evt_) + { + pthread_mutex_lock(&evt_->mtx); + int ret = pthread_cond_wait(&evt_->cvar, &evt_->mtx); + pthread_mutex_unlock(&evt_->mtx); + return ret; + } + + int named_event_wait(named_event_t* evt_, struct timespec* ts_) + { + pthread_mutex_lock(&evt_->mtx); + int ret = pthread_cond_timedwait(&evt_->cvar, &evt_->mtx, ts_); + pthread_mutex_unlock(&evt_->mtx); + return ret; + } + + void named_event_close(named_event_t* evt_) + { + munmap(static_cast(evt_), sizeof(named_event_t)); + } +} namespace eCAL { - class CNamedEvent + class CEvent { public: - explicit CNamedEvent(const std::string& name_) : - m_sema(nullptr) + CEvent() : m_sigcount(0) { } + + void set() + { + std::unique_lock< std::mutex > lock(m_mutex); + ++m_sigcount; + m_condition.notify_one(); + } + + bool wait() + { + std::unique_lock< std::mutex > lock(m_mutex); + m_condition.wait(lock,[&]()->bool{ return m_sigcount>0; }); + --m_sigcount; + return true; + } + + template< typename R,typename P > + bool wait(const std::chrono::duration& timeout_) { - if(name_[0] != '/') + std::unique_lock< std::mutex > lock(m_mutex); + if (!m_condition.wait_for(lock, timeout_, [&]()->bool{ return m_sigcount>0; })) { - m_name = "/"; + return false; } - m_name += name_; + --m_sigcount; + return true; + } - int previous_umask = umask(000); // Set umask to nothing, so we can create files with all possible permission bits - m_sema = sem_open(m_name.c_str(), O_CREAT | O_EXCL, (S_IRUSR | S_IWUSR) | (S_IRGRP | S_IWGRP) | (S_IROTH | S_IWOTH), 0); - umask(previous_umask); // Reset umask to previous permissions + private: + unsigned int m_sigcount; + std::mutex m_mutex; + std::condition_variable m_condition; + }; - if(m_sema == SEM_FAILED) + class CNamedEvent + { + public: + explicit CNamedEvent(const std::string& name_) : + m_name(name_), + m_event(nullptr) + { + m_event = named_event_open(m_name.c_str()); + if(m_event == nullptr) { - m_sema = sem_open(m_name.c_str(), 0); - if(m_sema == SEM_FAILED) - { - m_sema = nullptr; - } + m_event = named_event_create(m_name.c_str()); } }; ~CNamedEvent() { - if(m_sema == nullptr) return; - sem_close(m_sema); + if(m_event == nullptr) return; + named_event_close(m_event); } - void signal() + void set() { - if(m_sema == nullptr) return; - sem_post(m_sema); + if(m_event == nullptr) return; + named_event_set(m_event); } - bool wait(const long timeout_) + bool wait() { - if(m_sema == nullptr) return(false); - if(timeout_ < 0) - { - return(sem_wait(m_sema) == 0); - } - else + if(m_event == nullptr) return false; + return(named_event_wait(m_event) == 0); + } + + bool wait(long timeout_) + { + if(m_event == nullptr) return false; + + struct timespec abstime; + clock_gettime(CLOCK_MONOTONIC, &abstime); + + abstime.tv_sec = abstime.tv_sec + timeout_ / 1000; + abstime.tv_nsec = abstime.tv_nsec + (timeout_ % 1000)*1000000; + while (abstime.tv_nsec >= 1000000000) { - struct timespec abstime; - struct timeval tv; - gettimeofday(&tv, NULL); - abstime.tv_sec = tv.tv_sec + timeout_ / 1000; - abstime.tv_nsec = tv.tv_usec*1000 + (timeout_ % 1000)*1000000; - if (abstime.tv_nsec >= 1000000000) - { - abstime.tv_nsec -= 1000000000; - abstime.tv_sec++; - } - return(sem_timedwait(m_sema, &abstime) == 0); + abstime.tv_nsec -= 1000000000; + abstime.tv_sec++; } + return(named_event_wait(m_event, &abstime) == 0); } private: - CNamedEvent(const CNamedEvent&); // prevent copy-construction - CNamedEvent& operator=(const CNamedEvent&); // prevent assignment + CNamedEvent(const CNamedEvent&); // prevent copy-construction + CNamedEvent& operator=(const CNamedEvent&); // prevent assignment - std::string m_name; - sem_t* m_sema; + std::string m_name; + named_event_t* m_event; }; bool gOpenEvent(EventHandleT* event_, const std::string& event_name_) @@ -174,52 +285,86 @@ namespace eCAL if(event_ == nullptr) return(false); EventHandleT event; - if(event_name_.size() == 0) + event.name = event_name_; + + if(event.name.empty()) { - std::stringstream counter; - static int cnt = 0; - counter << std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() + cnt++;; - event.name = "ecal_semaphore_" + counter.str(); + event.handle = new CEvent(); } else { - event.name = event_name_; + event.handle = new CNamedEvent(event.name); } - event.handle = new CNamedEvent(event.name); + if(event.handle != nullptr) { *event_ = event; - return(true); + return true; } - return(false); + return false; } bool gCloseEvent(const EventHandleT& event_) { - if(!event_.handle) return(false); - delete static_cast(event_.handle); - return(true); + if(!event_.handle) return false; + if(event_.name.empty()) + { + delete static_cast(event_.handle); + } + else + { + delete static_cast(event_.handle); + } + return true; } bool gSetEvent(const EventHandleT& event_) { - if(!event_.handle) return(false); - static_cast(event_.handle)->signal(); - return(true); + if(!event_.handle) return false; + if(event_.name.empty()) + { + static_cast(event_.handle)->set(); + } + else + { + static_cast(event_.handle)->set(); + } + return true; } bool gWaitForEvent(const EventHandleT& event_, const long timeout_) { - if(!event_.handle) return(false); - return(static_cast(event_.handle)->wait(timeout_)); + if(!event_.handle) return false; + if(event_.name.empty()) + { + if(timeout_ < 0) + { + return(static_cast(event_.handle)->wait()); + } + else + { + return(static_cast(event_.handle)->wait(std::chrono::milliseconds(timeout_))); + } + } + else + { + if(timeout_ < 0) + { + return(static_cast(event_.handle)->wait()); + } + else + { + return(static_cast(event_.handle)->wait(timeout_)); + } + } } bool gInvalidateEvent(EventHandleT* event_) { - if(!event_->handle) return(false); - if(event_->handle == nullptr) return(false); + if(!event_->handle) return false; + if(event_->handle == nullptr) return false; event_->handle = nullptr; - return(true); + return true; } bool gEventIsValid(const EventHandleT& event_) diff --git a/ecal/core/src/ecal_servgate.cpp b/ecal/core/src/ecal_servgate.cpp index 700f5fa2b5..0c92ae08dc 100644 --- a/ecal/core/src/ecal_servgate.cpp +++ b/ecal/core/src/ecal_servgate.cpp @@ -25,6 +25,7 @@ #include "ecal_def.h" #include "ecal_servgate.h" +#include "ecal_config_hlp.h" #include #include @@ -97,10 +98,11 @@ namespace eCAL auto service_reg_it = m_service_register_map.begin(); while (service_reg_it != m_service_register_map.end()) { - if (now - service_reg_it->second.second > std::chrono::seconds(5)) //TODO: This should be configurable in the ecal.ini + if (now - service_reg_it->second.second > std::chrono::milliseconds(eCALPAR(CMN, REGISTRATION_TO))) { #ifndef NDEBUG - printf("[CServGate] Removing service due to timeout: %s\n", service_reg_it->first.c_str()); + // log it + Logging::Log(log_level_debug1, "CServGate::GetServiceInfo - Removing service due to timeout"); #endif // !NDEBUG m_service_register_map.erase(service_reg_it++); } @@ -143,7 +145,8 @@ namespace eCAL { // Create new entry #ifndef NDEBUG - printf("[CServGate] Received new service registration: %s\n", key.c_str()); + // log it + Logging::Log(log_level_debug1, "CServGate::GetServiceInfo - Received new service registration"); #endif // !NDEBUG m_service_register_map.emplace(key, std::make_pair(service, registration_time)); diff --git a/ecal/core/src/ecal_thread.h b/ecal/core/src/ecal_thread.h index a6789db9a3..7c5fa3c65f 100644 --- a/ecal/core/src/ecal_thread.h +++ b/ecal/core/src/ecal_thread.h @@ -57,8 +57,8 @@ namespace eCAL int period; EventHandleT event; std::atomic is_running; - std::atomic is_started; - std::atomic do_stop; + std::atomic is_started; + std::atomic do_stop; std::function ext_caller; }; struct ThreadData m_tdata; diff --git a/ecal/core/src/pubsub/ecal_proto_dyn_json_sub.cpp b/ecal/core/src/pubsub/ecal_proto_dyn_json_sub.cpp index c3ada8a493..0b73d23e75 100644 --- a/ecal/core/src/pubsub/ecal_proto_dyn_json_sub.cpp +++ b/ecal/core/src/pubsub/ecal_proto_dyn_json_sub.cpp @@ -21,6 +21,7 @@ * dynamic protobuf message decoder **/ +#include #include #include @@ -28,16 +29,23 @@ #include #include -#ifdef _MSC_VER +#ifdef ECAL_OS_WINDOWS #pragma warning(push) #pragma warning(disable: 4100 4146 4800) #endif +#ifdef ECAL_OS_LINUX +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif #include #include #include -#ifdef _MSC_VER +#ifdef ECAL_OS_WINDOWS #pragma warning(pop) #endif +#ifdef ECAL_OS_LINUX +#pragma GCC diagnostic pop +#endif namespace eCAL { @@ -229,4 +237,4 @@ namespace eCAL return true; } } -} \ No newline at end of file +} diff --git a/ecal/core/src/readwrite/ecal_reader.cpp b/ecal/core/src/readwrite/ecal_reader.cpp index baf4dd4cc4..b82d72c7a1 100644 --- a/ecal/core/src/readwrite/ecal_reader.cpp +++ b/ecal/core/src/readwrite/ecal_reader.cpp @@ -159,7 +159,7 @@ namespace eCAL Logging::Log(log_level_debug1, m_topic_name + "::CDataReader::Destroy"); #endif - // stop transport layers + // stop transport layers StopDataLayers(); // reset receive callback @@ -479,7 +479,9 @@ namespace eCAL #endif // copy content to target string std::lock_guard lock(m_read_buf_sync); - buf_ = std::string(m_read_buf.data(), m_read_buf.size()); + buf_.clear(); + buf_.assign(m_read_buf.data(), m_read_buf.size()); + // apply time if(time_) *time_ = m_read_time; // return success @@ -575,7 +577,7 @@ namespace eCAL // push sample into read buffer std::lock_guard lock1(m_read_buf_sync); m_read_buf.clear(); - std::copy(payload_, payload_ + size_, std::back_inserter(m_read_buf)); + m_read_buf.assign(payload_, payload_ + size_); m_read_time = time_; // inform receive diff --git a/ecal/core/src/service/asio_server.h b/ecal/core/src/service/asio_server.h index 97ec2ef28b..780eb79c1b 100644 --- a/ecal/core/src/service/asio_server.h +++ b/ecal/core/src/service/asio_server.h @@ -21,6 +21,7 @@ #include #include +#include #include @@ -58,25 +59,38 @@ class CAsioSession { if (!ec) { - std::string response; if (request_callback_) { - std::string request(data_, bytes_transferred); - int ret = request_callback_(request, response); - // -1 means incomplete package - // read some more ... - if (ret < 0) + // collect request + //std::cout << "CAsioSession::handle_read read bytes " << bytes_transferred << std::endl; + request_ += std::string(data_, bytes_transferred); + // are there some more data on the socket ? + if (socket_.available()) { + // read some more bytes socket_.async_read_some(asio::buffer(data_, max_length), std::bind(&CAsioSession::handle_read, this, std::placeholders::_1, std::placeholders::_2)); } + // no more data + else + { + // execute service callback + //std::cout << "CAsioSession::handle_read final request size " << request_.size() << std::endl; + response_.clear(); + request_callback_(request_, response_); + request_.clear(); + //std::cout << "CAsioSession::handle_read server callback executed - reponse size " << response_.size() << std::endl; + + // write response back + asio::async_write(socket_, + asio::buffer(response_.data(), response_.length()), + bind(&CAsioSession::handle_write, this, + std::placeholders::_1, + std::placeholders::_2)); + } } - asio::async_write(socket_, - asio::buffer(response.data(), response.length()), - bind(&CAsioSession::handle_write, this, - std::placeholders::_1)); } else { @@ -84,10 +98,11 @@ class CAsioSession } } - void handle_write(const asio::error_code& ec) + void handle_write(const asio::error_code& ec, std::size_t /*bytes_transferred*/) { if (!ec) { + //std::cout << "CAsioSession::handle_write bytes sent " << bytes_transferred << std::endl; socket_.async_read_some(asio::buffer(data_, max_length), std::bind(&CAsioSession::handle_read, this, std::placeholders::_1, @@ -95,12 +110,15 @@ class CAsioSession } else { + std::cerr << "CAsioSession::handle_write: Failed write : " << ec.message() << std::endl; delete this; } } asio::ip::tcp::socket socket_; RequestCallbackT request_callback_; + std::string request_; + std::string response_; enum { max_length = 64 * 1024 }; char data_[max_length]; diff --git a/ecal/core/src/service/ecal_service_client_impl.cpp b/ecal/core/src/service/ecal_service_client_impl.cpp index 83f8b4d2ed..f404ab0e2e 100644 --- a/ecal/core/src/service/ecal_service_client_impl.cpp +++ b/ecal/core/src/service/ecal_service_client_impl.cpp @@ -136,6 +136,8 @@ namespace eCAL ) return false; + std::lock_guard req_lock(m_req_mtx); + // check for new server RefreshClientMap(); diff --git a/ecal/core/src/service/ecal_service_client_impl.h b/ecal/core/src/service/ecal_service_client_impl.h index 17e32d4d9d..2ef5b0ca87 100644 --- a/ecal/core/src/service/ecal_service_client_impl.h +++ b/ecal/core/src/service/ecal_service_client_impl.h @@ -27,6 +27,7 @@ #include "service/ecal_tcpclient.h" #include +#include namespace eCAL { @@ -67,6 +68,7 @@ namespace eCAL typedef std::map> ClientMapT; ClientMapT m_client_map; + std::mutex m_req_mtx; enum { max_length = 64 * 1024 }; char m_reply[max_length]; diff --git a/ecal/core/src/service/ecal_tcpclient.cpp b/ecal/core/src/service/ecal_tcpclient.cpp index c419dcd61c..6f775979c9 100644 --- a/ecal/core/src/service/ecal_tcpclient.cpp +++ b/ecal/core/src/service/ecal_tcpclient.cpp @@ -23,6 +23,8 @@ #include "ecal_tcpclient.h" +#include + namespace eCAL { ////////////////////////////////////////////////////////////////// @@ -86,12 +88,14 @@ namespace eCAL // send payload to server while (written != request_.size()) { - written += m_socket->write_some(asio::buffer(request_.c_str() + written, request_.size() - written)); + const size_t bytes_wrote = m_socket->write_some(asio::buffer(request_.c_str() + written, request_.size() - written)); + //std::cout << "CTcpClient::ExecuteRequest wrote request bytes " << bytes_wrote << std::endl; + written += bytes_wrote; } } - catch (std::exception& /*e*/) + catch (std::exception& e) { - //std::cerr << "CTcpClient::ExecuteRequest: Failed to send request: " << e.what() << "\n"; + std::cerr << "CTcpClient::ExecuteRequest: Failed to send request: " << e.what() << "\n"; m_connected = false; return 0; } @@ -104,16 +108,15 @@ namespace eCAL const size_t buffer_size(1024); char buffer[buffer_size]; const size_t bytes_read = m_socket->read_some(asio::buffer(buffer, buffer_size)); - + //std::cout << "CTcpClient::ExecuteRequest read response bytes " << bytes_read << " to " << response_.size() << std::endl; response_ += std::string(buffer, bytes_read); - } while (m_socket->available()); return response_.size(); } - catch (std::exception& /*e*/) + catch (std::exception& e) { - //std::cerr << "CTcpClient::ExecuteRequest: Failed to recieve response: " << e.what() << "\n"; + std::cerr << "CTcpClient::ExecuteRequest: Failed to recieve response: " << e.what() << std::endl; m_connected = false; return 0; } diff --git a/ecal/core/src/simpleini.h b/ecal/core/src/simpleini.h deleted file mode 100644 index a97a1f3dde..0000000000 --- a/ecal/core/src/simpleini.h +++ /dev/null @@ -1,3428 +0,0 @@ -/** @mainpage - - -
Library SimpleIni -
File SimpleIni.h -
Author Brodie Thiesfield [code at jellycan dot com] -
Source https://github.com/brofield/simpleini -
Version 4.17 -
- - Jump to the @link CSimpleIniTempl CSimpleIni @endlink interface documentation. - - @section intro INTRODUCTION - - This component allows an INI-style configuration file to be used on both - Windows and Linux/Unix. It is fast, simple and source code using this - component will compile unchanged on either OS. - - - @section features FEATURES - - - MIT Licence allows free use in all software (including GPL and commercial) - - multi-platform (Windows 95/98/ME/NT/2K/XP/2003, Windows CE, Linux, Unix) - - loading and saving of INI-style configuration files - - configuration files can have any newline format on all platforms - - liberal acceptance of file format - - key/values with no section - - removal of whitespace around sections, keys and values - - support for multi-line values (values with embedded newline characters) - - optional support for multiple keys with the same name - - optional case-insensitive sections and keys (for ASCII characters only) - - saves files with sections and keys in the same order as they were loaded - - preserves comments on the file, section and keys where possible. - - supports both char or wchar_t programming interfaces - - supports both MBCS (system locale) and UTF-8 file encodings - - system locale does not need to be UTF-8 on Linux/Unix to load UTF-8 file - - support for non-ASCII characters in section, keys, values and comments - - support for non-standard character types or file encodings - via user-written converter classes - - support for adding/modifying values programmatically - - compiles cleanly in the following compilers: - - Windows/VC6 (warning level 3) - - Windows/VC.NET 2003 (warning level 4) - - Windows/VC 2005 (warning level 4) - - Linux/gcc (-Wall) - - - @section usage USAGE SUMMARY - - -# Define the appropriate symbol for the converter you wish to use and - include the SimpleIni.h header file. If no specific converter is defined - then the default converter is used. The default conversion mode uses - SI_CONVERT_WIN32 on Windows and SI_CONVERT_GENERIC on all other - platforms. If you are using ICU then SI_CONVERT_ICU is supported on all - platforms. - -# Declare an instance the appropriate class. Note that the following - definitions are just shortcuts for commonly used types. Other types - (PRUnichar, unsigned short, unsigned char) are also possible. - -
Interface Case-sensitive Load UTF-8 Load MBCS Typedef -
SI_CONVERT_GENERIC -
char No Yes Yes #1 CSimpleIniA -
char Yes Yes Yes CSimpleIniCaseA -
wchar_t No Yes Yes CSimpleIniW -
wchar_t Yes Yes Yes CSimpleIniCaseW -
SI_CONVERT_WIN32 -
char No No #2 Yes CSimpleIniA -
char Yes Yes Yes CSimpleIniCaseA -
wchar_t No Yes Yes CSimpleIniW -
wchar_t Yes Yes Yes CSimpleIniCaseW -
SI_CONVERT_ICU -
char No Yes Yes CSimpleIniA -
char Yes Yes Yes CSimpleIniCaseA -
UChar No Yes Yes CSimpleIniW -
UChar Yes Yes Yes CSimpleIniCaseW -
- #1 On Windows you are better to use CSimpleIniA with SI_CONVERT_WIN32.
- #2 Only affects Windows. On Windows this uses MBCS functions and - so may fold case incorrectly leading to uncertain results. - -# Call LoadData() or LoadFile() to load and parse the INI configuration file - -# Access and modify the data of the file using the following functions - -
GetAllSections Return all section names -
GetAllKeys Return all key names within a section -
GetAllValues Return all values within a section & key -
GetSection Return all key names and values in a section -
GetSectionSize Return the number of keys in a section -
GetValue Return a value for a section & key -
SetValue Add or update a value for a section & key -
Delete Remove a section, or a key from a section -
- -# Call Save() or SaveFile() to save the INI configuration data - - @section iostreams IO STREAMS - - SimpleIni supports reading from and writing to STL IO streams. Enable this - by defining SI_SUPPORT_IOSTREAMS before including the SimpleIni.h header - file. Ensure that if the streams are backed by a file (e.g. ifstream or - ofstream) then the flag ios_base::binary has been used when the file was - opened. - - @section multiline MULTI-LINE VALUES - - Values that span multiple lines are created using the following format. - -
-        key = <<
-
-    Note the following:
-    - The text used for ENDTAG can be anything and is used to find
-      where the multi-line text ends.
-    - The newline after ENDTAG in the start tag, and the newline
-      before ENDTAG in the end tag is not included in the data value.
-    - The ending tag must be on it's own line with no whitespace before
-      or after it.
-    - The multi-line value is modified at load so that each line in the value
-      is delimited by a single '\\n' character on all platforms. At save time
-      it will be converted into the newline format used by the current
-      platform.
-
-    @section comments COMMENTS
-
-    Comments are preserved in the file within the following restrictions:
-    - Every file may have a single "file comment". It must start with the
-      first character in the file, and will end with the first non-comment
-      line in the file.
-    - Every section may have a single "section comment". It will start
-      with the first comment line following the file comment, or the last
-      data entry. It ends at the beginning of the section.
-    - Every key may have a single "key comment". This comment will start
-      with the first comment line following the section start, or the file
-      comment if there is no section name.
-    - Comments are set at the time that the file, section or key is first
-      created. The only way to modify a comment on a section or a key is to
-      delete that entry and recreate it with the new comment. There is no
-      way to change the file comment.
-
-    @section save SAVE ORDER
-
-    The sections and keys are written out in the same order as they were
-    read in from the file. Sections and keys added to the data after the
-    file has been loaded will be added to the end of the file when it is
-    written. There is no way to specify the location of a section or key
-    other than in first-created, first-saved order.
-
-    @section notes NOTES
-
-    - To load UTF-8 data on Windows 95, you need to use Microsoft Layer for
-      Unicode, or SI_CONVERT_GENERIC, or SI_CONVERT_ICU.
-    - When using SI_CONVERT_GENERIC, convert_utf.c must be compiled and linked.
-    - When using SI_CONVERT_ICU, ICU header files must be on the include
-      path and icuuc.lib must be linked in.
-    - To load a UTF-8 file on Windows AND expose it with SI_CHAR == char,
-      you should use SI_CONVERT_GENERIC.
-    - The collation (sorting) order used for sections and keys returned from
-      iterators is NOT DEFINED. If collation order of the text is important
-      then it should be done yourself by either supplying a replacement
-      SI_STRLESS class, or by sorting the strings external to this library.
-    - Usage of the  header on Windows can be disabled by defining
-      SI_NO_MBCS. This is defined automatically on Windows CE platforms.
-    - Not thread-safe so manage your own locking
-
-    @section contrib CONTRIBUTIONS
-    
-    - 2010/05/03: Tobias Gehrig: added GetDoubleValue()
-
-    @section licence MIT LICENCE
-
-    The licence text below is the boilerplate "MIT Licence" used from:
-    http://www.opensource.org/licenses/mit-license.php
-
-    Copyright (c) 2006-2012, Brodie Thiesfield
-
-    Permission is hereby granted, free of charge, to any person obtaining a copy
-    of this software and associated documentation files (the "Software"), to deal
-    in the Software without restriction, including without limitation the rights
-    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-    copies of the Software, and to permit persons to whom the Software is furnished
-    to do so, subject to the following conditions:
-
-    The above copyright notice and this permission notice shall be included in
-    all copies or substantial portions of the Software.
-
-    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-    FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-    COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#ifndef INCLUDED_SimpleIni_h
-#define INCLUDED_SimpleIni_h
-
-#if defined(_MSC_VER) && (_MSC_VER >= 1020)
-# pragma once
-#endif
-
-// Disable these warnings in MSVC:
-//  4127 "conditional expression is constant" as the conversion classes trigger
-//  it with the statement if (sizeof(SI_CHAR) == sizeof(char)). This test will
-//  be optimized away in a release build.
-//  4503 'insert' : decorated name length exceeded, name was truncated
-//  4702 "unreachable code" as the MS STL header causes it in release mode.
-//  Again, the code causing the warning will be cleaned up by the compiler.
-//  4786 "identifier truncated to 256 characters" as this is thrown hundreds
-//  of times VC6 as soon as STL is used.
-#ifdef _MSC_VER
-# pragma warning (push)
-# pragma warning (disable: 4127 4503 4702 4786)
-#endif
-
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-
-#ifdef SI_SUPPORT_IOSTREAMS
-# include 
-#endif // SI_SUPPORT_IOSTREAMS
-
-#ifdef _DEBUG
-# ifndef assert
-#  include 
-# endif
-# define SI_ASSERT(x)   assert(x)
-#else
-# define SI_ASSERT(x)
-#endif
-
-enum SI_Error {
-    SI_OK       =  0,   //!< No error
-    SI_UPDATED  =  1,   //!< An existing value was updated
-    SI_INSERTED =  2,   //!< A new value was inserted
-
-    // note: test for any error with (retval < 0)
-    SI_FAIL     = -1,   //!< Generic failure
-    SI_NOMEM    = -2,   //!< Out of memory error
-    SI_FILE     = -3    //!< File error (see errno for detail error)
-};
-
-#define SI_UTF8_SIGNATURE     "\xEF\xBB\xBF"
-
-#ifdef _WIN32
-# define SI_NEWLINE_A   "\r\n"
-# define SI_NEWLINE_W   L"\r\n"
-#else // !_WIN32
-# define SI_NEWLINE_A   "\n"
-# define SI_NEWLINE_W   L"\n"
-#endif // _WIN32
-
-#if defined(SI_CONVERT_ICU)
-# include 
-#endif
-
-#if defined(_WIN32)
-# define SI_HAS_WIDE_FILE
-# define SI_WCHAR_T     wchar_t
-#elif defined(SI_CONVERT_ICU)
-# define SI_HAS_WIDE_FILE
-# define SI_WCHAR_T     UChar
-#endif
-
-
-// ---------------------------------------------------------------------------
-//                              MAIN TEMPLATE CLASS
-// ---------------------------------------------------------------------------
-
-/** Simple INI file reader.
-
-    This can be instantiated with the choice of unicode or native characterset,
-    and case sensitive or insensitive comparisons of section and key names.
-    The supported combinations are pre-defined with the following typedefs:
-
-    
-        
Interface Case-sensitive Typedef -
char No CSimpleIniA -
char Yes CSimpleIniCaseA -
wchar_t No CSimpleIniW -
wchar_t Yes CSimpleIniCaseW -
- - Note that using other types for the SI_CHAR is supported. For instance, - unsigned char, unsigned short, etc. Note that where the alternative type - is a different size to char/wchar_t you may need to supply new helper - classes for SI_STRLESS and SI_CONVERTER. - */ -template -class CSimpleIniTempl -{ -public: - typedef SI_CHAR SI_CHAR_T; - - /** key entry */ - struct Entry { - const SI_CHAR * pItem; - const SI_CHAR * pComment; - int nOrder; - - Entry(const SI_CHAR * a_pszItem = NULL, int a_nOrder = 0) - : pItem(a_pszItem) - , pComment(NULL) - , nOrder(a_nOrder) - { } - Entry(const SI_CHAR * a_pszItem, const SI_CHAR * a_pszComment, int a_nOrder) - : pItem(a_pszItem) - , pComment(a_pszComment) - , nOrder(a_nOrder) - { } - Entry(const Entry & rhs) { operator=(rhs); } - Entry & operator=(const Entry & rhs) { - pItem = rhs.pItem; - pComment = rhs.pComment; - nOrder = rhs.nOrder; - return *this; - } - -#if defined(_MSC_VER) && _MSC_VER <= 1200 - /** STL of VC6 doesn't allow me to specify my own comparator for list::sort() */ - bool operator<(const Entry & rhs) const { return LoadOrder()(*this, rhs); } - bool operator>(const Entry & rhs) const { return LoadOrder()(rhs, *this); } -#endif - - /** Strict less ordering by name of key only */ - struct KeyOrder : std::binary_function { - bool operator()(const Entry & lhs, const Entry & rhs) const { - static const SI_STRLESS isLess = SI_STRLESS(); - return isLess(lhs.pItem, rhs.pItem); - } - }; - - /** Strict less ordering by order, and then name of key */ - struct LoadOrder : std::binary_function { - bool operator()(const Entry & lhs, const Entry & rhs) const { - if (lhs.nOrder != rhs.nOrder) { - return lhs.nOrder < rhs.nOrder; - } - return KeyOrder()(lhs.pItem, rhs.pItem); - } - }; - }; - - /** map keys to values */ - typedef std::multimap TKeyVal; - - /** map sections to key/value map */ - typedef std::map TSection; - - /** set of dependent string pointers. Note that these pointers are - dependent on memory owned by CSimpleIni. - */ - typedef std::list TNamesDepend; - - /** interface definition for the OutputWriter object to pass to Save() - in order to output the INI file data. - */ - class OutputWriter { - public: - OutputWriter() { } - virtual ~OutputWriter() { } - virtual void Write(const char * a_pBuf) = 0; - private: - OutputWriter(const OutputWriter &); // disable - OutputWriter & operator=(const OutputWriter &); // disable - }; - - /** OutputWriter class to write the INI data to a file */ - class FileWriter : public OutputWriter { - FILE * m_file; - public: - FileWriter(FILE * a_file) : m_file(a_file) { } - void Write(const char * a_pBuf) { - fputs(a_pBuf, m_file); - } - private: - FileWriter(const FileWriter &); // disable - FileWriter & operator=(const FileWriter &); // disable - }; - - /** OutputWriter class to write the INI data to a string */ - class StringWriter : public OutputWriter { - std::string & m_string; - public: - StringWriter(std::string & a_string) : m_string(a_string) { } - void Write(const char * a_pBuf) { - m_string.append(a_pBuf); - } - private: - StringWriter(const StringWriter &); // disable - StringWriter & operator=(const StringWriter &); // disable - }; - -#ifdef SI_SUPPORT_IOSTREAMS - /** OutputWriter class to write the INI data to an ostream */ - class StreamWriter : public OutputWriter { - std::ostream & m_ostream; - public: - StreamWriter(std::ostream & a_ostream) : m_ostream(a_ostream) { } - void Write(const char * a_pBuf) { - m_ostream << a_pBuf; - } - private: - StreamWriter(const StreamWriter &); // disable - StreamWriter & operator=(const StreamWriter &); // disable - }; -#endif // SI_SUPPORT_IOSTREAMS - - /** Characterset conversion utility class to convert strings to the - same format as is used for the storage. - */ - class Converter : private SI_CONVERTER { - public: - Converter(bool a_bStoreIsUtf8) : SI_CONVERTER(a_bStoreIsUtf8) { - m_scratch.resize(1024); - } - Converter(const Converter & rhs) { operator=(rhs); } - Converter & operator=(const Converter & rhs) { - m_scratch = rhs.m_scratch; - return *this; - } - bool ConvertToStore(const SI_CHAR * a_pszString) { - size_t uLen = SI_CONVERTER::SizeToStore(a_pszString); - if (uLen == (size_t)(-1)) { - return false; - } - while (uLen > m_scratch.size()) { - m_scratch.resize(m_scratch.size() * 2); - } - return SI_CONVERTER::ConvertToStore( - a_pszString, - const_cast(m_scratch.data()), - m_scratch.size()); - } - const char * Data() { return m_scratch.data(); } - private: - std::string m_scratch; - }; - -public: - /*-----------------------------------------------------------------------*/ - - /** Default constructor. - - @param a_bIsUtf8 See the method SetUnicode() for details. - @param a_bMultiKey See the method SetMultiKey() for details. - @param a_bMultiLine See the method SetMultiLine() for details. - */ - CSimpleIniTempl( - bool a_bIsUtf8 = false, - bool a_bMultiKey = false, - bool a_bMultiLine = false - ); - - /** Destructor */ - ~CSimpleIniTempl(); - - /** Deallocate all memory stored by this object */ - void Reset(); - - /** Has any data been loaded */ - bool IsEmpty() const { return m_data.empty(); } - - /*-----------------------------------------------------------------------*/ - /** @{ @name Settings */ - - /** Set the storage format of the INI data. This affects both the loading - and saving of the INI data using all of the Load/Save API functions. - This value cannot be changed after any INI data has been loaded. - - If the file is not set to Unicode (UTF-8), then the data encoding is - assumed to be the OS native encoding. This encoding is the system - locale on Linux/Unix and the legacy MBCS encoding on Windows NT/2K/XP. - If the storage format is set to Unicode then the file will be loaded - as UTF-8 encoded data regardless of the native file encoding. If - SI_CHAR == char then all of the char* parameters take and return UTF-8 - encoded data regardless of the system locale. - - \param a_bIsUtf8 Assume UTF-8 encoding for the source? - */ - void SetUnicode(bool a_bIsUtf8 = true) { - if (!m_pData) m_bStoreIsUtf8 = a_bIsUtf8; - } - - /** Get the storage format of the INI data. */ - bool IsUnicode() const { return m_bStoreIsUtf8; } - - /** Should multiple identical keys be permitted in the file. If set to false - then the last value encountered will be used as the value of the key. - If set to true, then all values will be available to be queried. For - example, with the following input: - -
-        [section]
-        test=value1
-        test=value2
-        
- - Then with SetMultiKey(true), both of the values "value1" and "value2" - will be returned for the key test. If SetMultiKey(false) is used, then - the value for "test" will only be "value2". This value may be changed - at any time. - - \param a_bAllowMultiKey Allow multi-keys in the source? - */ - void SetMultiKey(bool a_bAllowMultiKey = true) { - m_bAllowMultiKey = a_bAllowMultiKey; - } - - /** Get the storage format of the INI data. */ - bool IsMultiKey() const { return m_bAllowMultiKey; } - - /** Should data values be permitted to span multiple lines in the file. If - set to false then the multi-line construct << - SI_CHAR FORMAT - char same format as when loaded (MBCS or UTF-8) - wchar_t UTF-8 - other UTF-8 - - - Note that comments from the original data is preserved as per the - documentation on comments. The order of the sections and values - from the original file will be preserved. - - Any data prepended or appended to the output device must use the the - same format (MBCS or UTF-8). You may use the GetConverter() method to - convert text to the correct format regardless of the output format - being used by SimpleIni. - - To add a BOM to UTF-8 data, write it out manually at the very beginning - like is done in SaveFile when a_bUseBOM is true. - - @param a_oOutput Output writer to write the data to. - - @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in - UTF-8 format. If it is not UTF-8 then this value is - ignored. Do not set this to true if anything has - already been written to the OutputWriter. - - @return SI_Error See error definitions - */ - SI_Error Save( - OutputWriter & a_oOutput, - bool a_bAddSignature = false - ) const; - -#ifdef SI_SUPPORT_IOSTREAMS - /** Save the INI data to an ostream. See Save() for details. - - @param a_ostream String to have the INI data appended to. - - @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in - UTF-8 format. If it is not UTF-8 then this value is - ignored. Do not set this to true if anything has - already been written to the stream. - - @return SI_Error See error definitions - */ - SI_Error Save( - std::ostream & a_ostream, - bool a_bAddSignature = false - ) const - { - StreamWriter writer(a_ostream); - return Save(writer, a_bAddSignature); - } -#endif // SI_SUPPORT_IOSTREAMS - - /** Append the INI data to a string. See Save() for details. - - @param a_sBuffer String to have the INI data appended to. - - @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in - UTF-8 format. If it is not UTF-8 then this value is - ignored. Do not set this to true if anything has - already been written to the string. - - @return SI_Error See error definitions - */ - SI_Error Save( - std::string & a_sBuffer, - bool a_bAddSignature = false - ) const - { - StringWriter writer(a_sBuffer); - return Save(writer, a_bAddSignature); - } - - /*-----------------------------------------------------------------------*/ - /** @} - @{ @name Accessing INI Data */ - - /** Retrieve all section names. The list is returned as an STL vector of - names and can be iterated or searched as necessary. Note that the - sort order of the returned strings is NOT DEFINED. You can sort - the names into the load order if desired. Search this file for ".sort" - for an example. - - NOTE! This structure contains only pointers to strings. The actual - string data is stored in memory owned by CSimpleIni. Ensure that the - CSimpleIni object is not destroyed or Reset() while these pointers - are in use! - - @param a_names Vector that will receive all of the section - names. See note above! - */ - void GetAllSections( - TNamesDepend & a_names - ) const; - - /** Retrieve all unique key names in a section. The sort order of the - returned strings is NOT DEFINED. You can sort the names into the load - order if desired. Search this file for ".sort" for an example. Only - unique key names are returned. - - NOTE! This structure contains only pointers to strings. The actual - string data is stored in memory owned by CSimpleIni. Ensure that the - CSimpleIni object is not destroyed or Reset() while these strings - are in use! - - @param a_pSection Section to request data for - @param a_names List that will receive all of the key - names. See note above! - - @return true Section was found. - @return false Matching section was not found. - */ - bool GetAllKeys( - const SI_CHAR * a_pSection, - TNamesDepend & a_names - ) const; - - /** Retrieve all values for a specific key. This method can be used when - multiple keys are both enabled and disabled. Note that the sort order - of the returned strings is NOT DEFINED. You can sort the names into - the load order if desired. Search this file for ".sort" for an example. - - NOTE! The returned values are pointers to string data stored in memory - owned by CSimpleIni. Ensure that the CSimpleIni object is not destroyed - or Reset while you are using this pointer! - - @param a_pSection Section to search - @param a_pKey Key to search for - @param a_values List to return if the key is not found - - @return true Key was found. - @return false Matching section/key was not found. - */ - bool GetAllValues( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - TNamesDepend & a_values - ) const; - - /** Query the number of keys in a specific section. Note that if multiple - keys are enabled, then this value may be different to the number of - keys returned by GetAllKeys. - - @param a_pSection Section to request data for - - @return -1 Section does not exist in the file - @return >=0 Number of keys in the section - */ - int GetSectionSize( - const SI_CHAR * a_pSection - ) const; - - /** Retrieve all key and value pairs for a section. The data is returned - as a pointer to an STL map and can be iterated or searched as - desired. Note that multiple entries for the same key may exist when - multiple keys have been enabled. - - NOTE! This structure contains only pointers to strings. The actual - string data is stored in memory owned by CSimpleIni. Ensure that the - CSimpleIni object is not destroyed or Reset() while these strings - are in use! - - @param a_pSection Name of the section to return - @return boolean Was a section matching the supplied - name found. - */ - const TKeyVal * GetSection( - const SI_CHAR * a_pSection - ) const; - - /** Retrieve the value for a specific key. If multiple keys are enabled - (see SetMultiKey) then only the first value associated with that key - will be returned, see GetAllValues for getting all values with multikey. - - NOTE! The returned value is a pointer to string data stored in memory - owned by CSimpleIni. Ensure that the CSimpleIni object is not destroyed - or Reset while you are using this pointer! - - @param a_pSection Section to search - @param a_pKey Key to search for - @param a_pDefault Value to return if the key is not found - @param a_pHasMultiple Optionally receive notification of if there are - multiple entries for this key. - - @return a_pDefault Key was not found in the section - @return other Value of the key - */ - const SI_CHAR * GetValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pDefault = NULL, - bool * a_pHasMultiple = NULL - ) const; - - /** Retrieve a numeric value for a specific key. If multiple keys are enabled - (see SetMultiKey) then only the first value associated with that key - will be returned, see GetAllValues for getting all values with multikey. - - @param a_pSection Section to search - @param a_pKey Key to search for - @param a_nDefault Value to return if the key is not found - @param a_pHasMultiple Optionally receive notification of if there are - multiple entries for this key. - - @return a_nDefault Key was not found in the section - @return other Value of the key - */ - long GetLongValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - long a_nDefault = 0, - bool * a_pHasMultiple = NULL - ) const; - - /** Retrieve a numeric value for a specific key. If multiple keys are enabled - (see SetMultiKey) then only the first value associated with that key - will be returned, see GetAllValues for getting all values with multikey. - - @param a_pSection Section to search - @param a_pKey Key to search for - @param a_nDefault Value to return if the key is not found - @param a_pHasMultiple Optionally receive notification of if there are - multiple entries for this key. - - @return a_nDefault Key was not found in the section - @return other Value of the key - */ - double GetDoubleValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - double a_nDefault = 0, - bool * a_pHasMultiple = NULL - ) const; - - /** Retrieve a boolean value for a specific key. If multiple keys are enabled - (see SetMultiKey) then only the first value associated with that key - will be returned, see GetAllValues for getting all values with multikey. - - Strings starting with "t", "y", "on" or "1" are returned as logically true. - Strings starting with "f", "n", "of" or "0" are returned as logically false. - For all other values the default is returned. Character comparisons are - case-insensitive. - - @param a_pSection Section to search - @param a_pKey Key to search for - @param a_bDefault Value to return if the key is not found - @param a_pHasMultiple Optionally receive notification of if there are - multiple entries for this key. - - @return a_nDefault Key was not found in the section - @return other Value of the key - */ - bool GetBoolValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - bool a_bDefault = false, - bool * a_pHasMultiple = NULL - ) const; - - /** Add or update a section or value. This will always insert - when multiple keys are enabled. - - @param a_pSection Section to add or update - @param a_pKey Key to add or update. Set to NULL to - create an empty section. - @param a_pValue Value to set. Set to NULL to create an - empty section. - @param a_pComment Comment to be associated with the section or the - key. If a_pKey is NULL then it will be associated - with the section, otherwise the key. Note that a - comment may be set ONLY when the section or key is - first created (i.e. when this function returns the - value SI_INSERTED). If you wish to create a section - with a comment then you need to create the section - separately to the key. The comment string must be - in full comment form already (have a comment - character starting every line). - @param a_bForceReplace Should all existing values in a multi-key INI - file be replaced with this entry. This option has - no effect if not using multi-key files. The - difference between Delete/SetValue and SetValue - with a_bForceReplace = true, is that the load - order and comment will be preserved this way. - - @return SI_Error See error definitions - @return SI_UPDATED Value was updated - @return SI_INSERTED Value was inserted - */ - SI_Error SetValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pValue, - const SI_CHAR * a_pComment = NULL, - bool a_bForceReplace = false - ) - { - return AddEntry(a_pSection, a_pKey, a_pValue, a_pComment, a_bForceReplace, true); - } - - /** Add or update a numeric value. This will always insert - when multiple keys are enabled. - - @param a_pSection Section to add or update - @param a_pKey Key to add or update. - @param a_nValue Value to set. - @param a_pComment Comment to be associated with the key. See the - notes on SetValue() for comments. - @param a_bUseHex By default the value will be written to the file - in decimal format. Set this to true to write it - as hexadecimal. - @param a_bForceReplace Should all existing values in a multi-key INI - file be replaced with this entry. This option has - no effect if not using multi-key files. The - difference between Delete/SetLongValue and - SetLongValue with a_bForceReplace = true, is that - the load order and comment will be preserved this - way. - - @return SI_Error See error definitions - @return SI_UPDATED Value was updated - @return SI_INSERTED Value was inserted - */ - SI_Error SetLongValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - long a_nValue, - const SI_CHAR * a_pComment = NULL, - bool a_bUseHex = false, - bool a_bForceReplace = false - ); - - /** Add or update a double value. This will always insert - when multiple keys are enabled. - - @param a_pSection Section to add or update - @param a_pKey Key to add or update. - @param a_nValue Value to set. - @param a_pComment Comment to be associated with the key. See the - notes on SetValue() for comments. - @param a_bForceReplace Should all existing values in a multi-key INI - file be replaced with this entry. This option has - no effect if not using multi-key files. The - difference between Delete/SetDoubleValue and - SetDoubleValue with a_bForceReplace = true, is that - the load order and comment will be preserved this - way. - - @return SI_Error See error definitions - @return SI_UPDATED Value was updated - @return SI_INSERTED Value was inserted - */ - SI_Error SetDoubleValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - double a_nValue, - const SI_CHAR * a_pComment = NULL, - bool a_bForceReplace = false - ); - - /** Add or update a boolean value. This will always insert - when multiple keys are enabled. - - @param a_pSection Section to add or update - @param a_pKey Key to add or update. - @param a_bValue Value to set. - @param a_pComment Comment to be associated with the key. See the - notes on SetValue() for comments. - @param a_bForceReplace Should all existing values in a multi-key INI - file be replaced with this entry. This option has - no effect if not using multi-key files. The - difference between Delete/SetBoolValue and - SetBoolValue with a_bForceReplace = true, is that - the load order and comment will be preserved this - way. - - @return SI_Error See error definitions - @return SI_UPDATED Value was updated - @return SI_INSERTED Value was inserted - */ - SI_Error SetBoolValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - bool a_bValue, - const SI_CHAR * a_pComment = NULL, - bool a_bForceReplace = false - ); - - /** Delete an entire section, or a key from a section. Note that the - data returned by GetSection is invalid and must not be used after - anything has been deleted from that section using this method. - Note when multiple keys is enabled, this will delete all keys with - that name; to selectively delete individual key/values, use - DeleteValue. - - @param a_pSection Section to delete key from, or if - a_pKey is NULL, the section to remove. - @param a_pKey Key to remove from the section. Set to - NULL to remove the entire section. - @param a_bRemoveEmpty If the section is empty after this key has - been deleted, should the empty section be - removed? - - @return true Key or section was deleted. - @return false Key or section was not found. - */ - bool Delete( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - bool a_bRemoveEmpty = false - ); - - /** Delete an entire section, or a key from a section. If value is - provided, only remove keys with the value. Note that the data - returned by GetSection is invalid and must not be used after - anything has been deleted from that section using this method. - Note when multiple keys is enabled, all keys with the value will - be deleted. - - @param a_pSection Section to delete key from, or if - a_pKey is NULL, the section to remove. - @param a_pKey Key to remove from the section. Set to - NULL to remove the entire section. - @param a_pValue Value of key to remove from the section. - Set to NULL to remove all keys. - @param a_bRemoveEmpty If the section is empty after this key has - been deleted, should the empty section be - removed? - - @return true Key/value or section was deleted. - @return false Key/value or section was not found. - */ - bool DeleteValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pValue, - bool a_bRemoveEmpty = false - ); - - /*-----------------------------------------------------------------------*/ - /** @} - @{ @name Converter */ - - /** Return a conversion object to convert text to the same encoding - as is used by the Save(), SaveFile() and SaveString() functions. - Use this to prepare the strings that you wish to append or prepend - to the output INI data. - */ - Converter GetConverter() const { - return Converter(m_bStoreIsUtf8); - } - - /*-----------------------------------------------------------------------*/ - /** @} */ - -private: - // copying is not permitted - CSimpleIniTempl(const CSimpleIniTempl &); // disabled - CSimpleIniTempl & operator=(const CSimpleIniTempl &); // disabled - - /** Parse the data looking for a file comment and store it if found. - */ - SI_Error FindFileComment( - SI_CHAR *& a_pData, - bool a_bCopyStrings - ); - - /** Parse the data looking for the next valid entry. The memory pointed to - by a_pData is modified by inserting NULL characters. The pointer is - updated to the current location in the block of text. - */ - bool FindEntry( - SI_CHAR *& a_pData, - const SI_CHAR *& a_pSection, - const SI_CHAR *& a_pKey, - const SI_CHAR *& a_pVal, - const SI_CHAR *& a_pComment - ) const; - - /** Add the section/key/value to our data. - - @param a_pSection Section name. Sections will be created if they - don't already exist. - @param a_pKey Key name. May be NULL to create an empty section. - Existing entries will be updated. New entries will - be created. - @param a_pValue Value for the key. - @param a_pComment Comment to be associated with the section or the - key. If a_pKey is NULL then it will be associated - with the section, otherwise the key. This must be - a string in full comment form already (have a - comment character starting every line). - @param a_bForceReplace Should all existing values in a multi-key INI - file be replaced with this entry. This option has - no effect if not using multi-key files. The - difference between Delete/AddEntry and AddEntry - with a_bForceReplace = true, is that the load - order and comment will be preserved this way. - @param a_bCopyStrings Should copies of the strings be made or not. - If false then the pointers will be used as is. - */ - SI_Error AddEntry( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pValue, - const SI_CHAR * a_pComment, - bool a_bForceReplace, - bool a_bCopyStrings - ); - - /** Is the supplied character a whitespace character? */ - inline bool IsSpace(SI_CHAR ch) const { - return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); - } - - /** Does the supplied character start a comment line? */ - inline bool IsComment(SI_CHAR ch) const { - return (ch == ';' || ch == '#' || ch == '\0'); - } - - - /** Skip over a newline character (or characters) for either DOS or UNIX */ - inline void SkipNewLine(SI_CHAR *& a_pData) const { - a_pData += (*a_pData == '\r' && *(a_pData+1) == '\n') ? 2 : 1; - } - - /** Make a copy of the supplied string, replacing the original pointer */ - SI_Error CopyString(const SI_CHAR *& a_pString); - - /** Delete a string from the copied strings buffer if necessary */ - void DeleteString(const SI_CHAR * a_pString); - - /** Internal use of our string comparison function */ - bool IsLess(const SI_CHAR * a_pLeft, const SI_CHAR * a_pRight) const { - static const SI_STRLESS isLess = SI_STRLESS(); - return isLess(a_pLeft, a_pRight); - } - - bool IsMultiLineTag(const SI_CHAR * a_pData) const; - bool IsMultiLineData(const SI_CHAR * a_pData) const; - bool LoadMultiLineText( - SI_CHAR *& a_pData, - const SI_CHAR *& a_pVal, - const SI_CHAR * a_pTagName, - bool a_bAllowBlankLinesInComment = false - ) const; - bool IsNewLineChar(SI_CHAR a_c) const; - - bool OutputMultiLineText( - OutputWriter & a_oOutput, - Converter & a_oConverter, - const SI_CHAR * a_pText - ) const; - -private: - /** Copy of the INI file data in our character format. This will be - modified when parsed to have NULL characters added after all - interesting string entries. All of the string pointers to sections, - keys and values point into this block of memory. - */ - SI_CHAR * m_pData; - - /** Length of the data that we have stored. Used when deleting strings - to determine if the string is stored here or in the allocated string - buffer. - */ - size_t m_uDataLen; - - /** File comment for this data, if one exists. */ - const SI_CHAR * m_pFileComment; - - /** Parsed INI data. Section -> (Key -> Value). */ - TSection m_data; - - /** This vector stores allocated memory for copies of strings that have - been supplied after the file load. It will be empty unless SetValue() - has been called. - */ - TNamesDepend m_strings; - - /** Is the format of our datafile UTF-8 or MBCS? */ - bool m_bStoreIsUtf8; - - /** Are multiple values permitted for the same key? */ - bool m_bAllowMultiKey; - - /** Are data values permitted to span multiple lines? */ - bool m_bAllowMultiLine; - - /** Should spaces be written out surrounding the equals sign? */ - bool m_bSpaces; - - /** Next order value, used to ensure sections and keys are output in the - same order that they are loaded/added. - */ - int m_nOrder; -}; - -// --------------------------------------------------------------------------- -// IMPLEMENTATION -// --------------------------------------------------------------------------- - -template -CSimpleIniTempl::CSimpleIniTempl( - bool a_bIsUtf8, - bool a_bAllowMultiKey, - bool a_bAllowMultiLine - ) - : m_pData(0) - , m_uDataLen(0) - , m_pFileComment(NULL) - , m_bStoreIsUtf8(a_bIsUtf8) - , m_bAllowMultiKey(a_bAllowMultiKey) - , m_bAllowMultiLine(a_bAllowMultiLine) - , m_bSpaces(true) - , m_nOrder(0) -{ } - -template -CSimpleIniTempl::~CSimpleIniTempl() -{ - Reset(); -} - -template -void -CSimpleIniTempl::Reset() -{ - // remove all data - delete[] m_pData; - m_pData = NULL; - m_uDataLen = 0; - m_pFileComment = NULL; - if (!m_data.empty()) { - m_data.erase(m_data.begin(), m_data.end()); - } - - // remove all strings - if (!m_strings.empty()) { - typename TNamesDepend::iterator i = m_strings.begin(); - for (; i != m_strings.end(); ++i) { - delete[] const_cast(i->pItem); - } - m_strings.erase(m_strings.begin(), m_strings.end()); - } -} - -template -SI_Error -CSimpleIniTempl::LoadFile( - const char * a_pszFile - ) -{ - FILE * fp = NULL; -#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE - fopen_s(&fp, a_pszFile, "rb"); -#else // !__STDC_WANT_SECURE_LIB__ - fp = fopen(a_pszFile, "rb"); -#endif // __STDC_WANT_SECURE_LIB__ - if (!fp) { - return SI_FILE; - } - SI_Error rc = LoadFile(fp); - fclose(fp); - return rc; -} - -#ifdef SI_HAS_WIDE_FILE -template -SI_Error -CSimpleIniTempl::LoadFile( - const SI_WCHAR_T * a_pwszFile - ) -{ -#ifdef _WIN32 - FILE * fp = NULL; -#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE - _wfopen_s(&fp, a_pwszFile, L"rb"); -#else // !__STDC_WANT_SECURE_LIB__ - fp = _wfopen(a_pwszFile, L"rb"); -#endif // __STDC_WANT_SECURE_LIB__ - if (!fp) return SI_FILE; - SI_Error rc = LoadFile(fp); - fclose(fp); - return rc; -#else // !_WIN32 (therefore SI_CONVERT_ICU) - char szFile[256]; - u_austrncpy(szFile, a_pwszFile, sizeof(szFile)); - return LoadFile(szFile); -#endif // _WIN32 -} -#endif // SI_HAS_WIDE_FILE - -template -SI_Error -CSimpleIniTempl::LoadFile( - FILE * a_fpFile - ) -{ - // load the raw file data - int retval = fseek(a_fpFile, 0, SEEK_END); - if (retval != 0) { - return SI_FILE; - } - long lSize = ftell(a_fpFile); - if (lSize < 0) { - return SI_FILE; - } - if (lSize == 0) { - return SI_OK; - } - - // allocate and ensure NULL terminated - char * pData = new char[lSize+1]; - pData[lSize] = 0; - - // load data into buffer - fseek(a_fpFile, 0, SEEK_SET); - size_t uRead = fread(pData, sizeof(char), lSize, a_fpFile); - if (uRead != (size_t) lSize) { - delete[] pData; - return SI_FILE; - } - - // convert the raw data to unicode - SI_Error rc = LoadData(pData, uRead); - delete[] pData; - return rc; -} - -template -SI_Error -CSimpleIniTempl::LoadData( - const char * a_pData, - size_t a_uDataLen - ) -{ - SI_CONVERTER converter(m_bStoreIsUtf8); - - if (a_uDataLen == 0) { - return SI_OK; - } - - // consume the UTF-8 BOM if it exists - if (m_bStoreIsUtf8 && a_uDataLen >= 3) { - if (memcmp(a_pData, SI_UTF8_SIGNATURE, 3) == 0) { - a_pData += 3; - a_uDataLen -= 3; - } - } - - // determine the length of the converted data - size_t uLen = converter.SizeFromStore(a_pData, a_uDataLen); - if (uLen == (size_t)(-1)) { - return SI_FAIL; - } - - // allocate memory for the data, ensure that there is a NULL - // terminator wherever the converted data ends - SI_CHAR * pData = new SI_CHAR[uLen+1]; - memset(pData, 0, sizeof(SI_CHAR)*(uLen+1)); - - // convert the data - if (!converter.ConvertFromStore(a_pData, a_uDataLen, pData, uLen)) { - delete[] pData; - return SI_FAIL; - } - - // parse it - static const SI_CHAR empty = 0; - SI_CHAR * pWork = pData; - const SI_CHAR * pSection = ∅ - const SI_CHAR * pItem = NULL; - const SI_CHAR * pVal = NULL; - const SI_CHAR * pComment = NULL; - - // We copy the strings if we are loading data into this class when we - // already have stored some. - bool bCopyStrings = (m_pData != NULL); - - // find a file comment if it exists, this is a comment that starts at the - // beginning of the file and continues until the first blank line. - SI_Error rc = FindFileComment(pWork, bCopyStrings); - if (rc < 0) return rc; - - // add every entry in the file to the data table - while (FindEntry(pWork, pSection, pItem, pVal, pComment)) { - rc = AddEntry(pSection, pItem, pVal, pComment, false, bCopyStrings); - if (rc < 0) return rc; - } - - // store these strings if we didn't copy them - if (bCopyStrings) { - delete[] pData; - } - else { - m_pData = pData; - m_uDataLen = uLen+1; - } - - return SI_OK; -} - -#ifdef SI_SUPPORT_IOSTREAMS -template -SI_Error -CSimpleIniTempl::LoadData( - std::istream & a_istream - ) -{ - std::string strData; - char szBuf[512]; - do { - a_istream.get(szBuf, sizeof(szBuf), '\0'); - strData.append(szBuf); - } - while (a_istream.good()); - return LoadData(strData); -} -#endif // SI_SUPPORT_IOSTREAMS - -template -SI_Error -CSimpleIniTempl::FindFileComment( - SI_CHAR *& a_pData, - bool a_bCopyStrings - ) -{ - // there can only be a single file comment - if (m_pFileComment) { - return SI_OK; - } - - // Load the file comment as multi-line text, this will modify all of - // the newline characters to be single \n chars - if (!LoadMultiLineText(a_pData, m_pFileComment, NULL, false)) { - return SI_OK; - } - - // copy the string if necessary - if (a_bCopyStrings) { - SI_Error rc = CopyString(m_pFileComment); - if (rc < 0) return rc; - } - - return SI_OK; -} - -template -bool -CSimpleIniTempl::FindEntry( - SI_CHAR *& a_pData, - const SI_CHAR *& a_pSection, - const SI_CHAR *& a_pKey, - const SI_CHAR *& a_pVal, - const SI_CHAR *& a_pComment - ) const -{ - a_pComment = NULL; - - SI_CHAR * pTrail = NULL; - while (*a_pData) { - // skip spaces and empty lines - while (*a_pData && IsSpace(*a_pData)) { - ++a_pData; - } - if (!*a_pData) { - break; - } - - // skip processing of comment lines but keep a pointer to - // the start of the comment. - if (IsComment(*a_pData)) { - LoadMultiLineText(a_pData, a_pComment, NULL, true); - continue; - } - - // process section names - if (*a_pData == '[') { - // skip leading spaces - ++a_pData; - while (*a_pData && IsSpace(*a_pData)) { - ++a_pData; - } - - // find the end of the section name (it may contain spaces) - // and convert it to lowercase as necessary - a_pSection = a_pData; - while (*a_pData && *a_pData != ']' && !IsNewLineChar(*a_pData)) { - ++a_pData; - } - - // if it's an invalid line, just skip it - if (*a_pData != ']') { - continue; - } - - // remove trailing spaces from the section - pTrail = a_pData - 1; - while (pTrail >= a_pSection && IsSpace(*pTrail)) { - --pTrail; - } - ++pTrail; - *pTrail = 0; - - // skip to the end of the line - ++a_pData; // safe as checked that it == ']' above - while (*a_pData && !IsNewLineChar(*a_pData)) { - ++a_pData; - } - - a_pKey = NULL; - a_pVal = NULL; - return true; - } - - // find the end of the key name (it may contain spaces) - // and convert it to lowercase as necessary - a_pKey = a_pData; - while (*a_pData && *a_pData != '=' && !IsNewLineChar(*a_pData)) { - ++a_pData; - } - - // if it's an invalid line, just skip it - if (*a_pData != '=') { - continue; - } - - // empty keys are invalid - if (a_pKey == a_pData) { - while (*a_pData && !IsNewLineChar(*a_pData)) { - ++a_pData; - } - continue; - } - - // remove trailing spaces from the key - pTrail = a_pData - 1; - while (pTrail >= a_pKey && IsSpace(*pTrail)) { - --pTrail; - } - ++pTrail; - *pTrail = 0; - - // skip leading whitespace on the value - ++a_pData; // safe as checked that it == '=' above - while (*a_pData && !IsNewLineChar(*a_pData) && IsSpace(*a_pData)) { - ++a_pData; - } - - // find the end of the value which is the end of this line - a_pVal = a_pData; - while (*a_pData && !IsNewLineChar(*a_pData)) { - ++a_pData; - } - - // remove trailing spaces from the value - pTrail = a_pData - 1; - if (*a_pData) { // prepare for the next round - SkipNewLine(a_pData); - } - while (pTrail >= a_pVal && IsSpace(*pTrail)) { - --pTrail; - } - ++pTrail; - *pTrail = 0; - - // check for multi-line entries - if (m_bAllowMultiLine && IsMultiLineTag(a_pVal)) { - // skip the "<<<" to get the tag that will end the multiline - const SI_CHAR * pTagName = a_pVal + 3; - return LoadMultiLineText(a_pData, a_pVal, pTagName); - } - - // return the standard entry - return true; - } - - return false; -} - -template -bool -CSimpleIniTempl::IsMultiLineTag( - const SI_CHAR * a_pVal - ) const -{ - // check for the "<<<" prefix for a multi-line entry - if (*a_pVal++ != '<') return false; - if (*a_pVal++ != '<') return false; - if (*a_pVal++ != '<') return false; - return true; -} - -template -bool -CSimpleIniTempl::IsMultiLineData( - const SI_CHAR * a_pData - ) const -{ - // data is multi-line if it has any of the following features: - // * whitespace prefix - // * embedded newlines - // * whitespace suffix - - // empty string - if (!*a_pData) { - return false; - } - - // check for prefix - if (IsSpace(*a_pData)) { - return true; - } - - // embedded newlines - while (*a_pData) { - if (IsNewLineChar(*a_pData)) { - return true; - } - ++a_pData; - } - - // check for suffix - if (IsSpace(*--a_pData)) { - return true; - } - - return false; -} - -template -bool -CSimpleIniTempl::IsNewLineChar( - SI_CHAR a_c - ) const -{ - return (a_c == '\n' || a_c == '\r'); -} - -template -bool -CSimpleIniTempl::LoadMultiLineText( - SI_CHAR *& a_pData, - const SI_CHAR *& a_pVal, - const SI_CHAR * a_pTagName, - bool a_bAllowBlankLinesInComment - ) const -{ - // we modify this data to strip all newlines down to a single '\n' - // character. This means that on Windows we need to strip out some - // characters which will make the data shorter. - // i.e. LINE1-LINE1\r\nLINE2-LINE2\0 will become - // LINE1-LINE1\nLINE2-LINE2\0 - // The pDataLine entry is the pointer to the location in memory that - // the current line needs to start to run following the existing one. - // This may be the same as pCurrLine in which case no move is needed. - SI_CHAR * pDataLine = a_pData; - SI_CHAR * pCurrLine; - - // value starts at the current line - a_pVal = a_pData; - - // find the end tag. This tag must start in column 1 and be - // followed by a newline. No whitespace removal is done while - // searching for this tag. - SI_CHAR cEndOfLineChar = *a_pData; - for(;;) { - // if we are loading comments then we need a comment character as - // the first character on every line - if (!a_pTagName && !IsComment(*a_pData)) { - // if we aren't allowing blank lines then we're done - if (!a_bAllowBlankLinesInComment) { - break; - } - - // if we are allowing blank lines then we only include them - // in this comment if another comment follows, so read ahead - // to find out. - SI_CHAR * pCurr = a_pData; - int nNewLines = 0; - while (IsSpace(*pCurr)) { - if (IsNewLineChar(*pCurr)) { - ++nNewLines; - SkipNewLine(pCurr); - } - else { - ++pCurr; - } - } - - // we have a comment, add the blank lines to the output - // and continue processing from here - if (IsComment(*pCurr)) { - for (; nNewLines > 0; --nNewLines) *pDataLine++ = '\n'; - a_pData = pCurr; - continue; - } - - // the comment ends here - break; - } - - // find the end of this line - pCurrLine = a_pData; - while (*a_pData && !IsNewLineChar(*a_pData)) ++a_pData; - - // move this line down to the location that it should be if necessary - if (pDataLine < pCurrLine) { - size_t nLen = (size_t) (a_pData - pCurrLine); - memmove(pDataLine, pCurrLine, nLen * sizeof(SI_CHAR)); - pDataLine[nLen] = '\0'; - } - - // end the line with a NULL - cEndOfLineChar = *a_pData; - *a_pData = 0; - - // if are looking for a tag then do the check now. This is done before - // checking for end of the data, so that if we have the tag at the end - // of the data then the tag is removed correctly. - if (a_pTagName && - (!IsLess(pDataLine, a_pTagName) && !IsLess(a_pTagName, pDataLine))) - { - break; - } - - // if we are at the end of the data then we just automatically end - // this entry and return the current data. - if (!cEndOfLineChar) { - return true; - } - - // otherwise we need to process this newline to ensure that it consists - // of just a single \n character. - pDataLine += (a_pData - pCurrLine); - *a_pData = cEndOfLineChar; - SkipNewLine(a_pData); - *pDataLine++ = '\n'; - } - - // if we didn't find a comment at all then return false - if (a_pVal == a_pData) { - a_pVal = NULL; - return false; - } - - // the data (which ends at the end of the last line) needs to be - // null-terminated BEFORE before the newline character(s). If the - // user wants a new line in the multi-line data then they need to - // add an empty line before the tag. - *--pDataLine = '\0'; - - // if looking for a tag and if we aren't at the end of the data, - // then move a_pData to the start of the next line. - if (a_pTagName && cEndOfLineChar) { - SI_ASSERT(IsNewLineChar(cEndOfLineChar)); - *a_pData = cEndOfLineChar; - SkipNewLine(a_pData); - } - - return true; -} - -template -SI_Error -CSimpleIniTempl::CopyString( - const SI_CHAR *& a_pString - ) -{ - size_t uLen = 0; - if (sizeof(SI_CHAR) == sizeof(char)) { - uLen = strlen((const char *)a_pString); - } - else if (sizeof(SI_CHAR) == sizeof(wchar_t)) { - uLen = wcslen((const wchar_t *)a_pString); - } - else { - for ( ; a_pString[uLen]; ++uLen) /*loop*/ ; - } - ++uLen; // NULL character - SI_CHAR * pCopy = new SI_CHAR[uLen]; - memcpy(pCopy, a_pString, sizeof(SI_CHAR)*uLen); - m_strings.push_back(pCopy); - a_pString = pCopy; - return SI_OK; -} - -template -SI_Error -CSimpleIniTempl::AddEntry( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pValue, - const SI_CHAR * a_pComment, - bool a_bForceReplace, - bool a_bCopyStrings - ) -{ - SI_Error rc; - bool bInserted = false; - - SI_ASSERT(!a_pComment || IsComment(*a_pComment)); - - // if we are copying strings then make a copy of the comment now - // because we will need it when we add the entry. - if (a_bCopyStrings && a_pComment) { - rc = CopyString(a_pComment); - if (rc < 0) return rc; - } - - // create the section entry if necessary - typename TSection::iterator iSection = m_data.find(a_pSection); - if (iSection == m_data.end()) { - // if the section doesn't exist then we need a copy as the - // string needs to last beyond the end of this function - if (a_bCopyStrings) { - rc = CopyString(a_pSection); - if (rc < 0) return rc; - } - - // only set the comment if this is a section only entry - Entry oSection(a_pSection, ++m_nOrder); - if (a_pComment && (!a_pKey || !a_pValue)) { - oSection.pComment = a_pComment; - } - - typename TSection::value_type oEntry(oSection, TKeyVal()); - typedef typename TSection::iterator SectionIterator; - std::pair i = m_data.insert(oEntry); - iSection = i.first; - bInserted = true; - } - if (!a_pKey || !a_pValue) { - // section only entries are specified with pItem and pVal as NULL - return bInserted ? SI_INSERTED : SI_UPDATED; - } - - // check for existence of the key - TKeyVal & keyval = iSection->second; - typename TKeyVal::iterator iKey = keyval.find(a_pKey); - - // remove all existing entries but save the load order and - // comment of the first entry - int nLoadOrder = ++m_nOrder; - if (iKey != keyval.end() && m_bAllowMultiKey && a_bForceReplace) { - const SI_CHAR * pComment = NULL; - while (iKey != keyval.end() && !IsLess(a_pKey, iKey->first.pItem)) { - if (iKey->first.nOrder < nLoadOrder) { - nLoadOrder = iKey->first.nOrder; - pComment = iKey->first.pComment; - } - ++iKey; - } - if (pComment) { - DeleteString(a_pComment); - a_pComment = pComment; - CopyString(a_pComment); - } - Delete(a_pSection, a_pKey); - iKey = keyval.end(); - } - - // make string copies if necessary - bool bForceCreateNewKey = m_bAllowMultiKey && !a_bForceReplace; - if (a_bCopyStrings) { - if (bForceCreateNewKey || iKey == keyval.end()) { - // if the key doesn't exist then we need a copy as the - // string needs to last beyond the end of this function - // because we will be inserting the key next - rc = CopyString(a_pKey); - if (rc < 0) return rc; - } - - // we always need a copy of the value - rc = CopyString(a_pValue); - if (rc < 0) return rc; - } - - // create the key entry - if (iKey == keyval.end() || bForceCreateNewKey) { - Entry oKey(a_pKey, nLoadOrder); - if (a_pComment) { - oKey.pComment = a_pComment; - } - typename TKeyVal::value_type oEntry(oKey, static_cast(NULL)); - iKey = keyval.insert(oEntry); - bInserted = true; - } - iKey->second = a_pValue; - return bInserted ? SI_INSERTED : SI_UPDATED; -} - -template -const SI_CHAR * -CSimpleIniTempl::GetValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pDefault, - bool * a_pHasMultiple - ) const -{ - if (a_pHasMultiple) { - *a_pHasMultiple = false; - } - if (!a_pSection || !a_pKey) { - return a_pDefault; - } - typename TSection::const_iterator iSection = m_data.find(a_pSection); - if (iSection == m_data.end()) { - return a_pDefault; - } - typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey); - if (iKeyVal == iSection->second.end()) { - return a_pDefault; - } - - // check for multiple entries with the same key - if (m_bAllowMultiKey && a_pHasMultiple) { - typename TKeyVal::const_iterator iTemp = iKeyVal; - if (++iTemp != iSection->second.end()) { - if (!IsLess(a_pKey, iTemp->first.pItem)) { - *a_pHasMultiple = true; - } - } - } - - return iKeyVal->second; -} - -template -long -CSimpleIniTempl::GetLongValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - long a_nDefault, - bool * a_pHasMultiple - ) const -{ - // return the default if we don't have a value - const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple); - if (!pszValue || !*pszValue) return a_nDefault; - - // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII - char szValue[64] = { 0 }; - SI_CONVERTER c(m_bStoreIsUtf8); - if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) { - return a_nDefault; - } - - // handle the value as hex if prefaced with "0x" - long nValue = a_nDefault; - char * pszSuffix = szValue; - if (szValue[0] == '0' && (szValue[1] == 'x' || szValue[1] == 'X')) { - if (!szValue[2]) return a_nDefault; - nValue = strtol(&szValue[2], &pszSuffix, 16); - } - else { - nValue = strtol(szValue, &pszSuffix, 10); - } - - // any invalid strings will return the default value - if (*pszSuffix) { - return a_nDefault; - } - - return nValue; -} - -template -SI_Error -CSimpleIniTempl::SetLongValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - long a_nValue, - const SI_CHAR * a_pComment, - bool a_bUseHex, - bool a_bForceReplace - ) -{ - // use SetValue to create sections - if (!a_pSection || !a_pKey) return SI_FAIL; - - // convert to an ASCII string - char szInput[64]; -#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE - sprintf_s(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue); -#else // !__STDC_WANT_SECURE_LIB__ - sprintf(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue); -#endif // __STDC_WANT_SECURE_LIB__ - - // convert to output text - SI_CHAR szOutput[64]; - SI_CONVERTER c(m_bStoreIsUtf8); - c.ConvertFromStore(szInput, strlen(szInput) + 1, - szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); - - // actually add it - return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); -} - -template -double -CSimpleIniTempl::GetDoubleValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - double a_nDefault, - bool * a_pHasMultiple - ) const -{ - // return the default if we don't have a value - const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple); - if (!pszValue || !*pszValue) return a_nDefault; - - // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII - char szValue[64] = { 0 }; - SI_CONVERTER c(m_bStoreIsUtf8); - if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) { - return a_nDefault; - } - - char * pszSuffix = NULL; - double nValue = strtod(szValue, &pszSuffix); - - // any invalid strings will return the default value - if (!pszSuffix || *pszSuffix) { - return a_nDefault; - } - - return nValue; -} - -template -SI_Error -CSimpleIniTempl::SetDoubleValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - double a_nValue, - const SI_CHAR * a_pComment, - bool a_bForceReplace - ) -{ - // use SetValue to create sections - if (!a_pSection || !a_pKey) return SI_FAIL; - - // convert to an ASCII string - char szInput[64]; -#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE - sprintf_s(szInput, "%f", a_nValue); -#else // !__STDC_WANT_SECURE_LIB__ - sprintf(szInput, "%f", a_nValue); -#endif // __STDC_WANT_SECURE_LIB__ - - // convert to output text - SI_CHAR szOutput[64]; - SI_CONVERTER c(m_bStoreIsUtf8); - c.ConvertFromStore(szInput, strlen(szInput) + 1, - szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); - - // actually add it - return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); -} - -template -bool -CSimpleIniTempl::GetBoolValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - bool a_bDefault, - bool * a_pHasMultiple - ) const -{ - // return the default if we don't have a value - const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple); - if (!pszValue || !*pszValue) return a_bDefault; - - // we only look at the minimum number of characters - switch (pszValue[0]) { - case 't': case 'T': // true - case 'y': case 'Y': // yes - case '1': // 1 (one) - return true; - - case 'f': case 'F': // false - case 'n': case 'N': // no - case '0': // 0 (zero) - return false; - - case 'o': case 'O': - if (pszValue[1] == 'n' || pszValue[1] == 'N') return true; // on - if (pszValue[1] == 'f' || pszValue[1] == 'F') return false; // off - break; - } - - // no recognized value, return the default - return a_bDefault; -} - -template -SI_Error -CSimpleIniTempl::SetBoolValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - bool a_bValue, - const SI_CHAR * a_pComment, - bool a_bForceReplace - ) -{ - // use SetValue to create sections - if (!a_pSection || !a_pKey) return SI_FAIL; - - // convert to an ASCII string - const char * pszInput = a_bValue ? "true" : "false"; - - // convert to output text - SI_CHAR szOutput[64]; - SI_CONVERTER c(m_bStoreIsUtf8); - c.ConvertFromStore(pszInput, strlen(pszInput) + 1, - szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); - - // actually add it - return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); -} - -template -bool -CSimpleIniTempl::GetAllValues( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - TNamesDepend & a_values - ) const -{ - a_values.clear(); - - if (!a_pSection || !a_pKey) { - return false; - } - typename TSection::const_iterator iSection = m_data.find(a_pSection); - if (iSection == m_data.end()) { - return false; - } - typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey); - if (iKeyVal == iSection->second.end()) { - return false; - } - - // insert all values for this key - a_values.push_back(Entry(iKeyVal->second, iKeyVal->first.pComment, iKeyVal->first.nOrder)); - if (m_bAllowMultiKey) { - ++iKeyVal; - while (iKeyVal != iSection->second.end() && !IsLess(a_pKey, iKeyVal->first.pItem)) { - a_values.push_back(Entry(iKeyVal->second, iKeyVal->first.pComment, iKeyVal->first.nOrder)); - ++iKeyVal; - } - } - - return true; -} - -template -int -CSimpleIniTempl::GetSectionSize( - const SI_CHAR * a_pSection - ) const -{ - if (!a_pSection) { - return -1; - } - - typename TSection::const_iterator iSection = m_data.find(a_pSection); - if (iSection == m_data.end()) { - return -1; - } - const TKeyVal & section = iSection->second; - - // if multi-key isn't permitted then the section size is - // the number of keys that we have. - if (!m_bAllowMultiKey || section.empty()) { - return (int) section.size(); - } - - // otherwise we need to count them - int nCount = 0; - const SI_CHAR * pLastKey = NULL; - typename TKeyVal::const_iterator iKeyVal = section.begin(); - for (int n = 0; iKeyVal != section.end(); ++iKeyVal, ++n) { - if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) { - ++nCount; - pLastKey = iKeyVal->first.pItem; - } - } - return nCount; -} - -template -const typename CSimpleIniTempl::TKeyVal * -CSimpleIniTempl::GetSection( - const SI_CHAR * a_pSection - ) const -{ - if (a_pSection) { - typename TSection::const_iterator i = m_data.find(a_pSection); - if (i != m_data.end()) { - return &(i->second); - } - } - return 0; -} - -template -void -CSimpleIniTempl::GetAllSections( - TNamesDepend & a_names - ) const -{ - a_names.clear(); - typename TSection::const_iterator i = m_data.begin(); - for (int n = 0; i != m_data.end(); ++i, ++n ) { - a_names.push_back(i->first); - } -} - -template -bool -CSimpleIniTempl::GetAllKeys( - const SI_CHAR * a_pSection, - TNamesDepend & a_names - ) const -{ - a_names.clear(); - - if (!a_pSection) { - return false; - } - - typename TSection::const_iterator iSection = m_data.find(a_pSection); - if (iSection == m_data.end()) { - return false; - } - - const TKeyVal & section = iSection->second; - const SI_CHAR * pLastKey = NULL; - typename TKeyVal::const_iterator iKeyVal = section.begin(); - for (int n = 0; iKeyVal != section.end(); ++iKeyVal, ++n ) { - if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) { - a_names.push_back(iKeyVal->first); - pLastKey = iKeyVal->first.pItem; - } - } - - return true; -} - -template -SI_Error -CSimpleIniTempl::SaveFile( - const char * a_pszFile, - bool a_bAddSignature - ) const -{ - FILE * fp = NULL; -#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE - fopen_s(&fp, a_pszFile, "wb"); -#else // !__STDC_WANT_SECURE_LIB__ - fp = fopen(a_pszFile, "wb"); -#endif // __STDC_WANT_SECURE_LIB__ - if (!fp) return SI_FILE; - SI_Error rc = SaveFile(fp, a_bAddSignature); - fclose(fp); - return rc; -} - -#ifdef SI_HAS_WIDE_FILE -template -SI_Error -CSimpleIniTempl::SaveFile( - const SI_WCHAR_T * a_pwszFile, - bool a_bAddSignature - ) const -{ -#ifdef _WIN32 - FILE * fp = NULL; -#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE - _wfopen_s(&fp, a_pwszFile, L"wb"); -#else // !__STDC_WANT_SECURE_LIB__ - fp = _wfopen(a_pwszFile, L"wb"); -#endif // __STDC_WANT_SECURE_LIB__ - if (!fp) return SI_FILE; - SI_Error rc = SaveFile(fp, a_bAddSignature); - fclose(fp); - return rc; -#else // !_WIN32 (therefore SI_CONVERT_ICU) - char szFile[256]; - u_austrncpy(szFile, a_pwszFile, sizeof(szFile)); - return SaveFile(szFile, a_bAddSignature); -#endif // _WIN32 -} -#endif // SI_HAS_WIDE_FILE - -template -SI_Error -CSimpleIniTempl::SaveFile( - FILE * a_pFile, - bool a_bAddSignature - ) const -{ - FileWriter writer(a_pFile); - return Save(writer, a_bAddSignature); -} - -template -SI_Error -CSimpleIniTempl::Save( - OutputWriter & a_oOutput, - bool a_bAddSignature - ) const -{ - Converter convert(m_bStoreIsUtf8); - - // add the UTF-8 signature if it is desired - if (m_bStoreIsUtf8 && a_bAddSignature) { - a_oOutput.Write(SI_UTF8_SIGNATURE); - } - - // get all of the sections sorted in load order - TNamesDepend oSections; - GetAllSections(oSections); -#if defined(_MSC_VER) && _MSC_VER <= 1200 - oSections.sort(); -#elif defined(__BORLANDC__) - oSections.sort(Entry::LoadOrder()); -#else - oSections.sort(typename Entry::LoadOrder()); -#endif - - // write the file comment if we have one - bool bNeedNewLine = false; - if (m_pFileComment) { - if (!OutputMultiLineText(a_oOutput, convert, m_pFileComment)) { - return SI_FAIL; - } - bNeedNewLine = true; - } - - // iterate through our sections and output the data - typename TNamesDepend::const_iterator iSection = oSections.begin(); - for ( ; iSection != oSections.end(); ++iSection ) { - // write out the comment if there is one - if (iSection->pComment) { - if (bNeedNewLine) { - a_oOutput.Write(SI_NEWLINE_A); - a_oOutput.Write(SI_NEWLINE_A); - } - if (!OutputMultiLineText(a_oOutput, convert, iSection->pComment)) { - return SI_FAIL; - } - bNeedNewLine = false; - } - - if (bNeedNewLine) { - a_oOutput.Write(SI_NEWLINE_A); - a_oOutput.Write(SI_NEWLINE_A); - bNeedNewLine = false; - } - - // write the section (unless there is no section name) - if (*iSection->pItem) { - if (!convert.ConvertToStore(iSection->pItem)) { - return SI_FAIL; - } - a_oOutput.Write("["); - a_oOutput.Write(convert.Data()); - a_oOutput.Write("]"); - a_oOutput.Write(SI_NEWLINE_A); - } - - // get all of the keys sorted in load order - TNamesDepend oKeys; - GetAllKeys(iSection->pItem, oKeys); -#if defined(_MSC_VER) && _MSC_VER <= 1200 - oKeys.sort(); -#elif defined(__BORLANDC__) - oKeys.sort(Entry::LoadOrder()); -#else - oKeys.sort(typename Entry::LoadOrder()); -#endif - - // write all keys and values - typename TNamesDepend::const_iterator iKey = oKeys.begin(); - for ( ; iKey != oKeys.end(); ++iKey) { - // get all values for this key - TNamesDepend oValues; - GetAllValues(iSection->pItem, iKey->pItem, oValues); - - typename TNamesDepend::const_iterator iValue = oValues.begin(); - for ( ; iValue != oValues.end(); ++iValue) { - // write out the comment if there is one - if (iValue->pComment) { - a_oOutput.Write(SI_NEWLINE_A); - if (!OutputMultiLineText(a_oOutput, convert, iValue->pComment)) { - return SI_FAIL; - } - } - - // write the key - if (!convert.ConvertToStore(iKey->pItem)) { - return SI_FAIL; - } - a_oOutput.Write(convert.Data()); - - // write the value - if (!convert.ConvertToStore(iValue->pItem)) { - return SI_FAIL; - } - a_oOutput.Write(m_bSpaces ? " = " : "="); - if (m_bAllowMultiLine && IsMultiLineData(iValue->pItem)) { - // multi-line data needs to be processed specially to ensure - // that we use the correct newline format for the current system - a_oOutput.Write("<<pItem)) { - return SI_FAIL; - } - a_oOutput.Write("END_OF_TEXT"); - } - else { - a_oOutput.Write(convert.Data()); - } - a_oOutput.Write(SI_NEWLINE_A); - } - } - - bNeedNewLine = true; - } - - return SI_OK; -} - -template -bool -CSimpleIniTempl::OutputMultiLineText( - OutputWriter & a_oOutput, - Converter & a_oConverter, - const SI_CHAR * a_pText - ) const -{ - const SI_CHAR * pEndOfLine; - SI_CHAR cEndOfLineChar = *a_pText; - while (cEndOfLineChar) { - // find the end of this line - pEndOfLine = a_pText; - for (; *pEndOfLine && *pEndOfLine != '\n'; ++pEndOfLine) /*loop*/ ; - cEndOfLineChar = *pEndOfLine; - - // temporarily null terminate, convert and output the line - *const_cast(pEndOfLine) = 0; - if (!a_oConverter.ConvertToStore(a_pText)) { - return false; - } - *const_cast(pEndOfLine) = cEndOfLineChar; - a_pText += (pEndOfLine - a_pText) + 1; - a_oOutput.Write(a_oConverter.Data()); - a_oOutput.Write(SI_NEWLINE_A); - } - return true; -} - -template -bool -CSimpleIniTempl::Delete( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - bool a_bRemoveEmpty - ) -{ - return DeleteValue(a_pSection, a_pKey, NULL, a_bRemoveEmpty); -} - -template -bool -CSimpleIniTempl::DeleteValue( - const SI_CHAR * a_pSection, - const SI_CHAR * a_pKey, - const SI_CHAR * a_pValue, - bool a_bRemoveEmpty - ) -{ - if (!a_pSection) { - return false; - } - - typename TSection::iterator iSection = m_data.find(a_pSection); - if (iSection == m_data.end()) { - return false; - } - - // remove a single key if we have a keyname - if (a_pKey) { - typename TKeyVal::iterator iKeyVal = iSection->second.find(a_pKey); - if (iKeyVal == iSection->second.end()) { - return false; - } - - static const SI_STRLESS isLess = SI_STRLESS(); - - // remove any copied strings and then the key - typename TKeyVal::iterator iDelete; - bool bDeleted = false; - do { - iDelete = iKeyVal++; - - if(a_pValue == NULL || - (isLess(a_pValue, iDelete->second) == false && - isLess(iDelete->second, a_pValue) == false)) { - DeleteString(iDelete->first.pItem); - DeleteString(iDelete->second); - iSection->second.erase(iDelete); - bDeleted = true; - } - } - while (iKeyVal != iSection->second.end() - && !IsLess(a_pKey, iKeyVal->first.pItem)); - - if(!bDeleted) { - return false; - } - - // done now if the section is not empty or we are not pruning away - // the empty sections. Otherwise let it fall through into the section - // deletion code - if (!a_bRemoveEmpty || !iSection->second.empty()) { - return true; - } - } - else { - // delete all copied strings from this section. The actual - // entries will be removed when the section is removed. - typename TKeyVal::iterator iKeyVal = iSection->second.begin(); - for ( ; iKeyVal != iSection->second.end(); ++iKeyVal) { - DeleteString(iKeyVal->first.pItem); - DeleteString(iKeyVal->second); - } - } - - // delete the section itself - DeleteString(iSection->first.pItem); - m_data.erase(iSection); - - return true; -} - -template -void -CSimpleIniTempl::DeleteString( - const SI_CHAR * a_pString - ) -{ - // strings may exist either inside the data block, or they will be - // individually allocated and stored in m_strings. We only physically - // delete those stored in m_strings. - if (a_pString < m_pData || a_pString >= m_pData + m_uDataLen) { - typename TNamesDepend::iterator i = m_strings.begin(); - for (;i != m_strings.end(); ++i) { - if (a_pString == i->pItem) { - delete[] const_cast(i->pItem); - m_strings.erase(i); - break; - } - } - } -} - -// --------------------------------------------------------------------------- -// CONVERSION FUNCTIONS -// --------------------------------------------------------------------------- - -// Defines the conversion classes for different libraries. Before including -// SimpleIni.h, set the converter that you wish you use by defining one of the -// following symbols. -// -// SI_CONVERT_GENERIC Use the Unicode reference conversion library in -// the accompanying files convert_utf.h/c -// SI_CONVERT_ICU Use the IBM ICU conversion library. Requires -// ICU headers on include path and icuuc.lib -// SI_CONVERT_WIN32 Use the Win32 API functions for conversion. - -#if !defined(SI_CONVERT_GENERIC) && !defined(SI_CONVERT_WIN32) && !defined(SI_CONVERT_ICU) -# ifdef _WIN32 -# define SI_CONVERT_WIN32 -# else -# define SI_CONVERT_GENERIC -# endif -#endif - -/** - * Generic case-sensitive less than comparison. This class returns numerically - * ordered ASCII case-sensitive text for all possible sizes and types of - * SI_CHAR. - */ -template -struct SI_GenericCase { - bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const { - long cmp; - for ( ;*pLeft && *pRight; ++pLeft, ++pRight) { - cmp = (long) *pLeft - (long) *pRight; - if (cmp != 0) { - return cmp < 0; - } - } - return *pRight != 0; - } -}; - -/** - * Generic ASCII case-insensitive less than comparison. This class returns - * numerically ordered ASCII case-insensitive text for all possible sizes - * and types of SI_CHAR. It is not safe for MBCS text comparison where - * ASCII A-Z characters are used in the encoding of multi-byte characters. - */ -template -struct SI_GenericNoCase { - inline SI_CHAR locase(SI_CHAR ch) const { - return (ch < 'A' || ch > 'Z') ? ch : (ch - 'A' + 'a'); - } - bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const { - long cmp; - for ( ;*pLeft && *pRight; ++pLeft, ++pRight) { - cmp = (long) locase(*pLeft) - (long) locase(*pRight); - if (cmp != 0) { - return cmp < 0; - } - } - return *pRight != 0; - } -}; - -/** - * Null conversion class for MBCS/UTF-8 to char (or equivalent). - */ -template -class SI_ConvertA { - bool m_bStoreIsUtf8; -protected: - SI_ConvertA() : m_bStoreIsUtf8(false) { } -public: - SI_ConvertA(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) { } - - /* copy and assignment */ - SI_ConvertA(const SI_ConvertA & rhs) { operator=(rhs); } - SI_ConvertA & operator=(const SI_ConvertA & rhs) { - m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8; - return *this; - } - - /** Calculate the number of SI_CHAR required for converting the input - * from the storage format. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to SI_CHAR. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @return Number of SI_CHAR required by the string when - * converted. If there are embedded NULL bytes in the - * input data, only the string up and not including - * the NULL byte will be converted. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeFromStore( - const char * a_pInputData, - size_t a_uInputDataLen) - { - (void)a_pInputData; - SI_ASSERT(a_uInputDataLen != (size_t) -1); - - // ASCII/MBCS/UTF-8 needs no conversion - return a_uInputDataLen; - } - - /** Convert the input string from the storage format to SI_CHAR. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to SI_CHAR. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @param a_pOutputData Pointer to the output buffer to received the - * converted data. - * @param a_uOutputDataSize Size of the output buffer in SI_CHAR. - * @return true if all of the input data was successfully - * converted. - */ - bool ConvertFromStore( - const char * a_pInputData, - size_t a_uInputDataLen, - SI_CHAR * a_pOutputData, - size_t a_uOutputDataSize) - { - // ASCII/MBCS/UTF-8 needs no conversion - if (a_uInputDataLen > a_uOutputDataSize) { - return false; - } - memcpy(a_pOutputData, a_pInputData, a_uInputDataLen); - return true; - } - - /** Calculate the number of char required by the storage format of this - * data. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated string to calculate the number of - * bytes required to be converted to storage format. - * @return Number of bytes required by the string when - * converted to storage format. This size always - * includes space for the terminating NULL character. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeToStore( - const SI_CHAR * a_pInputData) - { - // ASCII/MBCS/UTF-8 needs no conversion - return strlen((const char *)a_pInputData) + 1; - } - - /** Convert the input string to the storage format of this data. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated source string to convert. All of - * the data will be converted including the - * terminating NULL character. - * @param a_pOutputData Pointer to the buffer to receive the converted - * string. - * @param a_uOutputDataSize Size of the output buffer in char. - * @return true if all of the input data, including the - * terminating NULL character was successfully - * converted. - */ - bool ConvertToStore( - const SI_CHAR * a_pInputData, - char * a_pOutputData, - size_t a_uOutputDataSize) - { - // calc input string length (SI_CHAR type and size independent) - size_t uInputLen = strlen((const char *)a_pInputData) + 1; - if (uInputLen > a_uOutputDataSize) { - return false; - } - - // ascii/UTF-8 needs no conversion - memcpy(a_pOutputData, a_pInputData, uInputLen); - return true; - } -}; - - -// --------------------------------------------------------------------------- -// SI_CONVERT_GENERIC -// --------------------------------------------------------------------------- -#ifdef SI_CONVERT_GENERIC - -#define SI_Case SI_GenericCase -#define SI_NoCase SI_GenericNoCase - -#include -#include "convert_utf.h" - -/** - * Converts UTF-8 to a wchar_t (or equivalent) using the Unicode reference - * library functions. This can be used on all platforms. - */ -template -class SI_ConvertW { - bool m_bStoreIsUtf8; -protected: - SI_ConvertW() { } -public: - SI_ConvertW(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) { } - - /* copy and assignment */ - SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); } - SI_ConvertW & operator=(const SI_ConvertW & rhs) { - m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8; - return *this; - } - - /** Calculate the number of SI_CHAR required for converting the input - * from the storage format. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to SI_CHAR. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @return Number of SI_CHAR required by the string when - * converted. If there are embedded NULL bytes in the - * input data, only the string up and not including - * the NULL byte will be converted. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeFromStore( - const char * a_pInputData, - size_t a_uInputDataLen) - { - SI_ASSERT(a_uInputDataLen != (size_t) -1); - - if (m_bStoreIsUtf8) { - // worst case scenario for UTF-8 to wchar_t is 1 char -> 1 wchar_t - // so we just return the same number of characters required as for - // the source text. - return a_uInputDataLen; - } - -#if defined(SI_NO_MBSTOWCS_NULL) || (!defined(_MSC_VER) && !defined(_linux)) - // fall back processing for platforms that don't support a NULL dest to mbstowcs - // worst case scenario is 1:1, this will be a sufficient buffer size - (void)a_pInputData; - return a_uInputDataLen; -#else - // get the actual required buffer size - return mbstowcs(NULL, a_pInputData, a_uInputDataLen); -#endif - } - - /** Convert the input string from the storage format to SI_CHAR. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to SI_CHAR. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @param a_pOutputData Pointer to the output buffer to received the - * converted data. - * @param a_uOutputDataSize Size of the output buffer in SI_CHAR. - * @return true if all of the input data was successfully - * converted. - */ - bool ConvertFromStore( - const char * a_pInputData, - size_t a_uInputDataLen, - SI_CHAR * a_pOutputData, - size_t a_uOutputDataSize) - { - if (m_bStoreIsUtf8) { - // This uses the Unicode reference implementation to do the - // conversion from UTF-8 to wchar_t. The required files are - // convert_utf.h and convert_utf.c which should be included in - // the distribution but are publically available from unicode.org - // at http://www.unicode.org/Public/PROGRAMS/CVTUTF/ - ConversionResult retval; - const UTF8 * pUtf8 = (const UTF8 *) a_pInputData; - if (sizeof(wchar_t) == sizeof(UTF32)) { - UTF32 * pUtf32 = (UTF32 *) a_pOutputData; - retval = ConvertUTF8toUTF32( - &pUtf8, pUtf8 + a_uInputDataLen, - &pUtf32, pUtf32 + a_uOutputDataSize, - lenientConversion); - } - else if (sizeof(wchar_t) == sizeof(UTF16)) { - UTF16 * pUtf16 = (UTF16 *) a_pOutputData; - retval = ConvertUTF8toUTF16( - &pUtf8, pUtf8 + a_uInputDataLen, - &pUtf16, pUtf16 + a_uOutputDataSize, - lenientConversion); - } - return retval == conversionOK; - } - - // convert to wchar_t - size_t retval = mbstowcs(a_pOutputData, - a_pInputData, a_uOutputDataSize); - return retval != (size_t)(-1); - } - - /** Calculate the number of char required by the storage format of this - * data. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated string to calculate the number of - * bytes required to be converted to storage format. - * @return Number of bytes required by the string when - * converted to storage format. This size always - * includes space for the terminating NULL character. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeToStore( - const SI_CHAR * a_pInputData) - { - if (m_bStoreIsUtf8) { - // worst case scenario for wchar_t to UTF-8 is 1 wchar_t -> 6 char - size_t uLen = 0; - while (a_pInputData[uLen]) { - ++uLen; - } - return (6 * uLen) + 1; - } - else { - size_t uLen = wcstombs(NULL, a_pInputData, 0); - if (uLen == (size_t)(-1)) { - return uLen; - } - return uLen + 1; // include NULL terminator - } - } - - /** Convert the input string to the storage format of this data. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated source string to convert. All of - * the data will be converted including the - * terminating NULL character. - * @param a_pOutputData Pointer to the buffer to receive the converted - * string. - * @param a_uOutputDataSize Size of the output buffer in char. - * @return true if all of the input data, including the - * terminating NULL character was successfully - * converted. - */ - bool ConvertToStore( - const SI_CHAR * a_pInputData, - char * a_pOutputData, - size_t a_uOutputDataSize - ) - { - if (m_bStoreIsUtf8) { - // calc input string length (SI_CHAR type and size independent) - size_t uInputLen = 0; - while (a_pInputData[uInputLen]) { - ++uInputLen; - } - ++uInputLen; // include the NULL char - - // This uses the Unicode reference implementation to do the - // conversion from wchar_t to UTF-8. The required files are - // convert_utf.h and convert_utf.c which should be included in - // the distribution but are publically available from unicode.org - // at http://www.unicode.org/Public/PROGRAMS/CVTUTF/ - ConversionResult retval; - UTF8 * pUtf8 = (UTF8 *) a_pOutputData; - if (sizeof(wchar_t) == sizeof(UTF32)) { - const UTF32 * pUtf32 = (const UTF32 *) a_pInputData; - retval = ConvertUTF32toUTF8( - &pUtf32, pUtf32 + uInputLen, - &pUtf8, pUtf8 + a_uOutputDataSize, - lenientConversion); - } - else if (sizeof(wchar_t) == sizeof(UTF16)) { - const UTF16 * pUtf16 = (const UTF16 *) a_pInputData; - retval = ConvertUTF16toUTF8( - &pUtf16, pUtf16 + uInputLen, - &pUtf8, pUtf8 + a_uOutputDataSize, - lenientConversion); - } - return retval == conversionOK; - } - else { - size_t retval = wcstombs(a_pOutputData, - a_pInputData, a_uOutputDataSize); - return retval != (size_t) -1; - } - } -}; - -#endif // SI_CONVERT_GENERIC - - -// --------------------------------------------------------------------------- -// SI_CONVERT_ICU -// --------------------------------------------------------------------------- -#ifdef SI_CONVERT_ICU - -#define SI_Case SI_GenericCase -#define SI_NoCase SI_GenericNoCase - -#include - -/** - * Converts MBCS/UTF-8 to UChar using ICU. This can be used on all platforms. - */ -template -class SI_ConvertW { - const char * m_pEncoding; - UConverter * m_pConverter; -protected: - SI_ConvertW() : m_pEncoding(NULL), m_pConverter(NULL) { } -public: - SI_ConvertW(bool a_bStoreIsUtf8) : m_pConverter(NULL) { - m_pEncoding = a_bStoreIsUtf8 ? "UTF-8" : NULL; - } - - /* copy and assignment */ - SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); } - SI_ConvertW & operator=(const SI_ConvertW & rhs) { - m_pEncoding = rhs.m_pEncoding; - m_pConverter = NULL; - return *this; - } - ~SI_ConvertW() { if (m_pConverter) ucnv_close(m_pConverter); } - - /** Calculate the number of UChar required for converting the input - * from the storage format. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to UChar. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @return Number of UChar required by the string when - * converted. If there are embedded NULL bytes in the - * input data, only the string up and not including - * the NULL byte will be converted. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeFromStore( - const char * a_pInputData, - size_t a_uInputDataLen) - { - SI_ASSERT(a_uInputDataLen != (size_t) -1); - - UErrorCode nError; - - if (!m_pConverter) { - nError = U_ZERO_ERROR; - m_pConverter = ucnv_open(m_pEncoding, &nError); - if (U_FAILURE(nError)) { - return (size_t) -1; - } - } - - nError = U_ZERO_ERROR; - int32_t nLen = ucnv_toUChars(m_pConverter, NULL, 0, - a_pInputData, (int32_t) a_uInputDataLen, &nError); - if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) { - return (size_t) -1; - } - - return (size_t) nLen; - } - - /** Convert the input string from the storage format to UChar. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to UChar. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @param a_pOutputData Pointer to the output buffer to received the - * converted data. - * @param a_uOutputDataSize Size of the output buffer in UChar. - * @return true if all of the input data was successfully - * converted. - */ - bool ConvertFromStore( - const char * a_pInputData, - size_t a_uInputDataLen, - UChar * a_pOutputData, - size_t a_uOutputDataSize) - { - UErrorCode nError; - - if (!m_pConverter) { - nError = U_ZERO_ERROR; - m_pConverter = ucnv_open(m_pEncoding, &nError); - if (U_FAILURE(nError)) { - return false; - } - } - - nError = U_ZERO_ERROR; - ucnv_toUChars(m_pConverter, - a_pOutputData, (int32_t) a_uOutputDataSize, - a_pInputData, (int32_t) a_uInputDataLen, &nError); - if (U_FAILURE(nError)) { - return false; - } - - return true; - } - - /** Calculate the number of char required by the storage format of this - * data. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated string to calculate the number of - * bytes required to be converted to storage format. - * @return Number of bytes required by the string when - * converted to storage format. This size always - * includes space for the terminating NULL character. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeToStore( - const UChar * a_pInputData) - { - UErrorCode nError; - - if (!m_pConverter) { - nError = U_ZERO_ERROR; - m_pConverter = ucnv_open(m_pEncoding, &nError); - if (U_FAILURE(nError)) { - return (size_t) -1; - } - } - - nError = U_ZERO_ERROR; - int32_t nLen = ucnv_fromUChars(m_pConverter, NULL, 0, - a_pInputData, -1, &nError); - if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) { - return (size_t) -1; - } - - return (size_t) nLen + 1; - } - - /** Convert the input string to the storage format of this data. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated source string to convert. All of - * the data will be converted including the - * terminating NULL character. - * @param a_pOutputData Pointer to the buffer to receive the converted - * string. - * @param a_pOutputDataSize Size of the output buffer in char. - * @return true if all of the input data, including the - * terminating NULL character was successfully - * converted. - */ - bool ConvertToStore( - const UChar * a_pInputData, - char * a_pOutputData, - size_t a_uOutputDataSize) - { - UErrorCode nError; - - if (!m_pConverter) { - nError = U_ZERO_ERROR; - m_pConverter = ucnv_open(m_pEncoding, &nError); - if (U_FAILURE(nError)) { - return false; - } - } - - nError = U_ZERO_ERROR; - ucnv_fromUChars(m_pConverter, - a_pOutputData, (int32_t) a_uOutputDataSize, - a_pInputData, -1, &nError); - if (U_FAILURE(nError)) { - return false; - } - - return true; - } -}; - -#endif // SI_CONVERT_ICU - - -// --------------------------------------------------------------------------- -// SI_CONVERT_WIN32 -// --------------------------------------------------------------------------- -#ifdef SI_CONVERT_WIN32 - -#define SI_Case SI_GenericCase - -// Windows CE doesn't have errno or MBCS libraries -#ifdef _WIN32_WCE -# ifndef SI_NO_MBCS -# define SI_NO_MBCS -# endif -#endif - -#ifdef SI_NO_MBCS -# define SI_NoCase SI_GenericNoCase -#else // !SI_NO_MBCS -/** - * Case-insensitive comparison class using Win32 MBCS functions. This class - * returns a case-insensitive semi-collation order for MBCS text. It may not - * be safe for UTF-8 text returned in char format as we don't know what - * characters will be folded by the function! Therefore, if you are using - * SI_CHAR == char and SetUnicode(true), then you need to use the generic - * SI_NoCase class instead. - */ -#include -template -struct SI_NoCase { - bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const { - auto tsize = sizeof(SI_CHAR); - if (tsize == sizeof(char)) { - return _mbsicmp((const unsigned char *)pLeft, - (const unsigned char *)pRight) < 0; - } - if (tsize == sizeof(wchar_t)) { - return _wcsicmp((const wchar_t *)pLeft, - (const wchar_t *)pRight) < 0; - } - return SI_GenericNoCase()(pLeft, pRight); - } -}; -#endif // SI_NO_MBCS - -/** - * Converts MBCS and UTF-8 to a wchar_t (or equivalent) on Windows. This uses - * only the Win32 functions and doesn't require the external Unicode UTF-8 - * conversion library. It will not work on Windows 95 without using Microsoft - * Layer for Unicode in your application. - */ -template -class SI_ConvertW { - unsigned int m_uCodePage; -protected: - SI_ConvertW() : m_uCodePage(0) { } -public: - SI_ConvertW(bool a_bStoreIsUtf8) { - m_uCodePage = a_bStoreIsUtf8 ? CP_UTF8 : CP_ACP; - } - - /* copy and assignment */ - SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); } - SI_ConvertW & operator=(const SI_ConvertW & rhs) { - m_uCodePage = rhs.m_uCodePage; - return *this; - } - - /** Calculate the number of SI_CHAR required for converting the input - * from the storage format. The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to SI_CHAR. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @return Number of SI_CHAR required by the string when - * converted. If there are embedded NULL bytes in the - * input data, only the string up and not including - * the NULL byte will be converted. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeFromStore( - const char * a_pInputData, - size_t a_uInputDataLen) - { - SI_ASSERT(a_uInputDataLen != (size_t) -1); - - int retval = MultiByteToWideChar( - m_uCodePage, 0, - a_pInputData, (int) a_uInputDataLen, - 0, 0); - return (size_t)(retval > 0 ? retval : -1); - } - - /** Convert the input string from the storage format to SI_CHAR. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData Data in storage format to be converted to SI_CHAR. - * @param a_uInputDataLen Length of storage format data in bytes. This - * must be the actual length of the data, including - * NULL byte if NULL terminated string is required. - * @param a_pOutputData Pointer to the output buffer to received the - * converted data. - * @param a_uOutputDataSize Size of the output buffer in SI_CHAR. - * @return true if all of the input data was successfully - * converted. - */ - bool ConvertFromStore( - const char * a_pInputData, - size_t a_uInputDataLen, - SI_CHAR * a_pOutputData, - size_t a_uOutputDataSize) - { - int nSize = MultiByteToWideChar( - m_uCodePage, 0, - a_pInputData, (int) a_uInputDataLen, - (wchar_t *) a_pOutputData, (int) a_uOutputDataSize); - return (nSize > 0); - } - - /** Calculate the number of char required by the storage format of this - * data. The storage format is always UTF-8. - * - * @param a_pInputData NULL terminated string to calculate the number of - * bytes required to be converted to storage format. - * @return Number of bytes required by the string when - * converted to storage format. This size always - * includes space for the terminating NULL character. - * @return -1 cast to size_t on a conversion error. - */ - size_t SizeToStore( - const SI_CHAR * a_pInputData) - { - int retval = WideCharToMultiByte( - m_uCodePage, 0, - (const wchar_t *) a_pInputData, -1, - 0, 0, 0, 0); - return (size_t) (retval > 0 ? retval : -1); - } - - /** Convert the input string to the storage format of this data. - * The storage format is always UTF-8 or MBCS. - * - * @param a_pInputData NULL terminated source string to convert. All of - * the data will be converted including the - * terminating NULL character. - * @param a_pOutputData Pointer to the buffer to receive the converted - * string. - * @param a_pOutputDataSize Size of the output buffer in char. - * @return true if all of the input data, including the - * terminating NULL character was successfully - * converted. - */ - bool ConvertToStore( - const SI_CHAR * a_pInputData, - char * a_pOutputData, - size_t a_uOutputDataSize) - { - int retval = WideCharToMultiByte( - m_uCodePage, 0, - (const wchar_t *) a_pInputData, -1, - a_pOutputData, (int) a_uOutputDataSize, 0, 0); - return retval > 0; - } -}; - -#endif // SI_CONVERT_WIN32 - - -// --------------------------------------------------------------------------- -// TYPE DEFINITIONS -// --------------------------------------------------------------------------- - -typedef CSimpleIniTempl,SI_ConvertA > CSimpleIniA; -typedef CSimpleIniTempl,SI_ConvertA > CSimpleIniCaseA; - -#if defined(SI_CONVERT_ICU) -typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniW; -typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniCaseW; -#else -typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniW; -typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniCaseW; -#endif - -#ifdef _UNICODE -# define CSimpleIni CSimpleIniW -# define CSimpleIniCase CSimpleIniCaseW -# define SI_NEWLINE SI_NEWLINE_W -#else // !_UNICODE -# define CSimpleIni CSimpleIniA -# define CSimpleIniCase CSimpleIniCaseA -# define SI_NEWLINE SI_NEWLINE_A -#endif // _UNICODE - -#ifdef _MSC_VER -# pragma warning (pop) -#endif - -#endif // INCLUDED_SimpleIni_h - diff --git a/ecal/core/src/simpleini_template.cpp b/ecal/core/src/simpleini_template.cpp deleted file mode 100644 index 698206f257..0000000000 --- a/ecal/core/src/simpleini_template.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* ========================= eCAL LICENSE ================================= - * - * Copyright (C) 2016 - 2019 Continental Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ========================= eCAL LICENSE ================================= -*/ - -#include - -/* -* This file provides two methods to access the Simple Inifile Setter/Getters on a template basis. -*/ -template<> -bool GetValue(const CSimpleIni& ini_, const std::string& section_, const std::string& key_, const bool& default_) -{ - return ini_.GetBoolValue(section_.c_str(), key_.c_str(), default_); -} - -template<> -SI_Error SetValue(CSimpleIni& ini_, const std::string& section_, const std::string& key_, const bool& value_, const std::string& comment_, bool force_overwrite_) -{ - return ini_.SetBoolValue(section_.c_str(), key_.c_str(), value_, comment_.c_str(), force_overwrite_); -} - -template<> -std::string GetValue(const CSimpleIni& ini_, const std::string& section_, const std::string& key_, const std::string& default_) -{ - return std::string(ini_.GetValue(section_.c_str(), key_.c_str(), default_.c_str())); -} - -template<> -SI_Error SetValue(CSimpleIni& ini_, const std::string& section_, const std::string& key_, const std::string& value_, const std::string& comment_, bool force_overwrite_) -{ - return ini_.SetValue(section_.c_str(), key_.c_str(), value_.c_str(), comment_.c_str(), force_overwrite_); -} - -template<> -double GetValue(const CSimpleIni& ini_, const std::string& section_, const std::string& key_, const double& default_) -{ - return ini_.GetDoubleValue(section_.c_str(), key_.c_str(), default_); -} - -template<> -SI_Error SetValue(CSimpleIni& ini_, const std::string& section_, const std::string& key_, const double& value_, const std::string& comment_, bool force_overwrite_) -{ - return ini_.SetDoubleValue(section_.c_str(), key_.c_str(), value_, comment_.c_str(), force_overwrite_); -} - -template<> -float GetValue(const CSimpleIni& ini_, const std::string& section_, const std::string& key_, const float& default_) -{ - return static_cast(ini_.GetDoubleValue(section_.c_str(), key_.c_str(), default_)); -} - -template<> -SI_Error SetValue(CSimpleIni& ini_, const std::string& section_, const std::string& key_, const float& value_, const std::string& comment_, bool force_overwrite_) -{ - return ini_.SetDoubleValue(section_.c_str(), key_.c_str(), value_, comment_.c_str(), force_overwrite_); -} - -template<> -int32_t GetValue(const CSimpleIni& ini_, const std::string& section_, const std::string& key_, const int32_t& default_) -{ - return ini_.GetLongValue(section_.c_str(), key_.c_str(), default_); -} - -template<> -SI_Error SetValue(CSimpleIni& ini_, const std::string& section_, const std::string& key_, const int32_t& value_, const std::string& comment_, bool force_overwrite_) -{ - return ini_.SetLongValue(section_.c_str(), key_.c_str(), value_, comment_.c_str(), force_overwrite_); -} \ No newline at end of file diff --git a/lib/CustomQt/src/QStableSortFilterProxyModel.cpp b/lib/CustomQt/src/QStableSortFilterProxyModel.cpp index 75a1a7f3aa..fcf6934753 100644 --- a/lib/CustomQt/src/QStableSortFilterProxyModel.cpp +++ b/lib/CustomQt/src/QStableSortFilterProxyModel.cpp @@ -107,9 +107,9 @@ void QStableSortFilterProxyModel::setSourceModel(QAbstractItemModel *source_mode QAbstractItemModel* old_source = sourceModel(); if (old_source) { - disconnect(source_model, &QAbstractItemModel::rowsInserted, this, &QStableSortFilterProxyModel::invalidateFilterIfRecursiveFilteringEnabled); - disconnect(source_model, &QAbstractItemModel::rowsRemoved, this, &QStableSortFilterProxyModel::invalidateFilterIfRecursiveFilteringEnabled); - disconnect(source_model, &QAbstractItemModel::dataChanged, this, &QStableSortFilterProxyModel::invalidateFilterIfRecursiveFilteringEnabled); + disconnect(old_source, &QAbstractItemModel::rowsInserted, this, &QStableSortFilterProxyModel::invalidateFilterIfRecursiveFilteringEnabled); + disconnect(old_source, &QAbstractItemModel::rowsRemoved, this, &QStableSortFilterProxyModel::invalidateFilterIfRecursiveFilteringEnabled); + disconnect(old_source, &QAbstractItemModel::dataChanged, this, &QStableSortFilterProxyModel::invalidateFilterIfRecursiveFilteringEnabled); } QSortFilterProxyModel::setSourceModel(source_model); diff --git a/lib/EcalUtils/include/EcalUtils/EcalUtils.h b/lib/EcalUtils/include/EcalUtils/EcalUtils.h index 3257e8d233..c2cb61e8ed 100644 --- a/lib/EcalUtils/include/EcalUtils/EcalUtils.h +++ b/lib/EcalUtils/include/EcalUtils/EcalUtils.h @@ -499,6 +499,11 @@ namespace EcalUtils output += input[++n]; break; } + else + { + output += input[n]; + } + break; default: output += input[n]; diff --git a/lib/Udpcap/include/udpcap/npcap_helpers.h b/lib/Udpcap/include/udpcap/npcap_helpers.h index 9ab087a198..90100fb733 100644 --- a/lib/Udpcap/include/udpcap/npcap_helpers.h +++ b/lib/Udpcap/include/udpcap/npcap_helpers.h @@ -72,4 +72,15 @@ namespace Udpcap * @return True, if the device matches the NPCAP loopback device. */ bool IsLoopbackDevice(const std::string& device_name_or_uuid_string); + + /** + * @brief Returns a human readible status message. + * + * This message is intended to be displayed in a graphical user interface. + * For terminal based applications it is not needed, as the messages are also + * printed to stderr. + * + * @return The Udpcap status as human-readible text (may be multi-line) + */ + std::string GetHumanReadibleErrorText(); } diff --git a/lib/Udpcap/include/udpcap/udpcap_socket.h b/lib/Udpcap/include/udpcap/udpcap_socket.h index b88262360b..0d5cdc297e 100644 --- a/lib/Udpcap/include/udpcap/udpcap_socket.h +++ b/lib/Udpcap/include/udpcap/udpcap_socket.h @@ -34,6 +34,7 @@ namespace Udpcap { class UdpcapSocketPrivate; + /** * @brief The UdpcapSocket is a (receive-only) UDP Socket implementation using Npcap. * diff --git a/lib/Udpcap/src/npcap_helpers.cpp b/lib/Udpcap/src/npcap_helpers.cpp index 83b8d0fbb5..c2bc80e615 100644 --- a/lib/Udpcap/src/npcap_helpers.cpp +++ b/lib/Udpcap/src/npcap_helpers.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -32,6 +33,8 @@ #define WIN32_LEAN_AND_MEAN #include +#include + namespace Udpcap { namespace // Private Namespace @@ -42,17 +45,21 @@ namespace Udpcap static std::string loopback_device_uuid_string; static bool loopback_device_name_initialized(false); + static std::string human_readible_error_("Npcap has not been initialized, yet"); + bool LoadNpcapDlls() { _TCHAR npcap_dir[512]; UINT len; len = GetSystemDirectory(npcap_dir, 480); if (!len) { + human_readible_error_ = "Error in GetSystemDirectory"; fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError()); return false; } _tcscat_s(npcap_dir, 512, _T("\\Npcap")); if (SetDllDirectory(npcap_dir) == 0) { + human_readible_error_ = "Error in SetDllDirectory"; fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError()); return false; } @@ -111,7 +118,8 @@ namespace Udpcap LONG error_code = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\npcap\\Parameters", 0, KEY_READ, &hkey); if (error_code) { - std::cerr << "Udpcap ERROR: NPCAP doesn't seem to be installed. Please download and install Npcap from https://nmap.org/npcap/#download" << std::endl; + human_readible_error_ = "NPCAP doesn't seem to be installed. Please download and install Npcap from https://nmap.org/npcap/#download"; + std::cerr << "Udpcap ERROR: " << human_readible_error_ << std::endl; return false; } @@ -120,7 +128,8 @@ namespace Udpcap if (!loopback_supported) { - std::cerr << "Udpcap ERROR: NPCAP was installed without loopback support. Please re-install NPCAP" << std::endl; + human_readible_error_ = "NPCAP was installed without loopback support. Please re-install NPCAP"; + std::cerr << "Udpcap ERROR: " << human_readible_error_ << std::endl; RegCloseKey(hkey); return false; } @@ -130,11 +139,18 @@ namespace Udpcap if (loopback_device_name_w.empty()) { - std::cerr << "Udpcap ERROR: unable to retrieve NPCAP Loopback adapter name. Please reinstall Npcap:" << std::endl; - std::cerr << " 1) Uninstall Npcap" << std::endl; - std::cerr << " 2) Uninstall all \"Npcap Loopback Adapters\" from the device manager" << std::endl; - std::cerr << " 3) Uninstall all \"Microsoft KM_TEST Loopback Adapters\" from the device manager" << std::endl; - std::cerr << " 4) Install Npcap again" << std::endl; + std::stringstream error_ss; + + error_ss << "Unable to retrieve NPCAP Loopback adapter name. Please reinstall Npcap:" << std::endl; + error_ss << " 1) Uninstall Npcap" << std::endl; + error_ss << " 2) Uninstall all \"Npcap Loopback Adapters\" from the device manager" << std::endl; + error_ss << " 3) Uninstall all \"Microsoft KM_TEST Loopback Adapters\" from the device manager" << std::endl; + error_ss << " 4) Install Npcap again"; + + human_readible_error_ = error_ss.str(); + + std::cerr << "Udpcap ERROR: " << human_readible_error_ << std::endl; + RegCloseKey(hkey); return false; } @@ -152,6 +168,97 @@ namespace Udpcap return true; } + + bool IsLoopbackDevice_NoLock(const std::string& device_name_or_uuid_string) + { + if (!loopback_device_name_initialized) + { + loopback_device_name_initialized = LoadLoopbackDeviceNameFromRegistry(); + } + + if (!loopback_device_name_initialized) + { + return false; + } + else + { + // Extract the UUID from the input, as it might be an entire device path + size_t open_bracket_pos = device_name_or_uuid_string.find('{'); + size_t closing_bracket_pos = device_name_or_uuid_string.find('}'); + + std::string given_uuid; + + if ((open_bracket_pos == std::string::npos) || (closing_bracket_pos == std::string::npos)) + { + given_uuid = device_name_or_uuid_string; + } + else + { + given_uuid = device_name_or_uuid_string.substr(open_bracket_pos + 1, closing_bracket_pos - open_bracket_pos - 1); + } + + // Lower-case everything + //std::transform(given_uuid.begin(), given_uuid.end(), given_uuid.begin(), ::tolower); // cause warning C4244 with VS2017 + std::transform(given_uuid.begin(), given_uuid.end(), given_uuid.begin(), + [](char c) {return static_cast(::tolower(c)); }); + std::string loopback_uuid_lower = loopback_device_uuid_string; + //std::transform(loopback_uuid_lower.begin(), loopback_uuid_lower.end(), loopback_uuid_lower.begin(), ::tolower); // cause warning C4244 with VS2017 + std::transform(loopback_uuid_lower.begin(), loopback_uuid_lower.end(), loopback_uuid_lower.begin(), + [](char c) {return static_cast(::tolower(c)); }); + + // String compare + return (loopback_uuid_lower == given_uuid); + } + } + + bool TestLoopbackDevice() + { + typedef std::unique_ptr pcap_if_t_uniqueptr; + + char errbuf[PCAP_ERRBUF_SIZE]; + pcap_if_t* alldevs_rawptr; + pcap_if_t_uniqueptr alldevs(&alldevs_rawptr, [](pcap_if_t** p) { pcap_freealldevs(*p); }); + + bool loopback_device_found = false; + + if (pcap_findalldevs(alldevs.get(), errbuf) == -1) + { + human_readible_error_ = "Error in pcap_findalldevs: " + std::string(errbuf); + fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf); + return false; + } + + // Check if the loopback device is accessible + for (pcap_if_t* pcap_dev = *alldevs.get(); pcap_dev; pcap_dev = pcap_dev->next) + { + if (IsLoopbackDevice_NoLock(pcap_dev->name)) + { + loopback_device_found = true; + } + } + + // if we didn't find the loopback device, the test has failed + if (!loopback_device_found) + { + std::stringstream error_ss; + + error_ss << "Udpcap ERROR: Loopback adapter is inaccessible. On some systems the Npcap driver fails to start properly. Please open a command prompt with administrative privileges and run the following commands:" << std::endl; + error_ss << " When npcap was installed in normal mode:" << std::endl; + error_ss << " > sc stop npcap" << std::endl; + error_ss << " > sc start npcap" << std::endl; + error_ss << " When npcap was installed in WinPcap compatible mode:" << std::endl; + error_ss << " > sc stop npf" << std::endl; + error_ss << " > sc start npf"; + + human_readible_error_ = error_ss.str(); + + std::cerr << "Udpcap ERROR: " << human_readible_error_ << std::endl; + + return false; + } + + return true; + } } bool Initialize() @@ -160,6 +267,8 @@ namespace Udpcap if (is_initialized) return true; + human_readible_error_ = "Unknown error"; + std::cout << "Udpcap: Initializing Npcap..." << std::endl; if (!LoadLoopbackDeviceNameFromRegistry()) @@ -175,7 +284,13 @@ namespace Udpcap return false; } - std::cout << "Udpcap: Npcap is ready" << std::endl; + if (!TestLoopbackDevice()) + { + return false; + } + + human_readible_error_ = "Npcap is ready"; + std::cout << "Udpcap: " << human_readible_error_ << std::endl; is_initialized = true; return true; @@ -213,44 +328,12 @@ namespace Udpcap bool IsLoopbackDevice(const std::string& device_name_or_uuid_string) { std::lock_guard npcap_lock(npcap_mutex); + return IsLoopbackDevice_NoLock(device_name_or_uuid_string); + } - if (!loopback_device_name_initialized) - { - loopback_device_name_initialized = LoadLoopbackDeviceNameFromRegistry(); - } - - if (!loopback_device_name_initialized) - { - return false; - } - else - { - // Extract the UUID from the input, as it might be an entire device path - size_t open_bracket_pos = device_name_or_uuid_string.find('{'); - size_t closing_bracket_pos = device_name_or_uuid_string.find('}'); - - std::string given_uuid; - - if ((open_bracket_pos == std::string::npos) || (closing_bracket_pos == std::string::npos)) - { - given_uuid = device_name_or_uuid_string; - } - else - { - given_uuid = device_name_or_uuid_string.substr(open_bracket_pos + 1, closing_bracket_pos - open_bracket_pos - 1); - } - - // Lower-case everything - //std::transform(given_uuid.begin(), given_uuid.end(), given_uuid.begin(), ::tolower); // cause warning C4244 with VS2017 - std::transform(given_uuid.begin(), given_uuid.end(), given_uuid.begin(), - [](char c) {return static_cast(::tolower(c)); }); - std::string loopback_uuid_lower = loopback_device_uuid_string; - //std::transform(loopback_uuid_lower.begin(), loopback_uuid_lower.end(), loopback_uuid_lower.begin(), ::tolower); // cause warning C4244 with VS2017 - std::transform(loopback_uuid_lower.begin(), loopback_uuid_lower.end(), loopback_uuid_lower.begin(), - [](char c) {return static_cast(::tolower(c)); }); - - // String compare - return (loopback_uuid_lower == given_uuid); - } + std::string GetHumanReadibleErrorText() + { + std::lock_guard npcap_lock(npcap_mutex); + return human_readible_error_; } } \ No newline at end of file diff --git a/lib/Udpcap/src/udpcap_socket_private.cpp b/lib/Udpcap/src/udpcap_socket_private.cpp index cdc2f995bc..e4d60a2526 100644 --- a/lib/Udpcap/src/udpcap_socket_private.cpp +++ b/lib/Udpcap/src/udpcap_socket_private.cpp @@ -33,16 +33,6 @@ #include -#ifdef _MSC_VER -#pragma warning( push ) -#pragma warning( disable: 4200 ) -#endif // _MSC_VER -#include // Pcap++ IPv4 -#include // Pcap++ UDP -#ifdef _MSC_VER -#pragma warning( pop ) -#endif // _MSC_VER - namespace Udpcap { ////////////////////////////////////////// @@ -290,31 +280,57 @@ namespace Udpcap num_handles = MAXIMUM_WAIT_OBJECTS; } - DWORD wait_result = WaitForMultipleObjects(num_handles, &pcap_win32_handles_[0], false, timeout_ms); + bool wait_forever = (timeout_ms == INFINITE); + auto wait_until = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); + + std::vector datagram; + CallbackArgsVector callback_args(&datagram, source_address, source_port, bound_port_, pcpp::LinkLayerType::LINKTYPE_NULL, &ip_reassembly_); - if ((wait_result >= WAIT_OBJECT_0) && wait_result <= (WAIT_OBJECT_0 + num_handles - 1)) + do { - int dev_index = (wait_result - WAIT_OBJECT_0); + unsigned long remaining_time_to_wait_ms = 0; + if (wait_forever) + { + remaining_time_to_wait_ms = INFINITE; + } + else + { + auto now = std::chrono::steady_clock::now(); + if (now < wait_until) + { + remaining_time_to_wait_ms = static_cast(std::chrono::duration_cast(wait_until - now).count()); + } + } - std::vector datagram; - CallbackArgsVector callback_args(&datagram, source_address, source_port, static_cast(pcap_datalink(pcap_devices_[dev_index].pcap_handle_))); + DWORD wait_result = WaitForMultipleObjects(num_handles, &pcap_win32_handles_[0], false, remaining_time_to_wait_ms); - pcap_dispatch(pcap_devices_[dev_index].pcap_handle_, 1, UdpcapSocketPrivate::PacketHandlerVector, reinterpret_cast(&callback_args)); + if ((wait_result >= WAIT_OBJECT_0) && wait_result <= (WAIT_OBJECT_0 + num_handles - 1)) + { + int dev_index = (wait_result - WAIT_OBJECT_0); + + callback_args.link_type_ = static_cast(pcap_datalink(pcap_devices_[dev_index].pcap_handle_)); - return datagram; - } - else if ((wait_result >= WAIT_ABANDONED_0) && wait_result <= (WAIT_ABANDONED_0 + num_handles - 1)) - { - LOG_DEBUG("Receive error: WAIT_ABANDONED"); - } - else if (wait_result == WAIT_TIMEOUT) - { - // LOG_DEBUG("Receive error: WAIT_TIMEOUT"); - } - else if (wait_result == WAIT_FAILED) - { - LOG_DEBUG("Receive error: WAIT_FAILED: " + std::to_string(GetLastError())); - } + pcap_dispatch(pcap_devices_[dev_index].pcap_handle_, 1, UdpcapSocketPrivate::PacketHandlerVector, reinterpret_cast(&callback_args)); + + if (callback_args.success_) + { + // Only return datagram if we successfully received a packet. Otherwise, we will continue receiving data, if there is time left. + return datagram; + } + } + else if ((wait_result >= WAIT_ABANDONED_0) && wait_result <= (WAIT_ABANDONED_0 + num_handles - 1)) + { + LOG_DEBUG("Receive error: WAIT_ABANDONED"); + } + else if (wait_result == WAIT_TIMEOUT) + { + // LOG_DEBUG("Receive error: WAIT_TIMEOUT"); + } + else if (wait_result == WAIT_FAILED) + { + LOG_DEBUG("Receive error: WAIT_FAILED: " + std::to_string(GetLastError())); + } + } while (wait_forever || (std::chrono::steady_clock::now() < wait_until)); return{}; } @@ -354,32 +370,58 @@ namespace Udpcap num_handles = MAXIMUM_WAIT_OBJECTS; } - DWORD wait_result = WaitForMultipleObjects(num_handles, &pcap_win32_handles_[0], false, timeout_ms); + bool wait_forever = (timeout_ms == INFINITE); + auto wait_until = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); + + CallbackArgsRawPtr callback_args(data, max_len, source_address, source_port, bound_port_, pcpp::LinkLayerType::LINKTYPE_NULL, &ip_reassembly_); - if ((wait_result >= WAIT_OBJECT_0) && wait_result <= (WAIT_OBJECT_0 + num_handles - 1)) + do { - int dev_index = (wait_result - WAIT_OBJECT_0); + unsigned long remaining_time_to_wait_ms = 0; + if (wait_forever) + { + remaining_time_to_wait_ms = INFINITE; + } + else + { + auto now = std::chrono::steady_clock::now(); + if (now < wait_until) + { + remaining_time_to_wait_ms = static_cast(std::chrono::duration_cast(wait_until - now).count()); + } + } - CallbackArgsRawPtr callback_args(data, max_len, source_address, source_port, static_cast(pcap_datalink(pcap_devices_[dev_index].pcap_handle_))); + DWORD wait_result = WaitForMultipleObjects(num_handles, &pcap_win32_handles_[0], false, remaining_time_to_wait_ms); - pcap_dispatch(pcap_devices_[dev_index].pcap_handle_, 1, UdpcapSocketPrivate::PacketHandlerRawPtr, reinterpret_cast(&callback_args)); + if ((wait_result >= WAIT_OBJECT_0) && wait_result <= (WAIT_OBJECT_0 + num_handles - 1)) + { + int dev_index = (wait_result - WAIT_OBJECT_0); - return callback_args.bytes_copied_; - } - else if ((wait_result >= WAIT_ABANDONED_0) && wait_result <= (WAIT_ABANDONED_0 + num_handles - 1)) - { - LOG_DEBUG("Receive error: WAIT_ABANDONED"); - } - else if (wait_result == WAIT_TIMEOUT) - { - // LOG_DEBUG("Receive error: WAIT_TIMEOUT"); - } - else if (wait_result == WAIT_FAILED) - { - LOG_DEBUG("Receive error: WAIT_FAILED: " + std::to_string(GetLastError())); - } + callback_args.link_type_ = static_cast(pcap_datalink(pcap_devices_[dev_index].pcap_handle_)); - return{}; + pcap_dispatch(pcap_devices_[dev_index].pcap_handle_, 1, UdpcapSocketPrivate::PacketHandlerRawPtr, reinterpret_cast(&callback_args)); + + if (callback_args.success_) + { + // Only return datagram if we successfully received a packet. Otherwise, we will continue receiving data, if there is time left. + return callback_args.bytes_copied_; + } + } + else if ((wait_result >= WAIT_ABANDONED_0) && wait_result <= (WAIT_ABANDONED_0 + num_handles - 1)) + { + LOG_DEBUG("Receive error: WAIT_ABANDONED"); + } + else if (wait_result == WAIT_TIMEOUT) + { + // LOG_DEBUG("Receive error: WAIT_TIMEOUT"); + } + else if (wait_result == WAIT_FAILED) + { + LOG_DEBUG("Receive error: WAIT_FAILED: " + std::to_string(GetLastError())); + } + } while (wait_forever || (std::chrono::steady_clock::now() < wait_until)); + + return 0; } @@ -683,8 +725,8 @@ namespace Udpcap // IP traffic having UDP payload ss << "ip and udp"; - // Port - ss << " and port " << bound_port_; + // UDP Port or IPv4 fragmented traffic (in IP fragments we cannot see the UDP port, yet) + ss << " and (udp port " << bound_port_ << " or (ip[6:2] & 0x3fff != 0))"; // IP // Unicast traffic @@ -804,23 +846,57 @@ namespace Udpcap pcpp::RawPacket rawPacket(pkt_data, header->caplen, header->ts, false, callback_args->link_type_); pcpp::Packet packet(&rawPacket, pcpp::ProtocolType::UDP); - if (callback_args->source_address_) + pcpp::IPv4Layer* ip_layer = packet.getLayerOfType(); + pcpp::UdpLayer* udp_layer = packet.getLayerOfType(); + + if (ip_layer) { - pcpp::IPv4Layer* ip_layer = packet.getLayerOfType(); - if (ip_layer) + if (ip_layer->isFragment()) { - *callback_args->source_address_ = HostAddress(ip_layer->getSrcIpAddress().toInt()); + // Handle fragmented IP traffic + pcpp::IPReassembly::ReassemblyStatus status; + + // Try to reasseble packet + pcpp::Packet* reassembled_packet = callback_args->ip_reassembly_->processPacket(&rawPacket, status); + + // If we are done reassembling the packet, we return it to the user + if (reassembled_packet) + { + pcpp::Packet re_parsed_packet(reassembled_packet->getRawPacket(), pcpp::ProtocolType::UDP); + + pcpp::IPv4Layer* reassembled_ip_layer = re_parsed_packet.getLayerOfType(); + pcpp::UdpLayer* reassembled_udp_layer = re_parsed_packet.getLayerOfType(); + + if (reassembled_ip_layer && reassembled_udp_layer) + FillCallbackArgsVector(callback_args, reassembled_ip_layer, reassembled_udp_layer); + + free(reassembled_packet); // We need to manually free the packet pointer + } + } + else if (udp_layer) + { + // Handle normal IP traffic (un-fragmented) + FillCallbackArgsVector(callback_args, ip_layer, udp_layer); } } + } - if (callback_args->source_port_) + void UdpcapSocketPrivate::FillCallbackArgsVector(CallbackArgsVector* callback_args, pcpp::IPv4Layer* ip_layer, pcpp::UdpLayer* udp_layer) + { + auto dst_port = ntohs(udp_layer->getUdpHeader()->portDst); + + if (dst_port == callback_args->bound_port_) { - *callback_args->source_port_ = static_cast(packet.getLastLayer())->getUdpHeader()->portSrc; - } + if (callback_args->source_address_) + *callback_args->source_address_ = HostAddress(ip_layer->getSrcIpAddress().toInt()); + + if (callback_args->source_port_) + *callback_args->source_port_ = ntohs(udp_layer->getUdpHeader()->portSrc); - auto packet_lastlayer = packet.getLastLayer(); - callback_args->destination_vector_->reserve(packet_lastlayer->getLayerPayloadSize()); - callback_args->destination_vector_->assign(packet_lastlayer->getLayerPayload(), packet_lastlayer->getLayerPayload() + packet_lastlayer->getLayerPayloadSize()); + callback_args->destination_vector_->reserve(udp_layer->getLayerPayloadSize()); + callback_args->destination_vector_->assign(udp_layer->getLayerPayload(), udp_layer->getLayerPayload() + udp_layer->getLayerPayloadSize()); + callback_args->success_ = true; + } } void UdpcapSocketPrivate::PacketHandlerRawPtr(unsigned char* param, const struct pcap_pkthdr* header, const unsigned char* pkt_data) @@ -830,23 +906,62 @@ namespace Udpcap pcpp::RawPacket rawPacket(pkt_data, header->caplen, header->ts, false, callback_args->link_type_); pcpp::Packet packet(&rawPacket, pcpp::ProtocolType::UDP); - if (callback_args->source_address_) + pcpp::IPv4Layer* ip_layer = packet.getLayerOfType(); + pcpp::UdpLayer* udp_layer = packet.getLayerOfType(); + + if (ip_layer) { - pcpp::IPv4Layer* ip_layer = packet.getLayerOfType(); - if (ip_layer) + if (ip_layer->isFragment()) { - *callback_args->source_address_ = HostAddress(ip_layer->getSrcIpAddress().toInt()); + // Handle fragmented IP traffic + pcpp::IPReassembly::ReassemblyStatus status; + + // Try to reasseble packet + pcpp::Packet* reassembled_packet = callback_args->ip_reassembly_->processPacket(&rawPacket, status); + + // If we are done reassembling the packet, we return it to the user + if (reassembled_packet) + { + pcpp::Packet re_parsed_packet(reassembled_packet->getRawPacket(), pcpp::ProtocolType::UDP); + + pcpp::IPv4Layer* reassembled_ip_layer = re_parsed_packet.getLayerOfType(); + pcpp::UdpLayer* reassembled_udp_layer = re_parsed_packet.getLayerOfType(); + + if (reassembled_ip_layer && reassembled_udp_layer) + FillCallbackArgsRawPtr(callback_args, reassembled_ip_layer, reassembled_udp_layer); + + free(reassembled_packet); // We need to manually free the packet pointer + } + } + else if (udp_layer) + { + // Handle normal IP traffic (un-fragmented) + FillCallbackArgsRawPtr(callback_args, ip_layer, udp_layer); } } - if (callback_args->source_port_) + } + + void UdpcapSocketPrivate::FillCallbackArgsRawPtr(CallbackArgsRawPtr* callback_args, pcpp::IPv4Layer* ip_layer, pcpp::UdpLayer* udp_layer) + { + auto dst_port = ntohs(udp_layer->getUdpHeader()->portDst); + + if (dst_port == callback_args->bound_port_) { - *callback_args->source_port_ = static_cast(packet.getLastLayer())->getUdpHeader()->portSrc; - } + if (callback_args->source_address_) + *callback_args->source_address_ = HostAddress(ip_layer->getSrcIpAddress().toInt()); - size_t bytes_to_copy = std::min(callback_args->destination_buffer_size_, packet.getLastLayer()->getLayerPayloadSize()); + if (callback_args->source_port_) + *callback_args->source_port_ = ntohs(udp_layer->getUdpHeader()->portSrc); + + + size_t bytes_to_copy = std::min(callback_args->destination_buffer_size_, udp_layer->getLayerPayloadSize()); + + memcpy_s(callback_args->destination_buffer_, callback_args->destination_buffer_size_, udp_layer->getLayerPayload(), bytes_to_copy); + callback_args->bytes_copied_ = bytes_to_copy; + + callback_args->success_ = true; + } - memcpy_s(callback_args->destination_buffer_, callback_args->destination_buffer_size_, packet.getLastLayer()->getLayerPayload(), bytes_to_copy); - callback_args->bytes_copied_ = bytes_to_copy; } } diff --git a/lib/Udpcap/src/udpcap_socket_private.h b/lib/Udpcap/src/udpcap_socket_private.h index 1a838fb2ae..4b2766e3db 100644 --- a/lib/Udpcap/src/udpcap_socket_private.h +++ b/lib/Udpcap/src/udpcap_socket_private.h @@ -24,6 +24,7 @@ #include #include #include +#include #define WIN32_LEAN_AND_MEAN #define NOMINMAX @@ -31,9 +32,12 @@ #ifdef _MSC_VER #pragma warning( push ) -#pragma warning( disable: 4800 ) +#pragma warning( disable: 4800 4200) #endif // _MSC_VER #include // Pcap++ +#include // Pcap++ IPv4 +#include // Pcap++ UDP +#include // Pcap++ de-fragmentation of IP packets #ifdef _MSC_VER #pragma warning( pop ) #endif // _MSC_VER @@ -58,34 +62,48 @@ namespace Udpcap struct CallbackArgsVector { - CallbackArgsVector(std::vector* destination_vector, HostAddress* source_address, uint16_t* source_port, pcpp::LinkLayerType link_type) + CallbackArgsVector(std::vector* destination_vector, HostAddress* source_address, uint16_t* source_port, uint16_t bound_port, pcpp::LinkLayerType link_type, pcpp::IPReassembly* ip_reassembly) : destination_vector_(destination_vector) , source_address_ (source_address) , source_port_ (source_port) + , success_ (false) , link_type_ (link_type) + , bound_port_ (bound_port) + , ip_reassembly_ (ip_reassembly) {} std::vector* const destination_vector_; HostAddress* const source_address_; uint16_t* const source_port_; - const pcpp::LinkLayerType link_type_; + bool success_; + + pcpp::LinkLayerType link_type_; + const uint16_t bound_port_; + pcpp::IPReassembly* const ip_reassembly_; }; struct CallbackArgsRawPtr { - CallbackArgsRawPtr(char* destination_buffer, size_t destination_buffer_size, HostAddress* source_address, uint16_t* source_port, pcpp::LinkLayerType link_type) + CallbackArgsRawPtr(char* destination_buffer, size_t destination_buffer_size, HostAddress* source_address, uint16_t* source_port, uint16_t bound_port, pcpp::LinkLayerType link_type, pcpp::IPReassembly* ip_reassembly) : destination_buffer_ (destination_buffer) , destination_buffer_size_(destination_buffer_size) , bytes_copied_ (0) , source_address_ (source_address) , source_port_ (source_port) + , success_ (false) , link_type_ (link_type) + , bound_port_ (bound_port) + , ip_reassembly_ (ip_reassembly) {} char* const destination_buffer_; const size_t destination_buffer_size_; size_t bytes_copied_; HostAddress* const source_address_; uint16_t* const source_port_; - const pcpp::LinkLayerType link_type_; + bool success_; + + pcpp::LinkLayerType link_type_; + const uint16_t bound_port_; + pcpp::IPReassembly* const ip_reassembly_; }; ////////////////////////////////////////// @@ -148,7 +166,10 @@ namespace Udpcap // Callbacks static void PacketHandlerVector(unsigned char* param, const struct pcap_pkthdr* header, const unsigned char* pkt_data); + static void FillCallbackArgsVector(CallbackArgsVector* callback_args, pcpp::IPv4Layer* ip_layer, pcpp::UdpLayer* udp_layer); + static void PacketHandlerRawPtr(unsigned char* param, const struct pcap_pkthdr* header, const unsigned char* pkt_data); + static void FillCallbackArgsRawPtr(CallbackArgsRawPtr* callback_args, pcpp::IPv4Layer* ip_layer, pcpp::UdpLayer* udp_layer); private: bool is_valid_; /**< If the socket is valid and ready to use (e.g. npcap was initialized successfully) */ @@ -163,6 +184,8 @@ namespace Udpcap std::vector pcap_devices_; std::vector pcap_win32_handles_; + pcpp::IPReassembly ip_reassembly_; + int receive_buffer_; }; } diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 0928b0ae29..3e0208325f 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -33,6 +33,8 @@ add_subdirectory(cpp/counter/counter_rec_cb/src) add_subdirectory(cpp/counter/counter_snd/src) add_subdirectory(cpp/event/event_rec/src) add_subdirectory(cpp/event/event_snd/src) +add_subdirectory(cpp/latency/latency_rec/src) +add_subdirectory(cpp/latency/latency_snd/src) add_subdirectory(cpp/minimal/minimal_rec/src) add_subdirectory(cpp/minimal/minimal_snd/src) add_subdirectory(cpp/misc/process/src) diff --git a/samples/cpp/capnp/addressbook_rec/src/addressbook_rec.cpp b/samples/cpp/capnp/addressbook_rec/src/addressbook_rec.cpp index dffcaecde6..1c29225daa 100644 --- a/samples/cpp/capnp/addressbook_rec/src/addressbook_rec.cpp +++ b/samples/cpp/capnp/addressbook_rec/src/addressbook_rec.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * Copyright (C) 2016 - 2018 Continental Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,13 +17,6 @@ * ========================= eCAL LICENSE ================================= */ -#include -#include - -#include -#include -#include - // capnp includes #ifdef _MSC_VER #pragma warning(push) @@ -34,10 +27,17 @@ #pragma warning(pop) #endif -void printAddressBook(capnp::MallocMessageBuilder& msg_) -{ - AddressBook::Reader addressBook = msg_.getRoot(); +#include +#include + +#include +#include +#include + + +void printAddressBook(const AddressBook::Reader& addressBook) +{ for (Person::Reader person : addressBook.getPeople()) { std::cout << person.getName().cStr() << ": " << person.getEmail().cStr() << std::endl; @@ -70,19 +70,11 @@ void printAddressBook(capnp::MallocMessageBuilder& msg_) std::cout << " self-employed" << std::endl; break; } + + std::cout << std::endl; } } -void OnAddressbook(const char* topic_name_, const capnp::MallocMessageBuilder& msg_, const long long time_) -{ - // print content - std::cout << "topic name : " << topic_name_ << std::endl; - std::cout << "time : " << time_ << std::endl; - std::cout << std::endl; - printAddressBook(const_cast(msg_)); - std::cout << std::endl; -} - int main(int argc, char **argv) { // initialize eCAL API @@ -92,17 +84,17 @@ int main(int argc, char **argv) eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !"); // create a subscriber (topic name "addressbook") - eCAL::CCapnpSubscriber sub("addressbook"); - - // add receive callback function (_1 = topic_name, _2 = msg, _3 = time) - auto callback = std::bind(OnAddressbook, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); - sub.AddReceiveCallback(callback); - + eCAL::CCapnpSubscriber sub("addressbook"); // enter main loop while (eCAL::Ok()) { - // sleep 500 ms - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + // receive content + if (sub.Receive(nullptr, 0)) + { + AddressBook::Reader reader{ sub.getReader() }; + printAddressBook(reader); + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } // finalize eCAL API diff --git a/samples/cpp/capnp/addressbook_snd/src/addressbook_snd.cpp b/samples/cpp/capnp/addressbook_snd/src/addressbook_snd.cpp index b1af311b91..ad7a428472 100644 --- a/samples/cpp/capnp/addressbook_snd/src/addressbook_snd.cpp +++ b/samples/cpp/capnp/addressbook_snd/src/addressbook_snd.cpp @@ -1,6 +1,6 @@ /* ========================= eCAL LICENSE ================================= * - * Copyright (C) 2016 - 2019 Continental Corporation + * Copyright (C) 2016 - 2018 Continental Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,10 @@ #include #include +#include #include +#include #include #include @@ -34,43 +36,10 @@ #pragma warning(pop) #endif -void printAddressBook(capnp::MallocMessageBuilder& msg_) +void printAddressBook(AddressBook::Builder addressBook) { - AddressBook::Reader addressBook = msg_.getRoot(); - - for (Person::Reader person : addressBook.getPeople()) - { - std::cout << person.getName().cStr() << ": " << person.getEmail().cStr() << std::endl; - for (Person::PhoneNumber::Reader phone : person.getPhones()) - { - const char* typeName = "UNKNOWN"; - switch (phone.getType()) { - case Person::PhoneNumber::Type::MOBILE: typeName = "mobile"; break; - case Person::PhoneNumber::Type::HOME: typeName = "home"; break; - case Person::PhoneNumber::Type::WORK: typeName = "work"; break; - } - std::cout << " " << typeName << " phone: " << phone.getNumber().cStr() << std::endl; - } - Person::Employment::Reader employment = person.getEmployment(); - - switch (employment.which()) - { - case Person::Employment::UNEMPLOYED: - std::cout << " unemployed" << std::endl; - break; - case Person::Employment::EMPLOYER: - std::cout << " employer: " - << employment.getEmployer().cStr() << std::endl; - break; - case Person::Employment::SCHOOL: - std::cout << " student at: " - << employment.getSchool().cStr() << std::endl; - break; - case Person::Employment::SELF_EMPLOYED: - std::cout << " self-employed" << std::endl; - break; - } - } + auto m_string = addressBook.toString().flatten(); + std::cout << "our string: " << m_string.cStr() << std::endl; } int main(int argc, char **argv) @@ -82,12 +51,9 @@ int main(int argc, char **argv) eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !"); // create a publisher (topic name "addressbook") - eCAL::CCapnpPublisher pub("addressbook"); - - // create a capnp message - capnp::MallocMessageBuilder message; + eCAL::CCapnpPublisher pub("addressbook"); - auto addressBook = message.initRoot(); + auto addressBook = pub.GetBuilder(); auto people = addressBook.initPeople(2); auto alice = people[0]; @@ -99,6 +65,17 @@ int main(int argc, char **argv) alicePhones[0].setNumber("555-1212"); alicePhones[0].setType(Person::PhoneNumber::Type::MOBILE); alice.getEmployment().setSchool("MIT"); + alice.setWeight(60.4); + + kj::byte* data = new kj::byte[6]; + data[0] = 0x1F; + data[1] = 0x00; + data[2] = 0xA1; + data[3] = 0xB4; + data[4] = 0x14; + data[5] = 0x54; + capnp::Data::Builder aliceData(data, 6); + alice.setData(aliceData); auto bob = people[1]; bob.setId(456); @@ -111,15 +88,16 @@ int main(int argc, char **argv) bobPhones[1].setNumber("555-7654"); bobPhones[1].setType(Person::PhoneNumber::Type::WORK); bob.getEmployment().setUnemployed(); + bob.setWeight(80.8); // enter main loop while (eCAL::Ok()) { // send content - pub.Send(message, -1); + pub.Send(); // print content - printAddressBook(message); + printAddressBook(addressBook); std::cout << std::endl; // sleep 500 ms diff --git a/samples/cpp/capnp/addressbook_snd/src/capnp/addressbook.capnp b/samples/cpp/capnp/addressbook_snd/src/capnp/addressbook.capnp index d1ea54a18d..88df42ecfb 100644 --- a/samples/cpp/capnp/addressbook_snd/src/capnp/addressbook.capnp +++ b/samples/cpp/capnp/addressbook_snd/src/capnp/addressbook.capnp @@ -48,6 +48,9 @@ struct Person { selfEmployed @7 :Void; # We assume that a person is only one of these. } + + weight @8 :Float32; + data @9 :Data; } struct AddressBook { diff --git a/samples/cpp/flatbuffer/monster_rec/src/monster_rec.cpp b/samples/cpp/flatbuffer/monster_rec/src/monster_rec.cpp index b8700036b9..d044da018a 100644 --- a/samples/cpp/flatbuffer/monster_rec/src/monster_rec.cpp +++ b/samples/cpp/flatbuffer/monster_rec/src/monster_rec.cpp @@ -82,7 +82,7 @@ int main(int argc, char **argv) eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !"); // create a subscriber (topic name "monster") - eCAL::CFlatSubscriber sub("monster"); + eCAL::flatbuffers::CSubscriber sub("monster"); // add receive callback function (_1 = topic_name, _2 = msg, _3 = time) auto callback = std::bind(OnMonster, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); diff --git a/samples/cpp/flatbuffer/monster_snd/src/monster_snd.cpp b/samples/cpp/flatbuffer/monster_snd/src/monster_snd.cpp index 8163629b6f..ba9d241410 100644 --- a/samples/cpp/flatbuffer/monster_snd/src/monster_snd.cpp +++ b/samples/cpp/flatbuffer/monster_snd/src/monster_snd.cpp @@ -40,7 +40,7 @@ int main(int argc, char **argv) eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !"); // create a publisher (topic name "monster") - eCAL::CFlatPublisher pub("monster"); + eCAL::flatbuffers::CPublisher pub("monster"); // the generic builder instance flatbuffers::FlatBufferBuilder builder; diff --git a/samples/cpp/latency/latency_rec/src/CMakeLists.txt b/samples/cpp/latency/latency_rec/src/CMakeLists.txt new file mode 100644 index 0000000000..5bb150a844 --- /dev/null +++ b/samples/cpp/latency/latency_rec/src/CMakeLists.txt @@ -0,0 +1,35 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +project(latency_rec) + +find_package(eCAL REQUIRED) + +set(latency_rec_src + latency_rec.cpp +) + +ecal_add_sample(${PROJECT_NAME} ${latency_rec_src}) + +target_include_directories(${PROJECT_NAME} PRIVATE .) + +target_link_libraries(${PROJECT_NAME} eCAL::core) + +ecal_install_sample(${PROJECT_NAME}) + +set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER samples/cpp/latency) diff --git a/samples/cpp/latency/latency_rec/src/latency_rec.cpp b/samples/cpp/latency/latency_rec/src/latency_rec.cpp new file mode 100644 index 0000000000..79ac7f2b06 --- /dev/null +++ b/samples/cpp/latency/latency_rec/src/latency_rec.cpp @@ -0,0 +1,96 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2019 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include + +#include +#include +#include + +void do_run() +{ + // initialize eCAL API + eCAL::Initialize(0, nullptr, "latency_reply"); + + // create publisher and subscriber + eCAL::CSubscriber sub_pkg("pkg_send"); + eCAL::CPublisher pub_pkg("pkg_reply"); + + std::cout << "-------------------------------" << std::endl; + std::cout << " LATENCY / THROUGHPUT TEST" << std::endl; + std::cout << "-------------------------------" << std::endl; + + // prepare timestamp array and + std::vector diff_array; + // reserve for 5000 elements + diff_array.reserve(5000); + + // prepare receive buffer + std::string rec_buf; + int rec_timeout(-1); + int rec_pkgs(0); + + for(;;) + { + long long snd_time(0); + if (sub_pkg.Receive(rec_buf, &snd_time, rec_timeout)) + { + // store time stamp + diff_array.push_back(eCAL::Time::GetMicroSeconds() - snd_time); + rec_pkgs++; + // reply + pub_pkg.Send(rec_buf); + // we reduce timeout to 1000 ms to detect lost packages + rec_timeout = 1000; + } + else + { + // timeout, let's stop and summarize + break; + } + } + + // calculate receive time over all received messages + long long sum_time(0); + for (int pgk = 0; pgk < rec_pkgs; pgk++) + { + sum_time += diff_array[pgk]; + } + long long avg_time = sum_time/rec_pkgs; + std::cout << "Received buffer size : " << rec_buf.size()/1024 << " kB" << std::endl; + std::cout << "Received packages : " << rec_pkgs << std::endl; + std::cout << "Message average receive time : " << avg_time << " us" << std::endl; + std::cout << "Throughput : " << static_cast(((rec_buf.size()*rec_pkgs)/1024.0)/(sum_time /1000.0/1000.0)) << " kB/s" << std::endl; + std::cout << " : " << static_cast(((rec_buf.size()*rec_pkgs)/1024.0/1024.0)/(sum_time /1000.0/1000.0)) << " MB/s" << std::endl; + std::cout << " : " << static_cast(rec_pkgs /(sum_time /1000.0/1000.0)) << " Msg/s" << std::endl << std::endl; + + // finalize eCAL API + eCAL::Finalize(); +} + +int main(int /*argc*/, char** /*argv*/) +{ + // run tests + while (eCAL::Ok()) + { + do_run(); + } + + return(0); +} diff --git a/samples/cpp/latency/latency_snd/src/CMakeLists.txt b/samples/cpp/latency/latency_snd/src/CMakeLists.txt new file mode 100644 index 0000000000..a2bbbc806c --- /dev/null +++ b/samples/cpp/latency/latency_snd/src/CMakeLists.txt @@ -0,0 +1,39 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +project(latency_snd) + +find_package(eCAL REQUIRED) +find_package(tclap REQUIRED) + +set(latency_snd_src + latency_snd.cpp +) + +ecal_add_sample(${PROJECT_NAME} ${latency_snd_src}) + +target_include_directories(${PROJECT_NAME} PRIVATE .) + +target_link_libraries(${PROJECT_NAME} eCAL::core) +target_link_libraries(${PROJECT_NAME} + eCAL::core + tclap::tclap) + +ecal_install_sample(${PROJECT_NAME}) + +set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER samples/cpp/latency) diff --git a/samples/cpp/latency/latency_snd/src/latency_snd.cpp b/samples/cpp/latency/latency_snd/src/latency_snd.cpp new file mode 100644 index 0000000000..06cd5076e6 --- /dev/null +++ b/samples/cpp/latency/latency_snd/src/latency_snd.cpp @@ -0,0 +1,112 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2019 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include + +#include + +#include +#include +#include + +void do_run(const int runs, int snd_size /*kB*/) +{ + // initialize eCAL API + eCAL::Initialize(0, nullptr, "latency_snd"); + + // create publisher and subscriber + eCAL::CPublisher pub_pkg("pkg_send"); + eCAL::CSubscriber sub_pkg("pkg_reply"); + + // let them match + eCAL::Process::SleepMS(2000); + + // prepare timestamp array + std::vector diff_array(runs); + std::vector snd_array(snd_size * 1024); + int snd_pkgs(0); + + // prepare receive buffer + std::string rec_buf; + int rec_timeout(1000); + + std::cout << "-------------------------------" << std::endl; + std::cout << " LATENCY / THROUGHPUT TEST" << std::endl; + std::cout << " RUNS : " << runs << std::endl; + std::cout << " SIZE : " << snd_size << " kB" << std::endl; + std::cout << "-------------------------------" << std::endl; + + for (int reply = 0; reply < runs; ++reply) + { + // get time and send message + long long snd_time = eCAL::Time::GetMicroSeconds(); + pub_pkg.Send(snd_array.data(), snd_array.size(), snd_time); + // store sent packages + snd_pkgs++; + + // receive reply with timeout + if (sub_pkg.Receive(rec_buf, nullptr, rec_timeout)) + { + // store time stamp + diff_array[reply] = eCAL::Time::GetMicroSeconds() - snd_time; + } + else + { + // we lost a package -> stop + std::cout << "Packages lost after message " << snd_pkgs << " -> stop." << std::endl << std::endl; + break; + } + } + + // calculate roundtrip time over all received messages + long long sum_time(0); + for (int reply = 0; reply < snd_pkgs; reply++) + { + sum_time += diff_array[reply]; + } + long long avg_time = sum_time/ snd_pkgs; + std::cout << "Message average roundtrip time " << avg_time << " us" << std::endl << std::endl; + + // finalize eCAL API + eCAL::Finalize(); +} + +int main(int argc, char **argv) +{ + try + { + // parse command line + TCLAP::CmdLine cmd("latency_snd"); + TCLAP::ValueArg runs("r", "runs", "Number of messages to send.", false, 1000, "int"); + TCLAP::ValueArg size("s", "size", "Messages size in kB.", false, 64, "int"); + cmd.add(runs); + cmd.add(size); + cmd.parse(argc, argv); + + // run test + do_run(runs.getValue(), size.getValue()); + } + catch (TCLAP::ArgException &e) // catch any exceptions + { + std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; + return EXIT_FAILURE; + } + + return(0); +} diff --git a/samples/cpp/msgpack/address_rec/src/address_rec.cpp b/samples/cpp/msgpack/address_rec/src/address_rec.cpp index 41a4df703d..0cff8b28c2 100644 --- a/samples/cpp/msgpack/address_rec/src/address_rec.cpp +++ b/samples/cpp/msgpack/address_rec/src/address_rec.cpp @@ -63,7 +63,7 @@ int main(int argc, char **argv) eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !"); // create a subscriber (topic name "address") - eCAL::CMsgPackSubscriber sub("address"); + eCAL::messagepack::CSubscriber sub("address"); // add receive callback function (_1 = topic_name, _2 = msg, _3 = time) auto callback = std::bind(OnAddress, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); diff --git a/samples/cpp/msgpack/address_snd/src/address_snd.cpp b/samples/cpp/msgpack/address_snd/src/address_snd.cpp index c1ae2a7c57..810adb247a 100644 --- a/samples/cpp/msgpack/address_snd/src/address_snd.cpp +++ b/samples/cpp/msgpack/address_snd/src/address_snd.cpp @@ -50,7 +50,7 @@ int main(int argc, char **argv) eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !"); // create a publisher (topic name "address") - eCAL::CMsgPackPublisher pub("address"); + eCAL::messagepack::CPublisher pub("address"); // create the message CAddress address; diff --git a/samples/cpp/services/math_server/src/math_server.cpp b/samples/cpp/services/math_server/src/math_server.cpp index 04ae434455..811f8fcebf 100644 --- a/samples/cpp/services/math_server/src/math_server.cpp +++ b/samples/cpp/services/math_server/src/math_server.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include "math.pb.h"