diff options
author | takeshi_hoshina <takeshi_hoshina@mail.toyota.co.jp> | 2020-10-27 11:16:21 +0900 |
---|---|---|
committer | takeshi_hoshina <takeshi_hoshina@mail.toyota.co.jp> | 2020-10-27 11:16:21 +0900 |
commit | 947c78887e791596d4a5ec2d1079f8b1a049628b (patch) | |
tree | 3981e88eb8764d7180722f8466f36b756dc005af /nsframework/framework_unified/client/NS_ConfigParser | |
parent | 706ad73eb02caf8532deaf5d38995bd258725cb8 (diff) |
basesystem 0.1sandbox/ToshikazuOhiwa/basesystem
Diffstat (limited to 'nsframework/framework_unified/client/NS_ConfigParser')
15 files changed, 2137 insertions, 0 deletions
diff --git a/nsframework/framework_unified/client/NS_ConfigParser/cfg/depends.mk b/nsframework/framework_unified/client/NS_ConfigParser/cfg/depends.mk new file mode 100644 index 00000000..93b9f97a --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/cfg/depends.mk @@ -0,0 +1,25 @@ +# +# @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. +# + +# +# Standard Dependency Makefile Version 2.0 +# +# Dependency file dictates not only what other modules the project is dependent on, but +# also where to get that dependent element which logically includes versioning or +# baselining information + +# Utilizing Base Domain Dependency File. Can override at any point +include $(PRJ_ROOT)/../NativeServices/cfg/depends.mk diff --git a/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_internal.h b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_internal.h new file mode 100644 index 00000000..64aad8b0 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_internal.h @@ -0,0 +1,54 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains the declaration of internal structure for parsing cfg configuration +/// file. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_INTERNAL_H_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_INTERNAL_H_ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <string> +#include <vector> + +class CNode { + public: + CNode(): m_cComment(""), m_cTag(""), m_cValue("") {} // LCOV_EXCL_BR_LINE 11:except branch + + ~CNode() {} + + std::string m_cComment; + std::string m_cTag; + std::string m_cValue; +}; + +class CBlock { + public: + CBlock(): m_cComment("") {} // LCOV_EXCL_BR_LINE 11:except branch + + ~CBlock() {} + + std::string m_cComment; + std::vector<CNode> m_vElementPair; +}; + +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_INTERNAL_H_ diff --git a/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_parser.h b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_parser.h new file mode 100644 index 00000000..8d7a47fd --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_parser.h @@ -0,0 +1,148 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains the declaration of class CCFGParser. +/// This class contains the parsing logic for reading and writing from and to cfg files +/// respectively. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_PARSER_H_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_PARSER_H_ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/frameworkunified_types.h> +#include <string> +#include <map> + +// forward declaration of classes +class CNode; +class CBlock; + +typedef std::map<std::string, CBlock *> CFGData_Type; + +class CCFGParser { + public: + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CCFGParser + /// Constructor of CCFGReader class + /// + /// \param + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + CCFGParser(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CCFGParser + /// Constructor of CCFGReader class + /// + /// \param [IN] f_c_filepath + /// std::string - Full path of the configuration file + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + explicit CCFGParser(const std::string &f_c_filepath); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// ~CCFGParser + /// Destructor of CCFGReader class + /// + /// \param + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + ~CCFGParser(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CFGParseFile + /// This function is used to parse the configuration file and store data in internal structure + /// + /// \param [IN] f_c_filepath + /// std::string - full path of the configuration file + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus CFGParseFile(const std::string &f_c_filepath); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CFGGetData + /// This function is used to get the value associated with the key from internal CFG structure + /// + /// \param [IN] f_c_key + /// std::string - key to search + /// + /// \return std::string - value for key + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + std::string CFGGetData(const std::string &f_c_key); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CFGGetData + /// This function is used to get the value associated with the key from internal CFG structure + /// + /// \param [IN] f_c_key + /// std::string - key to search + /// \param [IN] f_c_value + /// std::string - Value of key + /// + /// \return EFrameworkunifiedStatus + /// EFrameworkunifiedStatus - error if key not founf or else eFrameworkunifiedStatusOK + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus CFGGetData(const std::string &f_c_key, std::string &f_c_value); // NOLINT (readability/nolint) + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CFGSetData + /// This function is used to set the value for the key in internal CFG structure + /// + /// \param [IN] f_c_key + /// std::string - key to search + /// \param [IN] f_c_value + /// std::string - value to set + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus CFGSetData(const std::string &f_c_key, std::string f_c_value); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CFGSaveData + /// This function is used to save the changed value to the cfg config file + /// + /// \param [IN] f_c_filepath + /// std::string - full path of the configuration file + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus CFGSaveData(const std::string &f_c_filepath); + + private: + // internal data structure created by parsing the cfg file + CFGData_Type *m_pmCFGData; + std::string m_cLastComment; +}; + +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_PARSER_H_ diff --git a/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_reader.h b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_reader.h new file mode 100644 index 00000000..d47c04b1 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_reader.h @@ -0,0 +1,133 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains declaration of class CCFGReader. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_READER_H_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_READER_H_ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_reader.h> +#include <string> + +class CCFGParser; + +/* + * This class provides functionalities to read from CFG config file + */ +class CCFGReader: public IConfigReader { + public: + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CCFGReader + /// Constructor of CCFGReader class + /// + /// \param + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + CCFGReader(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CCFGReader + /// Constructor of CCFGReader class + /// + /// \param [IN] f_c_filepath + /// std::string - Full path of the configuration file + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + explicit CCFGReader(const std::string &f_c_filepath); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// ~CCFGReader + /// Destructor of CCFGReader class + /// + /// \param + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + ~CCFGReader(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// ParseFile + /// This function is used to parse the file + /// + /// \param [IN] f_c_filepath + /// std::string - path of file to parse + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus ParseFile(const std::string &f_c_filepath); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// GetString + /// This function is used to get the value associated with the key + /// + /// \param [IN] f_c_key + /// std::string - key to search + /// + /// \return std::string - value for key + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + std::string GetValue(const std::string &f_c_key); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// GetString + /// This function is used to get the value associated with the key + /// + /// \param [IN] f_c_key + /// std::string - key to search + /// \param [REF] f_c_value + /// std::string - Value of key + /// + /// \return EFrameworkunifiedStatus + /// EFrameworkunifiedStatus - error if key not found else eFrameworkunifiedStatusOK + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus GetValue(const std::string &f_c_key, std::string &f_c_value); // NOLINT (readability/nolint) + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// GetDataPtr + /// This function is used to get the data pointer. This pointer is then set in config writer. + /// This is needed to avoid recreation of same data structure object in both reader and writer + /// when we create object of NSConfigParser. + /// + /// \param + /// + /// \return PVOID - pointer of data structure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + PVOID GetDataPtr(); + + private: + // path of config file + std::string m_cFilePath; + + // CFG file parser object + CCFGParser *m_pCFGParser; +}; + +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_READER_H_ diff --git a/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_writer.h b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_writer.h new file mode 100644 index 00000000..58d16399 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_cfg_writer.h @@ -0,0 +1,146 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains declaration of class CCFGWriter. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_WRITER_H_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_WRITER_H_ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_writer.h> +#include <string> + +class CCFGParser; + +/* + * This class provides functionalities to write to CFG config file + */ +class CCFGWriter: public IConfigWriter { + public: + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CCFGWriter + /// Constructor of CCFGWriter class + /// + /// \param + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + CCFGWriter(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// CCFGWriter + /// Constructor of CCFGWriter class + /// + /// \param [IN] f_c_filepath + /// std::string - Full path of the configuration file + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + explicit CCFGWriter(const std::string &f_c_filepath); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// ~CCFGWriter + /// Destructor of CCFGWriter class + /// + /// \param + /// + /// \return + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + ~CCFGWriter(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// ParseFile + /// This function is used to parse the file + /// + /// \param [IN] f_c_filepath + /// std::string - path of file to parse + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus ParseFile(const std::string &f_c_filepath); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// SetValue + /// This function is used to set the value for the key + /// + /// \param [IN] f_c_key + /// std::string - key to search + /// \param [IN] f_c_value + /// std::string - value to set + /// + /// \return EFrameworkunifiedStatus + /// EFrameworkunifiedStatus - error if key not found else eFrameworkunifiedStatusOK + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus SetValue(const std::string &f_c_key, std::string f_c_value); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// SaveData + /// This function is used to save the changed value to the cfg config file + /// + /// \param + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus SaveData(); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// SetDataPtr + /// This function is used to set the config file path + /// + /// \param [IN] f_c_path + /// std::string - Path of file + /// + /// \return EFrameworkunifiedStatus - success or failure + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + EFrameworkunifiedStatus SetPath(const std::string &f_c_path); + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// SetDataPtr + /// This function is used to set the data pointer in writer class with data pointer + /// created in config reader class. + /// This is needed to avoid recreation of same data structure object in both reader and writer + /// when we create object of NSConfigParser + /// + /// \param [IN] f_p_data + /// PVOID - Pointer to data structure + /// + /// \return VOID + /// + //////////////////////////////////////////////////////////////////////////////////////////////// + VOID SetDataPtr(PVOID f_p_data); + + private: + // path of config file + std::string m_cFilePath; + + // CFG file parser object + CCFGParser *m_pCFGParser; +}; + +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CFG_WRITER_H_ diff --git a/nsframework/framework_unified/client/NS_ConfigParser/include/ns_config_parser_frameworkunifiedlog.h b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_config_parser_frameworkunifiedlog.h new file mode 100644 index 00000000..c48f09d7 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/include/ns_config_parser_frameworkunifiedlog.h @@ -0,0 +1,88 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +/////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief +/// +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifndef FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CONFIG_PARSER_FRAMEWORKUNIFIEDLOG_H_ +#define FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CONFIG_PARSER_FRAMEWORKUNIFIEDLOG_H_ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_logger_if.h> + +#define ZONE_INIT ZONEMASK(10) +#define ZONE_FUNC ZONEMASK(11) +#define ZONE_MEM ZONEMASK(12) +#define ZONE_13 ZONEMASK(13) +#define ZONE_14 ZONEMASK(14) +#define ZONE_15 ZONEMASK(15) +#define ZONE_16 ZONEMASK(16) +#define ZONE_17 ZONEMASK(17) +#define ZONE_18 ZONEMASK(18) +#define ZONE_19 ZONEMASK(19) +#define ZONE_20 ZONEMASK(20) +#define ZONE_21 ZONEMASK(21) +#define ZONE_22 ZONEMASK(22) +#define ZONE_23 ZONEMASK(23) +#define ZONE_24 ZONEMASK(24) +#define ZONE_25 ZONEMASK(25) +#define ZONE_26 ZONEMASK(26) +#define ZONE_27 ZONEMASK(27) +#define ZONE_28 ZONEMASK(28) +#define ZONE_INFO ZONEMASK(29) +#define ZONE_WARN ZONEMASK(30) +#define ZONE_ERR ZONEMASK(31) + +#define ZONE_TEXT_10 "Init" +#define ZONE_TEXT_11 "Function" +#define ZONE_TEXT_12 "Memory" +#define ZONE_TEXT_13 "" +#define ZONE_TEXT_14 "" +#define ZONE_TEXT_15 "" +#define ZONE_TEXT_16 "" +#define ZONE_TEXT_17 "" +#define ZONE_TEXT_18 "" +#define ZONE_TEXT_19 "" +#define ZONE_TEXT_20 "" +#define ZONE_TEXT_21 "" +#define ZONE_TEXT_22 "" +#define ZONE_TEXT_23 "" +#define ZONE_TEXT_24 "" +#define ZONE_TEXT_25 "" +#define ZONE_TEXT_26 "" +#define ZONE_TEXT_27 "" +#define ZONE_TEXT_28 "" +#define ZONE_TEXT_29 "Info" +#define ZONE_TEXT_30 "Warning" +#define ZONE_TEXT_31 "Error" + +#ifndef FRAMEWORKUNIFIEDLOGOPTIONS +#define FRAMEWORKUNIFIEDLOGOPTIONS (LPRINT) // LPRINT , LMSGQ, LSLOGGER +#endif + +#ifndef FRAMEWORKUNIFIEDLOGZONES +// #define FRAMEWORKUNIFIEDLOGZONES (ZONE_WARN|ZONE_ERR) +#define FRAMEWORKUNIFIEDLOGZONES (ZONE_INIT|ZONE_FUNC|ZONE_INFO|ZONE_WARN|ZONE_ERR|ZONE_NS_WAR|ZONE_NS_ERR) +#endif + +extern const CHAR kAppName[]; +#endif // FRAMEWORK_UNIFIED_CLIENT_NS_CONFIGPARSER_INCLUDE_NS_CONFIG_PARSER_FRAMEWORKUNIFIEDLOG_H_ diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/makefile_PosixBasedOS001 b/nsframework/framework_unified/client/NS_ConfigParser/src/makefile_PosixBasedOS001 new file mode 100644 index 00000000..1e6e556b --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/makefile_PosixBasedOS001 @@ -0,0 +1,207 @@ +# +# @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. +# + +# +# Standard Module Makefile version 2.0 +# + +# Name of the componet (team/domain prefix '_' component name) +COMPONENT_NAME = NS_ConfigParser + +ifndef PRJ_ROOT +export PRJ_ROOT = $(CURDIR)/../ +endif +include $(PRJ_ROOT)cfg/depends.mk + +# Name of the componet (team/domain prefix '_' component name) +# This must be the same as the RTC Component name and Eclipse Project Name +COMPONENT_NAME = NS_ConfigParser + + +# Additive Compile Flags (Flags from initiating make process will still apply) +DEFS += + +# Set local includes and then the reference includes (priority order determines search path) +# Default pattern are any configuration includes (which would be things like PosixBasedOS001), local (Team) component directories, +# dependencies includes (other teams) +# Local (current component references should be in the form of +# $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/directory +# Example your public include directory would be +# $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/inc +# Team references should only be to other's public includes such as +# $(CC_IFLAG)$(TEAM_ROOT)NS_MessageCenter/inc +# Global (non-team) references should be only to other's public includes such +# these are found in the depends include file and captured in the (DEPENDS_INCLUDES) variable +INCLUDES = \ + $(CFG_INCS) \ + $(CC_IFLAG)./ \ + $(DEPENDS_INCLUDES) \ + $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/inc \ + $(CC_IFLAG)$(TEAM_ROOT)NS_XMLConfigParser/inc + +# Do the same if you need to include library paths as well +# Do an incremental in case additional library paths are defined +# at the top-level make. Use similar guidelines as for includes +# for example to include a team component library it would be +# $(TEAM_ROOT)NS_MessageCenter/lib/NS_MessageCenter/ +LIB_PATHS += \ + $(DEPENDS_LIB_PATHS) \ + + + +# Define binary outputs. These can be libraries or executables. +# Name a variable for each deliverable. Suffixes should be +# EXEC - For Executables -> output to the bin directory +#TIME_EXEC = $(BIN_PATH)time +# LIB - For Static Libraries -> output to lib directory with specific naming +#MATH_LIB = $(LIB_PATH)$(LIB_PREFIX)math.$(LIB_EXT) +# SLIB - For Shared Objects +#FRMWRK_SLIB = $(SLIB_PATH)frmwrk.$(SO_EXT) +# LIB - Define the static library for Message Queue +# +# +ifdef DYNAMIC + COMPONENT_LIB = $(SLIB_PATH)$(LIB_PREFIX)$(COMPONENT_NAME)$(DEBUG_EXT).$(SO_EXT) +else + COMPONENT_LIB = $(LIB_PATH)$(LIB_PREFIX)$(COMPONENT_NAME)$(DEBUG_EXT).$(LIB_EXT) +endif + + +## Sources Section + +# Define Library & Executable Sources (on a per deliverable basis) +# This includes sources located in subdirectories. + +# Define generic line that pulls all c, cc, cpp files +# since your in the src folder is pull only files from there +COMPONENT_SRCS = \ + $(wildcard *.c) \ + $(wildcard *.cpp) + +# Define sources that my not be local to your component +# here, you can define indivial files or wildcard from +# a different folder. +NON_LOCAL_SRCS = \ + + +# List of all sources to be built. Can be assembled from the other defintitions. +# This only defines sources for the current directory, so if there are subdirectories +# those are not included. (Those are found in simple subdirectory makefiles that only +# direct the building of sources, but no linking into a binary) +SOURCES = \ + $(COMPONENT_SRCS) \ + $(NON_LOCAL_SRCS) \ + + + +# +# Convert the source files to object files with correct folder location. +# +# +C_LANG_OBJECTS = $(addprefix $(BLD_PATH),$(addsuffix .$(OBJ_EXT),$(basename $(filter %.c ,$(SOURCES) ) ) ) ) +CPP_LANG_OBJECTS = $(addprefix $(BLD_PATH),$(addsuffix .$(OBJ_EXT),$(basename $(filter %.cpp %.cc %.cxx,$(SOURCES) ) ) ) ) + + +# List of all sources to be generated. Can be assembled from the other defintitions. +OBJECTS = \ + $(C_LANG_OBJECTS) \ + $(CPP_LANG_OBJECTS) + + + +# All headers that are dependencies. Wildcard is easy to pickup local headers. +# This is only to automate the rebuilding, all builds on the servers are cleans +# So this is not a huge deal when building on a component level. +HEADERS = \ + $(wildcard *.h) \ + $(wildcard $(TEAM_ROOT)$(COMPONENT_NAME)/inc/*.h) \ + $(wildcard $(TEAM_ROOT)NativeServices/inc/*.h) \ + $(wildcard $(TEAM_ROOT)NativeServices/inc/native/*.h) \ + + +LIBRARIES = \ + $(COMPONENT_LIB) \ + +#DYNAMIC_LIBS += \ +# xml2 \ +# m + +# Make targets +# Standard +all: banner module_dirs subdirs local library binary + +debug: + $(MAKE) TARGET=arm DEBUG=TRUE all + +base: banner module_dirs subdirs local + +# Standard Building of Source Files (Default builds for all objects defined above) +$(C_LANG_OBJECTS): $(SOURCES) $(HEADERS) + $(CC_CMD) + +$(CPP_LANG_OBJECTS): $(SOURCES) $(HEADERS) + $(CPP_CMD) + +local: $(OBJECTS) + +# Defines specific for each deliverable + +# For a static library +$(COMPONENT_LIB): $(OBJECTS) +ifdef DYNAMIC +# For a dynamic library + $(SLIB_CMD) + $(HIDE_ECHO_FLAG)$(OBJCPY) --only-keep-debug $(@) $(@).debug + $(HIDE_ECHO_FLAG)$(OBJCPY) --strip-all $(@) + $(HIDE_ECHO_FLAG)$(OBJCPY) --add-gnu-debuglink=$(@).debug $(@) +else +# For a static library + $(AR_CMD) +endif + +# Standard set of derived targets +library: base \ + $(LIBRARIES) + @echo "***** `date` Done building library: $(COMPONENT_NAME) ******" + +binary: base \ + $(BINARIES) + +# Subdirs should be to jump to subdirectories +# standard form is of +# $(MAKE) -C subdirectory_name $(MAKECMDGOALS) +subdirs: + +clean: + -rm -f $(BINARIES) + -rm -f $(LIBRARIES) + -rm -f $(OBJECTS) + -rm -f $(COMPONENT_LIB).map + -rm -f $(COMPONENT_LIB).debug + +-v: + @echo "objs: --> $(OBJECTS)" + @echo "sources: --> $(SOURCES)" + @echo "headers: --> $(HEADERS)" + @echo "includes: --> $(INCLUDES)" + @echo "lib paths: --> $(LIB_PATHS)" + @echo "static libs: --> $(LD_STATIC_LIBS)" + @echo "dynamic libs: --> $(LD_DYNAMIC_LIBS)" + @echo "lib: --> $(LIBRARIES)" + @echo "bin: --> $(BINARIES)" + +module_dirs: build_dirs + diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_parser.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_parser.cpp new file mode 100644 index 00000000..a2abd785 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_parser.cpp @@ -0,0 +1,365 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of CCFGParser class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include <ns_cfg_internal.h> +#include <ns_cfg_parser.h> +#include <ns_config_parser_frameworkunifiedlog.h> + +#include <fstream> +#include <string> + +CCFGParser::CCFGParser() + : m_pmCFGData(NULL) { +} + +// LCOV_EXCL_START 200:CCFGParser is not external API class, this constructed function isn't used by other function +CCFGParser::CCFGParser(const std::string &f_cfilepath) + : m_pmCFGData(NULL) { + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + CFGParseFile(f_cfilepath); +} +// LCOV_EXCL_STOP + +CCFGParser::~CCFGParser() { + CFGData_Type::iterator l_itCfgStructure; + + if (NULL != m_pmCFGData) { // LCOV_EXCL_BR_LINE 5:m_pmCFGData cannot be NULL, because internal logic guarantee + // release the structure + for (l_itCfgStructure = m_pmCFGData->begin(); + l_itCfgStructure != m_pmCFGData->end(); + l_itCfgStructure++) { + CBlock *l_pBlock = (*l_itCfgStructure).second; + if (NULL != l_pBlock) { // LCOV_EXCL_BR_LINE 5:l_pBlock cannot be NULL, because map insert data success. + // clear the vector + l_pBlock->m_vElementPair.clear(); + + delete l_pBlock; // LCOV_EXCL_BR_LINE 11:except branch + l_pBlock = NULL; + } + } + + delete m_pmCFGData; // LCOV_EXCL_BR_LINE 11:except branch + m_pmCFGData = NULL; + } +} + +EFrameworkunifiedStatus CCFGParser::CFGParseFile(const std::string &f_cfilepath) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + size_t l_uiPos = 0; + std::string l_cLine(""); + std::string l_cTag(""); // LCOV_EXCL_BR_LINE 11:except branch + std::string l_cComment(""); // LCOV_EXCL_BR_LINE 11:except branch + + m_pmCFGData = new(std::nothrow) CFGData_Type(); // LCOV_EXCL_BR_LINE 11:except branch + + if (NULL != m_pmCFGData) { // LCOV_EXCL_BR_LINE 5:new's error case + // open the file + std::ifstream l_objFile(f_cfilepath.c_str()); // LCOV_EXCL_BR_LINE 11:except branch + + // check if file is opened + if (l_objFile.is_open()) { + CBlock *l_pBlock = NULL; + + // read until end of file is not reached or any other stream error occurred + while (!l_objFile.eof()) { + // read the line from the file + getline(l_objFile, l_cLine); // LCOV_EXCL_BR_LINE 11:except branch + + // if line is a comment + if ('#' == l_cLine[0] || ';' == l_cLine[0]) { + if (!l_cComment.empty()) { + l_cComment.append("\n"); // LCOV_EXCL_BR_LINE 11:except branch + } + l_cComment.append(l_cLine); + } else if ('[' == l_cLine[0]) { // if line is main tag + // creates new CBlock object, + // each block contains information of single like all its key-value pair and + // all the comments associated with it. + l_pBlock = new CBlock(); // LCOV_EXCL_BR_LINE 11:except branch + if (NULL != l_pBlock) { // LCOV_EXCL_BR_LINE 5:new's error case + // set the tag comment + l_pBlock->m_cComment = l_cComment; + l_cComment.clear(); + + if (std::string::npos != (l_uiPos = l_cLine.find(']'))) { + // set the tag + l_cTag = l_cLine.substr(1, l_uiPos - 1); // LCOV_EXCL_BR_LINE 11:except branch + + // insert the block with its tag name in structure(map) + m_pmCFGData->insert(make_pair(l_cTag, l_pBlock)); // NOLINT (readability/nolint) + } else { + delete l_pBlock; // LCOV_EXCL_BR_LINE 11:except branch + l_pBlock = NULL; + + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "End Tag missing"); + // LCOV_EXCL_BR_STOP + l_eStatus = eFrameworkunifiedStatusErrOther; + break; + } + } else { + // LCOV_EXCL_START 5:new's error case + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error allocating memory for l_pBlock"); + l_eStatus = eFrameworkunifiedStatusNullPointer; + break; + // LCOV_EXCL_STOP + } + } else if (std::string::npos != (l_uiPos = l_cLine.find('='))) { // LCOV_EXCL_BR_LINE 11:except branch + if (NULL != l_pBlock) { // LCOV_EXCL_BR_LINE 5:new's error case + // create new node object, + // each node object contains the information of singlr key-value pair and + /// it's comment(if any) + // LCOV_EXCL_BR_START 11:except branch + CNode l_objNode; + l_objNode.m_cComment.append(l_cComment); + l_objNode.m_cTag = l_cLine.substr(0, l_uiPos); + l_objNode.m_cValue = l_cLine.substr(l_uiPos + 1); + + // insert the node object in block + l_pBlock->m_vElementPair.push_back(l_objNode); + // LCOV_EXCL_BR_STOP + + l_cComment.clear(); + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "l_pBlock is NULL"); + // LCOV_EXCL_BR_STOP + l_eStatus = eFrameworkunifiedStatusNullPointer; + break; + } + } + } + + // last comment + if (!l_cComment.empty()) { + m_cLastComment = l_cComment; + } else { + m_cLastComment.clear(); + } + + // close the file + l_objFile.close(); // LCOV_EXCL_BR_LINE 11:except branch + } else { + l_eStatus = eFrameworkunifiedStatusFail; + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error Opening File"); + // LCOV_EXCL_BR_STOP + } + } else { + // LCOV_EXCL_START 5:new's error case + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pmCFGData is NULL"); + l_eStatus = eFrameworkunifiedStatusNullPointer; + // LCOV_EXCL_STOP + } + + return l_eStatus; +} + +std::string CCFGParser::CFGGetData(const std::string &f_ckey) { + CFGData_Type::iterator l_itCfgStructure; + std::string l_cHeadTag = ""; + std::string l_cTag = ""; // LCOV_EXCL_BR_LINE 11:except branch + std::string l_cValue = ""; // LCOV_EXCL_BR_LINE 11:except branch + + size_t l_uiPos = 0; + + if (NULL != m_pmCFGData && !f_ckey.empty()) { // LCOV_EXCL_BR_LINE 5:new's error case(in function CFGParseFile) + if (std::string::npos != (l_uiPos = f_ckey.find('.'))) { // LCOV_EXCL_BR_LINE 11:except branch + l_cHeadTag = f_ckey.substr(0, l_uiPos); // LCOV_EXCL_BR_LINE 11:except branch + l_cTag = f_ckey.substr(l_uiPos + 1); // LCOV_EXCL_BR_LINE 11:except branch + } + + // search for the key and its associated value in internal structure + for (l_itCfgStructure = m_pmCFGData->begin(); + l_itCfgStructure != m_pmCFGData->end(); + l_itCfgStructure++) { + CBlock *l_pBlock = (*l_itCfgStructure).second; + + if (!l_cHeadTag.compare((*l_itCfgStructure).first) && NULL != l_pBlock) { + for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) { + if (!l_cTag.compare(l_pBlock->m_vElementPair[l_uiCount].m_cTag)) { + // LCOV_EXCL_BR_START 200:After l_cValue assignment is empty,there is no other place to assignment the value + if (!l_cValue.empty()) { + // LCOV_EXCL_BR_STOP + // LCOV_EXCL_START 200:After l_cValue assignment is empty,there is no other place to assignment the value + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + l_cValue.append("\n"); + // LCOV_EXCL_STOP 200 + } + l_cValue.append(l_pBlock->m_vElementPair[l_uiCount].m_cValue); + } + } + } + } + } + + return l_cValue; +} + +EFrameworkunifiedStatus CCFGParser::CFGGetData(const std::string &f_ckey, std::string &f_cvalue) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail; + + CFGData_Type::iterator l_itCfgStructure; + std::string l_cHeadTag = ""; + std::string l_cTag = ""; // LCOV_EXCL_BR_LINE 11:except branch + std::string l_cValue = ""; // LCOV_EXCL_BR_LINE 11:except branch + + size_t l_uiPos = 0; + + if (NULL != m_pmCFGData && !f_ckey.empty()) { // LCOV_EXCL_BR_LINE 5:new's error case(in function CFGParseFile) + if (std::string::npos != (l_uiPos = f_ckey.find('.'))) { // LCOV_EXCL_BR_LINE 11:except branch + l_cHeadTag = f_ckey.substr(0, l_uiPos); // LCOV_EXCL_BR_LINE 11:except branch + l_cTag = f_ckey.substr(l_uiPos + 1); // LCOV_EXCL_BR_LINE 11:except branch + } + + // search for the key and its associated value in internal structure + for (l_itCfgStructure = m_pmCFGData->begin(); + l_itCfgStructure != m_pmCFGData->end(); + l_itCfgStructure++) { + CBlock *l_pBlock = (*l_itCfgStructure).second; + + if (!l_cHeadTag.compare((*l_itCfgStructure).first) && NULL != l_pBlock) { + for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) { + if (!l_cTag.compare(l_pBlock->m_vElementPair[l_uiCount].m_cTag)) { + if (!f_cvalue.empty()) { + f_cvalue.append("\n"); + } + f_cvalue.append(l_pBlock->m_vElementPair[l_uiCount].m_cValue); + + l_eStatus = eFrameworkunifiedStatusOK; + } + } + } + } + } + + return l_eStatus; +} + +EFrameworkunifiedStatus CCFGParser::CFGSetData(const std::string &f_cKey, std::string f_cvalue) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail; + + CFGData_Type::iterator l_itCfgStructure; + std::string l_cHeadTag = ""; + std::string l_cTag = ""; // LCOV_EXCL_BR_LINE 11:except branch + std::string l_cValue = ""; // LCOV_EXCL_BR_LINE 11:except branch + size_t l_uiPos = 0; + + // LCOV_EXCL_BR_START 6:f_cKey is not NULL, because checked in CCFGWriter::SetValue + if (NULL != m_pmCFGData && !f_cKey.empty()) { + // LCOV_EXCL_BR_STOP + if (std::string::npos != (l_uiPos = f_cKey.find('.'))) { // LCOV_EXCL_BR_LINE 11:except branch + l_cHeadTag = f_cKey.substr(0, l_uiPos); // LCOV_EXCL_BR_LINE 11:except branch + l_cTag = f_cKey.substr(l_uiPos + 1); // LCOV_EXCL_BR_LINE 11:except branch + } + + // search for the key in internal structure and set the data for it + for (l_itCfgStructure = m_pmCFGData->begin(); + l_itCfgStructure != m_pmCFGData->end(); + l_itCfgStructure++) { + CBlock *l_pBlock = (*l_itCfgStructure).second; + + if (!l_cHeadTag.compare((*l_itCfgStructure).first) && NULL != l_pBlock) { + for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) { + if (!l_cTag.compare(l_pBlock->m_vElementPair[l_uiCount].m_cTag)) { + l_pBlock->m_vElementPair[l_uiCount].m_cValue.assign(f_cvalue); + l_eStatus = eFrameworkunifiedStatusOK; + } + } + } + } + } else { + // LCOV_EXCL_START 6:f_cKey is not NULL, because checked in CCFGWriter::SetValue + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + l_eStatus = eFrameworkunifiedStatusNullPointer; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pmCFGData is NULL"); + // LCOV_EXCL_STOP + } + + return l_eStatus; +} + +EFrameworkunifiedStatus CCFGParser::CFGSaveData(const std::string &f_cfilepath) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + CFGData_Type::iterator l_itCfgStructure; + + if (NULL != m_pmCFGData) { // LCOV_EXCL_BR_LINE 5:m_pmCFGData cannot be NULL, because internal logic guarantee + std::ofstream l_objFile(f_cfilepath.c_str()); + + // check if file is open + if (l_objFile.is_open()) { + // write the updated CFG structure back to the file + for (l_itCfgStructure = m_pmCFGData->begin(); + l_itCfgStructure != m_pmCFGData->end(); + l_itCfgStructure++) { + CBlock *l_pBlock = (*l_itCfgStructure).second; + if (NULL != l_pBlock) { // LCOV_EXCL_BR_LINE 11:except branch + // write the main tag comment in file + if (!l_pBlock->m_cComment.empty()) { + l_objFile << l_pBlock->m_cComment << std::endl; + } + + // write main tag to file + l_objFile << "[" << (*l_itCfgStructure).first << "]" << std::endl; + + for (UI_32 l_uiCount = 0; l_uiCount < l_pBlock->m_vElementPair.size(); l_uiCount++) { + // comment over the key-value pair + if (!l_pBlock->m_vElementPair[l_uiCount].m_cComment.empty()) { + l_objFile << l_pBlock->m_vElementPair[l_uiCount].m_cComment << std::endl;; + } + + // key-value + l_objFile << l_pBlock->m_vElementPair[l_uiCount].m_cTag << "=" + << l_pBlock->m_vElementPair[l_uiCount].m_cValue << std::endl; + } + } + } + + // last comment + if (!m_cLastComment.empty()) { + l_objFile << m_cLastComment << std::endl; + } + + // close the file + l_objFile.close(); // LCOV_EXCL_BR_LINE 11:except branch + } else { + l_eStatus = eFrameworkunifiedStatusFail; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Cannot Open File"); + } + } else { + // LCOV_EXCL_START 5:m_pmCFGData cannot be NULL, because internal logic guarantee + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + l_eStatus = eFrameworkunifiedStatusNullPointer; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pmCFGData is NULL"); + // LCOV_EXCL_STOP + } + + return l_eStatus; +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_reader.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_reader.cpp new file mode 100644 index 00000000..9eeb3c00 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_reader.cpp @@ -0,0 +1,117 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of CCFGReader class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <ns_cfg_reader.h> +#include <ns_cfg_parser.h> +#include <ns_config_parser_frameworkunifiedlog.h> +#include <string> + + + +CCFGReader::CCFGReader(): m_cFilePath(""), m_pCFGParser(NULL) { // LCOV_EXCL_BR_LINE 11:except branch +} + +// LCOV_EXCL_START 200:CCFGReader is not external API class, this constructed function isn't used by other function +CCFGReader::CCFGReader(const std::string &f_c_filepath): m_cFilePath(f_c_filepath), m_pCFGParser(NULL) { + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", m_cFilePath.c_str()); + + m_pCFGParser = new CCFGParser(m_cFilePath); + + if (NULL == m_pCFGParser) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser"); + } +} +// LCOV_EXCL_STOP + +EFrameworkunifiedStatus CCFGReader::ParseFile(const std::string &f_c_filepath) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); + + // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + m_cFilePath.assign(f_c_filepath); + + m_pCFGParser = new(std::nothrow) CCFGParser(); // LCOV_EXCL_BR_LINE 11:except branch + + if (NULL == m_pCFGParser) { // LCOV_EXCL_BR_LINE 5:new's error case + // LCOV_EXCL_START 5:new's error case + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser"); + l_e_status = eFrameworkunifiedStatusNullPointer; + // LCOV_EXCL_STOP + } else { + l_e_status = m_pCFGParser->CFGParseFile(f_c_filepath); + } + + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-"); + return l_e_status; +} + +CCFGReader::~CCFGReader() { + if (NULL != m_pCFGParser) { + delete m_pCFGParser; + m_pCFGParser = NULL; + } +} + +PVOID CCFGReader::GetDataPtr() { + return m_pCFGParser; +} + +std::string CCFGReader::GetValue(const std::string &f_c_key) { + std::string l_cValue = ""; + + if (NULL != m_pCFGParser && !f_c_key.empty()) { // LCOV_EXCL_BR_LINE 11:except branch + // read the data from internal CFG structure + l_cValue = m_pCFGParser->CFGGetData(f_c_key); // LCOV_EXCL_BR_LINE 11:except branch + } + + return l_cValue; +} + +EFrameworkunifiedStatus CCFGReader::GetValue(const std::string &f_c_key, std::string &f_c_value) { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL == m_pCFGParser) { // LCOV_EXCL_BR_LINE 5:m_pCFGParser cannot be NULL, because internal logic guarantee + // LCOV_EXCL_START 5:m_pCFGParser cannot be NULL, because internal logic guarantee + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + l_e_status = eFrameworkunifiedStatusNullPointer; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Parser object(m_pCFGParser) is NULL"); + // LCOV_EXCL_STOP + } else if (f_c_key.empty()) { + l_e_status = eFrameworkunifiedStatusInvldParam; + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid Key"); + // LCOV_EXCL_BR_STOP + } else { + // read the data from internal CFG structure + l_e_status = m_pCFGParser->CFGGetData(f_c_key, f_c_value); + } + + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-"); + return l_e_status; +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_writer.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_writer.cpp new file mode 100644 index 00000000..4bf16d5f --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_cfg_writer.cpp @@ -0,0 +1,135 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of CCFGWriter class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <sys/types.h> +#include <sys/stat.h> +#include <stdio.h> +#include <unistd.h> + +#include <ns_cfg_parser.h> +#include <ns_cfg_writer.h> +#include <ns_config_parser_frameworkunifiedlog.h> + +#include <fstream> +#include <string> + + +CCFGWriter::CCFGWriter(): m_cFilePath(""), m_pCFGParser(NULL) { // LCOV_EXCL_BR_LINE 11:except branch + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); +} + +CCFGWriter::CCFGWriter(const std::string &f_c_filepath): m_cFilePath(f_c_filepath), m_pCFGParser(NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", m_cFilePath.c_str()); + + m_pCFGParser = new CCFGParser(m_cFilePath); + + if (NULL == m_pCFGParser) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser"); + } +} + +CCFGWriter::~CCFGWriter() { + if (NULL != m_pCFGParser) { + delete m_pCFGParser; // LCOV_EXCL_BR_LINE 11:except branch + m_pCFGParser = NULL; + } +} + +EFrameworkunifiedStatus CCFGWriter::ParseFile(const std::string &f_c_filepath) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); + + // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + m_cFilePath.assign(f_c_filepath); + + m_pCFGParser = new(std::nothrow) CCFGParser(); // LCOV_EXCL_BR_LINE 11:except branch + + if (NULL == m_pCFGParser) { // LCOV_EXCL_BR_LINE 5:m_pCFGParser cannot be NULL, because internal logic guarantee + // LCOV_EXCL_START 5:m_pCFGParser cannot be NULL, because internal logic guarantee + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Memory allocation error for m_pCFGParser"); + l_e_status = eFrameworkunifiedStatusNullPointer; + // LCOV_EXCL_STOP + } else { + l_e_status = m_pCFGParser->CFGParseFile(f_c_filepath); + } + + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-"); + return l_e_status; +} + +EFrameworkunifiedStatus CCFGWriter::SetPath(const std::string &f_c_path) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (!f_c_path.empty()) { // LCOV_EXCL_BR_LINE 11:except branch + // set the file path + m_cFilePath.assign(f_c_path); + } else { + l_e_status = eFrameworkunifiedStatusInvldParam; + } + + return l_e_status; +} + +VOID CCFGWriter::SetDataPtr(PVOID f_p_data) { + if (NULL != f_p_data) { + // set the doc pointer + m_pCFGParser = static_cast<CCFGParser *>(f_p_data); + } else { + m_pCFGParser = NULL; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Data Pointer not set in cfg writer"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + } +} + +EFrameworkunifiedStatus CCFGWriter::SetValue(const std::string &f_c_key, std::string f_c_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pCFGParser && !f_c_key.empty()) { + // set the data in internal CFG structure + l_e_status = m_pCFGParser->CFGSetData(f_c_key, f_c_value); // LCOV_EXCL_BR_LINE 11:except branch + } else { + l_e_status = eFrameworkunifiedStatusInvldParam; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CCFGWriter::SaveData() { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pCFGParser) { + if (0 == access(m_cFilePath.c_str(), W_OK)) { + // save the data permanently in config file + l_e_status = m_pCFGParser->CFGSaveData(m_cFilePath); + } else { + l_e_status = eFrameworkunifiedStatusFail; + } + } else { + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_reader.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_reader.cpp new file mode 100644 index 00000000..839053af --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_reader.cpp @@ -0,0 +1,346 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of CNSConfigReader class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <stdlib.h> +#include <ctype.h> +#include <dlfcn.h> +#include <errno.h> + +#include <native_service/ns_config_parser_if.h> +#include <native_service/ns_reader.h> + +#include <cstring> +#include <string> + +#include "ns_config_parser_frameworkunifiedlog.h" +#include "ns_cfg_reader.h" +CNSConfigReader::CNSConfigReader() { + m_pReader = NULL; +} + +CNSConfigReader::CNSConfigReader(const std::string &f_c_filepath): m_pReader(NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + if (eFrameworkunifiedStatusOK != Parse(f_c_filepath)) { + FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error Parsing file %s", f_c_filepath.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + } +} + +CNSConfigReader::~CNSConfigReader() { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor"); + + if (NULL != m_pReader) { + delete m_pReader; // LCOV_EXCL_BR_LINE 11:except branch + m_pReader = NULL; + } +} + +EFrameworkunifiedStatus CNSConfigReader::Parse(const std::string &f_c_filepath) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); + + // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + if (NULL != m_pReader) { + delete m_pReader; // LCOV_EXCL_BR_LINE 11:except branch + m_pReader = NULL; + } + + // create parser object depending on file extension + if (NULL != std::strstr(f_c_filepath.c_str(), ".cfg")) { + m_pReader = new(std::nothrow) CCFGReader(); // LCOV_EXCL_BR_LINE 11:except branch + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + l_e_status = eFrameworkunifiedStatusFail; + } + + if (NULL != m_pReader) { + l_e_status = m_pReader->ParseFile(f_c_filepath); + } else { + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-"); + return l_e_status; +} + +PVOID CNSConfigReader::GetDataPtr() { + if (NULL != m_pReader) { + // get the data pointer + return m_pReader->GetDataPtr(); + } else { + return NULL; + } +} + +BOOL CNSConfigReader::GetBool(const std::string &f_c_key) { + BOOL l_bResult = FALSE; + + if (NULL != m_pReader) { + std::string l_c_temp = m_pReader->GetValue(f_c_key); + + l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (!strcasecmp(l_c_temp.c_str(), "true")) { + l_bResult = TRUE; + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + } + + return l_bResult; +} + +F_64 CNSConfigReader::GetDouble(const std::string &f_c_key) { + F_64 l_fValue = 0; + + if (NULL != m_pReader) { + std::string l_c_temp = m_pReader->GetValue(f_c_key); + + errno = EOK; + + // convert string to double + l_fValue = strtod(l_c_temp.c_str(), NULL); + + if (EOK != errno) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to double, errno=%d", l_c_temp.c_str(), errno); + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + } + + return l_fValue; +} + +F_32 CNSConfigReader::GetFloat(const std::string &f_c_key) { + F_32 l_fValue = 0; + + if (NULL != m_pReader) { + std::string l_c_temp = m_pReader->GetValue(f_c_key); + errno = EOK; + l_fValue = strtof(l_c_temp.c_str(), NULL); + + if (EOK != errno) { + l_fValue = 0; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to float, errno=%d", l_c_temp.c_str(), errno); + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + } + + return l_fValue; +} + +SI_32 CNSConfigReader::GetInt(const std::string &f_c_key) { + UI_64 l_ui_value = 0; + + if (NULL != m_pReader) { + std::string l_c_temp = m_pReader->GetValue(f_c_key); + + errno = EOK; + + if ((l_c_temp.size() >= 3) && ('0' == l_c_temp[0]) && ('X' == toupper(l_c_temp[1]))) { + l_ui_value = strtoul(l_c_temp.c_str(), NULL, 16); + } else { + l_ui_value = strtoul(l_c_temp.c_str(), NULL, 10); + } + + if (EOK != errno) { + l_ui_value = 0; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to int, errno=%d", l_c_temp.c_str(), errno); + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + } + + return static_cast<SI_32>(l_ui_value); +} + +std::string CNSConfigReader::GetString(const std::string &f_c_key) { + std::string l_c_temp = ""; + + if (NULL != m_pReader) { + l_c_temp = m_pReader->GetValue(f_c_key); // LCOV_EXCL_BR_LINE 11:except branch + + l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); + } + + return l_c_temp; +} + +EFrameworkunifiedStatus CNSConfigReader::GetBool(const std::string &f_c_key, BOOL &f_b_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + std::string l_c_temp = ""; + + if (NULL != m_pReader) { + l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (eFrameworkunifiedStatusOK == l_e_status) { + l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (!strcasecmp(l_c_temp.c_str(), "true")) { + f_b_value = TRUE; + } else if (!strcasecmp(l_c_temp.c_str(), "false")) { + f_b_value = FALSE; + } else { + l_e_status = eFrameworkunifiedStatusErrOther; + } + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigReader::GetDouble(const std::string &f_c_key, F_64 &f_d_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + std::string l_c_temp = ""; + + if (NULL != m_pReader) { + l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (eFrameworkunifiedStatusOK == l_e_status) { + errno = EOK; + f_d_value = strtod(l_c_temp.c_str(), NULL); + + if (EOK != errno) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to double, errno=%d", l_c_temp.c_str(), errno); + l_e_status = eFrameworkunifiedStatusInvldBuf; + } + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigReader::GetFloat(const std::string &f_c_key, F_32 &f_f_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + std::string l_c_temp = ""; + + if (NULL != m_pReader) { + l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (eFrameworkunifiedStatusOK == l_e_status) { + errno = EOK; + f_f_value = strtof(l_c_temp.c_str(), NULL); + + if (EOK != errno) { + f_f_value = 0; + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to float, errno=%d", l_c_temp.c_str(), errno); + l_e_status = eFrameworkunifiedStatusInvldBuf; + } + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigReader::GetInt(const std::string &f_c_key, SI_32 &f_i_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + std::string l_c_temp = ""; + + UI_64 l_ui_value = 0; + + if (NULL != m_pReader) { + l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (eFrameworkunifiedStatusOK == l_e_status) { + errno = EOK; + + if ((l_c_temp.size() >= 3) && ('0' == l_c_temp[0]) && ('X' == toupper(l_c_temp[1]))) { + l_ui_value = strtoul(l_c_temp.c_str(), NULL, 16); + } else { + l_ui_value = strtoul(l_c_temp.c_str(), NULL, 10); + } + + f_i_value = static_cast<SI_32>(l_ui_value); + + if (EOK != errno) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error converting string %s to int, errno=%d", l_c_temp.c_str(), errno); + l_e_status = eFrameworkunifiedStatusInvldBuf; + } + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigReader::GetString(const std::string &f_c_key, std::string &f_c_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + std::string l_c_temp = ""; + + if (NULL != m_pReader) { + l_e_status = m_pReader->GetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + if (eFrameworkunifiedStatusOK == l_e_status) { + l_c_temp = FormatValue(l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + + f_c_value.assign(l_c_temp); + } + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Reader (m_pReader) is NULL"); + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +std::string CNSConfigReader::FormatValue(const std::string &f_c_value) { + std::string l_c_newvalue = f_c_value; + + if (0 < f_c_value.length()) { + UI_32 l_uiStrPos = static_cast<UI_32>(l_c_newvalue.length() - 1); + + while ('\n' == l_c_newvalue[l_uiStrPos] || '\r' == l_c_newvalue[l_uiStrPos]) { + l_c_newvalue = l_c_newvalue.substr(0, l_uiStrPos); + + if (0 == l_uiStrPos) { + break; + } + + l_uiStrPos--; + } + } + + return l_c_newvalue; +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_reader_writer.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_reader_writer.cpp new file mode 100644 index 00000000..6c2b2d6e --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_reader_writer.cpp @@ -0,0 +1,74 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of CNSConfigParser class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_config_parser_if.h> +#include <string> +#include "ns_config_parser_frameworkunifiedlog.h" + +CNSConfigParser::CNSConfigParser() : CNSConfigReader(), CNSConfigWriter() { // LCOV_EXCL_BR_LINE 11:except branch +} + +CNSConfigParser::CNSConfigParser(const std::string &f_c_filepath): CNSConfigReader(f_c_filepath), + CNSConfigWriter(f_c_filepath, TRUE) { // LCOV_EXCL_BR_LINE 11:except branch + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + // get the data pointer from the config reader + PVOID l_pdata = GetDataPtr(); // LCOV_EXCL_BR_LINE 11:except branch + + // set the same data pointer in config writer + SetDataPtr(l_pdata); // LCOV_EXCL_BR_LINE 11:except branch +} + +CNSConfigParser::~CNSConfigParser() { // LCOV_EXCL_BR_LINE 11:except branch + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor"); + + // set the data pointer to NULL in writer + SetDataPtr(NULL); // LCOV_EXCL_BR_LINE 11:except branch +} + +EFrameworkunifiedStatus CNSConfigParser::Parse(const std::string &f_c_filepath) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); + + // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + // parse and create config document structure with reader + if (eFrameworkunifiedStatusOK == (l_e_status = CNSConfigReader::Parse(f_c_filepath))) { + // set the data pointer to NULL in writer + SetDataPtr(NULL); + + // sets the same config document structure in writer + if (eFrameworkunifiedStatusOK == (l_e_status = CNSConfigWriter::Create(f_c_filepath))) { + // get the data pointer from the config reader + PVOID l_pdata = GetDataPtr(); + + // set the same data pointer in config writer + SetDataPtr(l_pdata); + } + } + + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-"); + return l_e_status; +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_writer.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_writer.cpp new file mode 100644 index 00000000..601500de --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_config_writer.cpp @@ -0,0 +1,227 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of CNSConfigWriter class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_config_parser_if.h> +#include <native_service/ns_writer.h> +#include <dlfcn.h> +#include <sstream> +#include <cstring> +#include <string> +#include "ns_cfg_writer.h" +#include "ns_config_parser_frameworkunifiedlog.h" + + +CNSConfigWriter::CNSConfigWriter(): m_pWriter(NULL) { +} + +CNSConfigWriter::CNSConfigWriter(const std::string &f_c_filepath): m_pWriter(NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + Parse(f_c_filepath); +} + +CNSConfigWriter::CNSConfigWriter(const std::string &f_c_filepath, BOOL f_b_noload): m_pWriter(NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_c_filepath.c_str()); + + Create(f_c_filepath); +} + +CNSConfigWriter::~CNSConfigWriter() { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor"); + + if (NULL != m_pWriter) { + delete m_pWriter; // LCOV_EXCL_BR_LINE 11:except branch + m_pWriter = NULL; + } + // TODO(my_username): dlclose. +} + +EFrameworkunifiedStatus CNSConfigWriter::Parse(const std::string &f_c_filepath) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + delete m_pWriter; // LCOV_EXCL_BR_LINE 11:except branch + m_pWriter = NULL; + } + + // create parser object depending on file extension + if (std::strstr(f_c_filepath.c_str(), ".cfg") != NULL) { + m_pWriter = new(std::nothrow) CCFGWriter(); // LCOV_EXCL_BR_LINE 11:except branch + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG"); + // LCOV_EXCL_BR_STOP + } + + if (NULL != m_pWriter) { + l_e_status = m_pWriter->ParseFile(f_c_filepath); + } else { + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigWriter::Create(const std::string &f_c_filepath) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + delete m_pWriter; // LCOV_EXCL_BR_LINE 11:except branch + m_pWriter = NULL; + } + + // create parser object depending on file extension + if (std::strstr(f_c_filepath.c_str(), ".cfg") != NULL) { + m_pWriter = new(std::nothrow) CCFGWriter(); // LCOV_EXCL_BR_LINE 11:except branch + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Not CFG"); + // LCOV_EXCL_BR_STOP + l_e_status = eFrameworkunifiedStatusFail; + } + + if (NULL != m_pWriter) { + l_e_status = m_pWriter->SetPath(f_c_filepath); + } else { + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +VOID CNSConfigWriter::SetDataPtr(PVOID f_p_data) { + if (NULL != m_pWriter) { + m_pWriter->SetDataPtr(f_p_data); + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pWriter is NULL"); + // LCOV_EXCL_BR_STOP + } +} + +EFrameworkunifiedStatus CNSConfigWriter::Save() { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + // save the updated config file + l_e_status = m_pWriter->SaveData(); + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); + // LCOV_EXCL_BR_STOP + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigWriter::SetBool(const std::string &f_c_key, BOOL f_b_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + std::string l_c_temp = ""; + + if (TRUE == f_b_value) { + l_c_temp = "TRUE"; + l_e_status = m_pWriter->SetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + } else if (FALSE == f_b_value) { // LCOV_EXCL_BR_LINE 11:except branch + l_c_temp = "FALSE"; + l_e_status = m_pWriter->SetValue(f_c_key, l_c_temp); // LCOV_EXCL_BR_LINE 11:except branch + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "received bool value is not proper:: %d", f_b_value); + l_e_status = eFrameworkunifiedStatusInvldParam; + } + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); + // LCOV_EXCL_BR_STOP + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, F_64 f_f_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + std::stringstream l_cStream; + l_cStream << f_f_value; + + l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str()); // LCOV_EXCL_BR_LINE 11:except branch + } else { + // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h" + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); + // LCOV_EXCL_BR_STOP + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, F_32 f_fValue) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { // LCOV_EXCL_BR_LINE 11:except branch + std::stringstream l_cStream; + l_cStream << f_fValue; + + l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str()); // LCOV_EXCL_BR_LINE 11:except branch + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, SI_32 f_si_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + std::stringstream l_cStream; + l_cStream << f_si_value; // LCOV_EXCL_BR_LINE 11:except branch + + l_e_status = m_pWriter->SetValue(f_cKey, l_cStream.str()); // LCOV_EXCL_BR_LINE 11:except branch + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); // LCOV_EXCL_BR_LINE 11:except branch + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} + +EFrameworkunifiedStatus CNSConfigWriter::Set(const std::string &f_cKey, const std::string &f_s_value) { + EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK; + + if (NULL != m_pWriter) { + l_e_status = m_pWriter->SetValue(f_cKey, f_s_value); // LCOV_EXCL_BR_LINE 11:except branch + } else { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Config Writer (m_pWriter) is NULL"); // LCOV_EXCL_BR_LINE 11:except branch + l_e_status = eFrameworkunifiedStatusNullPointer; + } + + return l_e_status; +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_reader.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_reader.cpp new file mode 100644 index 00000000..ff3cc7a4 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_reader.cpp @@ -0,0 +1,36 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of IConfigReader class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_reader.h> +#include <ns_config_parser_frameworkunifiedlog.h> + + +IConfigReader::IConfigReader() { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); +} + +IConfigReader::~IConfigReader() { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); +} diff --git a/nsframework/framework_unified/client/NS_ConfigParser/src/ns_writer.cpp b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_writer.cpp new file mode 100644 index 00000000..b2b52f08 --- /dev/null +++ b/nsframework/framework_unified/client/NS_ConfigParser/src/ns_writer.cpp @@ -0,0 +1,36 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NS_ConfigParser +/// \brief This file contains implementation of IConfigWriter class. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_writer.h> +#include <ns_config_parser_frameworkunifiedlog.h> + + +IConfigWriter::IConfigWriter() { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); +} + +IConfigWriter::~IConfigWriter() { + FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+"); +} |