From 99bd9bc48d3bd50ee5377036b918d4abc7d9ffb9 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 12:55:42 +0100 Subject: [PATCH 01/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 48 +++++++++---------- conanfile.py | 16 +++++++ rapidxml.hpp => include/rapidxml.hpp | 0 .../rapidxml_generator.hpp | 0 .../rapidxml_iterators.hpp | 0 .../rapidxml_predicates.hpp | 0 .../rapidxml_print.hpp | 0 .../rapidxml_utils.hpp | 0 .../rapidxml_wrappers.hpp | 0 CMakeLists.txt => test/CMakeLists.txt | 46 +++++++----------- test/conanfile.py | 32 +++++++++++++ test/{ => src}/iterators.cpp | 0 test/{ => src}/low-level-parse.cpp | 0 test/{ => src}/main.cc | 0 test/{ => src}/manipulations.cpp | 0 test/{ => src}/parse-simple.cpp | 0 test/{ => src}/perf.cpp | 0 test/{ => src}/round-trips.cpp | 0 test/{ => src}/xpath.cpp | 0 19 files changed, 86 insertions(+), 56 deletions(-) create mode 100644 conanfile.py rename rapidxml.hpp => include/rapidxml.hpp (100%) rename rapidxml_generator.hpp => include/rapidxml_generator.hpp (100%) rename rapidxml_iterators.hpp => include/rapidxml_iterators.hpp (100%) rename rapidxml_predicates.hpp => include/rapidxml_predicates.hpp (100%) rename rapidxml_print.hpp => include/rapidxml_print.hpp (100%) rename rapidxml_utils.hpp => include/rapidxml_utils.hpp (100%) rename rapidxml_wrappers.hpp => include/rapidxml_wrappers.hpp (100%) rename CMakeLists.txt => test/CMakeLists.txt (53%) create mode 100644 test/conanfile.py rename test/{ => src}/iterators.cpp (100%) rename test/{ => src}/low-level-parse.cpp (100%) rename test/{ => src}/main.cc (100%) rename test/{ => src}/manipulations.cpp (100%) rename test/{ => src}/parse-simple.cpp (100%) rename test/{ => src}/perf.cpp (100%) rename test/{ => src}/round-trips.cpp (100%) rename test/{ => src}/xpath.cpp (100%) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 60a9455..83f504e 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -14,38 +14,34 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Checkout Sentry Native - uses: actions/checkout@v4 - with: - repository: getsentry/sentry-native - path: sentry-native - - name: Apt dance - run: sudo apt-get update - - name: Install libcurl - run: sudo apt-get install libcurl4-openssl-dev - name: Set up Python 3.8 for gcovr uses: actions/setup-python@v4 - with: - python-version: 3.8 - - name: install gcovr 5.0 - run: | - pip install gcovr==5.0 # 5.1 is not supported - name: SonarQube install uses: SonarSource/sonarcloud-github-c-cpp@v3 - - name: Make build directory - run: mkdir gtest-build - - name: CMake - run: cmake -S . -B gtest-build -DCMAKE_BUILD_TYPE=Debug + - name: Install Conan + run: pip install conan + - name: Configure Conan Profile + run: | + conan profile new default --detect + conan profile update settings.compiler=gcc default + conan profile update settings.compiler.version=12 default + conan profile update settings.compiler.cppstd=23 default + - name: Conan Deps + run: conan install . --output-folder=gh-build -s build_type=Debug -b missing + - name: Create package + run: conan create . + - name: Conan deps for tests + run: cd tests && conan install . --output-folder=gh-build -s build_type=Debug -b missing + - name: CMake tests + run: cd tests && cmake -B gh-build -DCMAKE_TOOLCHAIN_FILE=gh-build/conan_toolchain.cmake - name: Build Wrapper - run: build-wrapper-linux-x86-64 --out-dir sonar-out cmake --build gtest-build - - name: Run Tests - run: cd ./gtest-build && ./rapidxml-test - - name: Show coverage - run: cd ./gtest-build && gcovr -r .. - - name: Collate coverage - run: cd ./gtest-build && gcovr -r .. --sonarqube >../coverage.xml + run: cd tests && build-wrapper-linux-x86-64 --out-dir sonar-out cmake --build gh-build - name: Sonar Scanner - run: sonar-scanner --define sonar.cfamily.compile-commands=sonar-out/compile_commands.json --define sonar.coverageReportPaths=coverage.xml + run: cd tests && sonar-scanner --define sonar.cfamily.compile-commands=sonar-out/compile_commands.json env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + - name: Upload + run: conan upload -r nexus 'flxml/*' + - name: Run Tests + run: cd gtest-build && ./rapidxml-test diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000..baaddb1 --- /dev/null +++ b/conanfile.py @@ -0,0 +1,16 @@ +from conan import ConanFile +from conan.tools.files import copy + +class FLXML(ConanFile): + name = "flxml" + version = "2.1.0" + exports_sources = "include/*" + no_copy_source = True + + def package(self): + copy(self, "include/*.hpp", self.source_folder, self.package_folder) + + def package_info(self): + self.cpp_info.includedirs = ['include'] + self.cpp_info.libdirs = [] + self.cpp_info.bindirs = [] diff --git a/rapidxml.hpp b/include/rapidxml.hpp similarity index 100% rename from rapidxml.hpp rename to include/rapidxml.hpp diff --git a/rapidxml_generator.hpp b/include/rapidxml_generator.hpp similarity index 100% rename from rapidxml_generator.hpp rename to include/rapidxml_generator.hpp diff --git a/rapidxml_iterators.hpp b/include/rapidxml_iterators.hpp similarity index 100% rename from rapidxml_iterators.hpp rename to include/rapidxml_iterators.hpp diff --git a/rapidxml_predicates.hpp b/include/rapidxml_predicates.hpp similarity index 100% rename from rapidxml_predicates.hpp rename to include/rapidxml_predicates.hpp diff --git a/rapidxml_print.hpp b/include/rapidxml_print.hpp similarity index 100% rename from rapidxml_print.hpp rename to include/rapidxml_print.hpp diff --git a/rapidxml_utils.hpp b/include/rapidxml_utils.hpp similarity index 100% rename from rapidxml_utils.hpp rename to include/rapidxml_utils.hpp diff --git a/rapidxml_wrappers.hpp b/include/rapidxml_wrappers.hpp similarity index 100% rename from rapidxml_wrappers.hpp rename to include/rapidxml_wrappers.hpp diff --git a/CMakeLists.txt b/test/CMakeLists.txt similarity index 53% rename from CMakeLists.txt rename to test/CMakeLists.txt index d0db150..264fbde 100644 --- a/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,60 +1,46 @@ cmake_minimum_required(VERSION 3.24) project(rapidxml) +# Include the Conan toolchain +include(${CMAKE_CURRENT_SOURCE_DIR}/conan_toolchain.cmake) + # GoogleTest requires at least C++14 set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) option(RAPIDXML_PERF_TESTS "Enable (very slow) performance tests" OFF) option(RAPIDXML_SENTRY "Use Sentry (for tests only)" ON) -include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip -) -# Used on Windows -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - -FetchContent_MakeAvailable(googletest) +find_package(GTest) +find_package(flxml CONFIG REQUIRED) if (RAPIDXML_SENTRY) set(SENTRY_BACKEND inproc) - add_subdirectory(sentry-native EXCLUDE_FROM_ALL) + find_package(sentry) endif(RAPIDXML_SENTRY) enable_testing() add_executable(rapidxml-test - test/parse-simple.cpp - rapidxml_iterators.hpp - rapidxml_print.hpp - rapidxml_utils.hpp - rapidxml.hpp - test/manipulations.cpp - test/round-trips.cpp - test/low-level-parse.cpp - test/perf.cpp - rapidxml_wrappers.hpp - test/iterators.cpp - rapidxml_predicates.hpp - test/xpath.cpp - rapidxml_generator.hpp - test/main.cc - rapidxml_predicates.hpp - rapidxml_tables.hpp + src/parse-simple.cpp + src/manipulations.cpp + src/round-trips.cpp + src/low-level-parse.cpp + src/perf.cpp + src/iterators.cpp + src/xpath.cpp + src/main.cc ) target_link_libraries(rapidxml-test PRIVATE GTest::gtest + flxml::flxml ) if(RAPIDXML_SENTRY) - target_link_libraries(rapidxml-test PRIVATE sentry) + target_link_libraries(rapidxml-test PRIVATE sentry-native::sentry-native) target_compile_definitions(rapidxml-test PRIVATE DWD_GTEST_SENTRY=1) endif() target_include_directories(rapidxml-test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) -target_compile_options(rapidxml-test PRIVATE -Werror -Wall --coverage -g -O0) -target_link_options(rapidxml-test PRIVATE --coverage -g) if (RAPIDXML_PERF_TESTS) message("Running performance tests") file(DOWNLOAD https://www.w3.org/TR/xml/REC-xml-20081126.xml ${CMAKE_CURRENT_BINARY_DIR}/REC-xml-20081126.xml) diff --git a/test/conanfile.py b/test/conanfile.py new file mode 100644 index 0000000..d2840b8 --- /dev/null +++ b/test/conanfile.py @@ -0,0 +1,32 @@ +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, CMakeDeps + +class FLXML(ConanFile): + name = "flxml-test" + version = "2.1.0" + settings = "os", "compiler", "build_type", "arch" + test_type = "explicit" + + def configure(self): + self.options["sentry-native"].backend = "inproc" + + def requirements(self): + self.requires(f'flxml/{self.version}') + self.requires("sentry-native/0.7.11") + self.requires("gtest/1.12.1") + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.user_presets_path = False + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + if not self.conf.get("tools.build:skip_test"): + self.run("./rapidxml-test", env="conanrun") diff --git a/test/iterators.cpp b/test/src/iterators.cpp similarity index 100% rename from test/iterators.cpp rename to test/src/iterators.cpp diff --git a/test/low-level-parse.cpp b/test/src/low-level-parse.cpp similarity index 100% rename from test/low-level-parse.cpp rename to test/src/low-level-parse.cpp diff --git a/test/main.cc b/test/src/main.cc similarity index 100% rename from test/main.cc rename to test/src/main.cc diff --git a/test/manipulations.cpp b/test/src/manipulations.cpp similarity index 100% rename from test/manipulations.cpp rename to test/src/manipulations.cpp diff --git a/test/parse-simple.cpp b/test/src/parse-simple.cpp similarity index 100% rename from test/parse-simple.cpp rename to test/src/parse-simple.cpp diff --git a/test/perf.cpp b/test/src/perf.cpp similarity index 100% rename from test/perf.cpp rename to test/src/perf.cpp diff --git a/test/round-trips.cpp b/test/src/round-trips.cpp similarity index 100% rename from test/round-trips.cpp rename to test/src/round-trips.cpp diff --git a/test/xpath.cpp b/test/src/xpath.cpp similarity index 100% rename from test/xpath.cpp rename to test/src/xpath.cpp From 85c676c5c82551c9619ff860bb7020efd573ac04 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 13:00:37 +0100 Subject: [PATCH 02/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 83f504e..f05a1dd 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -1,10 +1,8 @@ name: gtest on: - push: - branches: - - master - pull_request: + - push + - pull_request jobs: gtest: From bd86e197383f05a312f5dd03058f7099d26deca4 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 13:02:42 +0100 Subject: [PATCH 03/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index f05a1dd..4e62715 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -20,7 +20,7 @@ jobs: run: pip install conan - name: Configure Conan Profile run: | - conan profile new default --detect + conan profile new default detect conan profile update settings.compiler=gcc default conan profile update settings.compiler.version=12 default conan profile update settings.compiler.cppstd=23 default From 5d978af56527a8d64ce3ebeb4444b74c132328a0 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 13:03:54 +0100 Subject: [PATCH 04/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 4e62715..225db95 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -20,7 +20,7 @@ jobs: run: pip install conan - name: Configure Conan Profile run: | - conan profile new default detect + conan profile detect conan profile update settings.compiler=gcc default conan profile update settings.compiler.version=12 default conan profile update settings.compiler.cppstd=23 default From 18196d3aabbb2bb74a22b403e8652f45fa5e53b3 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 13:16:19 +0100 Subject: [PATCH 05/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 225db95..ec580c2 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -21,15 +21,12 @@ jobs: - name: Configure Conan Profile run: | conan profile detect - conan profile update settings.compiler=gcc default - conan profile update settings.compiler.version=12 default - conan profile update settings.compiler.cppstd=23 default - name: Conan Deps - run: conan install . --output-folder=gh-build -s build_type=Debug -b missing + run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing - name: Create package run: conan create . - name: Conan deps for tests - run: cd tests && conan install . --output-folder=gh-build -s build_type=Debug -b missing + run: cd tests && conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing - name: CMake tests run: cd tests && cmake -B gh-build -DCMAKE_TOOLCHAIN_FILE=gh-build/conan_toolchain.cmake - name: Build Wrapper From 8ce750e6f8297376d9aca2b0fcf11fd43549f345 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 13:51:08 +0100 Subject: [PATCH 06/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index ec580c2..dbdbfb1 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -21,22 +21,24 @@ jobs: - name: Configure Conan Profile run: | conan profile detect + conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan + conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing - name: Create package run: conan create . - name: Conan deps for tests - run: cd tests && conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing + run: cd test && conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing - name: CMake tests - run: cd tests && cmake -B gh-build -DCMAKE_TOOLCHAIN_FILE=gh-build/conan_toolchain.cmake + run: cd test && cmake -B gh-build -DCMAKE_TOOLCHAIN_FILE=gh-build/conan_toolchain.cmake - name: Build Wrapper - run: cd tests && build-wrapper-linux-x86-64 --out-dir sonar-out cmake --build gh-build + run: cd test && build-wrapper-linux-x86-64 --out-dir sonar-out cmake --build gh-build - name: Sonar Scanner - run: cd tests && sonar-scanner --define sonar.cfamily.compile-commands=sonar-out/compile_commands.json + run: cd test && sonar-scanner --define sonar.cfamily.compile-commands=sonar-out/compile_commands.json env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - name: Upload - run: conan upload -r nexus 'flxml/*' + run: conan upload -r conan-nexus 'flxml/*' - name: Run Tests run: cd gtest-build && ./rapidxml-test From 72d38e82e8d5cc046d70df4261d480dc5aa8e5a0 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 13:59:58 +0100 Subject: [PATCH 07/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index dbdbfb1..bf68885 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -28,9 +28,9 @@ jobs: - name: Create package run: conan create . - name: Conan deps for tests - run: cd test && conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing + run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing - name: CMake tests - run: cd test && cmake -B gh-build -DCMAKE_TOOLCHAIN_FILE=gh-build/conan_toolchain.cmake + run: cd test && cmake -B gh-build - name: Build Wrapper run: cd test && build-wrapper-linux-x86-64 --out-dir sonar-out cmake --build gh-build - name: Sonar Scanner From bac29bed6ddacf60c596522c008afdd73c98d7cd Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 14:55:29 +0100 Subject: [PATCH 08/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index bf68885..68d640a 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -30,7 +30,7 @@ jobs: - name: Conan deps for tests run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing - name: CMake tests - run: cd test && cmake -B gh-build + run: cd test && cmake -B gh-build -DCMAKE_BUILD_TYPE=Debug - name: Build Wrapper run: cd test && build-wrapper-linux-x86-64 --out-dir sonar-out cmake --build gh-build - name: Sonar Scanner From 1c1931c48649dc1e983a8e52c021b0f84403646e Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 15:29:03 +0100 Subject: [PATCH 09/21] Start work to rename to flxml --- rapidxml_tables.hpp => include/rapidxml_tables.hpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rapidxml_tables.hpp => include/rapidxml_tables.hpp (100%) diff --git a/rapidxml_tables.hpp b/include/rapidxml_tables.hpp similarity index 100% rename from rapidxml_tables.hpp rename to include/rapidxml_tables.hpp From c684c10e90801d295b8c1e83af913cb1cd77f834 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 15:34:11 +0100 Subject: [PATCH 10/21] Start work to rename to flxml --- sonar-project.properties => test/sonar-project.properties | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sonar-project.properties => test/sonar-project.properties (100%) diff --git a/sonar-project.properties b/test/sonar-project.properties similarity index 100% rename from sonar-project.properties rename to test/sonar-project.properties From 9f1627cbbdda0ab9f64697b633247c3812548726 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 15:59:08 +0100 Subject: [PATCH 11/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 68d640a..9a0cfad 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -12,6 +12,13 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + - name: Cache Conan2 dependencies + uses: actions/cache@v3 + with: + path: ~/.conan2 + key: ${{ runner.os }}-conan2-${{ hashFiles('**/conanfile.py') }} + restore-keys: | + ${{ runner.os }}-conan2- - name: Set up Python 3.8 for gcovr uses: actions/setup-python@v4 - name: SonarQube install @@ -39,6 +46,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - name: Upload - run: conan upload -r conan-nexus 'flxml/*' + run: conan upload -r conan-nexus --confirm 'flxml/*' - name: Run Tests run: cd gtest-build && ./rapidxml-test From 6076273e75186ae77bceed57dcc0ae8cc17ebaed Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 16:22:59 +0100 Subject: [PATCH 12/21] Start work to rename to flxml --- .github/workflows/gtest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 9a0cfad..ca4d1fb 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -45,7 +45,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + - name: Run Tests + run: cd test/gh-build && ./rapidxml-test - name: Upload run: conan upload -r conan-nexus --confirm 'flxml/*' - - name: Run Tests - run: cd gtest-build && ./rapidxml-test From 050f3ea24e866b829bbdc584a4c7b0e19b4c5e64 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 17:50:42 +0100 Subject: [PATCH 13/21] Automatic versioning --- .github/workflows/gtest.yml | 16 +++++++++++++--- conanfile.py | 1 - test/conanfile.py | 1 - 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index ca4d1fb..084bd3a 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -12,6 +12,16 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + - name: Figure out version + id: tag + run: | + TAG=$(git describe --tags --abbrev=0) + COMMITS_SINCE_TAG=$(git rev-list ${TAG}..HEAD --count) + if [ "${COMMITS_SINCE_TAG}" -eq 0 ]; then + echo "VERSION=${VERSION}" >> $GITHUB_ENV + else + echo "VERSION="$(git describe --tags --abbrev=8) >> $GITHUB_ENV + fi - name: Cache Conan2 dependencies uses: actions/cache@v3 with: @@ -31,11 +41,11 @@ jobs: conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps - run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing + run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --vgersion={{steps.tag.outputs.version }} - name: Create package - run: conan create . + run: conan create . --version={{steps.tag.outputs.version }} - name: Conan deps for tests - run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing + run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version={{steps.tag.outputs.version }} - name: CMake tests run: cd test && cmake -B gh-build -DCMAKE_BUILD_TYPE=Debug - name: Build Wrapper diff --git a/conanfile.py b/conanfile.py index baaddb1..fe93a36 100644 --- a/conanfile.py +++ b/conanfile.py @@ -3,7 +3,6 @@ class FLXML(ConanFile): name = "flxml" - version = "2.1.0" exports_sources = "include/*" no_copy_source = True diff --git a/test/conanfile.py b/test/conanfile.py index d2840b8..ac82b80 100644 --- a/test/conanfile.py +++ b/test/conanfile.py @@ -3,7 +3,6 @@ class FLXML(ConanFile): name = "flxml-test" - version = "2.1.0" settings = "os", "compiler", "build_type", "arch" test_type = "explicit" From 155a3bda25eeaa7998a5bfa8a463496ad7452632 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 17:52:44 +0100 Subject: [PATCH 14/21] Conan fix --- .github/workflows/gtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 084bd3a..87bce4a 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -37,7 +37,7 @@ jobs: run: pip install conan - name: Configure Conan Profile run: | - conan profile detect + conan profile detect -e conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps From 46f194bc46b66aa018861a3317da6d5d65f5bef0 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 17:56:18 +0100 Subject: [PATCH 15/21] Conan remote fix --- .github/workflows/gtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 87bce4a..2be9b1f 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -38,7 +38,7 @@ jobs: - name: Configure Conan Profile run: | conan profile detect -e - conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan + conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan --force conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --vgersion={{steps.tag.outputs.version }} From 5ebef78611336f349120b5f0e686af42f451309a Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 17:58:37 +0100 Subject: [PATCH 16/21] Conan typo fix --- .github/workflows/gtest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 2be9b1f..5a825fa 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -41,7 +41,7 @@ jobs: conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan --force conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps - run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --vgersion={{steps.tag.outputs.version }} + run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version={{steps.tag.outputs.version }} - name: Create package run: conan create . --version={{steps.tag.outputs.version }} - name: Conan deps for tests From e850ed156a0bdf82c33b003de83a6fc9ca0105de Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 18:01:19 +0100 Subject: [PATCH 17/21] More typos... --- .github/workflows/gtest.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index 5a825fa..c5ceac4 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -41,11 +41,11 @@ jobs: conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan --force conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps - run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version={{steps.tag.outputs.version }} + run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version=${{ steps.tag.outputs.version }} - name: Create package - run: conan create . --version={{steps.tag.outputs.version }} + run: conan create . --version=${{ steps.tag.outputs.version }} - name: Conan deps for tests - run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version={{steps.tag.outputs.version }} + run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version=${{ steps.tag.outputs.version }} - name: CMake tests run: cd test && cmake -B gh-build -DCMAKE_BUILD_TYPE=Debug - name: Build Wrapper From 2b6d461e196d3f2d4532a87ab91ae3419c2866a0 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 18:05:52 +0100 Subject: [PATCH 18/21] Envvar, not output --- .github/workflows/gtest.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gtest.yml b/.github/workflows/gtest.yml index c5ceac4..79e27c6 100644 --- a/.github/workflows/gtest.yml +++ b/.github/workflows/gtest.yml @@ -41,11 +41,11 @@ jobs: conan remote add conan-nexus https://nexus.cridland.io/repository/dwd-conan --force conan remote login conan-nexus ci --password ${{ secrets.NEXUS_PASSWORD }} - name: Conan Deps - run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version=${{ steps.tag.outputs.version }} + run: conan install . --output-folder=gh-build -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version=${{ env.VERSION }} - name: Create package - run: conan create . --version=${{ steps.tag.outputs.version }} + run: conan create . --version=${{ env.VERSION }} - name: Conan deps for tests - run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version=${{ steps.tag.outputs.version }} + run: cd test && conan install . --output-folder=. -s build_type=Debug -s compiler.cppstd=gnu23 -b missing --version=${{ env.VERSION }} - name: CMake tests run: cd test && cmake -B gh-build -DCMAKE_BUILD_TYPE=Debug - name: Build Wrapper From 9192e3ca3a3cd34f7a69f409ce3ee4043b3109f8 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Sat, 19 Apr 2025 19:42:13 +0100 Subject: [PATCH 19/21] Rename namespace, tidy file layout --- include/flxml.hpp | 2640 ++++++++++++++++ .../generator.hpp} | 2 +- .../iterators.hpp} | 804 ++--- .../predicates.hpp} | 20 +- include/flxml/print.hpp | 477 +++ .../{rapidxml_tables.hpp => flxml/tables.hpp} | 2 +- .../{rapidxml_utils.hpp => flxml/utils.hpp} | 244 +- .../wrappers.hpp} | 2 +- include/rapidxml.hpp | 2647 +---------------- include/rapidxml_print.hpp | 487 +-- test/src/iterators.cpp | 30 +- test/src/low-level-parse.cpp | 40 +- test/src/manipulations.cpp | 26 +- test/src/parse-simple.cpp | 82 +- test/src/perf.cpp | 40 +- test/src/round-trips.cpp | 61 +- test/src/xpath.cpp | 72 +- 17 files changed, 3846 insertions(+), 3830 deletions(-) create mode 100644 include/flxml.hpp rename include/{rapidxml_generator.hpp => flxml/generator.hpp} (98%) rename include/{rapidxml_iterators.hpp => flxml/iterators.hpp} (94%) rename include/{rapidxml_predicates.hpp => flxml/predicates.hpp} (96%) create mode 100644 include/flxml/print.hpp rename include/{rapidxml_tables.hpp => flxml/tables.hpp} (99%) rename include/{rapidxml_utils.hpp => flxml/utils.hpp} (95%) rename include/{rapidxml_wrappers.hpp => flxml/wrappers.hpp} (99%) diff --git a/include/flxml.hpp b/include/flxml.hpp new file mode 100644 index 0000000..c2f743f --- /dev/null +++ b/include/flxml.hpp @@ -0,0 +1,2640 @@ +#ifndef RAPIDXML_HPP_INCLUDED +#define RAPIDXML_HPP_INCLUDED + +// Copyright (C) 2006, 2009 Marcin Kalicinski +// Version 1.13 +// Revision $DateTime: 2009/05/13 01:46:17 $ +//! \file rapidxml.hpp This file contains rapidxml parser and DOM implementation + +#include "flxml/wrappers.hpp" +#include "flxml/tables.hpp" + +#include // For std::size_t +#include // For assert +#include // For placement new +#include +#include +#include +#include +#include // For std::runtime_error + +// On MSVC, disable "conditional expression is constant" warning (level 4). +// This warning is almost impossible to avoid with certain types of templated code +#ifdef _MSC_VER + #pragma warning(push) + #pragma warning(disable:4127) // Conditional expression is constant +#endif + +/////////////////////////////////////////////////////////////////////////// +// RAPIDXML_PARSE_ERROR + +#if defined(FLXML_NO_EXCEPTIONS) + +#define FLXML_PARSE_ERROR(what, where) { parse_error_handler(what, where); assert(0); } +#define FLML_EOF_ERROR(what, where) { parse_error_handler(what, where); assert(0); } + +namespace flxml +{ + //! When exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, + //! this function is called to notify user about the error. + //! It must be defined by the user. + //!

+ //! This function cannot return. If it does, the results are undefined. + //!

+ //! A very simple definition might look like that: + //!
+    //! void %rapidxml::%parse_error_handler(const char *what, void *where)
+    //! {
+    //!     std::cout << "Parse error: " << what << "\n";
+    //!     std::abort();
+    //! }
+    //! 
+ //! \param what Human readable description of the error. + //! \param where Pointer to character data where error was detected. + void parse_error_handler(const char *what, void *where); +} + +#else + +#define FLXML_PARSE_ERROR(what, where) {if (*where == Ch(0)) throw eof_error(what, nullptr); else throw parse_error(what, nullptr);} (void)0 +#define FLXML_EOF_ERROR(what, where) throw eof_error(what, nullptr) + +namespace flxml +{ + + //! Parse error exception. + //! This exception is thrown by the parser when an error occurs. + //! Use what() function to get human-readable error message. + //! Use where() function to get a pointer to position within source text where error was detected. + //!

+ //! If throwing exceptions by the parser is undesirable, + //! it can be disabled by defining RAPIDXML_NO_EXCEPTIONS macro before rapidxml.hpp is included. + //! This will cause the parser to call rapidxml::parse_error_handler() function instead of throwing an exception. + //! This function must be defined by the user. + //!

+ //! This class derives from std::exception class. + class parse_error: public std::runtime_error + { + + public: + + //! Constructs parse error + parse_error(const char *what, void *where) + : std::runtime_error(what) + , m_where(where) + { + } + + //! Gets pointer to character data where error happened. + //! Ch should be the same as char type of xml_document that produced the error. + //! \return Pointer to location within the parsed string where error occured. + template + Ch *where() const + { + return reinterpret_cast(m_where); + } + + private: + void *m_where; + }; + + class eof_error : public parse_error { + public: + using parse_error::parse_error; + }; + + class validation_error : public std::runtime_error + { + public: + using std::runtime_error::runtime_error; + }; + + class xmlns_unbound : public validation_error { + public: + using validation_error::validation_error; + }; + + class duplicate_attribute : public validation_error { + public: + using validation_error::validation_error; + }; + + class attr_xmlns_unbound : public xmlns_unbound { + public: + using xmlns_unbound::xmlns_unbound; + }; + + class element_xmlns_unbound : public xmlns_unbound { + public: + using xmlns_unbound::xmlns_unbound; + }; +} + +#endif + +/////////////////////////////////////////////////////////////////////////// +// Pool sizes + +#ifndef FLXML_STATIC_POOL_SIZE + // Size of static memory block of memory_pool. + // Define RAPIDXML_STATIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value. + // No dynamic memory allocations are performed by memory_pool until static memory is exhausted. + #define FLXML_STATIC_POOL_SIZE (64 * 1024) +#endif + +#ifndef FLXML_DYNAMIC_POOL_SIZE + // Size of dynamic memory block of memory_pool. + // Define RAPIDXML_DYNAMIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value. + // After the static block is exhausted, dynamic blocks with approximately this size are allocated by memory_pool. + #define FLXML_DYNAMIC_POOL_SIZE (64 * 1024) +#endif + +namespace flxml +{ + // Forward declarations + template class xml_node; + template class xml_attribute; + template class xml_document; + template class children; + template class descendants; + template class attributes; + + //! Enumeration listing all node types produced by the parser. + //! Use xml_node::type() function to query node type. + enum class node_type + { + node_document, //!< A document node. Name and value are empty. + node_element, //!< An element node. Name contains element name. Value contains text of first data node. + node_data, //!< A data node. Name is empty. Value contains data text. + node_cdata, //!< A CDATA node. Name is empty. Value contains data text. + node_comment, //!< A comment node. Name is empty. Value contains comment text. + node_declaration, //!< A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalone) are in node attributes. + node_doctype, //!< A DOCTYPE node. Name is empty. Value contains DOCTYPE text. + node_pi, //!< A PI node. Name contains target. Value contains instructions. + node_literal //!< Value is unencoded text (used for inserting pre-rendered XML). + }; + using enum node_type; // Import this into the rapidxml namespace as before. + + /////////////////////////////////////////////////////////////////////// + // Parsing flags + + //! Parse flag instructing the parser to not create data nodes. + //! Text of first data node will still be placed in value of parent element, unless rapidxml::parse_no_element_values flag is also specified. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_no_data_nodes = 0x1; + + //! Parse flag instructing the parser to not use text of first data node as a value of parent element. + //! Can be combined with other flags by use of | operator. + //! Note that child data nodes of element node take precendence over its value when printing. + //! That is, if element has one or more child data nodes and a value, the value will be ignored. + //! Use rapidxml::parse_no_data_nodes flag to prevent creation of data nodes if you want to manipulate data using values of elements. + //!

+ //! See xml_document::parse() function. + const int parse_no_element_values = 0x2; + + //! Parse flag instructing the parser to not translate entities in the source text. + //! By default entities are translated, modifying source text. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_no_entity_translation = 0x8; + + //! Parse flag instructing the parser to disable UTF-8 handling and assume plain 8 bit characters. + //! By default, UTF-8 handling is enabled. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_no_utf8 = 0x10; + + //! Parse flag instructing the parser to create XML declaration node. + //! By default, declaration node is not created. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_declaration_node = 0x20; + + //! Parse flag instructing the parser to create comments nodes. + //! By default, comment nodes are not created. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_comment_nodes = 0x40; + + //! Parse flag instructing the parser to create DOCTYPE node. + //! By default, doctype node is not created. + //! Although W3C specification allows at most one DOCTYPE node, RapidXml will silently accept documents with more than one. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_doctype_node = 0x80; + + //! Parse flag instructing the parser to create PI nodes. + //! By default, PI nodes are not created. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_pi_nodes = 0x100; + + //! Parse flag instructing the parser to validate closing tag names. + //! If not set, name inside closing tag is irrelevant to the parser. + //! By default, closing tags are not validated. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_validate_closing_tags = 0x200; + + //! Parse flag instructing the parser to trim all leading and trailing whitespace of data nodes. + //! By default, whitespace is not trimmed. + //! This flag does not cause the parser to modify source text. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_trim_whitespace = 0x400; + + //! Parse flag instructing the parser to condense all whitespace runs of data nodes to a single space character. + //! Trimming of leading and trailing whitespace of data is controlled by rapidxml::parse_trim_whitespace flag. + //! By default, whitespace is not normalized. + //! If this flag is specified, source text will be modified. + //! Can be combined with other flags by use of | operator. + //!

+ //! See xml_document::parse() function. + const int parse_normalize_whitespace = 0x800; + + //! Parse flag to say "Parse only the initial element opening." + //! Useful for XMLstreams used in XMPP. + const int parse_open_only = 0x1000; + + //! Parse flag to say "Toss the children of the top node and parse off + //! one element. + //! Useful for parsing off XMPP top-level elements. + const int parse_parse_one = 0x2000; + + //! Parse flag to say "Validate XML namespaces fully." + //! This will generate additional errors, including unbound prefixes + //! and duplicate attributes (with different prefices) + const int parse_validate_xmlns = 0x4000; + + // Compound flags + + //! Parse flags which represent default behaviour of the parser. + //! This is always equal to 0, so that all other flags can be simply ored together. + //! Normally there is no need to inconveniently disable flags by anding with their negated (~) values. + //! This also means that meaning of each flag is a negation of the default setting. + //! For example, if flag name is rapidxml::parse_no_utf8, it means that utf-8 is enabled by default, + //! and using the flag will disable it. + //!

+ //! See xml_document::parse() function. + [[maybe_unused]] const int parse_default = 0; + + //! A combination of parse flags resulting in fastest possible parsing, without sacrificing important data. + //!

+ //! See xml_document::parse() function. + const int parse_fastest = parse_no_data_nodes; + + //! A combination of parse flags resulting in largest amount of data being extracted. + //! This usually results in slowest parsing. + //!

+ //! See xml_document::parse() function. + const int parse_full = parse_declaration_node | parse_comment_nodes | parse_doctype_node | parse_pi_nodes | parse_validate_closing_tags | parse_validate_xmlns; + + + /////////////////////////////////////////////////////////////////////// + // Memory pool + + //! This class is used by the parser to create new nodes and attributes, without overheads of dynamic memory allocation. + //! In most cases, you will not need to use this class directly. + //! However, if you need to create nodes manually or modify names/values of nodes, + //! you are encouraged to use memory_pool of relevant xml_document to allocate the memory. + //! Not only is this faster than allocating them by using new operator, + //! but also their lifetime will be tied to the lifetime of document, + //! possibly simplyfing memory management. + //!

+ //! Call allocate_node() or allocate_attribute() functions to obtain new nodes or attributes from the pool. + //! You can also call allocate_string() function to allocate strings. + //! Such strings can then be used as names or values of nodes without worrying about their lifetime. + //! Note that there is no free() function -- all allocations are freed at once when clear() function is called, + //! or when the pool is destroyed. + //!

+ //! It is also possible to create a standalone memory_pool, and use it + //! to allocate nodes, whose lifetime will not be tied to any document. + //!

+ //! Pool maintains RAPIDXML_STATIC_POOL_SIZE bytes of statically allocated memory. + //! Until static memory is exhausted, no dynamic memory allocations are done. + //! When static memory is exhausted, pool allocates additional blocks of memory of size RAPIDXML_DYNAMIC_POOL_SIZE each, + //! by using global new[] and delete[] operators. + //! This behaviour can be changed by setting custom allocation routines. + //! Use set_allocator() function to set them. + //!

+ //! Allocations for nodes, attributes and strings are aligned at RAPIDXML_ALIGNMENT bytes. + //! This value defaults to the size of pointer on target architecture. + //!

+ //! To obtain absolutely top performance from the parser, + //! it is important that all nodes are allocated from a single, contiguous block of memory. + //! Otherwise, cache misses when jumping between two (or more) disjoint blocks of memory can slow down parsing quite considerably. + //! If required, you can tweak RAPIDXML_STATIC_POOL_SIZE, RAPIDXML_DYNAMIC_POOL_SIZE and RAPIDXML_ALIGNMENT + //! to obtain best wasted memory to performance compromise. + //! To do it, define their values before rapidxml.hpp file is included. + //! \param Ch Character type of created nodes. + template + class memory_pool + { + + public: + + //! \cond internal + using alloc_func = void * (*)(std::size_t); // Type of user-defined function used to allocate memory + using free_func = void (*)(void *); // Type of user-defined function used to free memory + //! \endcond + + //! Constructs empty pool with default allocator functions. + memory_pool() { + init(); + } + memory_pool(memory_pool const &) = delete; + memory_pool(memory_pool &&) = delete; + + //! Destroys pool and frees all the memory. + //! This causes memory occupied by nodes allocated by the pool to be freed. + //! Nodes allocated from the pool are no longer valid. + ~memory_pool() + { + clear(); + } + + using view_type = std::basic_string_view; + + //! Allocates a new node from the pool, and optionally assigns name and value to it. + //! If the allocation request cannot be accomodated, this function will throw std::bad_alloc. + //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function + //! will call rapidxml::parse_error_handler() function. + //! \param type Type of node to create. + //! \param name Name to assign to the node, or 0 to assign no name. + //! \param value Value to assign to the node, or 0 to assign no value. + //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string. + //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string. + //! \return Pointer to allocated node. This pointer will never be NULL. + template + xml_node * allocate_node_low(Args... args) { + void *memory = allocate_aligned>(); + auto *node = new(memory) xml_node(args...); + return node; + } + xml_node * allocate_node(node_type type, view_type const & name, view_type const & value) { + auto * node = this->allocate_node_low(type, name); + node->value(value); + return node; + } + xml_node * allocate_node(node_type type, view_type const & name) { + return this->allocate_node_low(type, name); + } + xml_node * allocate_node(node_type type) { + return this->allocate_node_low(type); + } + + //! Allocates a new attribute from the pool, and optionally assigns name and value to it. + //! If the allocation request cannot be accomodated, this function will throw std::bad_alloc. + //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function + //! will call rapidxml::parse_error_handler() function. + //! \param name Name to assign to the attribute, or 0 to assign no name. + //! \param value Value to assign to the attribute, or 0 to assign no value. + //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string. + //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string. + //! \return Pointer to allocated attribute. This pointer will never be NULL. + template + xml_attribute *allocate_attribute_low(Args... args) { + void *memory = allocate_aligned>(); + auto *attribute = new(memory) xml_attribute(args...); + return attribute; + } + xml_attribute * allocate_attribute(view_type const & name, view_type const & value) { + auto * attr = this->allocate_attribute_low(name); + attr->value(value); + return attr; + } + xml_attribute * allocate_attribute(view_type const & name) { + return this->allocate_attribute_low(name); + } + xml_attribute * allocate_attribute() { + return this->allocate_attribute_low(); + } + + //! Allocates a char array of given size from the pool, and optionally copies a given string to it. + //! If the allocation request cannot be accomodated, this function will throw std::bad_alloc. + //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function + //! will call rapidxml::parse_error_handler() function. + //! \param source String to initialize the allocated memory with, or 0 to not initialize it. + //! \param size Number of characters to allocate, or zero to calculate it automatically from source string length; if size is 0, source string must be specified and null terminated. + //! \return Pointer to allocated char array. This pointer will never be NULL. + template + std::span allocate_span(std::basic_string_view const & source) + { + if (source.size() == 0) return {}; // No need to allocate. + Ch *result = allocate_aligned(source.size()); + for (std::size_t i = 0; i < source.size(); ++i) + result[i] = source[i]; + return {result, source.size()}; + } + + template + view_type allocate_string(std::basic_string_view const & source) { + auto span = allocate_span(source); + return {span.data(), span.size()}; + } + + template + view_type allocate_string(std::basic_string const & source) { + return allocate_string(std::basic_string_view{source.data(), source.size()}); + } + + template + view_type allocate_string(const Sch * source) { + return allocate_string(std::basic_string_view(source)); + } + + view_type const & nullstr() + { + return m_nullstr; + } + view_type const & xmlns_xml() + { + if (m_xmlns_xml.empty()) + m_xmlns_xml = allocate_string("http://www.w3.org/XML/1998/namespace"); + return m_xmlns_xml; + } + view_type const & xmlns_xmlns() + { + if (m_xmlns_xmlns.empty()) + m_xmlns_xmlns = allocate_string("http://www.w3.org/2000/xmlns/"); + return m_xmlns_xmlns; + } + + + //! Clones an xml_node and its hierarchy of child nodes and attributes. + //! Nodes and attributes are allocated from this memory pool. + //! Names and values are not cloned, they are shared between the clone and the source. + //! Result node can be optionally specified as a second parameter, + //! in which case its contents will be replaced with cloned source node. + //! This is useful when you want to clone entire document. + //! \param source Node to clone. + //! \param result Node to put results in, or 0 to automatically allocate result node + //! \return Pointer to cloned node. This pointer will never be NULL. + optional_ptr> clone_node(const optional_ptr> source, bool strings=false) + { + // Prepare result node + auto result = allocate_node(source->type()); + auto s = [this, strings](view_type const & sv) { return strings ? this->allocate_string(sv) : sv; }; + + // Clone name and value + result->name(s(source->name())); + result->value(s(source->value())); + result->prefix(s(source->prefix())); + + // Clone child nodes and attributes + for (auto child = source->first_node(); child; child = child->next_sibling()) + result->append_node(clone_node(child, strings)); + for (auto attr = source->first_attribute(); attr; attr = attr->next_attribute()) + result->append_attribute(allocate_attribute(s(attr->name()), s(attr->value()))); + + return result; + } + + //! Clears the pool. + //! This causes memory occupied by nodes allocated by the pool to be freed. + //! Any nodes or strings allocated from the pool will no longer be valid. + void clear() + { + while (m_begin != m_static_memory.data()) + { + std::size_t s = sizeof(header) * 2; + void * h = m_begin; + std::align(alignof(header), sizeof(header), h, s); + void *previous_begin = reinterpret_cast
(h)->previous_begin; + if (m_free_func) + m_free_func(m_begin); + else + delete[] reinterpret_cast(m_begin); + m_begin = previous_begin; + } + init(); + } + + //! Sets or resets the user-defined memory allocation functions for the pool. + //! This can only be called when no memory is allocated from the pool yet, otherwise results are undefined. + //! Allocation function must not return invalid pointer on failure. It should either throw, + //! stop the program, or use longjmp() function to pass control to other place of program. + //! If it returns invalid pointer, results are undefined. + //!

+ //! User defined allocation functions must have the following forms: + //!
+ //!
void *allocate(std::size_t size); + //!
void free(void *pointer); + //!

+ //! \param af Allocation function, or 0 to restore default function + //! \param ff Free function, or 0 to restore default function + [[maybe_unused]] void set_allocator(alloc_func af, free_func ff) + { + assert(m_begin == m_static_memory.data() && m_ptr == m_begin); // Verify that no memory is allocated yet + m_alloc_func = af; + m_free_func = ff; + } + + private: + + struct header + { + void *previous_begin; + }; + + void init() + { + m_begin = m_static_memory.data(); + m_ptr = m_begin; + m_space = m_static_memory.size(); + } + + void *allocate_raw(std::size_t size) + { + // Allocate + void *memory; + if (m_alloc_func) // Allocate memory using either user-specified allocation function or global operator new[] + { + memory = m_alloc_func(size); + assert(memory); // Allocator is not allowed to return 0, on failure it must either throw, stop the program or use longjmp + } + else + { + memory = new char[size]; +#ifdef FLXML_NO_EXCEPTIONS + if (!memory) // If exceptions are disabled, verify memory allocation, because new will not be able to throw bad_alloc + FLXML_PARSE_ERROR("out of memory", 0); +#endif + } + return memory; + } + + template + T *allocate_aligned(std::size_t n = 1) + { + auto size = n * sizeof(T); + // Calculate aligned pointer + if (!std::align(alignof(T), sizeof(T) * n, m_ptr, m_space)) { + // If not enough memory left in current pool, allocate a new pool + // Calculate required pool size (may be bigger than RAPIDXML_DYNAMIC_POOL_SIZE) + std::size_t pool_size = FLXML_DYNAMIC_POOL_SIZE; + if (pool_size < size) + pool_size = size; + + // Allocate + std::size_t alloc_size = sizeof(header) + (2 * alignof(header) - 2) + pool_size; // 2 alignments required in worst case: one for header, one for actual allocation + void *raw_memory = allocate_raw(alloc_size); + + // Setup new pool in allocated memory + void *new_header = raw_memory; + std::align(alignof(header), sizeof(header), new_header, alloc_size); + auto * h = reinterpret_cast
(new_header); + h->previous_begin = m_begin; + m_begin = raw_memory; + m_ptr = (h + 1); + m_space = alloc_size - sizeof(header); + + // Calculate aligned pointer again using new pool + return allocate_aligned(n); + } + auto * result = reinterpret_cast(m_ptr); + m_ptr = (result + n); + m_space -= size; + auto blank = reinterpret_cast(result); + auto end = blank + size; + while (blank != end) *blank++ = 'X'; + return result; + } + + void *m_begin = nullptr; // Start of raw memory making up current pool + void *m_ptr = nullptr; // First free byte in current pool + std::size_t m_space = FLXML_STATIC_POOL_SIZE; // Available space remaining + std::array m_static_memory = {}; // Static raw memory + alloc_func m_alloc_func = nullptr; // Allocator function, or 0 if default is to be used + free_func m_free_func = nullptr; // Free function, or 0 if default is to be used + view_type m_nullstr; + view_type m_xmlns_xml; + view_type m_xmlns_xmlns; + }; + + /////////////////////////////////////////////////////////////////////////// + // XML base + + //! Base class for xml_node and xml_attribute implementing common functions: + //! name(), name_size(), value(), value_size() and parent(). + //! \param Ch Character type to use + template + class xml_base + { + + public: + using view_type = std::basic_string_view; + + /////////////////////////////////////////////////////////////////////////// + // Construction & destruction + + // Construct a base with empty name, value and parent + xml_base() = default; + explicit xml_base(view_type const & name) : m_name(name) {} + xml_base(view_type const & name, view_type const & value) : m_name(name), m_value(value) {} + + /////////////////////////////////////////////////////////////////////////// + // Node data access + + //! Gets name of the node. + //! Interpretation of name depends on type of node. + //! Note that name will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse. + //!

+ //! Use name_size() function to determine length of the name. + //! \return Name of node, or empty string if node has no name. + view_type const & name() const + { + return m_name; + } + + //! Gets value of node. + //! Interpretation of value depends on type of node. + //! Note that value will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse. + //!

+ //! Use value_size() function to determine length of the value. + //! \return Value of node, or empty string if node has no value. + view_type const & value_raw() const + { + return m_value; + } + + /////////////////////////////////////////////////////////////////////////// + // Node modification + + //! Sets name of node to a non zero-terminated string. + //! See \ref ownership_of_strings. + //!

+ //! Note that node does not own its name or value, it only stores a pointer to it. + //! It will not delete or otherwise free the pointer on destruction. + //! It is reponsibility of the user to properly manage lifetime of the string. + //! The easiest way to achieve it is to use memory_pool of the document to allocate the string - + //! on destruction of the document the string will be automatically freed. + //!

+ //! Size of name must be specified separately, because name does not have to be zero terminated. + //! Use name(const Ch *) function to have the length automatically calculated (string must be zero terminated). + //! \param name Name of node to set. Does not have to be zero terminated. + //! \param size Size of name, in characters. This does not include zero terminator, if one is present. + void name(view_type const & name) { + m_name = name; + } + + //! Sets value of node to a non zero-terminated string. + //! See \ref ownership_of_strings. + //!

+ //! Note that node does not own its name or value, it only stores a pointer to it. + //! It will not delete or otherwise free the pointer on destruction. + //! It is reponsibility of the user to properly manage lifetime of the string. + //! The easiest way to achieve it is to use memory_pool of the document to allocate the string - + //! on destruction of the document the string will be automatically freed. + //!

+ //! Size of value must be specified separately, because it does not have to be zero terminated. + //! Use value(const Ch *) function to have the length automatically calculated (string must be zero terminated). + //!

+ //! If an element has a child node of type node_data, it will take precedence over element value when printing. + //! If you want to manipulate data of elements using values, use parser flag rapidxml::parse_no_data_nodes to prevent creation of data nodes by the parser. + //! \param value value of node to set. Does not have to be zero terminated. + //! \param size Size of value, in characters. This does not include zero terminator, if one is present. + void value_raw(view_type const & value) + { + m_value = value; + } + + /////////////////////////////////////////////////////////////////////////// + // Related nodes access + + //! Gets node parent. + //! \return Pointer to parent node, or 0 if there is no parent. + optional_ptr> parent() const + { + return m_parent; + } + + protected: + view_type m_name; // Name of node, or 0 if no name + view_type m_value; // Value of node, or 0 if no value + xml_node *m_parent = nullptr; // Pointer to parent node, or 0 if none + }; + + //! Class representing attribute node of XML document. + //! Each attribute has name and value strings, which are available through name() and value() functions (inherited from xml_base). + //! Note that after parse, both name and value of attribute will point to interior of source text used for parsing. + //! Thus, this text must persist in memory for the lifetime of attribute. + //! \param Ch Character type to use. + template + class xml_attribute: public xml_base + { + + friend class xml_node; + + public: + using view_type = std::basic_string_view; + using ptr = optional_ptr>; + + /////////////////////////////////////////////////////////////////////////// + // Construction & destruction + + //! Constructs an empty attribute with the specified type. + //! Consider using memory_pool of appropriate xml_document if allocating attributes manually. + xml_attribute() = default; + xml_attribute(view_type const & name) : xml_base(name) {} + xml_attribute(view_type const & name, view_type const & value) : xml_base(name, value) {} + + void quote(Ch q) { + m_quote = q; + } + Ch quote() const { + return m_quote; + } + + view_type const & value() const { + if (m_value.has_value()) return m_value.value(); + m_value = document()->decode_attr_value(this); + return m_value.value(); + } + void value(view_type const & v) { + m_value = v; + this->value_raw(""); + if (this->m_parent) this->m_parent->dirty_parent(); + } + // Return true if the value has been decoded. + bool value_decoded() const { + // Either we don't have a decoded value, or we do but it's identical. + return !m_value.has_value() || m_value.value().data() != this->value_raw().data(); + } + + /////////////////////////////////////////////////////////////////////////// + // Related nodes access + + //! Gets document of which attribute is a child. + //! \return Pointer to document that contains this attribute, or 0 if there is no parent document. + optional_ptr> document() const { + if (auto node = this->parent()) { + return node->document(); + } else { + return nullptr; + } + } + + view_type const & xmlns() const { + if (m_xmlns.has_value()) return m_xmlns.value(); + auto const & name = this->name(); + auto colon = name.find(':'); + if (colon != view_type::npos) { + auto element = this->parent(); + if (element) m_xmlns = element->xmlns_lookup(name.substr(0, colon), true); + } else { + m_xmlns = document()->nullstr(); + } + return m_xmlns.value(); + } + //! Gets previous attribute, optionally matching attribute name. + //! \param name Name of attribute to find, or 0 to return previous attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found attribute, or 0 if not found. + optional_ptr> previous_attribute(view_type const & name = {}) const + { + if (name) + { + for (xml_attribute *attribute = m_prev_attribute; attribute; attribute = attribute->m_prev_attribute) + if (name == attribute->name()) + return attribute; + return 0; + } + else + return this->m_parent ? m_prev_attribute : 0; + } + + //! Gets next attribute, optionally matching attribute name. + //! \param name Name of attribute to find, or 0 to return next attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found attribute, or 0 if not found. + optional_ptr> next_attribute(view_type const & name = {}) const + { + if (!name.empty()) + { + for (xml_attribute *attribute = m_next_attribute; attribute; attribute = attribute->m_next_attribute) + if (attribute->name() == name) + return attribute; + return nullptr; + } + else + return this->m_parent ? m_next_attribute : nullptr; + } + + view_type const & local_name() const + { + if (!m_local_name.empty()) return m_local_name; + auto colon = this->name().find(':'); + if (colon == view_type::npos) { + m_local_name = this->name(); + } else { + m_local_name = this->name().substr(colon + 1); + } + return m_local_name; + } + + private: + + xml_attribute *m_prev_attribute = nullptr; // Pointer to previous sibling of attribute, or 0 if none; only valid if parent is non-zero + xml_attribute *m_next_attribute = nullptr; // Pointer to next sibling of attribute, or 0 if none; only valid if parent is non-zero + Ch m_quote = 0; // When parsing, this should be set to the containing quote for the value. + mutable std::optional m_xmlns; + mutable std::optional m_value; // This is the decoded, not raw, value. + mutable view_type m_local_name; // ATTN: points inside m_name. + }; + + /////////////////////////////////////////////////////////////////////////// + // XML node + + //! Class representing a node of XML document. + //! Each node may have associated name and value strings, which are available through name() and value() functions. + //! Interpretation of name and value depends on type of the node. + //! Type of node can be determined by using type() function. + //!

+ //! Note that after parse, both name and value of node, if any, will point interior of source text used for parsing. + //! Thus, this text must persist in the memory for the lifetime of node. + //! \param Ch Character type to use. + template + class xml_node: public xml_base + { + public: + using view_type = std::basic_string_view; + using ptr = optional_ptr>; + + /////////////////////////////////////////////////////////////////////////// + // Construction & destruction + + //! Constructs an empty node with the specified type. + //! Consider using memory_pool of appropriate document to allocate nodes manually. + //! \param type Type of node to construct. + explicit xml_node(node_type type) + : m_type(type) + { + } + xml_node(node_type type, view_type const & name) : xml_base(name), m_type(type) {} + xml_node(node_type type, view_type const & name, view_type const & value) : xml_base(name, value), m_type(type) {} + + /////////////////////////////////////////////////////////////////////////// + // Node data access + view_type const & value() const { + if (m_value.has_value()) return m_value.value(); + if (m_type == node_element || m_type == node_data) { + m_value = document()->decode_data_value(this); + } else { + m_value = this->value_raw(); + } + return m_value.value(); + } + + void dirty() { + m_clean = false; + dirty_parent(); + } + void dirty_parent() { + if (this->m_parent) this->m_parent->dirty(); + } + bool clean() const { + return m_clean; + } + + void value(view_type const & v) { + if (this->m_type == node_element) { + // Set the first data node to the value, if one exists. + for (auto node = m_first_node; node; node = node->m_next_sibling) { + if (node->type() == node_data) { + node->value(v); + break; + } + } + } + m_value = v; + this->value_raw(""); + dirty(); + } + + bool value_decoded() const { + return !m_value.has_value() || m_value.value().data() != this->value_raw().data(); + } + + //! Gets type of node. + //! \return Type of node. + node_type type() const { + return m_type; + } + + void prefix(view_type const & prefix) { + m_prefix = prefix; + dirty_parent(); + } + + view_type const & prefix() const { + return m_prefix; + } + + void contents(view_type const & contents) { + m_contents = contents; + // Reset to clean here. + m_clean = true; + } + view_type const & contents() const + { + return m_contents; + } + + view_type const & xmlns() const { + if (m_xmlns.has_value()) return m_xmlns.value(); + m_xmlns = xmlns_lookup(m_prefix, false); + return m_xmlns.value(); + } + + view_type const & xmlns_lookup(view_type const & prefix, bool attribute) const + { + std::basic_string attrname{"xmlns"}; + if (!prefix.empty()) { + // Check if the prefix begins "xml". + if (prefix.size() >= 3 && prefix.starts_with("xml")) { + if (prefix.size() == 3) { + return this->document()->xmlns_xml(); + } else if (prefix.size() == 5 + && prefix[3] == Ch('n') + && prefix[4] == Ch('s')) { + return this->document()->xmlns_xmlns(); + } + } + attrname += ':'; + attrname += prefix; + } + for (const xml_node * node = this; + node; + node = node->m_parent) { + auto attr = node->first_attribute(attrname); + if (attr) { + return attr->value(); + } + } + if (!prefix.empty()) { + if (attribute) { + throw attr_xmlns_unbound(attrname.c_str()); + } else { + throw element_xmlns_unbound(attrname.c_str()); + } + } + return document()->nullstr(); + } + + /////////////////////////////////////////////////////////////////////////// + // Related nodes access + + //! Gets document of which node is a child. + //! \return Pointer to document that contains this node, or 0 if there is no parent document. + optional_ptr> document() const + { + auto *node = this; + while (node) { + if (node->type() == node_document) { + return static_cast *>(const_cast *>(node)); + } + node = node->parent().ptr_unsafe(); + } + return nullptr; + } + + flxml::children children() const { + return flxml::children{*this}; + } + + flxml::descendants descendants() const { + return flxml::descendants{optional_ptr>{const_cast *>(this)}}; + } + + flxml::attributes attributes() const { + return flxml::attributes{*this}; + } + + //! Gets first child node, optionally matching node name. + //! \param name Name of child to find, or 0 to return first child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found child, or 0 if not found. + optional_ptr> first_node(view_type const & name = {}, view_type const & asked_xmlns = {}) const + { + view_type xmlns = asked_xmlns; + if (asked_xmlns.empty() && !name.empty()) { + // No XMLNS asked for, but a name is present. + // Assume "same XMLNS". + xmlns = this->xmlns(); + } + for (xml_node *child = m_first_node; child; child = child->m_next_sibling) { + if ((name.empty() || child->name() == name) + && (xmlns.empty() || child->xmlns() == xmlns)) { + return child; + } + } + return nullptr; + } + + //! Gets last child node, optionally matching node name. + //! Behaviour is undefined if node has no children. + //! Use first_node() to test if node has children. + //! \param name Name of child to find, or 0 to return last child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found child, or 0 if not found. + optional_ptr> last_node(view_type const & name = {}, view_type const & asked_xmlns = {}) const + { + view_type xmlns = asked_xmlns; + if (asked_xmlns.empty() && !name.empty()) { + // No XMLNS asked for, but a name is present. + // Assume "same XMLNS". + xmlns = this->xmlns(); + } + for (xml_node *child = m_last_node; child; child = child->m_prev_sibling) { + if ((name.empty() || child->name() == name) + && (xmlns.empty() || child->xmlns() == xmlns)) { + return child; + } + } + return nullptr; + } + + //! Gets previous sibling node, optionally matching node name. + //! Behaviour is undefined if node has no parent. + //! Use parent() to test if node has a parent. + //! \param name Name of sibling to find, or 0 to return previous sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found sibling, or 0 if not found. + optional_ptr> previous_sibling(view_type const & name = {}, view_type const & asked_xmlns = {}) const + { + assert(this->m_parent); // Cannot query for siblings if node has no parent + if (!name.empty()) + { + view_type xmlns = asked_xmlns; + if (xmlns.empty() && !name.empty()) { + // No XMLNS asked for, but a name is present. + // Assume "same XMLNS". + xmlns = this->xmlns(); + } + for (xml_node *sibling = m_prev_sibling; sibling; sibling = sibling->m_prev_sibling) + if ((name.empty() || sibling->name() == name) + && (xmlns.empty() || sibling->xmlns() == xmlns)) + return sibling; + return nullptr; + } + else + return m_prev_sibling; + } + + //! Gets next sibling node, optionally matching node name. + //! Behaviour is undefined if node has no parent. + //! Use parent() to test if node has a parent. + //! \param name Name of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param xmlns Namespace of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found sibling, or 0 if not found. + optional_ptr> next_sibling(view_type const & name = {}, view_type const & asked_xmlns = {}) const + { + assert(this->m_parent); // Cannot query for siblings if node has no parent + view_type xmlns = asked_xmlns; + if (xmlns.empty() && !name.empty()) { + // No XMLNS asked for, but a name is present. + // Assume "same XMLNS". + xmlns = this->xmlns(); + } + for (xml_node *sibling = m_next_sibling; sibling; sibling = sibling->m_next_sibling) + if ((name.empty() || sibling->name() == name) + && (xmlns.empty() || sibling->xmlns() == xmlns)) + return sibling; + return nullptr; + } + + //! Gets first attribute of node, optionally matching attribute name. + //! \param name Name of attribute to find, or 0 to return first attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found attribute, or 0 if not found. + optional_ptr> first_attribute(view_type const & name = {}, view_type const & xmlns = {}) const + { + for (xml_attribute *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) + if ((name.empty() || attribute->name() == name) && (xmlns.empty() || attribute->xmlns() == xmlns)) + return attribute; + return nullptr; + } + + //! Gets last attribute of node, optionally matching attribute name. + //! \param name Name of attribute to find, or 0 to return last attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero + //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string + //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters + //! \return Pointer to found attribute, or 0 if not found. + optional_ptr> last_attribute(view_type const & name = {}, view_type const & xmlns = {}) const + { + for (xml_attribute *attribute = m_last_attribute; attribute; attribute = attribute->m_prev_attribute) + if ((name.empty() || attribute->name() == name) && (xmlns.empty() || attribute->xmlns() == xmlns)) + return attribute; + return nullptr; + } + + /////////////////////////////////////////////////////////////////////////// + // Node modification + + //! Sets type of node. + //! \param type Type of node to set. + void type(node_type type) { + m_type = type; + dirty(); + } + + /////////////////////////////////////////////////////////////////////////// + // Node manipulation + + //! Allocate a new element to be added as a child at this node. + //! If an XMLNS is specified via the clarke notation syntax, then the prefix will match the parent element (if any), + //! and any needed xmlns attributes will be added for you. + //! Strings are assumed to remain in scope - you should document()->allocate_string() any that might not. + //! \param name Name of the element, either string view, string, or clarke notation + protected: // These are too easy to accidentally forget to append, prepend, or insert. + optional_ptr> allocate_element(view_type const & name) { + return document()->allocate_node(node_element, name); + } + optional_ptr> allocate_element(std::tuple const & clark_name) { + auto [xmlns, name] = clark_name; + xml_node * child; + if (xmlns != this->xmlns()) { + child = document()->allocate_node(node_element, name); + child->append_attribute(document()->allocate_attribute("xmlns", xmlns)); + } else if (!this->prefix().empty()) { + std::basic_string pname = std::string(this->prefix()) + ':'; + pname += name; + child = document()->allocate_node(node_element, document()->allocate_string(pname)); + } else { + child = document()->allocate_node(node_element, name); + } + return child; + } + optional_ptr> allocate_element(view_type const & name, view_type const & value) { + auto child = allocate_element(name); + child->value(value); + return child; + } + optional_ptr> allocate_element(std::tuple const & clark_name, view_type const & value) { + auto child = allocate_element(clark_name); + child->value(value); + return child; + } + optional_ptr> allocate_element(std::initializer_list const & clark_name) { + auto it = clark_name.begin(); + auto a = *it; + auto b = *++it; + return allocate_element({view_type(a), view_type(b)}); + } + optional_ptr> allocate_element(std::initializer_list const & clark_name, view_type const & value) { + auto child = allocate_element(clark_name); + if (!value.empty()) child->value(value); + return child; + } + public: + + //! Prepends a new child node. + //! The prepended child becomes the first child, and all existing children are moved one position back. + //! \param child Node to prepend. + optional_ptr> prepend_node(xml_node *child) + { + assert(child && !child->parent() && child->type() != node_document); + dirty(); + if (first_node()) + { + child->m_next_sibling = m_first_node; + m_first_node->m_prev_sibling = child; + } + else + { + child->m_next_sibling = 0; + m_last_node = child; + } + m_first_node = child; + child->m_parent = this; + child->m_prev_sibling = 0; + return child; + } + auto prepend_node(optional_ptr> ptr) { + return prepend_node(ptr.get()); + } + auto prepend_element(view_type const & v, view_type const & value = {}) { + auto child = allocate_element(v, value); + return prepend_node(child); + } + auto prepend_element(std::tuple const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); + return prepend_node(child); + } + auto prepend_element(std::initializer_list const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); + return prepend_node(child); + } + + //! Appends a new child node. + //! The appended child becomes the last child. + //! \param child Node to append. + optional_ptr> append_node(xml_node *child) + { + assert(child && !child->parent() && child->type() != node_document); + dirty(); + if (first_node()) + { + child->m_prev_sibling = m_last_node; + m_last_node->m_next_sibling = child; + } + else + { + child->m_prev_sibling = nullptr; + m_first_node = child; + } + m_last_node = child; + child->m_parent = this; + child->m_next_sibling = nullptr; + return child; + } + optional_ptr> append_node(optional_ptr> ptr) { + return append_node(ptr.get()); + } + auto append_element(view_type const & v, view_type const & value = {}) { + auto child = allocate_element(v, value); + return append_node(child); + } + auto append_element(std::tuple const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); + return append_node(child); + } + auto append_element(std::initializer_list const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); + return append_node(child); + } + + //! Inserts a new child node at specified place inside the node. + //! All children after and including the specified node are moved one position back. + //! \param where Place where to insert the child, or 0 to insert at the back. + //! \param child Node to insert. + optional_ptr> insert_node(xml_node *where, xml_node *child) + { + assert(!where || where->parent() == this); + assert(child && !child->parent() && child->type() != node_document); + dirty(); + if (where == m_first_node) + prepend_node(child); + else if (!where) + append_node(child); + else + { + child->m_prev_sibling = where->m_prev_sibling; + child->m_next_sibling = where; + where->m_prev_sibling->m_next_sibling = child; + where->m_prev_sibling = child; + child->m_parent = this; + } + return child; + } + auto insert_node(optional_ptr> where, optional_ptr> ptr) { + return insert_node(where.ptr(), ptr.ptr()); + } + auto insert_element(optional_ptr> where, view_type const & v, view_type const & value = {}) { + auto child = allocate_element(v, value); + return insert_node(where, child); + } + auto insert_element(optional_ptr> where, std::tuple const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); + return insert_node(where, child); + } + auto insert_element(optional_ptr> where, std::initializer_list const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); + return insert_node(where, child); + } + + //! Removes first child node. + //! If node has no children, behaviour is undefined. + //! Use first_node() to test if node has children. + void remove_first_node() + { + assert(first_node()); + dirty(); + xml_node *child = m_first_node; + m_first_node = child->m_next_sibling; + if (child->m_next_sibling) + child->m_next_sibling->m_prev_sibling = nullptr; + else + m_last_node = nullptr; + child->m_parent = nullptr; + } + + //! Removes last child of the node. + //! If node has no children, behaviour is undefined. + //! Use first_node() to test if node has children. + void remove_last_node() + { + assert(first_node()); + dirty(); + xml_node *child = m_last_node; + if (child->m_prev_sibling) + { + m_last_node = child->m_prev_sibling; + child->m_prev_sibling->m_next_sibling = nullptr; + } + else + m_first_node = nullptr; + child->m_parent = nullptr; + } + + //! Removes specified child from the node + // \param where Pointer to child to be removed. + void remove_node(optional_ptr> where) + { + assert(where->parent() == this); + assert(first_node()); + dirty(); + if (where == m_first_node) + remove_first_node(); + else if (where == m_last_node) + remove_last_node(); + else + { + where->m_prev_sibling->m_next_sibling = where->m_next_sibling; + where->m_next_sibling->m_prev_sibling = where->m_prev_sibling; + where->m_parent = nullptr; + } + } + + //! Removes all child nodes (but not attributes). + void remove_all_nodes() + { + if (!m_first_node) return; + dirty(); + for (xml_node *node = m_first_node; node; node = node->m_next_sibling) { + node->m_parent = nullptr; + } + m_first_node = nullptr; + m_last_node = nullptr; + } + + //! Prepends a new attribute to the node. + //! \param attribute Attribute to prepend. + void prepend_attribute(xml_attribute *attribute) + { + assert(attribute && !attribute->parent()); + dirty_parent(); + if (first_attribute()) + { + attribute->m_next_attribute = m_first_attribute; + m_first_attribute->m_prev_attribute = attribute; + } + else + { + attribute->m_next_attribute = nullptr; + m_last_attribute = attribute; + } + m_first_attribute = attribute; + attribute->m_parent = this; + attribute->m_prev_attribute = nullptr; + } + + //! Appends a new attribute to the node. + //! \param attribute Attribute to append. + void append_attribute(xml_attribute *attribute) + { + assert(attribute && !attribute->parent()); + dirty_parent(); + if (first_attribute()) + { + attribute->m_prev_attribute = m_last_attribute; + m_last_attribute->m_next_attribute = attribute; + } + else + { + attribute->m_prev_attribute = nullptr; + m_first_attribute = attribute; + } + m_last_attribute = attribute; + attribute->m_parent = this; + attribute->m_next_attribute = nullptr; + } + + //! Inserts a new attribute at specified place inside the node. + //! All attributes after and including the specified attribute are moved one position back. + //! \param where Place where to insert the attribute, or 0 to insert at the back. + //! \param attribute Attribute to insert. + void insert_attribute(xml_attribute *where, xml_attribute *attribute) + { + assert(!where || where->parent() == this); + assert(attribute && !attribute->parent()); + dirty_parent(); + if (where == m_first_attribute) + prepend_attribute(attribute); + else if (!where) + append_attribute(attribute); + else + { + attribute->m_prev_attribute = where->m_prev_attribute; + attribute->m_next_attribute = where; + where->m_prev_attribute->m_next_attribute = attribute; + where->m_prev_attribute = attribute; + attribute->m_parent = this; + } + } + + //! Removes first attribute of the node. + //! If node has no attributes, behaviour is undefined. + //! Use first_attribute() to test if node has attributes. + void remove_first_attribute() + { + assert(first_attribute()); + dirty_parent(); + xml_attribute *attribute = m_first_attribute; + if (attribute->m_next_attribute) + { + attribute->m_next_attribute->m_prev_attribute = 0; + } + else + m_last_attribute = nullptr; + attribute->m_parent = nullptr; + m_first_attribute = attribute->m_next_attribute; + } + + //! Removes last attribute of the node. + //! If node has no attributes, behaviour is undefined. + //! Use first_attribute() to test if node has attributes. + void remove_last_attribute() + { + assert(first_attribute()); + dirty_parent(); + xml_attribute *attribute = m_last_attribute; + if (attribute->m_prev_attribute) + { + attribute->m_prev_attribute->m_next_attribute = 0; + m_last_attribute = attribute->m_prev_attribute; + } + else + m_first_attribute = nullptr; + attribute->m_parent = nullptr; + } + + //! Removes specified attribute from node. + //! \param where Pointer to attribute to be removed. + void remove_attribute(optional_ptr> where) + { + assert(first_attribute() && where->parent() == this); + dirty_parent(); + if (where == m_first_attribute) + remove_first_attribute(); + else if (where == m_last_attribute) + remove_last_attribute(); + else + { + where->m_prev_attribute->m_next_attribute = where->m_next_attribute; + where->m_next_attribute->m_prev_attribute = where->m_prev_attribute; + where->m_parent = nullptr; + } + } + + //! Removes all attributes of node. + void remove_all_attributes() + { + if (!m_first_attribute) return; + dirty_parent(); + for (xml_attribute *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) { + attribute->m_parent = nullptr; + } + m_first_attribute = nullptr; + } + + void validate() const + { + this->xmlns(); + for (auto child = this->first_node(); + child; + child = child->next_sibling()) { + child->validate(); + } + for (auto attribute = first_attribute(); + attribute; + attribute = attribute->m_next_attribute) { + attribute->xmlns(); + for (auto otherattr = first_attribute(); + otherattr != attribute; + otherattr = otherattr->m_next_attribute) { + if (attribute->name() == otherattr->name()) { + throw duplicate_attribute("Attribute doubled"); + } + if ((attribute->local_name() == otherattr->local_name()) + && (attribute->xmlns() == otherattr->xmlns())) + throw duplicate_attribute("Attribute XMLNS doubled"); + } + } + } + + private: + + /////////////////////////////////////////////////////////////////////////// + // Restrictions + + // No copying + xml_node(const xml_node &) = delete; + void operator =(const xml_node &) = delete; + + /////////////////////////////////////////////////////////////////////////// + // Data members + + // Note that some of the pointers below have UNDEFINED values if certain other pointers are 0. + // This is required for maximum performance, as it allows the parser to omit initialization of + // unneded/redundant values. + // + // The rules are as follows: + // 1. first_node and first_attribute contain valid pointers, or 0 if node has no children/attributes respectively + // 2. last_node and last_attribute are valid only if node has at least one child/attribute respectively, otherwise they contain garbage + // 3. prev_sibling and next_sibling are valid only if node has a parent, otherwise they contain garbage + + view_type m_prefix; + mutable std::optional m_xmlns; // Cache + node_type m_type; // Type of node; always valid + xml_node *m_first_node = nullptr; // Pointer to first child node, or 0 if none; always valid + xml_node *m_last_node = nullptr; // Pointer to last child node, or 0 if none; this value is only valid if m_first_node is non-zero + xml_attribute *m_first_attribute = nullptr; // Pointer to first attribute of node, or 0 if none; always valid + xml_attribute *m_last_attribute = nullptr; // Pointer to last attribute of node, or 0 if none; this value is only valid if m_first_attribute is non-zero + xml_node *m_prev_sibling = nullptr; // Pointer to previous sibling of node, or 0 if none; this value is only valid if m_parent is non-zero + xml_node *m_next_sibling = nullptr; // Pointer to next sibling of node, or 0 if none; this value is only valid if m_parent is non-zero + view_type m_contents; // Pointer to original contents in buffer. + bool m_clean = false; // Unchanged since parsing (ie, contents are good). + mutable std::optional m_value; + }; + + /////////////////////////////////////////////////////////////////////////// + // XML document + + //! This class represents root of the DOM hierarchy. + //! It is also an xml_node and a memory_pool through public inheritance. + //! Use parse() function to build a DOM tree from a zero-terminated XML text string. + //! parse() function allocates memory for nodes and attributes by using functions of xml_document, + //! which are inherited from memory_pool. + //! To access root node of the document, use the document itself, as if it was an xml_node. + //! \param Ch Character type to use. + template + class xml_document: public xml_node, public memory_pool + { + public: + using view_type = std::basic_string_view; + using ptr = optional_ptr>; + + //! Constructs empty XML document + xml_document() + : xml_node(node_document) + { + } + + //! Parses zero-terminated XML string according to given flags. + //! Passed string will be modified by the parser, unless rapidxml::parse_non_destructive flag is used. + //! The string must persist for the lifetime of the document. + //! In case of error, rapidxml::parse_error exception will be thrown. + //!

+ //! If you want to parse contents of a file, you must first load the file into the memory, and pass pointer to its beginning. + //! Make sure that data is zero-terminated. + //!

+ //! Document can be parsed into multiple times. + //! Each new call to parse removes previous nodes and attributes (if any), but does not clear memory pool. + //! \param text XML data to parse; pointer is non-const to denote fact that this data may be modified by the parser. + template + auto parse(const Ch * text, xml_document * parent = nullptr) { + return this->parse_low(text, parent); + } + + template + auto parse(std::basic_string const & str, xml_document * parent = nullptr) { + return this->parse_low(str.c_str(), parent); + } + + template + requires std::is_same_v + auto parse(C const & container, xml_document * parent = nullptr) { + return this->parse_low(buffer_ptr(container), parent); + } + + template + T parse_low(T text, xml_document * parent) { + this->m_parse_flags = Flags; + + // Remove current contents + this->remove_all_nodes(); + this->remove_all_attributes(); + this->m_parent = parent ? parent->first_node().get() : nullptr; + + // Parse BOM, if any + parse_bom(text); + + // Parse children + while (true) + { + // Skip whitespace before node + skip(text); + if (*text == 0) + break; + + // Parse and append new child + if (*text == Ch('<')) + { + ++text; // Skip '<' + if (xml_node *node = parse_node(text)) { + this->append_node(node); + if (Flags & (parse_open_only|parse_parse_one) && node->type() == node_element) { + break; + } + } + } + else + FLXML_PARSE_ERROR("expected <", text); + } + if (!this->first_node()) FLXML_PARSE_ERROR("no root element", text); + return text; + } + + //! Clears the document by deleting all nodes and clearing the memory pool. + //! All nodes owned by document pool are destroyed. + void clear() + { + this->remove_all_nodes(); + this->remove_all_attributes(); + memory_pool::clear(); + } + + template + view_type decode_data_value_low(view_type const & v) { + buffer_ptr first{v}; + if (Flags & parse_normalize_whitespace) { + skip(first); + } else { + skip(first); + } + if (!*first) return v; + auto buf = this->allocate_string(v); + auto * start = buf.data(); + buffer_ptr tmp{buf}; + auto end = (Flags & parse_normalize_whitespace) ? + skip_and_expand_character_refs(tmp) : + skip_and_expand_character_refs(tmp); + // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after > + if (Flags & parse_trim_whitespace) + { + if (Flags & parse_normalize_whitespace) + { + // Whitespace is already condensed to single space characters by skipping function, so just trim 1 char off the end + if (*(end - 1) == Ch(' ')) + --end; + } + else + { + // Backup until non-whitespace character is found + while (whitespace_pred::test(*(end - 1))) + --end; + } + } + + return {start, end}; + } + + template + view_type decode_attr_value_low(view_type const & v) { + buffer_ptr first{v}; + skip,0>(first); + if (!*first || *first == Q) return v; + auto buf = this->allocate_string(v); + const Ch * start = buf.data(); + buffer_ptr tmp{buf}; + const Ch * end = skip_and_expand_character_refs,attribute_value_pure_pred,0>(tmp); + return {start, end}; + } + + view_type decode_attr_value(const xml_attribute * attr) { + if (attr->quote() == Ch('"')) { + return decode_attr_value_low<'"'>(attr->value_raw()); + } else if (attr->quote() == Ch('\'')){ + return decode_attr_value_low<'\''>(attr->value_raw()); + } else { + return attr->value_raw(); + } + } + + view_type decode_data_value(const xml_node * node) { + if (node->value_raw().empty()) return node->value_raw(); + if (m_parse_flags & parse_normalize_whitespace) { + if (m_parse_flags & parse_trim_whitespace) { + const int Flags = parse_normalize_whitespace | parse_trim_whitespace; + return decode_data_value_low(node->value_raw()); + } else { + const int Flags = parse_normalize_whitespace; + return decode_data_value_low(node->value_raw()); + } + } else { + if (m_parse_flags & parse_trim_whitespace) { + const int Flags = parse_trim_whitespace; + return decode_data_value_low(node->value_raw()); + } else { + const int Flags = 0; + return decode_data_value_low(node->value_raw()); + } + } + } + + void validate() const + { + for (auto child = this->first_node(); + child; + child = child->next_sibling()) { + child->validate(); + } + } + +#ifndef RAPIDXML_TESTING + private: +#endif + + /////////////////////////////////////////////////////////////////////// + // Internal character utility functions + + // Detect whitespace character + struct whitespace_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_whitespace[static_cast(ch)]; + } + }; + + // Detect node name character + struct node_name_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_node_name[static_cast(ch)]; + } + }; + + // Detect element name character + struct element_name_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_element_name[static_cast(ch)]; + } + }; + + // Detect attribute name character + struct attribute_name_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_attribute_name[static_cast(ch)]; + } + }; + + // Detect text character (PCDATA) + struct text_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_text[static_cast(ch)]; + } + }; + + // Detect text character (PCDATA) that does not require processing + struct text_pure_no_ws_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_text_pure_no_ws[static_cast(ch)]; + } + }; + + // Detect text character (PCDATA) that does not require processing + struct text_pure_with_ws_pred + { + static unsigned char test(Ch ch) + { + return internal::lookup_tables::lookup_text_pure_with_ws[static_cast(ch)]; + } + }; + + // Detect attribute value character + template + struct attribute_value_pred + { + static unsigned char test(Ch ch) + { + if (Quote == Ch('\'')) + return internal::lookup_tables::lookup_attribute_data_1[static_cast(ch)]; + if (Quote == Ch('\"')) + return internal::lookup_tables::lookup_attribute_data_2[static_cast(ch)]; + return 0; // Should never be executed, to avoid warnings on Comeau + } + }; + + // Detect attribute value character + template + struct attribute_value_pure_pred + { + static unsigned char test(Ch ch) + { + if (Quote == Ch('\'')) + return internal::lookup_tables::lookup_attribute_data_1_pure[static_cast(ch)]; + if (Quote == Ch('\"')) + return internal::lookup_tables::lookup_attribute_data_2_pure[static_cast(ch)]; + return 0; // Should never be executed, to avoid warnings on Comeau + } + }; + + // Insert coded character, using UTF8 or 8-bit ASCII + template + static void insert_coded_character(Ch *&text, unsigned long code) + { + if (Flags & parse_no_utf8) + { + // Insert 8-bit ASCII character + // Todo: possibly verify that code is less than 256 and use replacement char otherwise? + text[0] = static_cast(code); + text += 1; + } + else + { + // Insert UTF8 sequence + if (code < 0x80) // 1 byte sequence + { + text[0] = static_cast(code); + text += 1; + } + else if (code < 0x800) // 2 byte sequence + { + text[1] = static_cast((code | 0x80) & 0xBF); code >>= 6; + text[0] = static_cast(code | 0xC0); + text += 2; + } + else if (code < 0x10000) // 3 byte sequence + { + text[2] = static_cast((code | 0x80) & 0xBF); code >>= 6; + text[1] = static_cast((code | 0x80) & 0xBF); code >>= 6; + text[0] = static_cast(code | 0xE0); + text += 3; + } + else if (code < 0x110000) // 4 byte sequence + { + text[3] = static_cast((code | 0x80) & 0xBF); code >>= 6; + text[2] = static_cast((code | 0x80) & 0xBF); code >>= 6; + text[1] = static_cast((code | 0x80) & 0xBF); code >>= 6; + text[0] = static_cast(code | 0xF0); + text += 4; + } + else // Invalid, only codes up to 0x10FFFF are allowed in Unicode + { + FLXML_PARSE_ERROR("invalid numeric character entity", text); + } + } + } + + // Skip characters until predicate evaluates to true + template + static void skip(Chp & b) + { + while (StopPred::test(*b)) + ++b; + } + + // Skip characters until predicate evaluates to true while doing the following: + // - replacing XML character entity references with proper characters (' & " < > &#...;) + // - condensing whitespace sequences to single space character + template + static const Ch *skip_and_expand_character_refs(Chp text) + { + // If entity translation, whitespace condense and whitespace trimming is disabled, use plain skip + if (Flags & parse_no_entity_translation && + !(Flags & parse_normalize_whitespace) && + !(Flags & parse_trim_whitespace)) + { + skip(text); + return &*text; + } + + // Use simple skip until first modification is detected + skip(text); + + // Use translation skip + Chp src = text; + Ch * dest = const_cast(&*src); + while (StopPred::test(*src)) + { + // If entity translation is enabled + if (!(Flags & parse_no_entity_translation)) + { + // Test if replacement is needed + if (src[0] == Ch('&')) + { + switch (src[1]) + { + + // & ' + case Ch('a'): + if (src[2] == Ch('m') && src[3] == Ch('p') && src[4] == Ch(';')) + { + *dest = Ch('&'); + ++dest; + src += 5; + continue; + } + if (src[2] == Ch('p') && src[3] == Ch('o') && src[4] == Ch('s') && src[5] == Ch(';')) + { + *dest = Ch('\''); + ++dest; + src += 6; + continue; + } + break; + + // " + case Ch('q'): + if (src[2] == Ch('u') && src[3] == Ch('o') && src[4] == Ch('t') && src[5] == Ch(';')) + { + *dest = Ch('"'); + ++dest; + src += 6; + continue; + } + break; + + // > + case Ch('g'): + if (src[2] == Ch('t') && src[3] == Ch(';')) + { + *dest = Ch('>'); + ++dest; + src += 4; + continue; + } + break; + + // < + case Ch('l'): + if (src[2] == Ch('t') && src[3] == Ch(';')) + { + *dest = Ch('<'); + ++dest; + src += 4; + continue; + } + break; + + // &#...; - assumes ASCII + case Ch('#'): + if (src[2] == Ch('x')) + { + unsigned long code = 0; + src += 3; // Skip &#x + while (true) + { + unsigned char digit = internal::lookup_tables::lookup_digits[static_cast(*src)]; + if (digit == 0xFF) + break; + code = code * 16 + digit; + ++src; + } + insert_coded_character(dest, code); // Put character in output + } + else + { + unsigned long code = 0; + src += 2; // Skip &# + while (true) + { + unsigned char digit = internal::lookup_tables::lookup_digits[static_cast(*src)]; + if (digit == 0xFF) + break; + code = code * 10 + digit; + ++src; + } + insert_coded_character(dest, code); // Put character in output + } + if (*src == Ch(';')) + ++src; + else + FLXML_PARSE_ERROR("expected ;", src); + continue; + + // Something else + default: + // Ignore, just copy '&' verbatim + break; + + } + } + } + + // If whitespace condensing is enabled + if (Flags & parse_normalize_whitespace && whitespace_pred::test(*src)) { + *dest = Ch(' '); ++dest; // Put single space in dest + ++src; // Skip first whitespace char + // Skip remaining whitespace chars + while (whitespace_pred::test(*src)) + ++src; + continue; + } + + // No replacement, only copy character + *dest++ = *src++; + + } + + // Return new end + return dest; + } + + /////////////////////////////////////////////////////////////////////// + // Internal parsing functions + + // Parse BOM, if any + template + void parse_bom(Chp &texta) + { + Chp text = texta; + // UTF-8? + if (static_cast(*text++) == 0xEF && + static_cast(*text++) == 0xBB && + static_cast(*text++) == 0xBF) + { + texta = text; // Skup utf-8 bom + } + } + + // Parse XML declaration ( + xml_node *parse_xml_declaration(Chp &text) + { + // If parsing of declaration is disabled + if (!(Flags & parse_declaration_node)) + { + // Skip until end of declaration + while (text[0] != Ch('?') || text[1] != Ch('>')) + { + if (!text[0]) FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + text += 2; // Skip '?>' + return 0; + } + + // Create declaration + xml_node *declaration = this->allocate_node(node_declaration); + + // Skip whitespace before attributes or ?> + skip(text); + + // Parse declaration attributes + parse_node_attributes(text, declaration); + + // Skip ?> + if (text[0] != Ch('?') || text[1] != Ch('>')) FLXML_PARSE_ERROR("expected ?>", text); + text += 2; + + return declaration; + } + + // Parse XML comment (' + return 0; // Do not produce comment node + } + + // Remember value start + Chp value = text; + + // Skip until end of comment + while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>')) + { + if (!text[0]) FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + + // Create comment node + xml_node *comment = this->allocate_node(node_comment); + comment->value({value, text}); + + text += 3; // Skip '-->' + return comment; + } + + // Parse DOCTYPE + template + xml_node *parse_doctype(Chp &text) + { + // Remember value start + Chp value = text; + + // Skip to > + while (*text != Ch('>')) + { + // Determine character type + switch (*text) + { + + // If '[' encountered, scan for matching ending ']' using naive algorithm with depth + // This works for all W3C test files except for 2 most wicked + case Ch('['): + { + ++text; // Skip '[' + int depth = 1; + while (depth > 0) + { + switch (*text) + { + case Ch('['): ++depth; break; + case Ch(']'): --depth; break; + case 0: FLXML_PARSE_ERROR("unexpected end of data", text); + default: break; + } + ++text; + } + break; + } + + // Error on end of text + case Ch('\0'): + FLXML_PARSE_ERROR("unexpected end of data", text); + + // Other character, skip it + default: + ++text; + + } + } + + // If DOCTYPE nodes enabled + if (Flags & parse_doctype_node) + { + // Create a new doctype node + xml_node *doctype = this->allocate_node(node_doctype); + doctype->value({value, text}); + + text += 1; // skip '>' + return doctype; + } + else + { + text += 1; // skip '>' + return 0; + } + + } + + // Parse PI + template + xml_node *parse_pi(Chp &text) + { + // If creation of PI nodes is enabled + if (Flags & parse_pi_nodes) + { + // Create pi node + xml_node *pi = this->allocate_node(node_pi); + + // Extract PI target name + Chp name = text; + skip(text); + if (text == name) FLXML_PARSE_ERROR("expected PI target", text); + pi->name({name, text}); + + // Skip whitespace between pi target and pi + skip(text); + + // Remember start of pi + Chp value = text; + + // Skip to '?>' + while (text[0] != Ch('?') || text[1] != Ch('>')) + { + if (*text == Ch('\0')) + FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + + // Set pi value (verbatim, no entity expansion or whitespace normalization) + pi->value({value, text}); + + text += 2; // Skip '?>' + return pi; + } + else + { + // Skip to '?>' + while (text[0] != Ch('?') || text[1] != Ch('>')) + { + if (*text == Ch('\0')) + FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + text += 2; // Skip '?>' + return 0; + } + } + + // Parse and append data + // Return character that ends data. + // This is necessary because this character might have been overwritten by a terminating 0 + template + Ch parse_and_append_data(xml_node *node, Chp &text, Chp contents_start) + { + // Backup to contents start if whitespace trimming is disabled + if (!(Flags & parse_trim_whitespace)) + text = contents_start; + + // Skip until end of data. We should check if the contents will need decoding. + Chp value = text; + bool encoded = false; + skip(text); + if (text_pred::test(*text)) { + encoded = true; + skip(text); + } + + // If characters are still left between end and value (this test is only necessary if normalization is enabled) + // Create new data node + if (!(Flags & parse_no_data_nodes)) + { + xml_node *data = this->allocate_node(node_data); + data->value_raw({value, text}); + if (!encoded) data->value(data->value_raw()); + node->append_node(data); + } + + // Add data to parent node if no data exists yet + if (!(Flags & parse_no_element_values)) { + if (node->value_raw().empty()) { + node->value_raw({value, text}); + if (!encoded) node->value(node->value_raw()); + } + } + + // Return character that ends data + return *text; + } + + // Parse CDATA + template + xml_node *parse_cdata(Chp &text) + { + // If CDATA is disabled + if (Flags & parse_no_data_nodes) + { + // Skip until end of cdata + while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>')) + { + if (!text[0]) + FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + text += 3; // Skip ]]> + return 0; // Do not produce CDATA node + } + + // Skip until end of cdata + Chp value = text; + while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>')) + { + if (!text[0]) + FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + + // Create new cdata node + xml_node *cdata = this->allocate_node(node_cdata); + cdata->value({value, text}); + + text += 3; // Skip ]]> + return cdata; + } + + // Parse element node + template + xml_node *parse_element(Chp &text) + { + // Create element node + xml_node *element = this->allocate_node(node_element); + + // Extract element name + Chp prefix = text; + view_type qname; + skip(text); + if (text == prefix) + FLXML_PARSE_ERROR("expected element name or prefix", text); + if (*text == Ch(':')) { + element->prefix({prefix, text}); + ++text; + Chp name = text; + skip(text); + if (text == name) + FLXML_PARSE_ERROR("expected element local name", text); + element->name({name, text}); + } else { + element->name({prefix, text}); + } + qname = {prefix, text}; + + // Skip whitespace between element name and attributes or > + skip(text); + + // Parse attributes, if any + parse_node_attributes(text, element); + // Once we have all the attributes, we should be able to fully validate: + if (Flags & parse_validate_xmlns) this->validate(); + + // Determine ending type + if (*text == Ch('>')) + { + Chp contents = ++text; + Chp contents_end = contents; + if (!(Flags & parse_open_only)) + contents_end = parse_node_contents(text, element, qname); + if (contents != contents_end) element->contents({contents, contents_end}); + } + else if (*text == Ch('/')) + { + ++text; + if (*text != Ch('>')) + FLXML_PARSE_ERROR("expected >", text); + ++text; + if (Flags & parse_open_only) + FLXML_PARSE_ERROR("open_only, but closed", text); + } + else + FLXML_PARSE_ERROR("expected >", text); + + // Return parsed element + return element; + } + + // Determine node type, and parse it + template + xml_node *parse_node(Chp &text) + { + // Parse proper node type + switch (text[0]) + { + + // <... + default: + // Parse and append element node + return parse_element(text); + + // (text); + } + else + { + // Parse PI + return parse_pi(text); + } + + // (text); + } + break; + + // (text); + } + break; + + // (text); + } + + default: + break; + } // switch + + // Attempt to skip other, unrecognized node types starting with ')) + { + if (*text == 0) + FLXML_PARSE_ERROR("unexpected end of data", text); + ++text; + } + ++text; // Skip '>' + return 0; // No node recognized + + } + } + + // Parse contents of the node - children, data etc. + // Return end pointer. + template + Chp parse_node_contents(Chp &text, xml_node *node, view_type const & qname) + { + Chp retval; + // For all children and text + while (true) + { + // Skip whitespace between > and node contents + Chp contents_start = text; // Store start of node contents before whitespace is skipped + skip(text); + Ch next_char = *text; + + // After data nodes, instead of continuing the loop, control jumps here. + // This is because zero termination inside parse_and_append_data() function + // would wreak havoc with the above code. + // Also, skipping whitespace after data nodes is unnecessary. + after_data_node: + + // Determine what comes next: node closing, child node, data node, or 0? + switch (next_char) + { + + // Node closing or child node + case Ch('<'): + if (text[1] == Ch('/')) + { + // Node closing + retval = text; + text += 2; // Skip '(text); + if (qname != view_type{closing_name, text}) + FLXML_PARSE_ERROR("invalid closing tag name", text); + } + else + { + // No validation, just skip name + skip(text); + } + // Skip remaining whitespace after node name + skip(text); + if (*text != Ch('>')) + FLXML_PARSE_ERROR("expected >", text); + ++text; // Skip '>' + if (Flags & parse_open_only) + FLXML_PARSE_ERROR("Unclosed element actually closed.", text); + return retval; // Node closed, finished parsing contents + } + else + { + // Child node + ++text; // Skip '<' + if (xml_node *child = parse_node(text)) + node->append_node(child); + } + break; + + // End of data - error unless we expected this. + case Ch('\0'): + if (Flags & parse_open_only) { + return Chp(); + } else { + FLXML_PARSE_ERROR("unexpected end of data", text); + } + + // Data node + default: + next_char = parse_and_append_data(node, text, contents_start); + goto after_data_node; // Bypass regular processing after data nodes + + } + } + } + + // Parse XML attributes of the node + template + void parse_node_attributes(Chp &text, xml_node *node) + { + // For all attributes + while (attribute_name_pred::test(*text)) + { + // Extract attribute name + Chp name = text; + ++text; // Skip first character of attribute name + skip(text); + if (text == name) + FLXML_PARSE_ERROR("expected attribute name", name); + + // Create new attribute + xml_attribute *attribute = this->allocate_attribute(view_type{name, text}); + node->append_attribute(attribute); + + // Skip whitespace after attribute name + skip(text); + + // Skip = + if (*text != Ch('=')) + FLXML_PARSE_ERROR("expected =", text); + ++text; + + // Skip whitespace after = + skip(text); + + // Skip quote and remember if it was ' or " + Ch quote = *text; + if (quote != Ch('\'') && quote != Ch('"')) + FLXML_PARSE_ERROR("expected ' or \"", text); + attribute->quote(quote); + ++text; + + // Extract attribute value and expand char refs in it + Chp value = text; + Chp end; + const int AttFlags = Flags & ~parse_normalize_whitespace; // No whitespace normalization in attributes + if (quote == Ch('\'')) + skip, AttFlags>(text); + else + skip, AttFlags>(text); + end = text; + + // Set attribute value + attribute->value_raw({value, end}); + + // Make sure that end quote is present + if (*text != quote) + FLXML_PARSE_ERROR("expected ' or \"", text); + ++text; // Skip quote + + // Skip whitespace after attribute value + skip(text); + } + } + private: + int m_parse_flags = 0; + }; + + +} + +// Also include this now. +#include + +// Undefine internal macros +#undef FLXML_PARSE_ERROR + +// On MSVC, restore warnings state +#ifdef _MSC_VER + #pragma warning(pop) +#endif + +#endif diff --git a/include/rapidxml_generator.hpp b/include/flxml/generator.hpp similarity index 98% rename from include/rapidxml_generator.hpp rename to include/flxml/generator.hpp index c20e17e..f2d74db 100644 --- a/include/rapidxml_generator.hpp +++ b/include/flxml/generator.hpp @@ -8,7 +8,7 @@ #include #include -namespace rapidxml { +namespace flxml { template class generator { public: diff --git a/include/rapidxml_iterators.hpp b/include/flxml/iterators.hpp similarity index 94% rename from include/rapidxml_iterators.hpp rename to include/flxml/iterators.hpp index e5a0ba3..5da9447 100644 --- a/include/rapidxml_iterators.hpp +++ b/include/flxml/iterators.hpp @@ -1,402 +1,402 @@ -#ifndef RAPIDXML_ITERATORS_HPP_INCLUDED -#define RAPIDXML_ITERATORS_HPP_INCLUDED - -// Copyright (C) 2006, 2009 Marcin Kalicinski -// Version 1.13 -// Revision $DateTime: 2009/05/13 01:46:17 $ -//! \file rapidxml_iterators.hpp This file contains rapidxml iterators - -#include "rapidxml.hpp" - -namespace rapidxml -{ - //! Iterator of child nodes of xml_node - template -class node_iterator - { - public: - using value_type = xml_node; - using reference = xml_node &; - using pointer = xml_node *; - using iterator_category = std::bidirectional_iterator_tag; - using difference_type = long; - - node_iterator() - : m_node() - { - } - - explicit node_iterator(xml_node const &node) - : m_node(node.first_node()) - { - } - - node_iterator(node_iterator && other) noexcept : m_node(other.m_node) {} - node_iterator(node_iterator const & other) : m_node(other.m_node) {} - - reference operator *() const - { - return const_cast(*m_node); - } - - pointer operator->() const - { - return const_cast(m_node.get()); - } - - node_iterator& operator++() - { - m_node = m_node->next_sibling(); - return *this; - } - - node_iterator operator++(int) - { - node_iterator tmp = *this; - ++(*this); - return tmp; - } - - node_iterator& operator--() - { - m_node = m_node->previous_sibling(); - return *this; - } - - node_iterator operator--(int) - { - node_iterator tmp = *this; - --(*this); - return tmp; - } - - bool operator == (const node_iterator& rhs) const - { - return m_node == rhs.m_node; - } - - bool operator != (const node_iterator& rhs) const - { - return m_node != rhs.m_node; - } - - node_iterator & operator = (node_iterator && other) noexcept { - m_node = other.m_node; - return *this; - } - - node_iterator & operator = (node_iterator const & other) { - m_node = other.m_node; - return *this; - } - - bool valid() - { - return m_node.has_value(); - } - - private: - - optional_ptr> m_node; - - }; - - //! Iterator of child nodes of xml_node - template -class descendant_iterator - { - public: - using value_type = xml_node; - using reference = xml_node &; - using pointer = xml_node *; - using iterator_category = std::bidirectional_iterator_tag; - using difference_type = long; - - descendant_iterator() - : m_parent(), m_node() - { - } - - explicit descendant_iterator(xml_node::ptr node) - : m_parent(node), m_node(node->first_node()) - { - } - - descendant_iterator(descendant_iterator && other) noexcept : m_parent(other.m_parent), m_node(other.m_node) {} - descendant_iterator(descendant_iterator const & other) : m_parent(other.m_parent), m_node(other.m_node) {} - - reference operator *() const - { - return const_cast(*m_node); - } - - pointer operator->() const - { - return const_cast(m_node.get()); - } - - descendant_iterator& operator++() - { - if (m_node->first_node()) { - m_node = m_node->first_node(); - } else if (m_node->next_sibling()) { - m_node = m_node->next_sibling(); - } else { - // Run out of children, so move upward until we can find a sibling. - while (true) { - m_node = m_node->parent(); - if (m_node == m_parent) { - m_node = nullptr; - break; - } - if (m_node->next_sibling()) { - m_node = m_node->next_sibling(); - break; - } - } - } - return *this; - } - - descendant_iterator operator++(int) - { - node_iterator tmp = *this; - ++(*this); - return tmp; - } - - descendant_iterator& operator--() - { - if (!m_node->previous_sibling()) { - m_node = m_node->parent(); - if (m_node == m_parent) { - m_node = nullptr; - } - } else { - m_node = m_node->previous_sibling(); - while (m_node->last_node()) { - m_node = m_node->last_node(); - } - } - return *this; - } - - descendant_iterator operator--(int) - { - node_iterator tmp = *this; - --(*this); - return tmp; - } - - bool operator == (const descendant_iterator& rhs) const - { - return m_node == rhs.m_node; - } - - bool operator != (const descendant_iterator& rhs) const - { - return m_node != rhs.m_node; - } - - descendant_iterator & operator = (descendant_iterator && other) noexcept { - m_parent = other.m_parent; - m_node = other.m_node; - return *this; - } - - descendant_iterator & operator = (descendant_iterator const & other) { - m_parent = other.m_parent; - m_node = other.m_node; - return *this; - } - - bool valid() - { - return m_node.has_value(); - } - - private: - - optional_ptr> m_parent; - optional_ptr> m_node; - }; - - //! Iterator of child attributes of xml_node - template - class attribute_iterator - { - - public: - - using value_type = xml_attribute; - using reference = xml_attribute &; - using pointer = xml_attribute *; - using iterator_category = std::bidirectional_iterator_tag; - using difference_type = long; - - attribute_iterator() - : m_attribute() - { - } - - explicit attribute_iterator(xml_node const &node) - : m_attribute(node.first_attribute()) - { - } - - attribute_iterator(attribute_iterator && other) noexcept : m_attribute(other.m_attribute) {} - attribute_iterator(attribute_iterator const & other) : m_attribute(other.m_attribute) {} - - reference operator *() const - { - return const_cast(*m_attribute); - } - - pointer operator->() const - { - return const_cast(m_attribute.get()); - } - - attribute_iterator& operator++() - { - m_attribute = m_attribute->next_attribute(); - return *this; - } - - attribute_iterator operator++(int) - { - attribute_iterator tmp = *this; - ++*this; - return tmp; - } - - attribute_iterator& operator--() - { - m_attribute = m_attribute->previous_attribute(); - return *this; - } - - attribute_iterator operator--(int) - { - attribute_iterator tmp = *this; - --*this; - return tmp; - } - - bool operator ==(const attribute_iterator &rhs) const - { - return m_attribute == rhs.m_attribute; - } - - bool operator !=(const attribute_iterator &rhs) const - { - return m_attribute != rhs.m_attribute; - } - - attribute_iterator & operator = (attribute_iterator && other) noexcept { - m_attribute = other.m_attribute; - return *this; - } - - attribute_iterator & operator = (attribute_iterator const & other) { - m_attribute = other.m_attribute; - return *this; - } - - private: - - optional_ptr> m_attribute; - - }; - - //! Container adaptor for child nodes - template - class children - { - xml_node const & m_node; - public: - explicit children(xml_node const & node) : m_node(node) {} - explicit children(optional_ptr> const ptr) : m_node(ptr.value()) {} - children(children && other) noexcept : m_node(other.m_node) {} - children(children const & other) : m_node(other.m_node) {} - - using const_iterator = node_iterator; - using iterator = node_iterator; - - iterator begin() { - return iterator(m_node); - } - iterator end() { - return {}; - } - const_iterator begin() const { - return const_iterator(m_node); - } - const_iterator end() const { - return {}; - } - }; - - //! Container adaptor for child nodes - template - class descendants - { - xml_node & m_node; - public: - explicit descendants(xml_node & node) : m_node(node) {} - explicit descendants(optional_ptr> ptr) : m_node(ptr.value()) {} - descendants(descendants && other) noexcept : m_node(other.m_node) {} - descendants(descendants const & other) : m_node(other.m_node) {} - - using const_iterator = descendant_iterator; - using iterator = descendant_iterator; - - iterator begin() { - return iterator(&m_node); - } - iterator end() { - return {}; - } - const_iterator begin() const { - return const_iterator(&m_node); - } - const_iterator end() const { - return {}; - } - }; - - //! Container adaptor for attributes - template - class attributes - { - xml_node const & m_node; - public: - explicit attributes(xml_node const & node) : m_node(node) {} - explicit attributes(optional_ptr> ptr) : m_node(ptr.value()) {} - - using const_iterator = attribute_iterator; - using iterator = attribute_iterator; - - iterator begin() { - return iterator{m_node}; - } - iterator end() { - return {}; - } - const_iterator begin() const { - return const_iterator{m_node}; - } - const_iterator end() const { - return {}; - } - }; -} - -template -inline constexpr bool std::ranges::enable_borrowed_range> = true; - -template -inline constexpr bool std::ranges::enable_borrowed_range> = true; - - -#endif +#ifndef RAPIDXML_ITERATORS_HPP_INCLUDED +#define RAPIDXML_ITERATORS_HPP_INCLUDED + +// Copyright (C) 2006, 2009 Marcin Kalicinski +// Version 1.13 +// Revision $DateTime: 2009/05/13 01:46:17 $ +//! \file rapidxml_iterators.hpp This file contains rapidxml iterators + +#include "flxml.hpp" + +namespace flxml +{ + //! Iterator of child nodes of xml_node + template +class node_iterator + { + public: + using value_type = xml_node; + using reference = xml_node &; + using pointer = xml_node *; + using iterator_category = std::bidirectional_iterator_tag; + using difference_type = long; + + node_iterator() + : m_node() + { + } + + explicit node_iterator(xml_node const &node) + : m_node(node.first_node()) + { + } + + node_iterator(node_iterator && other) noexcept : m_node(other.m_node) {} + node_iterator(node_iterator const & other) : m_node(other.m_node) {} + + reference operator *() const + { + return const_cast(*m_node); + } + + pointer operator->() const + { + return const_cast(m_node.get()); + } + + node_iterator& operator++() + { + m_node = m_node->next_sibling(); + return *this; + } + + node_iterator operator++(int) + { + node_iterator tmp = *this; + ++(*this); + return tmp; + } + + node_iterator& operator--() + { + m_node = m_node->previous_sibling(); + return *this; + } + + node_iterator operator--(int) + { + node_iterator tmp = *this; + --(*this); + return tmp; + } + + bool operator == (const node_iterator& rhs) const + { + return m_node == rhs.m_node; + } + + bool operator != (const node_iterator& rhs) const + { + return m_node != rhs.m_node; + } + + node_iterator & operator = (node_iterator && other) noexcept { + m_node = other.m_node; + return *this; + } + + node_iterator & operator = (node_iterator const & other) { + m_node = other.m_node; + return *this; + } + + bool valid() + { + return m_node.has_value(); + } + + private: + + optional_ptr> m_node; + + }; + + //! Iterator of child nodes of xml_node + template +class descendant_iterator + { + public: + using value_type = xml_node; + using reference = xml_node &; + using pointer = xml_node *; + using iterator_category = std::bidirectional_iterator_tag; + using difference_type = long; + + descendant_iterator() + : m_parent(), m_node() + { + } + + explicit descendant_iterator(xml_node::ptr node) + : m_parent(node), m_node(node->first_node()) + { + } + + descendant_iterator(descendant_iterator && other) noexcept : m_parent(other.m_parent), m_node(other.m_node) {} + descendant_iterator(descendant_iterator const & other) : m_parent(other.m_parent), m_node(other.m_node) {} + + reference operator *() const + { + return const_cast(*m_node); + } + + pointer operator->() const + { + return const_cast(m_node.get()); + } + + descendant_iterator& operator++() + { + if (m_node->first_node()) { + m_node = m_node->first_node(); + } else if (m_node->next_sibling()) { + m_node = m_node->next_sibling(); + } else { + // Run out of children, so move upward until we can find a sibling. + while (true) { + m_node = m_node->parent(); + if (m_node == m_parent) { + m_node = nullptr; + break; + } + if (m_node->next_sibling()) { + m_node = m_node->next_sibling(); + break; + } + } + } + return *this; + } + + descendant_iterator operator++(int) + { + node_iterator tmp = *this; + ++(*this); + return tmp; + } + + descendant_iterator& operator--() + { + if (!m_node->previous_sibling()) { + m_node = m_node->parent(); + if (m_node == m_parent) { + m_node = nullptr; + } + } else { + m_node = m_node->previous_sibling(); + while (m_node->last_node()) { + m_node = m_node->last_node(); + } + } + return *this; + } + + descendant_iterator operator--(int) + { + node_iterator tmp = *this; + --(*this); + return tmp; + } + + bool operator == (const descendant_iterator& rhs) const + { + return m_node == rhs.m_node; + } + + bool operator != (const descendant_iterator& rhs) const + { + return m_node != rhs.m_node; + } + + descendant_iterator & operator = (descendant_iterator && other) noexcept { + m_parent = other.m_parent; + m_node = other.m_node; + return *this; + } + + descendant_iterator & operator = (descendant_iterator const & other) { + m_parent = other.m_parent; + m_node = other.m_node; + return *this; + } + + bool valid() + { + return m_node.has_value(); + } + + private: + + optional_ptr> m_parent; + optional_ptr> m_node; + }; + + //! Iterator of child attributes of xml_node + template + class attribute_iterator + { + + public: + + using value_type = xml_attribute; + using reference = xml_attribute &; + using pointer = xml_attribute *; + using iterator_category = std::bidirectional_iterator_tag; + using difference_type = long; + + attribute_iterator() + : m_attribute() + { + } + + explicit attribute_iterator(xml_node const &node) + : m_attribute(node.first_attribute()) + { + } + + attribute_iterator(attribute_iterator && other) noexcept : m_attribute(other.m_attribute) {} + attribute_iterator(attribute_iterator const & other) : m_attribute(other.m_attribute) {} + + reference operator *() const + { + return const_cast(*m_attribute); + } + + pointer operator->() const + { + return const_cast(m_attribute.get()); + } + + attribute_iterator& operator++() + { + m_attribute = m_attribute->next_attribute(); + return *this; + } + + attribute_iterator operator++(int) + { + attribute_iterator tmp = *this; + ++*this; + return tmp; + } + + attribute_iterator& operator--() + { + m_attribute = m_attribute->previous_attribute(); + return *this; + } + + attribute_iterator operator--(int) + { + attribute_iterator tmp = *this; + --*this; + return tmp; + } + + bool operator ==(const attribute_iterator &rhs) const + { + return m_attribute == rhs.m_attribute; + } + + bool operator !=(const attribute_iterator &rhs) const + { + return m_attribute != rhs.m_attribute; + } + + attribute_iterator & operator = (attribute_iterator && other) noexcept { + m_attribute = other.m_attribute; + return *this; + } + + attribute_iterator & operator = (attribute_iterator const & other) { + m_attribute = other.m_attribute; + return *this; + } + + private: + + optional_ptr> m_attribute; + + }; + + //! Container adaptor for child nodes + template + class children + { + xml_node const & m_node; + public: + explicit children(xml_node const & node) : m_node(node) {} + explicit children(optional_ptr> const ptr) : m_node(ptr.value()) {} + children(children && other) noexcept : m_node(other.m_node) {} + children(children const & other) : m_node(other.m_node) {} + + using const_iterator = node_iterator; + using iterator = node_iterator; + + iterator begin() { + return iterator(m_node); + } + iterator end() { + return {}; + } + const_iterator begin() const { + return const_iterator(m_node); + } + const_iterator end() const { + return {}; + } + }; + + //! Container adaptor for child nodes + template + class descendants + { + xml_node & m_node; + public: + explicit descendants(xml_node & node) : m_node(node) {} + explicit descendants(optional_ptr> ptr) : m_node(ptr.value()) {} + descendants(descendants && other) noexcept : m_node(other.m_node) {} + descendants(descendants const & other) : m_node(other.m_node) {} + + using const_iterator = descendant_iterator; + using iterator = descendant_iterator; + + iterator begin() { + return iterator(&m_node); + } + iterator end() { + return {}; + } + const_iterator begin() const { + return const_iterator(&m_node); + } + const_iterator end() const { + return {}; + } + }; + + //! Container adaptor for attributes + template + class attributes + { + xml_node const & m_node; + public: + explicit attributes(xml_node const & node) : m_node(node) {} + explicit attributes(optional_ptr> ptr) : m_node(ptr.value()) {} + + using const_iterator = attribute_iterator; + using iterator = attribute_iterator; + + iterator begin() { + return iterator{m_node}; + } + iterator end() { + return {}; + } + const_iterator begin() const { + return const_iterator{m_node}; + } + const_iterator end() const { + return {}; + } + }; +} + +template +inline constexpr bool std::ranges::enable_borrowed_range> = true; + +template +inline constexpr bool std::ranges::enable_borrowed_range> = true; + + +#endif diff --git a/include/rapidxml_predicates.hpp b/include/flxml/predicates.hpp similarity index 96% rename from include/rapidxml_predicates.hpp rename to include/flxml/predicates.hpp index 2f778bd..b254a81 100644 --- a/include/rapidxml_predicates.hpp +++ b/include/flxml/predicates.hpp @@ -7,17 +7,17 @@ #include #include -#include "rapidxml_generator.hpp" -#include "rapidxml.hpp" +#include "flxml/generator.hpp" +#include "flxml.hpp" -namespace rapidxml { +namespace flxml { template class xpath; namespace internal { template class xpath_base; template - class name : public rapidxml::internal::xpath_base { + class name : public flxml::internal::xpath_base { private: std::basic_string m_name; std::optional> m_xmlns; @@ -35,7 +35,7 @@ namespace rapidxml { }; template - class value : public rapidxml::internal::xpath_base { + class value : public flxml::internal::xpath_base { private: std::basic_string m_value; public: @@ -48,7 +48,7 @@ namespace rapidxml { }; template - class xmlns : public rapidxml::internal::xpath_base { + class xmlns : public flxml::internal::xpath_base { private: std::basic_string m_xmlns; public: @@ -61,7 +61,7 @@ namespace rapidxml { }; template - class attr : public rapidxml::internal::xpath_base { + class attr : public flxml::internal::xpath_base { private: std::basic_string m_name; std::basic_string m_value; @@ -89,7 +89,7 @@ namespace rapidxml { }; template - class root : public rapidxml::internal::xpath_base { + class root : public flxml::internal::xpath_base { public: root() = default; @@ -105,7 +105,7 @@ namespace rapidxml { }; template - class any : public rapidxml::internal::xpath_base { + class any : public flxml::internal::xpath_base { public: any() = default; @@ -335,7 +335,7 @@ namespace rapidxml { explicit xpath(std::map & xmlns) : m_xmlns(xmlns) {} - rapidxml::generator &> all(xml_node & current, unsigned int depth = 0) { + flxml::generator &> all(xml_node & current, unsigned int depth = 0) { if (depth >= m_chain.size()) throw std::logic_error("Depth exceeded"); auto & xp = m_chain[depth]; depth++; diff --git a/include/flxml/print.hpp b/include/flxml/print.hpp new file mode 100644 index 0000000..5d99054 --- /dev/null +++ b/include/flxml/print.hpp @@ -0,0 +1,477 @@ +#ifndef RAPIDXML_PRINT_HPP_INCLUDED +#define RAPIDXML_PRINT_HPP_INCLUDED + +// Copyright (C) 2006, 2009 Marcin Kalicinski +// Version 1.13 +// Revision $DateTime: 2009/05/13 01:46:17 $ +//! \file rapidxml_print.hpp This file contains rapidxml printer implementation + +#include "flxml.hpp" + +// Only include streams if not disabled +#ifndef FLXML_NO_STREAMS + #include + #include +#endif + +namespace flxml +{ + + /////////////////////////////////////////////////////////////////////// + // Printing flags + + const int print_no_indenting = 0x1; //!< Printer flag instructing the printer to suppress indenting of XML. See print() function. + + /////////////////////////////////////////////////////////////////////// + // Internal + + //! \cond internal + namespace internal + { + + /////////////////////////////////////////////////////////////////////////// + // Internal character operations + + // Copy characters from given range to given output iterator + template + inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out) + { + while (begin != end) + *out++ = *begin++; + return out; + } + + template + inline OutIt copy_chars(std::basic_string_view const & sv, OutIt out) { + return copy_chars(sv.data(), sv.data() + sv.size(), out); + } + + // Copy characters from given range to given output iterator and expand + // characters into references (< > ' " &) + template + inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out) + { + while (begin != end) + { + if (*begin == noexpand) + { + *out++ = *begin; // No expansion, copy character + } + else + { + switch (*begin) + { + case Ch('<'): + *out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';'); + break; + case Ch('>'): + *out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';'); + break; + case Ch('\''): + *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';'); + break; + case Ch('"'): + *out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';'); + break; + case Ch('&'): + *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';'); + break; + default: + *out++ = *begin; // No expansion, copy character + } + } + ++begin; // Step to next character + } + return out; + } + + + template + inline OutIt copy_and_expand_chars(std::basic_string_view const & sv, Ch noexpand, OutIt out) { + return copy_and_expand_chars(sv.data(), sv.data() + sv.size(), noexpand, out); + } + // Fill given output iterator with repetitions of the same character + template + inline OutIt fill_chars(OutIt out, int n, Ch ch) + { + for (int i = 0; i < n; ++i) + *out++ = ch; + return out; + } + + // Find character + template + inline bool find_char(const Ch *begin, const Ch *end) + { + while (begin != end) + if (*begin++ == ch) + return true; + return false; + } + + /////////////////////////////////////////////////////////////////////////// + // Internal printing operations + + // Print node + template + inline OutIt print_node(OutIt out, const optional_ptr> node, int flags, int indent); + + // Print children of the node + template + inline OutIt print_children(OutIt out, const optional_ptr> node, int flags, int indent) + { + for (auto child = node->first_node(); child; child = child->next_sibling()) + out = print_node(out, child, flags, indent); + return out; + } + + // Print attributes of the node + template + inline OutIt print_attributes(OutIt out, const optional_ptr> node, int flags) + { + for (auto attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute()) + { + if (!(attribute->name().empty()) || attribute->value_raw().empty()) + { + // Print attribute name + *out = Ch(' '), ++out; + out = copy_chars(attribute->name(), out); + *out = Ch('='), ++out; + if (attribute->quote() && !attribute->value_decoded()) { + // Shortcut here; just dump out the raw value. + *out++ = attribute->quote(); + out = copy_chars(attribute->value_raw(), out); + **out++ = attribute->quote(); + } else { + // Print attribute value using appropriate quote type + if (attribute->value().find('"') != std::basic_string_view::npos) { + *out = Ch('\''), ++out; + out = copy_and_expand_chars(attribute->value(), Ch('"'), out); + *out = Ch('\''), ++out; + } else { + *out = Ch('"'), ++out; + out = copy_and_expand_chars(attribute->value(), Ch('\''), out); + *out = Ch('"'), ++out; + } + } + } + } + return out; + } + + // Print data node + template + inline OutIt print_data_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_data); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + if (!node->value_decoded()) { + out = copy_chars(node->value_raw(), out); + } else { + out = copy_and_expand_chars(node->value(), Ch(0), out); + } + return out; + } + + // Print data node + template + inline OutIt print_cdata_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_cdata); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + *out = Ch('<'); ++out; + *out = Ch('!'); ++out; + *out = Ch('['); ++out; + *out = Ch('C'); ++out; + *out = Ch('D'); ++out; + *out = Ch('A'); ++out; + *out = Ch('T'); ++out; + *out = Ch('A'); ++out; + *out = Ch('['); ++out; + out = copy_chars(node->value(), out); + *out = Ch(']'); ++out; + *out = Ch(']'); ++out; + *out = Ch('>'); ++out; + return out; + } + + // Print element node + template + inline OutIt print_element_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_element); + + // Print element name and attributes, if any + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + *out = Ch('<'), ++out; + if (!node->prefix().empty()) { + out = copy_chars(node->prefix(), out); + *out = Ch(':'); ++out; + } + out = copy_chars(node->name(), out); + out = print_attributes(out, node, flags); + + // If node is childless + if (node->value().empty() && !node->first_node()) + { + // Print childless node tag ending + *out = Ch('/'), ++out; + *out = Ch('>'), ++out; + } + else + { + // Print normal node tag ending + *out = Ch('>'), ++out; + + // If the node is clean, just output the contents and move on. + // Can only do this if we're not indenting, otherwise pretty-print won't work. + if (node->clean() && (flags & print_no_indenting)) { + out = copy_chars(node->contents(), out); + } else { + + // Test if node contains a single data node only (and no other nodes) + auto child = node->first_node(); + if (!child) { + // If node has no children, only print its value without indenting + if (!node->value_decoded()) { + out = copy_chars(node->value_raw(), out); + } else { + out = copy_and_expand_chars(node->value(), Ch(0), out); + } + } else if (!child->next_sibling() && child->type() == node_type::node_data) { + // If node has a sole data child, only print its value without indenting + if (!child->value_decoded()) { + out = copy_chars(child->value_raw(), out); + } else { + out = copy_and_expand_chars(child->value(), Ch(0), out); + } + } else { + // Print all children with full indenting + if (!(flags & print_no_indenting)) + *out = Ch('\n'), ++out; + out = print_children(out, node, flags, indent + 1); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + } + } + + // Print node end + *out = Ch('<'), ++out; + *out = Ch('/'), ++out; + if (!node->prefix().empty()) { + out = copy_chars(node->prefix(), out); + *out = Ch(':'); ++out; + } + out = copy_chars(node->name(), out); + *out = Ch('>'), ++out; + } + return out; + } + + // Print declaration node + template + inline OutIt print_declaration_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + // Print declaration start + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + *out = Ch('<'), ++out; + *out = Ch('?'), ++out; + *out = Ch('x'), ++out; + *out = Ch('m'), ++out; + *out = Ch('l'), ++out; + + // Print attributes + out = print_attributes(out, node, flags); + + // Print declaration end + *out = Ch('?'), ++out; + *out = Ch('>'), ++out; + + return out; + } + + // Print comment node + template + inline OutIt print_comment_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_comment); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + *out = Ch('<'), ++out; + *out = Ch('!'), ++out; + *out = Ch('-'), ++out; + *out = Ch('-'), ++out; + out = copy_chars(node->value(), out); + *out = Ch('-'), ++out; + *out = Ch('-'), ++out; + *out = Ch('>'), ++out; + return out; + } + + // Print doctype node + template + inline OutIt print_doctype_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_doctype); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + *out = Ch('<'), ++out; + *out = Ch('!'), ++out; + *out = Ch('D'), ++out; + *out = Ch('O'), ++out; + *out = Ch('C'), ++out; + *out = Ch('T'), ++out; + *out = Ch('Y'), ++out; + *out = Ch('P'), ++out; + *out = Ch('E'), ++out; + *out = Ch(' '), ++out; + out = copy_chars(node->value(), out); + *out = Ch('>'), ++out; + return out; + } + + // Print pi node + template + inline OutIt print_pi_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_pi); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + *out = Ch('<'), ++out; + *out = Ch('?'), ++out; + out = copy_chars(node->name(), out); + *out = Ch(' '), ++out; + out = copy_chars(node->value(), out); + *out = Ch('?'), ++out; + *out = Ch('>'), ++out; + return out; + } + + // Print literal node + template + inline OutIt print_literal_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + assert(node->type() == node_type::node_literal); + if (!(flags & print_no_indenting)) + out = fill_chars(out, indent, Ch('\t')); + out = copy_chars(node->value(), out); + return out; + } + + // Print node + // Print node + template + inline OutIt print_node(OutIt out, const optional_ptr> node, int flags, int indent) + { + // Print proper node type + switch (node->type()) + { + // Document + case node_document: + out = print_children(out, node, flags, indent); + break; + + // Element + case node_element: + out = print_element_node(out, node, flags, indent); + break; + + // Data + case node_data: + out = print_data_node(out, node, flags, indent); + break; + + // CDATA + case node_cdata: + out = print_cdata_node(out, node, flags, indent); + break; + + // Declaration + case node_declaration: + out = print_declaration_node(out, node, flags, indent); + break; + + // Comment + case node_comment: + out = print_comment_node(out, node, flags, indent); + break; + + // Doctype + case node_doctype: + out = print_doctype_node(out, node, flags, indent); + break; + + // Pi + case node_pi: + out = print_pi_node(out, node, flags, indent); + break; + + case node_literal: + out = print_literal_node(out, node, flags, indent); + break; + + // Unknown + default: + assert(0); + break; + } + + // If indenting not disabled, add line break after node + if (!(flags & print_no_indenting)) + *out = Ch('\n'), ++out; + + // Return modified iterator + return out; + } + + } + //! \endcond + + /////////////////////////////////////////////////////////////////////////// + // Printing + + //! Prints XML to given output iterator. + //! \param out Output iterator to print to. + //! \param node Node to be printed. Pass xml_document to print entire document. + //! \param flags Flags controlling how XML is printed. + //! \return Output iterator pointing to position immediately after last character of printed text. + template + inline OutIt print(OutIt out, const xml_node &node, int flags = 0) + { + flxml::optional_ptr ptr(const_cast *>(&node)); + return internal::print_node(out, ptr, flags, 0); + } + +#ifndef RAPIDXML_NO_STREAMS + + //! Prints XML to given output stream. + //! \param out Output stream to print to. + //! \param node Node to be printed. Pass xml_document to print entire document. + //! \param flags Flags controlling how XML is printed. + //! \return Output stream. + template + inline std::basic_ostream &print(std::basic_ostream &out, const xml_node &node, int flags = 0) + { + print(std::ostream_iterator(out), node, flags); + return out; + } + + //! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process. + //! \param out Output stream to print to. + //! \param node Node to be printed. + //! \return Output stream. + template + inline std::basic_ostream &operator <<(std::basic_ostream &out, const xml_node &node) + { + return print(out, node); + } + +#endif + +} + +#endif diff --git a/include/rapidxml_tables.hpp b/include/flxml/tables.hpp similarity index 99% rename from include/rapidxml_tables.hpp rename to include/flxml/tables.hpp index dccce22..815d02d 100644 --- a/include/rapidxml_tables.hpp +++ b/include/flxml/tables.hpp @@ -12,7 +12,7 @@ // Internals //! \cond internal -namespace rapidxml::internal { +namespace flxml::internal { // Struct that contains lookup tables for the parser struct lookup_tables { diff --git a/include/rapidxml_utils.hpp b/include/flxml/utils.hpp similarity index 95% rename from include/rapidxml_utils.hpp rename to include/flxml/utils.hpp index 5eafa35..df15b27 100644 --- a/include/rapidxml_utils.hpp +++ b/include/flxml/utils.hpp @@ -1,122 +1,122 @@ -#ifndef RAPIDXML_UTILS_HPP_INCLUDED -#define RAPIDXML_UTILS_HPP_INCLUDED - -// Copyright (C) 2006, 2009 Marcin Kalicinski -// Version 1.13 -// Revision $DateTime: 2009/05/13 01:46:17 $ -//! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful -//! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective. - -#include "rapidxml.hpp" -#include -#include -#include -#include - -namespace rapidxml -{ - - //! Represents data loaded from a file - template - class file - { - - public: - - //! Loads file into the memory. Data will be automatically destroyed by the destructor. - //! \param filename Filename to load. - file(const char *filename) - { - using namespace std; - - // Open stream - basic_ifstream stream(filename, ios::binary); - if (!stream) - throw runtime_error(string("cannot open file ") + filename); - stream.unsetf(ios::skipws); - - // Determine stream size - stream.seekg(0, ios::end); - size_t size = stream.tellg(); - stream.seekg(0); - - // Load data and add terminating 0 - m_data.resize(size + 1); - stream.read(&m_data.front(), static_cast(size)); - m_data[size] = 0; - } - - //! Loads file into the memory. Data will be automatically destroyed by the destructor - //! \param stream Stream to load from - file(std::basic_istream &stream) - { - using namespace std; - - // Load data and add terminating 0 - stream.unsetf(ios::skipws); - m_data.assign(istreambuf_iterator(stream), istreambuf_iterator()); - if (stream.fail() || stream.bad()) - throw runtime_error("error reading stream"); - m_data.push_back(0); - } - - //! Gets file data. - //! \return Pointer to data of file. - Ch *data() - { - return &m_data.front(); - } - - //! Gets file data. - //! \return Pointer to data of file. - const Ch *data() const - { - return &m_data.front(); - } - - //! Gets file data size. - //! \return Size of file data, in characters. - std::size_t size() const - { - return m_data.size(); - } - - private: - - std::vector m_data; // File data - - }; - - //! Counts children of node. Time complexity is O(n). - //! \return Number of children of node - template - inline std::size_t count_children(xml_node *node) - { - xml_node *child = node->first_node(); - std::size_t count = 0; - while (child) - { - ++count; - child = child->next_sibling(); - } - return count; - } - - //! Counts attributes of node. Time complexity is O(n). - //! \return Number of attributes of node - template - inline std::size_t count_attributes(xml_node *node) - { - xml_attribute *attr = node->first_attribute(); - std::size_t count = 0; - while (attr) - { - ++count; - attr = attr->next_attribute(); - } - return count; - } - -} - -#endif +#ifndef RAPIDXML_UTILS_HPP_INCLUDED +#define RAPIDXML_UTILS_HPP_INCLUDED + +// Copyright (C) 2006, 2009 Marcin Kalicinski +// Version 1.13 +// Revision $DateTime: 2009/05/13 01:46:17 $ +//! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful +//! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective. + +#include "flxml.hpp" +#include +#include +#include +#include + +namespace flxml +{ + + //! Represents data loaded from a file + template + class file + { + + public: + + //! Loads file into the memory. Data will be automatically destroyed by the destructor. + //! \param filename Filename to load. + file(const char *filename) + { + using namespace std; + + // Open stream + basic_ifstream stream(filename, ios::binary); + if (!stream) + throw runtime_error(string("cannot open file ") + filename); + stream.unsetf(ios::skipws); + + // Determine stream size + stream.seekg(0, ios::end); + size_t size = stream.tellg(); + stream.seekg(0); + + // Load data and add terminating 0 + m_data.resize(size + 1); + stream.read(&m_data.front(), static_cast(size)); + m_data[size] = 0; + } + + //! Loads file into the memory. Data will be automatically destroyed by the destructor + //! \param stream Stream to load from + file(std::basic_istream &stream) + { + using namespace std; + + // Load data and add terminating 0 + stream.unsetf(ios::skipws); + m_data.assign(istreambuf_iterator(stream), istreambuf_iterator()); + if (stream.fail() || stream.bad()) + throw runtime_error("error reading stream"); + m_data.push_back(0); + } + + //! Gets file data. + //! \return Pointer to data of file. + Ch *data() + { + return &m_data.front(); + } + + //! Gets file data. + //! \return Pointer to data of file. + const Ch *data() const + { + return &m_data.front(); + } + + //! Gets file data size. + //! \return Size of file data, in characters. + std::size_t size() const + { + return m_data.size(); + } + + private: + + std::vector m_data; // File data + + }; + + //! Counts children of node. Time complexity is O(n). + //! \return Number of children of node + template + inline std::size_t count_children(xml_node *node) + { + xml_node *child = node->first_node(); + std::size_t count = 0; + while (child) + { + ++count; + child = child->next_sibling(); + } + return count; + } + + //! Counts attributes of node. Time complexity is O(n). + //! \return Number of attributes of node + template + inline std::size_t count_attributes(xml_node *node) + { + xml_attribute *attr = node->first_attribute(); + std::size_t count = 0; + while (attr) + { + ++count; + attr = attr->next_attribute(); + } + return count; + } + +} + +#endif diff --git a/include/rapidxml_wrappers.hpp b/include/flxml/wrappers.hpp similarity index 99% rename from include/rapidxml_wrappers.hpp rename to include/flxml/wrappers.hpp index 9636402..6ec6214 100644 --- a/include/rapidxml_wrappers.hpp +++ b/include/flxml/wrappers.hpp @@ -9,7 +9,7 @@ #include #include -namespace rapidxml { +namespace flxml { // Most of rapidxml was written to use a NUL-terminated Ch * for parsing. // This utility struct wraps a buffer to provide something that // looks mostly like a pointer, deferencing to NUL when it hits the end. diff --git a/include/rapidxml.hpp b/include/rapidxml.hpp index 365e929..29d33c8 100644 --- a/include/rapidxml.hpp +++ b/include/rapidxml.hpp @@ -1,2643 +1,10 @@ -#ifndef RAPIDXML_HPP_INCLUDED -#define RAPIDXML_HPP_INCLUDED +// +// Created by dwd on 4/19/25. +// -// Copyright (C) 2006, 2009 Marcin Kalicinski -// Version 1.13 -// Revision $DateTime: 2009/05/13 01:46:17 $ -//! \file rapidxml.hpp This file contains rapidxml parser and DOM implementation +#ifndef RAPIDXML_HPP +#define RAPIDXML_HPP -#include "rapidxml_wrappers.hpp" -#include "rapidxml_tables.hpp" +namespace rapidxml = flxml; -// If standard library is disabled, user must provide implementations of required functions and typedefs -#if !defined(RAPIDXML_NO_STDLIB) - #include // For std::size_t - #include // For assert - #include // For placement new - #include - #include - #include - #include - #include // For std::runtime_error -#endif - -// On MSVC, disable "conditional expression is constant" warning (level 4). -// This warning is almost impossible to avoid with certain types of templated code -#ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable:4127) // Conditional expression is constant -#endif - -/////////////////////////////////////////////////////////////////////////// -// RAPIDXML_PARSE_ERROR - -#if defined(RAPIDXML_NO_EXCEPTIONS) - -#define RAPIDXML_PARSE_ERROR(what, where) { parse_error_handler(what, where); assert(0); } -#define RAPIDXML_EOF_ERROR(what, where) { parse_error_handler(what, where); assert(0); } - -namespace rapidxml -{ - //! When exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, - //! this function is called to notify user about the error. - //! It must be defined by the user. - //!

- //! This function cannot return. If it does, the results are undefined. - //!

- //! A very simple definition might look like that: - //!
-    //! void %rapidxml::%parse_error_handler(const char *what, void *where)
-    //! {
-    //!     std::cout << "Parse error: " << what << "\n";
-    //!     std::abort();
-    //! }
-    //! 
- //! \param what Human readable description of the error. - //! \param where Pointer to character data where error was detected. - void parse_error_handler(const char *what, void *where); -} - -#else - -#define RAPIDXML_PARSE_ERROR(what, where) {if (*where == Ch(0)) throw eof_error(what, nullptr); else throw parse_error(what, nullptr);} (void)0 -#define RAPIDXML_EOF_ERROR(what, where) throw eof_error(what, nullptr) - -namespace rapidxml -{ - - //! Parse error exception. - //! This exception is thrown by the parser when an error occurs. - //! Use what() function to get human-readable error message. - //! Use where() function to get a pointer to position within source text where error was detected. - //!

- //! If throwing exceptions by the parser is undesirable, - //! it can be disabled by defining RAPIDXML_NO_EXCEPTIONS macro before rapidxml.hpp is included. - //! This will cause the parser to call rapidxml::parse_error_handler() function instead of throwing an exception. - //! This function must be defined by the user. - //!

- //! This class derives from std::exception class. - class parse_error: public std::runtime_error - { - - public: - - //! Constructs parse error - parse_error(const char *what, void *where) - : std::runtime_error(what) - , m_where(where) - { - } - - //! Gets pointer to character data where error happened. - //! Ch should be the same as char type of xml_document that produced the error. - //! \return Pointer to location within the parsed string where error occured. - template - Ch *where() const - { - return reinterpret_cast(m_where); - } - - private: - void *m_where; - }; - - class eof_error : public parse_error { - public: - using parse_error::parse_error; - }; - - class validation_error : public std::runtime_error - { - public: - using std::runtime_error::runtime_error; - }; - - class xmlns_unbound : public validation_error { - public: - using validation_error::validation_error; - }; - - class duplicate_attribute : public validation_error { - public: - using validation_error::validation_error; - }; - - class attr_xmlns_unbound : public xmlns_unbound { - public: - using xmlns_unbound::xmlns_unbound; - }; - - class element_xmlns_unbound : public xmlns_unbound { - public: - using xmlns_unbound::xmlns_unbound; - }; -} - -#endif - -/////////////////////////////////////////////////////////////////////////// -// Pool sizes - -#ifndef RAPIDXML_STATIC_POOL_SIZE - // Size of static memory block of memory_pool. - // Define RAPIDXML_STATIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value. - // No dynamic memory allocations are performed by memory_pool until static memory is exhausted. - #define RAPIDXML_STATIC_POOL_SIZE (64 * 1024) -#endif - -#ifndef RAPIDXML_DYNAMIC_POOL_SIZE - // Size of dynamic memory block of memory_pool. - // Define RAPIDXML_DYNAMIC_POOL_SIZE before including rapidxml.hpp if you want to override the default value. - // After the static block is exhausted, dynamic blocks with approximately this size are allocated by memory_pool. - #define RAPIDXML_DYNAMIC_POOL_SIZE (64 * 1024) -#endif - -namespace rapidxml -{ - // Forward declarations - template class xml_node; - template class xml_attribute; - template class xml_document; - template class children; - template class descendants; - template class attributes; - - //! Enumeration listing all node types produced by the parser. - //! Use xml_node::type() function to query node type. - enum class node_type - { - node_document, //!< A document node. Name and value are empty. - node_element, //!< An element node. Name contains element name. Value contains text of first data node. - node_data, //!< A data node. Name is empty. Value contains data text. - node_cdata, //!< A CDATA node. Name is empty. Value contains data text. - node_comment, //!< A comment node. Name is empty. Value contains comment text. - node_declaration, //!< A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalone) are in node attributes. - node_doctype, //!< A DOCTYPE node. Name is empty. Value contains DOCTYPE text. - node_pi, //!< A PI node. Name contains target. Value contains instructions. - node_literal //!< Value is unencoded text (used for inserting pre-rendered XML). - }; - using enum node_type; // Import this into the rapidxml namespace as before. - - /////////////////////////////////////////////////////////////////////// - // Parsing flags - - //! Parse flag instructing the parser to not create data nodes. - //! Text of first data node will still be placed in value of parent element, unless rapidxml::parse_no_element_values flag is also specified. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_no_data_nodes = 0x1; - - //! Parse flag instructing the parser to not use text of first data node as a value of parent element. - //! Can be combined with other flags by use of | operator. - //! Note that child data nodes of element node take precendence over its value when printing. - //! That is, if element has one or more child data nodes and a value, the value will be ignored. - //! Use rapidxml::parse_no_data_nodes flag to prevent creation of data nodes if you want to manipulate data using values of elements. - //!

- //! See xml_document::parse() function. - const int parse_no_element_values = 0x2; - - //! Parse flag instructing the parser to not translate entities in the source text. - //! By default entities are translated, modifying source text. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_no_entity_translation = 0x8; - - //! Parse flag instructing the parser to disable UTF-8 handling and assume plain 8 bit characters. - //! By default, UTF-8 handling is enabled. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_no_utf8 = 0x10; - - //! Parse flag instructing the parser to create XML declaration node. - //! By default, declaration node is not created. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_declaration_node = 0x20; - - //! Parse flag instructing the parser to create comments nodes. - //! By default, comment nodes are not created. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_comment_nodes = 0x40; - - //! Parse flag instructing the parser to create DOCTYPE node. - //! By default, doctype node is not created. - //! Although W3C specification allows at most one DOCTYPE node, RapidXml will silently accept documents with more than one. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_doctype_node = 0x80; - - //! Parse flag instructing the parser to create PI nodes. - //! By default, PI nodes are not created. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_pi_nodes = 0x100; - - //! Parse flag instructing the parser to validate closing tag names. - //! If not set, name inside closing tag is irrelevant to the parser. - //! By default, closing tags are not validated. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_validate_closing_tags = 0x200; - - //! Parse flag instructing the parser to trim all leading and trailing whitespace of data nodes. - //! By default, whitespace is not trimmed. - //! This flag does not cause the parser to modify source text. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_trim_whitespace = 0x400; - - //! Parse flag instructing the parser to condense all whitespace runs of data nodes to a single space character. - //! Trimming of leading and trailing whitespace of data is controlled by rapidxml::parse_trim_whitespace flag. - //! By default, whitespace is not normalized. - //! If this flag is specified, source text will be modified. - //! Can be combined with other flags by use of | operator. - //!

- //! See xml_document::parse() function. - const int parse_normalize_whitespace = 0x800; - - //! Parse flag to say "Parse only the initial element opening." - //! Useful for XMLstreams used in XMPP. - const int parse_open_only = 0x1000; - - //! Parse flag to say "Toss the children of the top node and parse off - //! one element. - //! Useful for parsing off XMPP top-level elements. - const int parse_parse_one = 0x2000; - - //! Parse flag to say "Validate XML namespaces fully." - //! This will generate additional errors, including unbound prefixes - //! and duplicate attributes (with different prefices) - const int parse_validate_xmlns = 0x4000; - - // Compound flags - - //! Parse flags which represent default behaviour of the parser. - //! This is always equal to 0, so that all other flags can be simply ored together. - //! Normally there is no need to inconveniently disable flags by anding with their negated (~) values. - //! This also means that meaning of each flag is a negation of the default setting. - //! For example, if flag name is rapidxml::parse_no_utf8, it means that utf-8 is enabled by default, - //! and using the flag will disable it. - //!

- //! See xml_document::parse() function. - [[maybe_unused]] const int parse_default = 0; - - //! A combination of parse flags resulting in fastest possible parsing, without sacrificing important data. - //!

- //! See xml_document::parse() function. - const int parse_fastest = parse_no_data_nodes; - - //! A combination of parse flags resulting in largest amount of data being extracted. - //! This usually results in slowest parsing. - //!

- //! See xml_document::parse() function. - const int parse_full = parse_declaration_node | parse_comment_nodes | parse_doctype_node | parse_pi_nodes | parse_validate_closing_tags | parse_validate_xmlns; - - - /////////////////////////////////////////////////////////////////////// - // Memory pool - - //! This class is used by the parser to create new nodes and attributes, without overheads of dynamic memory allocation. - //! In most cases, you will not need to use this class directly. - //! However, if you need to create nodes manually or modify names/values of nodes, - //! you are encouraged to use memory_pool of relevant xml_document to allocate the memory. - //! Not only is this faster than allocating them by using new operator, - //! but also their lifetime will be tied to the lifetime of document, - //! possibly simplyfing memory management. - //!

- //! Call allocate_node() or allocate_attribute() functions to obtain new nodes or attributes from the pool. - //! You can also call allocate_string() function to allocate strings. - //! Such strings can then be used as names or values of nodes without worrying about their lifetime. - //! Note that there is no free() function -- all allocations are freed at once when clear() function is called, - //! or when the pool is destroyed. - //!

- //! It is also possible to create a standalone memory_pool, and use it - //! to allocate nodes, whose lifetime will not be tied to any document. - //!

- //! Pool maintains RAPIDXML_STATIC_POOL_SIZE bytes of statically allocated memory. - //! Until static memory is exhausted, no dynamic memory allocations are done. - //! When static memory is exhausted, pool allocates additional blocks of memory of size RAPIDXML_DYNAMIC_POOL_SIZE each, - //! by using global new[] and delete[] operators. - //! This behaviour can be changed by setting custom allocation routines. - //! Use set_allocator() function to set them. - //!

- //! Allocations for nodes, attributes and strings are aligned at RAPIDXML_ALIGNMENT bytes. - //! This value defaults to the size of pointer on target architecture. - //!

- //! To obtain absolutely top performance from the parser, - //! it is important that all nodes are allocated from a single, contiguous block of memory. - //! Otherwise, cache misses when jumping between two (or more) disjoint blocks of memory can slow down parsing quite considerably. - //! If required, you can tweak RAPIDXML_STATIC_POOL_SIZE, RAPIDXML_DYNAMIC_POOL_SIZE and RAPIDXML_ALIGNMENT - //! to obtain best wasted memory to performance compromise. - //! To do it, define their values before rapidxml.hpp file is included. - //! \param Ch Character type of created nodes. - template - class memory_pool - { - - public: - - //! \cond internal - using alloc_func = void * (*)(std::size_t); // Type of user-defined function used to allocate memory - using free_func = void (*)(void *); // Type of user-defined function used to free memory - //! \endcond - - //! Constructs empty pool with default allocator functions. - memory_pool() { - init(); - } - memory_pool(memory_pool const &) = delete; - memory_pool(memory_pool &&) = delete; - - //! Destroys pool and frees all the memory. - //! This causes memory occupied by nodes allocated by the pool to be freed. - //! Nodes allocated from the pool are no longer valid. - ~memory_pool() - { - clear(); - } - - using view_type = std::basic_string_view; - - //! Allocates a new node from the pool, and optionally assigns name and value to it. - //! If the allocation request cannot be accomodated, this function will throw std::bad_alloc. - //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function - //! will call rapidxml::parse_error_handler() function. - //! \param type Type of node to create. - //! \param name Name to assign to the node, or 0 to assign no name. - //! \param value Value to assign to the node, or 0 to assign no value. - //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string. - //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string. - //! \return Pointer to allocated node. This pointer will never be NULL. - template - xml_node * allocate_node_low(Args... args) { - void *memory = allocate_aligned>(); - auto *node = new(memory) xml_node(args...); - return node; - } - xml_node * allocate_node(node_type type, view_type const & name, view_type const & value) { - auto * node = this->allocate_node_low(type, name); - node->value(value); - return node; - } - xml_node * allocate_node(node_type type, view_type const & name) { - return this->allocate_node_low(type, name); - } - xml_node * allocate_node(node_type type) { - return this->allocate_node_low(type); - } - - //! Allocates a new attribute from the pool, and optionally assigns name and value to it. - //! If the allocation request cannot be accomodated, this function will throw std::bad_alloc. - //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function - //! will call rapidxml::parse_error_handler() function. - //! \param name Name to assign to the attribute, or 0 to assign no name. - //! \param value Value to assign to the attribute, or 0 to assign no value. - //! \param name_size Size of name to assign, or 0 to automatically calculate size from name string. - //! \param value_size Size of value to assign, or 0 to automatically calculate size from value string. - //! \return Pointer to allocated attribute. This pointer will never be NULL. - template - xml_attribute *allocate_attribute_low(Args... args) { - void *memory = allocate_aligned>(); - auto *attribute = new(memory) xml_attribute(args...); - return attribute; - } - xml_attribute * allocate_attribute(view_type const & name, view_type const & value) { - auto * attr = this->allocate_attribute_low(name); - attr->value(value); - return attr; - } - xml_attribute * allocate_attribute(view_type const & name) { - return this->allocate_attribute_low(name); - } - xml_attribute * allocate_attribute() { - return this->allocate_attribute_low(); - } - - //! Allocates a char array of given size from the pool, and optionally copies a given string to it. - //! If the allocation request cannot be accomodated, this function will throw std::bad_alloc. - //! If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function - //! will call rapidxml::parse_error_handler() function. - //! \param source String to initialize the allocated memory with, or 0 to not initialize it. - //! \param size Number of characters to allocate, or zero to calculate it automatically from source string length; if size is 0, source string must be specified and null terminated. - //! \return Pointer to allocated char array. This pointer will never be NULL. - template - std::span allocate_span(std::basic_string_view const & source) - { - if (source.size() == 0) return {}; // No need to allocate. - Ch *result = allocate_aligned(source.size()); - for (std::size_t i = 0; i < source.size(); ++i) - result[i] = source[i]; - return {result, source.size()}; - } - - template - view_type allocate_string(std::basic_string_view const & source) { - auto span = allocate_span(source); - return {span.data(), span.size()}; - } - - template - view_type allocate_string(std::basic_string const & source) { - return allocate_string(std::basic_string_view{source.data(), source.size()}); - } - - template - view_type allocate_string(const Sch * source) { - return allocate_string(std::basic_string_view(source)); - } - - view_type const & nullstr() - { - return m_nullstr; - } - view_type const & xmlns_xml() - { - if (m_xmlns_xml.empty()) - m_xmlns_xml = allocate_string("http://www.w3.org/XML/1998/namespace"); - return m_xmlns_xml; - } - view_type const & xmlns_xmlns() - { - if (m_xmlns_xmlns.empty()) - m_xmlns_xmlns = allocate_string("http://www.w3.org/2000/xmlns/"); - return m_xmlns_xmlns; - } - - - //! Clones an xml_node and its hierarchy of child nodes and attributes. - //! Nodes and attributes are allocated from this memory pool. - //! Names and values are not cloned, they are shared between the clone and the source. - //! Result node can be optionally specified as a second parameter, - //! in which case its contents will be replaced with cloned source node. - //! This is useful when you want to clone entire document. - //! \param source Node to clone. - //! \param result Node to put results in, or 0 to automatically allocate result node - //! \return Pointer to cloned node. This pointer will never be NULL. - optional_ptr> clone_node(const optional_ptr> source, bool strings=false) - { - // Prepare result node - auto result = allocate_node(source->type()); - auto s = [this, strings](view_type const & sv) { return strings ? this->allocate_string(sv) : sv; }; - - // Clone name and value - result->name(s(source->name())); - result->value(s(source->value())); - result->prefix(s(source->prefix())); - - // Clone child nodes and attributes - for (auto child = source->first_node(); child; child = child->next_sibling()) - result->append_node(clone_node(child, strings)); - for (auto attr = source->first_attribute(); attr; attr = attr->next_attribute()) - result->append_attribute(allocate_attribute(s(attr->name()), s(attr->value()))); - - return result; - } - - //! Clears the pool. - //! This causes memory occupied by nodes allocated by the pool to be freed. - //! Any nodes or strings allocated from the pool will no longer be valid. - void clear() - { - while (m_begin != m_static_memory.data()) - { - std::size_t s = sizeof(header) * 2; - void * h = m_begin; - std::align(alignof(header), sizeof(header), h, s); - void *previous_begin = reinterpret_cast
(h)->previous_begin; - if (m_free_func) - m_free_func(m_begin); - else - delete[] reinterpret_cast(m_begin); - m_begin = previous_begin; - } - init(); - } - - //! Sets or resets the user-defined memory allocation functions for the pool. - //! This can only be called when no memory is allocated from the pool yet, otherwise results are undefined. - //! Allocation function must not return invalid pointer on failure. It should either throw, - //! stop the program, or use longjmp() function to pass control to other place of program. - //! If it returns invalid pointer, results are undefined. - //!

- //! User defined allocation functions must have the following forms: - //!
- //!
void *allocate(std::size_t size); - //!
void free(void *pointer); - //!

- //! \param af Allocation function, or 0 to restore default function - //! \param ff Free function, or 0 to restore default function - [[maybe_unused]] void set_allocator(alloc_func af, free_func ff) - { - assert(m_begin == m_static_memory.data() && m_ptr == m_begin); // Verify that no memory is allocated yet - m_alloc_func = af; - m_free_func = ff; - } - - private: - - struct header - { - void *previous_begin; - }; - - void init() - { - m_begin = m_static_memory.data(); - m_ptr = m_begin; - m_space = m_static_memory.size(); - } - - void *allocate_raw(std::size_t size) - { - // Allocate - void *memory; - if (m_alloc_func) // Allocate memory using either user-specified allocation function or global operator new[] - { - memory = m_alloc_func(size); - assert(memory); // Allocator is not allowed to return 0, on failure it must either throw, stop the program or use longjmp - } - else - { - memory = new char[size]; -#ifdef RAPIDXML_NO_EXCEPTIONS - if (!memory) // If exceptions are disabled, verify memory allocation, because new will not be able to throw bad_alloc - RAPIDXML_PARSE_ERROR("out of memory", 0); -#endif - } - return memory; - } - - template - T *allocate_aligned(std::size_t n = 1) - { - auto size = n * sizeof(T); - // Calculate aligned pointer - if (!std::align(alignof(T), sizeof(T) * n, m_ptr, m_space)) { - // If not enough memory left in current pool, allocate a new pool - // Calculate required pool size (may be bigger than RAPIDXML_DYNAMIC_POOL_SIZE) - std::size_t pool_size = RAPIDXML_DYNAMIC_POOL_SIZE; - if (pool_size < size) - pool_size = size; - - // Allocate - std::size_t alloc_size = sizeof(header) + (2 * alignof(header) - 2) + pool_size; // 2 alignments required in worst case: one for header, one for actual allocation - void *raw_memory = allocate_raw(alloc_size); - - // Setup new pool in allocated memory - void *new_header = raw_memory; - std::align(alignof(header), sizeof(header), new_header, alloc_size); - auto * h = reinterpret_cast
(new_header); - h->previous_begin = m_begin; - m_begin = raw_memory; - m_ptr = (h + 1); - m_space = alloc_size - sizeof(header); - - // Calculate aligned pointer again using new pool - return allocate_aligned(n); - } - auto * result = reinterpret_cast(m_ptr); - m_ptr = (result + n); - m_space -= size; - auto blank = reinterpret_cast(result); - auto end = blank + size; - while (blank != end) *blank++ = 'X'; - return result; - } - - void *m_begin = nullptr; // Start of raw memory making up current pool - void *m_ptr = nullptr; // First free byte in current pool - std::size_t m_space = RAPIDXML_STATIC_POOL_SIZE; // Available space remaining - std::array m_static_memory = {}; // Static raw memory - alloc_func m_alloc_func = nullptr; // Allocator function, or 0 if default is to be used - free_func m_free_func = nullptr; // Free function, or 0 if default is to be used - view_type m_nullstr; - view_type m_xmlns_xml; - view_type m_xmlns_xmlns; - }; - - /////////////////////////////////////////////////////////////////////////// - // XML base - - //! Base class for xml_node and xml_attribute implementing common functions: - //! name(), name_size(), value(), value_size() and parent(). - //! \param Ch Character type to use - template - class xml_base - { - - public: - using view_type = std::basic_string_view; - - /////////////////////////////////////////////////////////////////////////// - // Construction & destruction - - // Construct a base with empty name, value and parent - xml_base() = default; - explicit xml_base(view_type const & name) : m_name(name) {} - xml_base(view_type const & name, view_type const & value) : m_name(name), m_value(value) {} - - /////////////////////////////////////////////////////////////////////////// - // Node data access - - //! Gets name of the node. - //! Interpretation of name depends on type of node. - //! Note that name will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse. - //!

- //! Use name_size() function to determine length of the name. - //! \return Name of node, or empty string if node has no name. - view_type const & name() const - { - return m_name; - } - - //! Gets value of node. - //! Interpretation of value depends on type of node. - //! Note that value will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse. - //!

- //! Use value_size() function to determine length of the value. - //! \return Value of node, or empty string if node has no value. - view_type const & value_raw() const - { - return m_value; - } - - /////////////////////////////////////////////////////////////////////////// - // Node modification - - //! Sets name of node to a non zero-terminated string. - //! See \ref ownership_of_strings. - //!

- //! Note that node does not own its name or value, it only stores a pointer to it. - //! It will not delete or otherwise free the pointer on destruction. - //! It is reponsibility of the user to properly manage lifetime of the string. - //! The easiest way to achieve it is to use memory_pool of the document to allocate the string - - //! on destruction of the document the string will be automatically freed. - //!

- //! Size of name must be specified separately, because name does not have to be zero terminated. - //! Use name(const Ch *) function to have the length automatically calculated (string must be zero terminated). - //! \param name Name of node to set. Does not have to be zero terminated. - //! \param size Size of name, in characters. This does not include zero terminator, if one is present. - void name(view_type const & name) { - m_name = name; - } - - //! Sets value of node to a non zero-terminated string. - //! See \ref ownership_of_strings. - //!

- //! Note that node does not own its name or value, it only stores a pointer to it. - //! It will not delete or otherwise free the pointer on destruction. - //! It is reponsibility of the user to properly manage lifetime of the string. - //! The easiest way to achieve it is to use memory_pool of the document to allocate the string - - //! on destruction of the document the string will be automatically freed. - //!

- //! Size of value must be specified separately, because it does not have to be zero terminated. - //! Use value(const Ch *) function to have the length automatically calculated (string must be zero terminated). - //!

- //! If an element has a child node of type node_data, it will take precedence over element value when printing. - //! If you want to manipulate data of elements using values, use parser flag rapidxml::parse_no_data_nodes to prevent creation of data nodes by the parser. - //! \param value value of node to set. Does not have to be zero terminated. - //! \param size Size of value, in characters. This does not include zero terminator, if one is present. - void value_raw(view_type const & value) - { - m_value = value; - } - - /////////////////////////////////////////////////////////////////////////// - // Related nodes access - - //! Gets node parent. - //! \return Pointer to parent node, or 0 if there is no parent. - optional_ptr> parent() const - { - return m_parent; - } - - protected: - view_type m_name; // Name of node, or 0 if no name - view_type m_value; // Value of node, or 0 if no value - xml_node *m_parent = nullptr; // Pointer to parent node, or 0 if none - }; - - //! Class representing attribute node of XML document. - //! Each attribute has name and value strings, which are available through name() and value() functions (inherited from xml_base). - //! Note that after parse, both name and value of attribute will point to interior of source text used for parsing. - //! Thus, this text must persist in memory for the lifetime of attribute. - //! \param Ch Character type to use. - template - class xml_attribute: public xml_base - { - - friend class xml_node; - - public: - using view_type = std::basic_string_view; - using ptr = optional_ptr>; - - /////////////////////////////////////////////////////////////////////////// - // Construction & destruction - - //! Constructs an empty attribute with the specified type. - //! Consider using memory_pool of appropriate xml_document if allocating attributes manually. - xml_attribute() = default; - xml_attribute(view_type const & name) : xml_base(name) {} - xml_attribute(view_type const & name, view_type const & value) : xml_base(name, value) {} - - void quote(Ch q) { - m_quote = q; - } - Ch quote() const { - return m_quote; - } - - view_type const & value() const { - if (m_value.has_value()) return m_value.value(); - m_value = document()->decode_attr_value(this); - return m_value.value(); - } - void value(view_type const & v) { - m_value = v; - this->value_raw(""); - if (this->m_parent) this->m_parent->dirty_parent(); - } - // Return true if the value has been decoded. - bool value_decoded() const { - // Either we don't have a decoded value, or we do but it's identical. - return !m_value.has_value() || m_value.value().data() != this->value_raw().data(); - } - - /////////////////////////////////////////////////////////////////////////// - // Related nodes access - - //! Gets document of which attribute is a child. - //! \return Pointer to document that contains this attribute, or 0 if there is no parent document. - optional_ptr> document() const { - if (auto node = this->parent()) { - return node->document(); - } else { - return nullptr; - } - } - - view_type const & xmlns() const { - if (m_xmlns.has_value()) return m_xmlns.value(); - auto const & name = this->name(); - auto colon = name.find(':'); - if (colon != view_type::npos) { - auto element = this->parent(); - if (element) m_xmlns = element->xmlns_lookup(name.substr(0, colon), true); - } else { - m_xmlns = document()->nullstr(); - } - return m_xmlns.value(); - } - //! Gets previous attribute, optionally matching attribute name. - //! \param name Name of attribute to find, or 0 to return previous attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found attribute, or 0 if not found. - optional_ptr> previous_attribute(view_type const & name = {}) const - { - if (name) - { - for (xml_attribute *attribute = m_prev_attribute; attribute; attribute = attribute->m_prev_attribute) - if (name == attribute->name()) - return attribute; - return 0; - } - else - return this->m_parent ? m_prev_attribute : 0; - } - - //! Gets next attribute, optionally matching attribute name. - //! \param name Name of attribute to find, or 0 to return next attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found attribute, or 0 if not found. - optional_ptr> next_attribute(view_type const & name = {}) const - { - if (!name.empty()) - { - for (xml_attribute *attribute = m_next_attribute; attribute; attribute = attribute->m_next_attribute) - if (attribute->name() == name) - return attribute; - return nullptr; - } - else - return this->m_parent ? m_next_attribute : nullptr; - } - - view_type const & local_name() const - { - if (!m_local_name.empty()) return m_local_name; - auto colon = this->name().find(':'); - if (colon == view_type::npos) { - m_local_name = this->name(); - } else { - m_local_name = this->name().substr(colon + 1); - } - return m_local_name; - } - - private: - - xml_attribute *m_prev_attribute = nullptr; // Pointer to previous sibling of attribute, or 0 if none; only valid if parent is non-zero - xml_attribute *m_next_attribute = nullptr; // Pointer to next sibling of attribute, or 0 if none; only valid if parent is non-zero - Ch m_quote = 0; // When parsing, this should be set to the containing quote for the value. - mutable std::optional m_xmlns; - mutable std::optional m_value; // This is the decoded, not raw, value. - mutable view_type m_local_name; // ATTN: points inside m_name. - }; - - /////////////////////////////////////////////////////////////////////////// - // XML node - - //! Class representing a node of XML document. - //! Each node may have associated name and value strings, which are available through name() and value() functions. - //! Interpretation of name and value depends on type of the node. - //! Type of node can be determined by using type() function. - //!

- //! Note that after parse, both name and value of node, if any, will point interior of source text used for parsing. - //! Thus, this text must persist in the memory for the lifetime of node. - //! \param Ch Character type to use. - template - class xml_node: public xml_base - { - public: - using view_type = std::basic_string_view; - using ptr = optional_ptr>; - - /////////////////////////////////////////////////////////////////////////// - // Construction & destruction - - //! Constructs an empty node with the specified type. - //! Consider using memory_pool of appropriate document to allocate nodes manually. - //! \param type Type of node to construct. - explicit xml_node(node_type type) - : m_type(type) - { - } - xml_node(node_type type, view_type const & name) : xml_base(name), m_type(type) {} - xml_node(node_type type, view_type const & name, view_type const & value) : xml_base(name, value), m_type(type) {} - - /////////////////////////////////////////////////////////////////////////// - // Node data access - view_type const & value() const { - if (m_value.has_value()) return m_value.value(); - if (m_type == node_element || m_type == node_data) { - m_value = document()->decode_data_value(this); - } else { - m_value = this->value_raw(); - } - return m_value.value(); - } - - void dirty() { - m_clean = false; - dirty_parent(); - } - void dirty_parent() { - if (this->m_parent) this->m_parent->dirty(); - } - bool clean() const { - return m_clean; - } - - void value(view_type const & v) { - if (this->m_type == node_element) { - // Set the first data node to the value, if one exists. - for (auto node = m_first_node; node; node = node->m_next_sibling) { - if (node->type() == node_data) { - node->value(v); - break; - } - } - } - m_value = v; - this->value_raw(""); - dirty(); - } - - bool value_decoded() const { - return !m_value.has_value() || m_value.value().data() != this->value_raw().data(); - } - - //! Gets type of node. - //! \return Type of node. - node_type type() const { - return m_type; - } - - void prefix(view_type const & prefix) { - m_prefix = prefix; - dirty_parent(); - } - - view_type const & prefix() const { - return m_prefix; - } - - void contents(view_type const & contents) { - m_contents = contents; - // Reset to clean here. - m_clean = true; - } - view_type const & contents() const - { - return m_contents; - } - - view_type const & xmlns() const { - if (m_xmlns.has_value()) return m_xmlns.value(); - m_xmlns = xmlns_lookup(m_prefix, false); - return m_xmlns.value(); - } - - view_type const & xmlns_lookup(view_type const & prefix, bool attribute) const - { - std::basic_string attrname{"xmlns"}; - if (!prefix.empty()) { - // Check if the prefix begins "xml". - if (prefix.size() >= 3 && prefix.starts_with("xml")) { - if (prefix.size() == 3) { - return this->document()->xmlns_xml(); - } else if (prefix.size() == 5 - && prefix[3] == Ch('n') - && prefix[4] == Ch('s')) { - return this->document()->xmlns_xmlns(); - } - } - attrname += ':'; - attrname += prefix; - } - for (const xml_node * node = this; - node; - node = node->m_parent) { - auto attr = node->first_attribute(attrname); - if (attr) { - return attr->value(); - } - } - if (!prefix.empty()) { - if (attribute) { - throw attr_xmlns_unbound(attrname.c_str()); - } else { - throw element_xmlns_unbound(attrname.c_str()); - } - } - return document()->nullstr(); - } - - /////////////////////////////////////////////////////////////////////////// - // Related nodes access - - //! Gets document of which node is a child. - //! \return Pointer to document that contains this node, or 0 if there is no parent document. - optional_ptr> document() const - { - auto *node = this; - while (node) { - if (node->type() == node_document) { - return static_cast *>(const_cast *>(node)); - } - node = node->parent().ptr_unsafe(); - } - return nullptr; - } - - rapidxml::children children() const { - return rapidxml::children{*this}; - } - - rapidxml::descendants descendants() const { - return rapidxml::descendants{optional_ptr>{const_cast *>(this)}}; - } - - rapidxml::attributes attributes() const { - return rapidxml::attributes{*this}; - } - - //! Gets first child node, optionally matching node name. - //! \param name Name of child to find, or 0 to return first child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found child, or 0 if not found. - optional_ptr> first_node(view_type const & name = {}, view_type const & asked_xmlns = {}) const - { - view_type xmlns = asked_xmlns; - if (asked_xmlns.empty() && !name.empty()) { - // No XMLNS asked for, but a name is present. - // Assume "same XMLNS". - xmlns = this->xmlns(); - } - for (xml_node *child = m_first_node; child; child = child->m_next_sibling) { - if ((name.empty() || child->name() == name) - && (xmlns.empty() || child->xmlns() == xmlns)) { - return child; - } - } - return nullptr; - } - - //! Gets last child node, optionally matching node name. - //! Behaviour is undefined if node has no children. - //! Use first_node() to test if node has children. - //! \param name Name of child to find, or 0 to return last child regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found child, or 0 if not found. - optional_ptr> last_node(view_type const & name = {}, view_type const & asked_xmlns = {}) const - { - view_type xmlns = asked_xmlns; - if (asked_xmlns.empty() && !name.empty()) { - // No XMLNS asked for, but a name is present. - // Assume "same XMLNS". - xmlns = this->xmlns(); - } - for (xml_node *child = m_last_node; child; child = child->m_prev_sibling) { - if ((name.empty() || child->name() == name) - && (xmlns.empty() || child->xmlns() == xmlns)) { - return child; - } - } - return nullptr; - } - - //! Gets previous sibling node, optionally matching node name. - //! Behaviour is undefined if node has no parent. - //! Use parent() to test if node has a parent. - //! \param name Name of sibling to find, or 0 to return previous sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found sibling, or 0 if not found. - optional_ptr> previous_sibling(view_type const & name = {}, view_type const & asked_xmlns = {}) const - { - assert(this->m_parent); // Cannot query for siblings if node has no parent - if (!name.empty()) - { - view_type xmlns = asked_xmlns; - if (xmlns.empty() && !name.empty()) { - // No XMLNS asked for, but a name is present. - // Assume "same XMLNS". - xmlns = this->xmlns(); - } - for (xml_node *sibling = m_prev_sibling; sibling; sibling = sibling->m_prev_sibling) - if ((name.empty() || sibling->name() == name) - && (xmlns.empty() || sibling->xmlns() == xmlns)) - return sibling; - return nullptr; - } - else - return m_prev_sibling; - } - - //! Gets next sibling node, optionally matching node name. - //! Behaviour is undefined if node has no parent. - //! Use parent() to test if node has a parent. - //! \param name Name of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param xmlns Namespace of sibling to find, or 0 to return next sibling regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found sibling, or 0 if not found. - optional_ptr> next_sibling(view_type const & name = {}, view_type const & asked_xmlns = {}) const - { - assert(this->m_parent); // Cannot query for siblings if node has no parent - view_type xmlns = asked_xmlns; - if (xmlns.empty() && !name.empty()) { - // No XMLNS asked for, but a name is present. - // Assume "same XMLNS". - xmlns = this->xmlns(); - } - for (xml_node *sibling = m_next_sibling; sibling; sibling = sibling->m_next_sibling) - if ((name.empty() || sibling->name() == name) - && (xmlns.empty() || sibling->xmlns() == xmlns)) - return sibling; - return nullptr; - } - - //! Gets first attribute of node, optionally matching attribute name. - //! \param name Name of attribute to find, or 0 to return first attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found attribute, or 0 if not found. - optional_ptr> first_attribute(view_type const & name = {}, view_type const & xmlns = {}) const - { - for (xml_attribute *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) - if ((name.empty() || attribute->name() == name) && (xmlns.empty() || attribute->xmlns() == xmlns)) - return attribute; - return nullptr; - } - - //! Gets last attribute of node, optionally matching attribute name. - //! \param name Name of attribute to find, or 0 to return last attribute regardless of its name; this string doesn't have to be zero-terminated if name_size is non-zero - //! \param name_size Size of name, in characters, or 0 to have size calculated automatically from string - //! \param case_sensitive Should name comparison be case-sensitive; non case-sensitive comparison works properly only for ASCII characters - //! \return Pointer to found attribute, or 0 if not found. - optional_ptr> last_attribute(view_type const & name = {}, view_type const & xmlns = {}) const - { - for (xml_attribute *attribute = m_last_attribute; attribute; attribute = attribute->m_prev_attribute) - if ((name.empty() || attribute->name() == name) && (xmlns.empty() || attribute->xmlns() == xmlns)) - return attribute; - return nullptr; - } - - /////////////////////////////////////////////////////////////////////////// - // Node modification - - //! Sets type of node. - //! \param type Type of node to set. - void type(node_type type) { - m_type = type; - dirty(); - } - - /////////////////////////////////////////////////////////////////////////// - // Node manipulation - - //! Allocate a new element to be added as a child at this node. - //! If an XMLNS is specified via the clarke notation syntax, then the prefix will match the parent element (if any), - //! and any needed xmlns attributes will be added for you. - //! Strings are assumed to remain in scope - you should document()->allocate_string() any that might not. - //! \param name Name of the element, either string view, string, or clarke notation - protected: // These are too easy to accidentally forget to append, prepend, or insert. - optional_ptr> allocate_element(view_type const & name) { - return document()->allocate_node(node_element, name); - } - optional_ptr> allocate_element(std::tuple const & clark_name) { - auto [xmlns, name] = clark_name; - xml_node * child; - if (xmlns != this->xmlns()) { - child = document()->allocate_node(node_element, name); - child->append_attribute(document()->allocate_attribute("xmlns", xmlns)); - } else if (!this->prefix().empty()) { - std::basic_string pname = std::string(this->prefix()) + ':'; - pname += name; - child = document()->allocate_node(node_element, document()->allocate_string(pname)); - } else { - child = document()->allocate_node(node_element, name); - } - return child; - } - optional_ptr> allocate_element(view_type const & name, view_type const & value) { - auto child = allocate_element(name); - child->value(value); - return child; - } - optional_ptr> allocate_element(std::tuple const & clark_name, view_type const & value) { - auto child = allocate_element(clark_name); - child->value(value); - return child; - } - optional_ptr> allocate_element(std::initializer_list const & clark_name) { - auto it = clark_name.begin(); - auto a = *it; - auto b = *++it; - return allocate_element({view_type(a), view_type(b)}); - } - optional_ptr> allocate_element(std::initializer_list const & clark_name, view_type const & value) { - auto child = allocate_element(clark_name); - if (!value.empty()) child->value(value); - return child; - } - public: - - //! Prepends a new child node. - //! The prepended child becomes the first child, and all existing children are moved one position back. - //! \param child Node to prepend. - optional_ptr> prepend_node(xml_node *child) - { - assert(child && !child->parent() && child->type() != node_document); - dirty(); - if (first_node()) - { - child->m_next_sibling = m_first_node; - m_first_node->m_prev_sibling = child; - } - else - { - child->m_next_sibling = 0; - m_last_node = child; - } - m_first_node = child; - child->m_parent = this; - child->m_prev_sibling = 0; - return child; - } - auto prepend_node(optional_ptr> ptr) { - return prepend_node(ptr.get()); - } - auto prepend_element(view_type const & v, view_type const & value = {}) { - auto child = allocate_element(v, value); - return prepend_node(child); - } - auto prepend_element(std::tuple const & il, view_type const & value = {}) { - auto child = allocate_element(il, value); - return prepend_node(child); - } - auto prepend_element(std::initializer_list const & il, view_type const & value = {}) { - auto child = allocate_element(il, value); - return prepend_node(child); - } - - //! Appends a new child node. - //! The appended child becomes the last child. - //! \param child Node to append. - optional_ptr> append_node(xml_node *child) - { - assert(child && !child->parent() && child->type() != node_document); - dirty(); - if (first_node()) - { - child->m_prev_sibling = m_last_node; - m_last_node->m_next_sibling = child; - } - else - { - child->m_prev_sibling = nullptr; - m_first_node = child; - } - m_last_node = child; - child->m_parent = this; - child->m_next_sibling = nullptr; - return child; - } - optional_ptr> append_node(optional_ptr> ptr) { - return append_node(ptr.get()); - } - auto append_element(view_type const & v, view_type const & value = {}) { - auto child = allocate_element(v, value); - return append_node(child); - } - auto append_element(std::tuple const & il, view_type const & value = {}) { - auto child = allocate_element(il, value); - return append_node(child); - } - auto append_element(std::initializer_list const & il, view_type const & value = {}) { - auto child = allocate_element(il, value); - return append_node(child); - } - - //! Inserts a new child node at specified place inside the node. - //! All children after and including the specified node are moved one position back. - //! \param where Place where to insert the child, or 0 to insert at the back. - //! \param child Node to insert. - optional_ptr> insert_node(xml_node *where, xml_node *child) - { - assert(!where || where->parent() == this); - assert(child && !child->parent() && child->type() != node_document); - dirty(); - if (where == m_first_node) - prepend_node(child); - else if (!where) - append_node(child); - else - { - child->m_prev_sibling = where->m_prev_sibling; - child->m_next_sibling = where; - where->m_prev_sibling->m_next_sibling = child; - where->m_prev_sibling = child; - child->m_parent = this; - } - return child; - } - auto insert_node(optional_ptr> where, optional_ptr> ptr) { - return insert_node(where.ptr(), ptr.ptr()); - } - auto insert_element(optional_ptr> where, view_type const & v, view_type const & value = {}) { - auto child = allocate_element(v, value); - return insert_node(where, child); - } - auto insert_element(optional_ptr> where, std::tuple const & il, view_type const & value = {}) { - auto child = allocate_element(il, value); - return insert_node(where, child); - } - auto insert_element(optional_ptr> where, std::initializer_list const & il, view_type const & value = {}) { - auto child = allocate_element(il, value); - return insert_node(where, child); - } - - //! Removes first child node. - //! If node has no children, behaviour is undefined. - //! Use first_node() to test if node has children. - void remove_first_node() - { - assert(first_node()); - dirty(); - xml_node *child = m_first_node; - m_first_node = child->m_next_sibling; - if (child->m_next_sibling) - child->m_next_sibling->m_prev_sibling = nullptr; - else - m_last_node = nullptr; - child->m_parent = nullptr; - } - - //! Removes last child of the node. - //! If node has no children, behaviour is undefined. - //! Use first_node() to test if node has children. - void remove_last_node() - { - assert(first_node()); - dirty(); - xml_node *child = m_last_node; - if (child->m_prev_sibling) - { - m_last_node = child->m_prev_sibling; - child->m_prev_sibling->m_next_sibling = nullptr; - } - else - m_first_node = nullptr; - child->m_parent = nullptr; - } - - //! Removes specified child from the node - // \param where Pointer to child to be removed. - void remove_node(optional_ptr> where) - { - assert(where->parent() == this); - assert(first_node()); - dirty(); - if (where == m_first_node) - remove_first_node(); - else if (where == m_last_node) - remove_last_node(); - else - { - where->m_prev_sibling->m_next_sibling = where->m_next_sibling; - where->m_next_sibling->m_prev_sibling = where->m_prev_sibling; - where->m_parent = nullptr; - } - } - - //! Removes all child nodes (but not attributes). - void remove_all_nodes() - { - if (!m_first_node) return; - dirty(); - for (xml_node *node = m_first_node; node; node = node->m_next_sibling) { - node->m_parent = nullptr; - } - m_first_node = nullptr; - m_last_node = nullptr; - } - - //! Prepends a new attribute to the node. - //! \param attribute Attribute to prepend. - void prepend_attribute(xml_attribute *attribute) - { - assert(attribute && !attribute->parent()); - dirty_parent(); - if (first_attribute()) - { - attribute->m_next_attribute = m_first_attribute; - m_first_attribute->m_prev_attribute = attribute; - } - else - { - attribute->m_next_attribute = nullptr; - m_last_attribute = attribute; - } - m_first_attribute = attribute; - attribute->m_parent = this; - attribute->m_prev_attribute = nullptr; - } - - //! Appends a new attribute to the node. - //! \param attribute Attribute to append. - void append_attribute(xml_attribute *attribute) - { - assert(attribute && !attribute->parent()); - dirty_parent(); - if (first_attribute()) - { - attribute->m_prev_attribute = m_last_attribute; - m_last_attribute->m_next_attribute = attribute; - } - else - { - attribute->m_prev_attribute = nullptr; - m_first_attribute = attribute; - } - m_last_attribute = attribute; - attribute->m_parent = this; - attribute->m_next_attribute = nullptr; - } - - //! Inserts a new attribute at specified place inside the node. - //! All attributes after and including the specified attribute are moved one position back. - //! \param where Place where to insert the attribute, or 0 to insert at the back. - //! \param attribute Attribute to insert. - void insert_attribute(xml_attribute *where, xml_attribute *attribute) - { - assert(!where || where->parent() == this); - assert(attribute && !attribute->parent()); - dirty_parent(); - if (where == m_first_attribute) - prepend_attribute(attribute); - else if (!where) - append_attribute(attribute); - else - { - attribute->m_prev_attribute = where->m_prev_attribute; - attribute->m_next_attribute = where; - where->m_prev_attribute->m_next_attribute = attribute; - where->m_prev_attribute = attribute; - attribute->m_parent = this; - } - } - - //! Removes first attribute of the node. - //! If node has no attributes, behaviour is undefined. - //! Use first_attribute() to test if node has attributes. - void remove_first_attribute() - { - assert(first_attribute()); - dirty_parent(); - xml_attribute *attribute = m_first_attribute; - if (attribute->m_next_attribute) - { - attribute->m_next_attribute->m_prev_attribute = 0; - } - else - m_last_attribute = nullptr; - attribute->m_parent = nullptr; - m_first_attribute = attribute->m_next_attribute; - } - - //! Removes last attribute of the node. - //! If node has no attributes, behaviour is undefined. - //! Use first_attribute() to test if node has attributes. - void remove_last_attribute() - { - assert(first_attribute()); - dirty_parent(); - xml_attribute *attribute = m_last_attribute; - if (attribute->m_prev_attribute) - { - attribute->m_prev_attribute->m_next_attribute = 0; - m_last_attribute = attribute->m_prev_attribute; - } - else - m_first_attribute = nullptr; - attribute->m_parent = nullptr; - } - - //! Removes specified attribute from node. - //! \param where Pointer to attribute to be removed. - void remove_attribute(optional_ptr> where) - { - assert(first_attribute() && where->parent() == this); - dirty_parent(); - if (where == m_first_attribute) - remove_first_attribute(); - else if (where == m_last_attribute) - remove_last_attribute(); - else - { - where->m_prev_attribute->m_next_attribute = where->m_next_attribute; - where->m_next_attribute->m_prev_attribute = where->m_prev_attribute; - where->m_parent = nullptr; - } - } - - //! Removes all attributes of node. - void remove_all_attributes() - { - if (!m_first_attribute) return; - dirty_parent(); - for (xml_attribute *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) { - attribute->m_parent = nullptr; - } - m_first_attribute = nullptr; - } - - void validate() const - { - this->xmlns(); - for (auto child = this->first_node(); - child; - child = child->next_sibling()) { - child->validate(); - } - for (auto attribute = first_attribute(); - attribute; - attribute = attribute->m_next_attribute) { - attribute->xmlns(); - for (auto otherattr = first_attribute(); - otherattr != attribute; - otherattr = otherattr->m_next_attribute) { - if (attribute->name() == otherattr->name()) { - throw duplicate_attribute("Attribute doubled"); - } - if ((attribute->local_name() == otherattr->local_name()) - && (attribute->xmlns() == otherattr->xmlns())) - throw duplicate_attribute("Attribute XMLNS doubled"); - } - } - } - - private: - - /////////////////////////////////////////////////////////////////////////// - // Restrictions - - // No copying - xml_node(const xml_node &) = delete; - void operator =(const xml_node &) = delete; - - /////////////////////////////////////////////////////////////////////////// - // Data members - - // Note that some of the pointers below have UNDEFINED values if certain other pointers are 0. - // This is required for maximum performance, as it allows the parser to omit initialization of - // unneded/redundant values. - // - // The rules are as follows: - // 1. first_node and first_attribute contain valid pointers, or 0 if node has no children/attributes respectively - // 2. last_node and last_attribute are valid only if node has at least one child/attribute respectively, otherwise they contain garbage - // 3. prev_sibling and next_sibling are valid only if node has a parent, otherwise they contain garbage - - view_type m_prefix; - mutable std::optional m_xmlns; // Cache - node_type m_type; // Type of node; always valid - xml_node *m_first_node = nullptr; // Pointer to first child node, or 0 if none; always valid - xml_node *m_last_node = nullptr; // Pointer to last child node, or 0 if none; this value is only valid if m_first_node is non-zero - xml_attribute *m_first_attribute = nullptr; // Pointer to first attribute of node, or 0 if none; always valid - xml_attribute *m_last_attribute = nullptr; // Pointer to last attribute of node, or 0 if none; this value is only valid if m_first_attribute is non-zero - xml_node *m_prev_sibling = nullptr; // Pointer to previous sibling of node, or 0 if none; this value is only valid if m_parent is non-zero - xml_node *m_next_sibling = nullptr; // Pointer to next sibling of node, or 0 if none; this value is only valid if m_parent is non-zero - view_type m_contents; // Pointer to original contents in buffer. - bool m_clean = false; // Unchanged since parsing (ie, contents are good). - mutable std::optional m_value; - }; - - /////////////////////////////////////////////////////////////////////////// - // XML document - - //! This class represents root of the DOM hierarchy. - //! It is also an xml_node and a memory_pool through public inheritance. - //! Use parse() function to build a DOM tree from a zero-terminated XML text string. - //! parse() function allocates memory for nodes and attributes by using functions of xml_document, - //! which are inherited from memory_pool. - //! To access root node of the document, use the document itself, as if it was an xml_node. - //! \param Ch Character type to use. - template - class xml_document: public xml_node, public memory_pool - { - public: - using view_type = std::basic_string_view; - using ptr = optional_ptr>; - - //! Constructs empty XML document - xml_document() - : xml_node(node_document) - { - } - - //! Parses zero-terminated XML string according to given flags. - //! Passed string will be modified by the parser, unless rapidxml::parse_non_destructive flag is used. - //! The string must persist for the lifetime of the document. - //! In case of error, rapidxml::parse_error exception will be thrown. - //!

- //! If you want to parse contents of a file, you must first load the file into the memory, and pass pointer to its beginning. - //! Make sure that data is zero-terminated. - //!

- //! Document can be parsed into multiple times. - //! Each new call to parse removes previous nodes and attributes (if any), but does not clear memory pool. - //! \param text XML data to parse; pointer is non-const to denote fact that this data may be modified by the parser. - template - auto parse(const Ch * text, xml_document * parent = nullptr) { - return this->parse_low(text, parent); - } - - template - auto parse(std::basic_string const & str, xml_document * parent = nullptr) { - return this->parse_low(str.c_str(), parent); - } - - template - requires std::is_same_v - auto parse(C const & container, xml_document * parent = nullptr) { - return this->parse_low(buffer_ptr(container), parent); - } - - template - T parse_low(T text, xml_document * parent) { - this->m_parse_flags = Flags; - - // Remove current contents - this->remove_all_nodes(); - this->remove_all_attributes(); - this->m_parent = parent ? parent->first_node().get() : nullptr; - - // Parse BOM, if any - parse_bom(text); - - // Parse children - while (true) - { - // Skip whitespace before node - skip(text); - if (*text == 0) - break; - - // Parse and append new child - if (*text == Ch('<')) - { - ++text; // Skip '<' - if (xml_node *node = parse_node(text)) { - this->append_node(node); - if (Flags & (parse_open_only|parse_parse_one) && node->type() == node_element) { - break; - } - } - } - else - RAPIDXML_PARSE_ERROR("expected <", text); - } - if (!this->first_node()) RAPIDXML_PARSE_ERROR("no root element", text); - return text; - } - - //! Clears the document by deleting all nodes and clearing the memory pool. - //! All nodes owned by document pool are destroyed. - void clear() - { - this->remove_all_nodes(); - this->remove_all_attributes(); - memory_pool::clear(); - } - - template - view_type decode_data_value_low(view_type const & v) { - buffer_ptr first{v}; - if (Flags & parse_normalize_whitespace) { - skip(first); - } else { - skip(first); - } - if (!*first) return v; - auto buf = this->allocate_string(v); - auto * start = buf.data(); - buffer_ptr tmp{buf}; - auto end = (Flags & parse_normalize_whitespace) ? - skip_and_expand_character_refs(tmp) : - skip_and_expand_character_refs(tmp); - // Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after > - if (Flags & parse_trim_whitespace) - { - if (Flags & parse_normalize_whitespace) - { - // Whitespace is already condensed to single space characters by skipping function, so just trim 1 char off the end - if (*(end - 1) == Ch(' ')) - --end; - } - else - { - // Backup until non-whitespace character is found - while (whitespace_pred::test(*(end - 1))) - --end; - } - } - - return {start, end}; - } - - template - view_type decode_attr_value_low(view_type const & v) { - buffer_ptr first{v}; - skip,0>(first); - if (!*first || *first == Q) return v; - auto buf = this->allocate_string(v); - const Ch * start = buf.data(); - buffer_ptr tmp{buf}; - const Ch * end = skip_and_expand_character_refs,attribute_value_pure_pred,0>(tmp); - return {start, end}; - } - - view_type decode_attr_value(const xml_attribute * attr) { - if (attr->quote() == Ch('"')) { - return decode_attr_value_low<'"'>(attr->value_raw()); - } else if (attr->quote() == Ch('\'')){ - return decode_attr_value_low<'\''>(attr->value_raw()); - } else { - return attr->value_raw(); - } - } - - view_type decode_data_value(const xml_node * node) { - if (node->value_raw().empty()) return node->value_raw(); - if (m_parse_flags & parse_normalize_whitespace) { - if (m_parse_flags & parse_trim_whitespace) { - const int Flags = parse_normalize_whitespace | parse_trim_whitespace; - return decode_data_value_low(node->value_raw()); - } else { - const int Flags = parse_normalize_whitespace; - return decode_data_value_low(node->value_raw()); - } - } else { - if (m_parse_flags & parse_trim_whitespace) { - const int Flags = parse_trim_whitespace; - return decode_data_value_low(node->value_raw()); - } else { - const int Flags = 0; - return decode_data_value_low(node->value_raw()); - } - } - } - - void validate() const - { - for (auto child = this->first_node(); - child; - child = child->next_sibling()) { - child->validate(); - } - } - -#ifndef RAPIDXML_TESTING - private: -#endif - - /////////////////////////////////////////////////////////////////////// - // Internal character utility functions - - // Detect whitespace character - struct whitespace_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_whitespace[static_cast(ch)]; - } - }; - - // Detect node name character - struct node_name_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_node_name[static_cast(ch)]; - } - }; - - // Detect element name character - struct element_name_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_element_name[static_cast(ch)]; - } - }; - - // Detect attribute name character - struct attribute_name_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_attribute_name[static_cast(ch)]; - } - }; - - // Detect text character (PCDATA) - struct text_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_text[static_cast(ch)]; - } - }; - - // Detect text character (PCDATA) that does not require processing - struct text_pure_no_ws_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_text_pure_no_ws[static_cast(ch)]; - } - }; - - // Detect text character (PCDATA) that does not require processing - struct text_pure_with_ws_pred - { - static unsigned char test(Ch ch) - { - return internal::lookup_tables::lookup_text_pure_with_ws[static_cast(ch)]; - } - }; - - // Detect attribute value character - template - struct attribute_value_pred - { - static unsigned char test(Ch ch) - { - if (Quote == Ch('\'')) - return internal::lookup_tables::lookup_attribute_data_1[static_cast(ch)]; - if (Quote == Ch('\"')) - return internal::lookup_tables::lookup_attribute_data_2[static_cast(ch)]; - return 0; // Should never be executed, to avoid warnings on Comeau - } - }; - - // Detect attribute value character - template - struct attribute_value_pure_pred - { - static unsigned char test(Ch ch) - { - if (Quote == Ch('\'')) - return internal::lookup_tables::lookup_attribute_data_1_pure[static_cast(ch)]; - if (Quote == Ch('\"')) - return internal::lookup_tables::lookup_attribute_data_2_pure[static_cast(ch)]; - return 0; // Should never be executed, to avoid warnings on Comeau - } - }; - - // Insert coded character, using UTF8 or 8-bit ASCII - template - static void insert_coded_character(Ch *&text, unsigned long code) - { - if (Flags & parse_no_utf8) - { - // Insert 8-bit ASCII character - // Todo: possibly verify that code is less than 256 and use replacement char otherwise? - text[0] = static_cast(code); - text += 1; - } - else - { - // Insert UTF8 sequence - if (code < 0x80) // 1 byte sequence - { - text[0] = static_cast(code); - text += 1; - } - else if (code < 0x800) // 2 byte sequence - { - text[1] = static_cast((code | 0x80) & 0xBF); code >>= 6; - text[0] = static_cast(code | 0xC0); - text += 2; - } - else if (code < 0x10000) // 3 byte sequence - { - text[2] = static_cast((code | 0x80) & 0xBF); code >>= 6; - text[1] = static_cast((code | 0x80) & 0xBF); code >>= 6; - text[0] = static_cast(code | 0xE0); - text += 3; - } - else if (code < 0x110000) // 4 byte sequence - { - text[3] = static_cast((code | 0x80) & 0xBF); code >>= 6; - text[2] = static_cast((code | 0x80) & 0xBF); code >>= 6; - text[1] = static_cast((code | 0x80) & 0xBF); code >>= 6; - text[0] = static_cast(code | 0xF0); - text += 4; - } - else // Invalid, only codes up to 0x10FFFF are allowed in Unicode - { - RAPIDXML_PARSE_ERROR("invalid numeric character entity", text); - } - } - } - - // Skip characters until predicate evaluates to true - template - static void skip(Chp & b) - { - while (StopPred::test(*b)) - ++b; - } - - // Skip characters until predicate evaluates to true while doing the following: - // - replacing XML character entity references with proper characters (' & " < > &#...;) - // - condensing whitespace sequences to single space character - template - static const Ch *skip_and_expand_character_refs(Chp text) - { - // If entity translation, whitespace condense and whitespace trimming is disabled, use plain skip - if (Flags & parse_no_entity_translation && - !(Flags & parse_normalize_whitespace) && - !(Flags & parse_trim_whitespace)) - { - skip(text); - return &*text; - } - - // Use simple skip until first modification is detected - skip(text); - - // Use translation skip - Chp src = text; - Ch * dest = const_cast(&*src); - while (StopPred::test(*src)) - { - // If entity translation is enabled - if (!(Flags & parse_no_entity_translation)) - { - // Test if replacement is needed - if (src[0] == Ch('&')) - { - switch (src[1]) - { - - // & ' - case Ch('a'): - if (src[2] == Ch('m') && src[3] == Ch('p') && src[4] == Ch(';')) - { - *dest = Ch('&'); - ++dest; - src += 5; - continue; - } - if (src[2] == Ch('p') && src[3] == Ch('o') && src[4] == Ch('s') && src[5] == Ch(';')) - { - *dest = Ch('\''); - ++dest; - src += 6; - continue; - } - break; - - // " - case Ch('q'): - if (src[2] == Ch('u') && src[3] == Ch('o') && src[4] == Ch('t') && src[5] == Ch(';')) - { - *dest = Ch('"'); - ++dest; - src += 6; - continue; - } - break; - - // > - case Ch('g'): - if (src[2] == Ch('t') && src[3] == Ch(';')) - { - *dest = Ch('>'); - ++dest; - src += 4; - continue; - } - break; - - // < - case Ch('l'): - if (src[2] == Ch('t') && src[3] == Ch(';')) - { - *dest = Ch('<'); - ++dest; - src += 4; - continue; - } - break; - - // &#...; - assumes ASCII - case Ch('#'): - if (src[2] == Ch('x')) - { - unsigned long code = 0; - src += 3; // Skip &#x - while (true) - { - unsigned char digit = internal::lookup_tables::lookup_digits[static_cast(*src)]; - if (digit == 0xFF) - break; - code = code * 16 + digit; - ++src; - } - insert_coded_character(dest, code); // Put character in output - } - else - { - unsigned long code = 0; - src += 2; // Skip &# - while (true) - { - unsigned char digit = internal::lookup_tables::lookup_digits[static_cast(*src)]; - if (digit == 0xFF) - break; - code = code * 10 + digit; - ++src; - } - insert_coded_character(dest, code); // Put character in output - } - if (*src == Ch(';')) - ++src; - else - RAPIDXML_PARSE_ERROR("expected ;", src); - continue; - - // Something else - default: - // Ignore, just copy '&' verbatim - break; - - } - } - } - - // If whitespace condensing is enabled - if (Flags & parse_normalize_whitespace && whitespace_pred::test(*src)) { - *dest = Ch(' '); ++dest; // Put single space in dest - ++src; // Skip first whitespace char - // Skip remaining whitespace chars - while (whitespace_pred::test(*src)) - ++src; - continue; - } - - // No replacement, only copy character - *dest++ = *src++; - - } - - // Return new end - return dest; - } - - /////////////////////////////////////////////////////////////////////// - // Internal parsing functions - - // Parse BOM, if any - template - void parse_bom(Chp &texta) - { - Chp text = texta; - // UTF-8? - if (static_cast(*text++) == 0xEF && - static_cast(*text++) == 0xBB && - static_cast(*text++) == 0xBF) - { - texta = text; // Skup utf-8 bom - } - } - - // Parse XML declaration ( - xml_node *parse_xml_declaration(Chp &text) - { - // If parsing of declaration is disabled - if (!(Flags & parse_declaration_node)) - { - // Skip until end of declaration - while (text[0] != Ch('?') || text[1] != Ch('>')) - { - if (!text[0]) RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - text += 2; // Skip '?>' - return 0; - } - - // Create declaration - xml_node *declaration = this->allocate_node(node_declaration); - - // Skip whitespace before attributes or ?> - skip(text); - - // Parse declaration attributes - parse_node_attributes(text, declaration); - - // Skip ?> - if (text[0] != Ch('?') || text[1] != Ch('>')) RAPIDXML_PARSE_ERROR("expected ?>", text); - text += 2; - - return declaration; - } - - // Parse XML comment (' - return 0; // Do not produce comment node - } - - // Remember value start - Chp value = text; - - // Skip until end of comment - while (text[0] != Ch('-') || text[1] != Ch('-') || text[2] != Ch('>')) - { - if (!text[0]) RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - - // Create comment node - xml_node *comment = this->allocate_node(node_comment); - comment->value({value, text}); - - text += 3; // Skip '-->' - return comment; - } - - // Parse DOCTYPE - template - xml_node *parse_doctype(Chp &text) - { - // Remember value start - Chp value = text; - - // Skip to > - while (*text != Ch('>')) - { - // Determine character type - switch (*text) - { - - // If '[' encountered, scan for matching ending ']' using naive algorithm with depth - // This works for all W3C test files except for 2 most wicked - case Ch('['): - { - ++text; // Skip '[' - int depth = 1; - while (depth > 0) - { - switch (*text) - { - case Ch('['): ++depth; break; - case Ch(']'): --depth; break; - case 0: RAPIDXML_PARSE_ERROR("unexpected end of data", text); - default: break; - } - ++text; - } - break; - } - - // Error on end of text - case Ch('\0'): - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - - // Other character, skip it - default: - ++text; - - } - } - - // If DOCTYPE nodes enabled - if (Flags & parse_doctype_node) - { - // Create a new doctype node - xml_node *doctype = this->allocate_node(node_doctype); - doctype->value({value, text}); - - text += 1; // skip '>' - return doctype; - } - else - { - text += 1; // skip '>' - return 0; - } - - } - - // Parse PI - template - xml_node *parse_pi(Chp &text) - { - // If creation of PI nodes is enabled - if (Flags & parse_pi_nodes) - { - // Create pi node - xml_node *pi = this->allocate_node(node_pi); - - // Extract PI target name - Chp name = text; - skip(text); - if (text == name) RAPIDXML_PARSE_ERROR("expected PI target", text); - pi->name({name, text}); - - // Skip whitespace between pi target and pi - skip(text); - - // Remember start of pi - Chp value = text; - - // Skip to '?>' - while (text[0] != Ch('?') || text[1] != Ch('>')) - { - if (*text == Ch('\0')) - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - - // Set pi value (verbatim, no entity expansion or whitespace normalization) - pi->value({value, text}); - - text += 2; // Skip '?>' - return pi; - } - else - { - // Skip to '?>' - while (text[0] != Ch('?') || text[1] != Ch('>')) - { - if (*text == Ch('\0')) - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - text += 2; // Skip '?>' - return 0; - } - } - - // Parse and append data - // Return character that ends data. - // This is necessary because this character might have been overwritten by a terminating 0 - template - Ch parse_and_append_data(xml_node *node, Chp &text, Chp contents_start) - { - // Backup to contents start if whitespace trimming is disabled - if (!(Flags & parse_trim_whitespace)) - text = contents_start; - - // Skip until end of data. We should check if the contents will need decoding. - Chp value = text; - bool encoded = false; - skip(text); - if (text_pred::test(*text)) { - encoded = true; - skip(text); - } - - // If characters are still left between end and value (this test is only necessary if normalization is enabled) - // Create new data node - if (!(Flags & parse_no_data_nodes)) - { - xml_node *data = this->allocate_node(node_data); - data->value_raw({value, text}); - if (!encoded) data->value(data->value_raw()); - node->append_node(data); - } - - // Add data to parent node if no data exists yet - if (!(Flags & parse_no_element_values)) { - if (node->value_raw().empty()) { - node->value_raw({value, text}); - if (!encoded) node->value(node->value_raw()); - } - } - - // Return character that ends data - return *text; - } - - // Parse CDATA - template - xml_node *parse_cdata(Chp &text) - { - // If CDATA is disabled - if (Flags & parse_no_data_nodes) - { - // Skip until end of cdata - while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>')) - { - if (!text[0]) - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - text += 3; // Skip ]]> - return 0; // Do not produce CDATA node - } - - // Skip until end of cdata - Chp value = text; - while (text[0] != Ch(']') || text[1] != Ch(']') || text[2] != Ch('>')) - { - if (!text[0]) - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - - // Create new cdata node - xml_node *cdata = this->allocate_node(node_cdata); - cdata->value({value, text}); - - text += 3; // Skip ]]> - return cdata; - } - - // Parse element node - template - xml_node *parse_element(Chp &text) - { - // Create element node - xml_node *element = this->allocate_node(node_element); - - // Extract element name - Chp prefix = text; - view_type qname; - skip(text); - if (text == prefix) - RAPIDXML_PARSE_ERROR("expected element name or prefix", text); - if (*text == Ch(':')) { - element->prefix({prefix, text}); - ++text; - Chp name = text; - skip(text); - if (text == name) - RAPIDXML_PARSE_ERROR("expected element local name", text); - element->name({name, text}); - } else { - element->name({prefix, text}); - } - qname = {prefix, text}; - - // Skip whitespace between element name and attributes or > - skip(text); - - // Parse attributes, if any - parse_node_attributes(text, element); - // Once we have all the attributes, we should be able to fully validate: - if (Flags & parse_validate_xmlns) this->validate(); - - // Determine ending type - if (*text == Ch('>')) - { - Chp contents = ++text; - Chp contents_end = contents; - if (!(Flags & parse_open_only)) - contents_end = parse_node_contents(text, element, qname); - if (contents != contents_end) element->contents({contents, contents_end}); - } - else if (*text == Ch('/')) - { - ++text; - if (*text != Ch('>')) - RAPIDXML_PARSE_ERROR("expected >", text); - ++text; - if (Flags & parse_open_only) - RAPIDXML_PARSE_ERROR("open_only, but closed", text); - } - else - RAPIDXML_PARSE_ERROR("expected >", text); - - // Return parsed element - return element; - } - - // Determine node type, and parse it - template - xml_node *parse_node(Chp &text) - { - // Parse proper node type - switch (text[0]) - { - - // <... - default: - // Parse and append element node - return parse_element(text); - - // (text); - } - else - { - // Parse PI - return parse_pi(text); - } - - // (text); - } - break; - - // (text); - } - break; - - // (text); - } - - default: - break; - } // switch - - // Attempt to skip other, unrecognized node types starting with ')) - { - if (*text == 0) - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - ++text; - } - ++text; // Skip '>' - return 0; // No node recognized - - } - } - - // Parse contents of the node - children, data etc. - // Return end pointer. - template - Chp parse_node_contents(Chp &text, xml_node *node, view_type const & qname) - { - Chp retval; - // For all children and text - while (true) - { - // Skip whitespace between > and node contents - Chp contents_start = text; // Store start of node contents before whitespace is skipped - skip(text); - Ch next_char = *text; - - // After data nodes, instead of continuing the loop, control jumps here. - // This is because zero termination inside parse_and_append_data() function - // would wreak havoc with the above code. - // Also, skipping whitespace after data nodes is unnecessary. - after_data_node: - - // Determine what comes next: node closing, child node, data node, or 0? - switch (next_char) - { - - // Node closing or child node - case Ch('<'): - if (text[1] == Ch('/')) - { - // Node closing - retval = text; - text += 2; // Skip '(text); - if (qname != view_type{closing_name, text}) - RAPIDXML_PARSE_ERROR("invalid closing tag name", text); - } - else - { - // No validation, just skip name - skip(text); - } - // Skip remaining whitespace after node name - skip(text); - if (*text != Ch('>')) - RAPIDXML_PARSE_ERROR("expected >", text); - ++text; // Skip '>' - if (Flags & parse_open_only) - RAPIDXML_PARSE_ERROR("Unclosed element actually closed.", text); - return retval; // Node closed, finished parsing contents - } - else - { - // Child node - ++text; // Skip '<' - if (xml_node *child = parse_node(text)) - node->append_node(child); - } - break; - - // End of data - error unless we expected this. - case Ch('\0'): - if (Flags & parse_open_only) { - return Chp(); - } else { - RAPIDXML_PARSE_ERROR("unexpected end of data", text); - } - - // Data node - default: - next_char = parse_and_append_data(node, text, contents_start); - goto after_data_node; // Bypass regular processing after data nodes - - } - } - } - - // Parse XML attributes of the node - template - void parse_node_attributes(Chp &text, xml_node *node) - { - // For all attributes - while (attribute_name_pred::test(*text)) - { - // Extract attribute name - Chp name = text; - ++text; // Skip first character of attribute name - skip(text); - if (text == name) - RAPIDXML_PARSE_ERROR("expected attribute name", name); - - // Create new attribute - xml_attribute *attribute = this->allocate_attribute(view_type{name, text}); - node->append_attribute(attribute); - - // Skip whitespace after attribute name - skip(text); - - // Skip = - if (*text != Ch('=')) - RAPIDXML_PARSE_ERROR("expected =", text); - ++text; - - // Skip whitespace after = - skip(text); - - // Skip quote and remember if it was ' or " - Ch quote = *text; - if (quote != Ch('\'') && quote != Ch('"')) - RAPIDXML_PARSE_ERROR("expected ' or \"", text); - attribute->quote(quote); - ++text; - - // Extract attribute value and expand char refs in it - Chp value = text; - Chp end; - const int AttFlags = Flags & ~parse_normalize_whitespace; // No whitespace normalization in attributes - if (quote == Ch('\'')) - skip, AttFlags>(text); - else - skip, AttFlags>(text); - end = text; - - // Set attribute value - attribute->value_raw({value, end}); - - // Make sure that end quote is present - if (*text != quote) - RAPIDXML_PARSE_ERROR("expected ' or \"", text); - ++text; // Skip quote - - // Skip whitespace after attribute value - skip(text); - } - } - private: - int m_parse_flags = 0; - }; - - -} - -// Also include this now. -#include - -// Undefine internal macros -#undef RAPIDXML_PARSE_ERROR - -// On MSVC, restore warnings state -#ifdef _MSC_VER - #pragma warning(pop) -#endif - -#endif +#endif //RAPIDXML_HPP diff --git a/include/rapidxml_print.hpp b/include/rapidxml_print.hpp index 2315eac..ed0d709 100644 --- a/include/rapidxml_print.hpp +++ b/include/rapidxml_print.hpp @@ -1,477 +1,10 @@ -#ifndef RAPIDXML_PRINT_HPP_INCLUDED -#define RAPIDXML_PRINT_HPP_INCLUDED - -// Copyright (C) 2006, 2009 Marcin Kalicinski -// Version 1.13 -// Revision $DateTime: 2009/05/13 01:46:17 $ -//! \file rapidxml_print.hpp This file contains rapidxml printer implementation - -#include "rapidxml.hpp" - -// Only include streams if not disabled -#ifndef RAPIDXML_NO_STREAMS - #include - #include -#endif - -namespace rapidxml -{ - - /////////////////////////////////////////////////////////////////////// - // Printing flags - - const int print_no_indenting = 0x1; //!< Printer flag instructing the printer to suppress indenting of XML. See print() function. - - /////////////////////////////////////////////////////////////////////// - // Internal - - //! \cond internal - namespace internal - { - - /////////////////////////////////////////////////////////////////////////// - // Internal character operations - - // Copy characters from given range to given output iterator - template - inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out) - { - while (begin != end) - *out++ = *begin++; - return out; - } - - template - inline OutIt copy_chars(std::basic_string_view const & sv, OutIt out) { - return copy_chars(sv.data(), sv.data() + sv.size(), out); - } - - // Copy characters from given range to given output iterator and expand - // characters into references (< > ' " &) - template - inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out) - { - while (begin != end) - { - if (*begin == noexpand) - { - *out++ = *begin; // No expansion, copy character - } - else - { - switch (*begin) - { - case Ch('<'): - *out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';'); - break; - case Ch('>'): - *out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';'); - break; - case Ch('\''): - *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';'); - break; - case Ch('"'): - *out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';'); - break; - case Ch('&'): - *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';'); - break; - default: - *out++ = *begin; // No expansion, copy character - } - } - ++begin; // Step to next character - } - return out; - } - - - template - inline OutIt copy_and_expand_chars(std::basic_string_view const & sv, Ch noexpand, OutIt out) { - return copy_and_expand_chars(sv.data(), sv.data() + sv.size(), noexpand, out); - } - // Fill given output iterator with repetitions of the same character - template - inline OutIt fill_chars(OutIt out, int n, Ch ch) - { - for (int i = 0; i < n; ++i) - *out++ = ch; - return out; - } - - // Find character - template - inline bool find_char(const Ch *begin, const Ch *end) - { - while (begin != end) - if (*begin++ == ch) - return true; - return false; - } - - /////////////////////////////////////////////////////////////////////////// - // Internal printing operations - - // Print node - template - inline OutIt print_node(OutIt out, const optional_ptr> node, int flags, int indent); - - // Print children of the node - template - inline OutIt print_children(OutIt out, const optional_ptr> node, int flags, int indent) - { - for (auto child = node->first_node(); child; child = child->next_sibling()) - out = print_node(out, child, flags, indent); - return out; - } - - // Print attributes of the node - template - inline OutIt print_attributes(OutIt out, const optional_ptr> node, int flags) - { - for (auto attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute()) - { - if (!(attribute->name().empty()) || attribute->value_raw().empty()) - { - // Print attribute name - *out = Ch(' '), ++out; - out = copy_chars(attribute->name(), out); - *out = Ch('='), ++out; - if (attribute->quote() && !attribute->value_decoded()) { - // Shortcut here; just dump out the raw value. - *out++ = attribute->quote(); - out = copy_chars(attribute->value_raw(), out); - **out++ = attribute->quote(); - } else { - // Print attribute value using appropriate quote type - if (attribute->value().find('"') != std::basic_string_view::npos) { - *out = Ch('\''), ++out; - out = copy_and_expand_chars(attribute->value(), Ch('"'), out); - *out = Ch('\''), ++out; - } else { - *out = Ch('"'), ++out; - out = copy_and_expand_chars(attribute->value(), Ch('\''), out); - *out = Ch('"'), ++out; - } - } - } - } - return out; - } - - // Print data node - template - inline OutIt print_data_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_data); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - if (!node->value_decoded()) { - out = copy_chars(node->value_raw(), out); - } else { - out = copy_and_expand_chars(node->value(), Ch(0), out); - } - return out; - } - - // Print data node - template - inline OutIt print_cdata_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_cdata); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - *out = Ch('<'); ++out; - *out = Ch('!'); ++out; - *out = Ch('['); ++out; - *out = Ch('C'); ++out; - *out = Ch('D'); ++out; - *out = Ch('A'); ++out; - *out = Ch('T'); ++out; - *out = Ch('A'); ++out; - *out = Ch('['); ++out; - out = copy_chars(node->value(), out); - *out = Ch(']'); ++out; - *out = Ch(']'); ++out; - *out = Ch('>'); ++out; - return out; - } - - // Print element node - template - inline OutIt print_element_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_element); - - // Print element name and attributes, if any - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - *out = Ch('<'), ++out; - if (!node->prefix().empty()) { - out = copy_chars(node->prefix(), out); - *out = Ch(':'); ++out; - } - out = copy_chars(node->name(), out); - out = print_attributes(out, node, flags); - - // If node is childless - if (node->value().empty() && !node->first_node()) - { - // Print childless node tag ending - *out = Ch('/'), ++out; - *out = Ch('>'), ++out; - } - else - { - // Print normal node tag ending - *out = Ch('>'), ++out; - - // If the node is clean, just output the contents and move on. - // Can only do this if we're not indenting, otherwise pretty-print won't work. - if (node->clean() && (flags & print_no_indenting)) { - out = copy_chars(node->contents(), out); - } else { - - // Test if node contains a single data node only (and no other nodes) - auto child = node->first_node(); - if (!child) { - // If node has no children, only print its value without indenting - if (!node->value_decoded()) { - out = copy_chars(node->value_raw(), out); - } else { - out = copy_and_expand_chars(node->value(), Ch(0), out); - } - } else if (!child->next_sibling() && child->type() == node_type::node_data) { - // If node has a sole data child, only print its value without indenting - if (!child->value_decoded()) { - out = copy_chars(child->value_raw(), out); - } else { - out = copy_and_expand_chars(child->value(), Ch(0), out); - } - } else { - // Print all children with full indenting - if (!(flags & print_no_indenting)) - *out = Ch('\n'), ++out; - out = print_children(out, node, flags, indent + 1); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - } - } - - // Print node end - *out = Ch('<'), ++out; - *out = Ch('/'), ++out; - if (!node->prefix().empty()) { - out = copy_chars(node->prefix(), out); - *out = Ch(':'); ++out; - } - out = copy_chars(node->name(), out); - *out = Ch('>'), ++out; - } - return out; - } - - // Print declaration node - template - inline OutIt print_declaration_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - // Print declaration start - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - *out = Ch('<'), ++out; - *out = Ch('?'), ++out; - *out = Ch('x'), ++out; - *out = Ch('m'), ++out; - *out = Ch('l'), ++out; - - // Print attributes - out = print_attributes(out, node, flags); - - // Print declaration end - *out = Ch('?'), ++out; - *out = Ch('>'), ++out; - - return out; - } - - // Print comment node - template - inline OutIt print_comment_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_comment); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - *out = Ch('<'), ++out; - *out = Ch('!'), ++out; - *out = Ch('-'), ++out; - *out = Ch('-'), ++out; - out = copy_chars(node->value(), out); - *out = Ch('-'), ++out; - *out = Ch('-'), ++out; - *out = Ch('>'), ++out; - return out; - } - - // Print doctype node - template - inline OutIt print_doctype_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_doctype); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - *out = Ch('<'), ++out; - *out = Ch('!'), ++out; - *out = Ch('D'), ++out; - *out = Ch('O'), ++out; - *out = Ch('C'), ++out; - *out = Ch('T'), ++out; - *out = Ch('Y'), ++out; - *out = Ch('P'), ++out; - *out = Ch('E'), ++out; - *out = Ch(' '), ++out; - out = copy_chars(node->value(), out); - *out = Ch('>'), ++out; - return out; - } - - // Print pi node - template - inline OutIt print_pi_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_pi); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - *out = Ch('<'), ++out; - *out = Ch('?'), ++out; - out = copy_chars(node->name(), out); - *out = Ch(' '), ++out; - out = copy_chars(node->value(), out); - *out = Ch('?'), ++out; - *out = Ch('>'), ++out; - return out; - } - - // Print literal node - template - inline OutIt print_literal_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - assert(node->type() == node_type::node_literal); - if (!(flags & print_no_indenting)) - out = fill_chars(out, indent, Ch('\t')); - out = copy_chars(node->value(), out); - return out; - } - - // Print node - // Print node - template - inline OutIt print_node(OutIt out, const optional_ptr> node, int flags, int indent) - { - // Print proper node type - switch (node->type()) - { - // Document - case node_document: - out = print_children(out, node, flags, indent); - break; - - // Element - case node_element: - out = print_element_node(out, node, flags, indent); - break; - - // Data - case node_data: - out = print_data_node(out, node, flags, indent); - break; - - // CDATA - case node_cdata: - out = print_cdata_node(out, node, flags, indent); - break; - - // Declaration - case node_declaration: - out = print_declaration_node(out, node, flags, indent); - break; - - // Comment - case node_comment: - out = print_comment_node(out, node, flags, indent); - break; - - // Doctype - case node_doctype: - out = print_doctype_node(out, node, flags, indent); - break; - - // Pi - case node_pi: - out = print_pi_node(out, node, flags, indent); - break; - - case node_literal: - out = print_literal_node(out, node, flags, indent); - break; - - // Unknown - default: - assert(0); - break; - } - - // If indenting not disabled, add line break after node - if (!(flags & print_no_indenting)) - *out = Ch('\n'), ++out; - - // Return modified iterator - return out; - } - - } - //! \endcond - - /////////////////////////////////////////////////////////////////////////// - // Printing - - //! Prints XML to given output iterator. - //! \param out Output iterator to print to. - //! \param node Node to be printed. Pass xml_document to print entire document. - //! \param flags Flags controlling how XML is printed. - //! \return Output iterator pointing to position immediately after last character of printed text. - template - inline OutIt print(OutIt out, const xml_node &node, int flags = 0) - { - rapidxml::optional_ptr ptr(const_cast *>(&node)); - return internal::print_node(out, ptr, flags, 0); - } - -#ifndef RAPIDXML_NO_STREAMS - - //! Prints XML to given output stream. - //! \param out Output stream to print to. - //! \param node Node to be printed. Pass xml_document to print entire document. - //! \param flags Flags controlling how XML is printed. - //! \return Output stream. - template - inline std::basic_ostream &print(std::basic_ostream &out, const xml_node &node, int flags = 0) - { - print(std::ostream_iterator(out), node, flags); - return out; - } - - //! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process. - //! \param out Output stream to print to. - //! \param node Node to be printed. - //! \return Output stream. - template - inline std::basic_ostream &operator <<(std::basic_ostream &out, const xml_node &node) - { - return print(out, node); - } - -#endif - -} - -#endif +// +// Created by dwd on 4/19/25. +// + +#ifndef RAPIDXML_PRINT_HPP +#define RAPIDXML_PRINT_HPP + +namespace rapidxml = flxml; + +#endif //RAPIDXML_PRINT_HPP diff --git a/test/src/iterators.cpp b/test/src/iterators.cpp index 141054b..19ef0db 100644 --- a/test/src/iterators.cpp +++ b/test/src/iterators.cpp @@ -6,12 +6,12 @@ #include #include #include -#include "rapidxml.hpp" +#include "flxml.hpp" TEST(Iterators, Nodes) { std::string xml = ""; - rapidxml::xml_document<> doc; - doc.parse(xml); + flxml::xml_document<> doc; + doc.parse(xml); int i = 0; for (auto & child : doc.first_node()->children()) { ++i; @@ -32,8 +32,8 @@ TEST(Iterators, Nodes) { TEST(Iterators, Attributes) { std::string xml = R"()"; - rapidxml::xml_document<> doc; - doc.parse(xml); + flxml::xml_document<> doc; + doc.parse(xml); int i = 0; for (auto & child : doc.first_node()->attributes()) { ++i; @@ -54,8 +54,8 @@ TEST(Iterators, Attributes) { TEST(Predicates, Nodes) { std::string xml = ""; - rapidxml::xml_document<> doc; - doc.parse(xml); + flxml::xml_document<> doc; + doc.parse(xml); auto r = doc.first_node()->children(); for (auto const & child : r | std::ranges::views::filter([](auto const & n) { return n.name() == "two"; })) { EXPECT_EQ(child.name(), "two"); @@ -68,9 +68,9 @@ TEST(Predicates, Nodes) { TEST(Predicates, AllNodes) { std::string xml = ""; - rapidxml::xml_document<> doc; - doc.parse(xml); - auto it = rapidxml::descendant_iterator<>(doc.first_node()); + flxml::xml_document<> doc; + doc.parse(xml); + auto it = flxml::descendant_iterator<>(doc.first_node()); EXPECT_EQ(it->name(), "one"); ++it; EXPECT_EQ(it->name(), "two"); @@ -88,9 +88,9 @@ TEST(Predicates, AllNodes) { TEST(Predicates, AllNodesRev) { std::string xml = ""; - rapidxml::xml_document<> doc; - doc.parse(xml); - auto it = rapidxml::descendant_iterator<>(doc.first_node()); + flxml::xml_document<> doc; + doc.parse(xml); + auto it = flxml::descendant_iterator<>(doc.first_node()); EXPECT_EQ(it->name(), "one"); ++it; EXPECT_EQ(it->name(), "two"); @@ -116,8 +116,8 @@ TEST(Predicates, AllNodesRev) { TEST(Predicates, Attributes) { std::string xml = R"()"; - rapidxml::xml_document<> doc; - doc.parse(xml); + flxml::xml_document<> doc; + doc.parse(xml); auto r = doc.first_node()->attributes(); for (auto const & child : r | std::ranges::views::filter([](auto const & n) { return n.name() == "two"; })) { EXPECT_EQ(child.name(), "two"); diff --git a/test/src/low-level-parse.cpp b/test/src/low-level-parse.cpp index df2fc75..2845029 100644 --- a/test/src/low-level-parse.cpp +++ b/test/src/low-level-parse.cpp @@ -3,10 +3,10 @@ // #include -#include "rapidxml.hpp" +#include "flxml.hpp" TEST(Constants, Empty) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; auto empty = doc.nullstr(); EXPECT_EQ(empty, ""); EXPECT_EQ(empty.size(), 0); @@ -16,7 +16,7 @@ TEST(Predicates, Skip) { std::string test_data{""}; auto start = test_data.c_str(); auto end = ++start; - rapidxml::xml_document<>::skip::element_name_pred,0>(end); + flxml::xml_document<>::skip::element_name_pred,0>(end); EXPECT_EQ(*end, '/'); std::string_view sv({start, end}); EXPECT_EQ(sv, "simple"); @@ -24,9 +24,9 @@ TEST(Predicates, Skip) { TEST(PredicateBuffer, Skip) { std::string test_data{""}; - auto start = rapidxml::buffer_ptr(test_data); + auto start = flxml::buffer_ptr(test_data); auto end = ++start; - rapidxml::xml_document<>::skip::element_name_pred,0>(end); + flxml::xml_document<>::skip::element_name_pred,0>(end); EXPECT_EQ(*end, '/'); std::string_view sv({start, end}); EXPECT_EQ(sv, "simple"); @@ -35,37 +35,37 @@ TEST(PredicateBuffer, Skip) { TEST(Predicates, SkipAndExpand) { std::string test_data{"&hello;<"}; char * start = const_cast(test_data.c_str()); - auto end = rapidxml::xml_document<>::skip_and_expand_character_refs< - rapidxml::xml_document<>::text_pred, - rapidxml::xml_document<>::text_pure_with_ws_pred, - rapidxml::parse_no_entity_translation>(start); + auto end = flxml::xml_document<>::skip_and_expand_character_refs< + flxml::xml_document<>::text_pred, + flxml::xml_document<>::text_pure_with_ws_pred, + flxml::parse_no_entity_translation>(start); EXPECT_EQ(*end, '<'); } TEST(Predicates, SkipAndExpandShort) { std::string test_data{"&hello;"}; char * start = const_cast(test_data.c_str()); - auto end = rapidxml::xml_document<>::skip_and_expand_character_refs< - rapidxml::xml_document<>::text_pred, - rapidxml::xml_document<>::text_pure_with_ws_pred, - rapidxml::parse_no_entity_translation>(start); + auto end = flxml::xml_document<>::skip_and_expand_character_refs< + flxml::xml_document<>::text_pred, + flxml::xml_document<>::text_pure_with_ws_pred, + flxml::parse_no_entity_translation>(start); EXPECT_EQ(*end, '\0'); } TEST(Predicates, SkipAndExpandShorter) { std::string test_data{"&hell"}; char * start = const_cast(test_data.c_str()); - auto end = rapidxml::xml_document<>::skip_and_expand_character_refs< - rapidxml::xml_document<>::text_pred, - rapidxml::xml_document<>::text_pure_with_ws_pred, - rapidxml::parse_no_entity_translation>(start); + auto end = flxml::xml_document<>::skip_and_expand_character_refs< + flxml::xml_document<>::text_pred, + flxml::xml_document<>::text_pure_with_ws_pred, + flxml::parse_no_entity_translation>(start); EXPECT_EQ(*end, '\0'); } TEST(ParseFns, ParseBom) { std::string test_data{"\xEF\xBB\xBF"}; char *start = const_cast(test_data.c_str()); - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; doc.parse_bom<0>(start); EXPECT_EQ(*start, '<'); } @@ -73,7 +73,7 @@ TEST(ParseFns, ParseBom) { TEST(ParseFns, ParseBomShort) { std::string test_data{"\xEF\xBB\xBF"}; char *start = const_cast(test_data.c_str()); - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; doc.parse_bom<0>(start); EXPECT_EQ(*start, '\0'); } @@ -81,7 +81,7 @@ TEST(ParseFns, ParseBomShort) { TEST(ParseFns, ParseBomShorter) { std::string test_data{"\xEF\xBB"}; char *start = const_cast(test_data.c_str()); - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; doc.parse_bom<0>(start); EXPECT_EQ(*start, '\xEF'); } diff --git a/test/src/manipulations.cpp b/test/src/manipulations.cpp index d1b4c4b..455c3b3 100644 --- a/test/src/manipulations.cpp +++ b/test/src/manipulations.cpp @@ -4,20 +4,20 @@ #include -#include "rapidxml.hpp" -#include "rapidxml_print.hpp" +#include "flxml.hpp" +#include "flxml/print.hpp" namespace { - auto print(rapidxml::xml_document<> & doc) { + auto print(flxml::xml_document<> & doc) { std::string output; - rapidxml::print(std::back_inserter(output), *doc.first_node()); + flxml::print(std::back_inserter(output), *doc.first_node()); return output; } } TEST(Create, Node) { - rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish", "cakes"); + flxml::xml_document<> doc; + auto node = doc.allocate_node(flxml::node_element, "fish", "cakes"); doc.append_node(node); EXPECT_EQ( @@ -27,8 +27,8 @@ TEST(Create, Node) { } TEST(Create, NodeEmpty) { - rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish"); + flxml::xml_document<> doc; + auto node = doc.allocate_node(flxml::node_element, "fish"); doc.append_node(node); EXPECT_EQ( @@ -38,8 +38,8 @@ TEST(Create, NodeEmpty) { } TEST(Create, Node2) { - rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish", "cakes"); + flxml::xml_document<> doc; + auto node = doc.allocate_node(flxml::node_element, "fish", "cakes"); doc.append_node(node); EXPECT_EQ( @@ -55,8 +55,8 @@ std::string const & fn() { } TEST(Create, NodeAttr) { - rapidxml::xml_document<> doc; - auto node = doc.allocate_node(rapidxml::node_element, "fish", "cakes"); + flxml::xml_document<> doc; + auto node = doc.allocate_node(flxml::node_element, "fish", "cakes"); auto haddock = doc.allocate_attribute("id", "haddock"); node->append_attribute(haddock); doc.append_node(node); @@ -67,7 +67,7 @@ TEST(Create, NodeAttr) { ); const std::string & s2 = fn(); - const rapidxml::xml_attribute<>::view_type & sv{s2}; + const flxml::xml_attribute<>::view_type & sv{s2}; auto tuna = doc.allocate_attribute("not-id", fn()); // These check that the same buffer is being used throughout, instead of creating temporaries. diff --git a/test/src/parse-simple.cpp b/test/src/parse-simple.cpp index dd2c64b..2b68270 100644 --- a/test/src/parse-simple.cpp +++ b/test/src/parse-simple.cpp @@ -3,11 +3,11 @@ // #include -#include +#include TEST(Parser, SingleElement) { char doc_text[] = ""; - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; doc.parse<0>(doc_text); auto node = doc.first_node(); @@ -19,8 +19,8 @@ TEST(Parser, SingleElement) { TEST(Parser, DefaultElementNS) { char doc_text[] = ""; - rapidxml::xml_document<> doc; - doc.parse(doc_text); + flxml::xml_document<> doc; + doc.parse(doc_text); auto node = doc.first_node(); EXPECT_NE(nullptr, node); @@ -32,11 +32,11 @@ TEST(Parser, DefaultElementNS) { EXPECT_EQ(child->xmlns(), "this"); doc.validate(); auto no_node = child->next_sibling(); - EXPECT_THROW(no_node->xmlns(), rapidxml::no_such_node); + EXPECT_THROW(no_node->xmlns(), flxml::no_such_node); } TEST(Parser, UnboundPrefix) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; doc.parse<0>(doc_text); @@ -44,12 +44,12 @@ TEST(Parser, UnboundPrefix) { EXPECT_EQ("single-element", node->name()); EXPECT_THROW( doc.validate(), - rapidxml::element_xmlns_unbound + flxml::element_xmlns_unbound ); } TEST(Parser, DuplicateAttribute) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; doc.parse<0>(doc_text); @@ -57,12 +57,12 @@ TEST(Parser, DuplicateAttribute) { EXPECT_EQ("single-element", node->name()); EXPECT_THROW( doc.validate(), - rapidxml::duplicate_attribute + flxml::duplicate_attribute ); } TEST(Parser, UnboundAttrPrefix) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; doc.parse<0>(doc_text); @@ -71,17 +71,17 @@ TEST(Parser, UnboundAttrPrefix) { auto attr = node->first_attribute(); EXPECT_THROW( doc.validate(), - rapidxml::attr_xmlns_unbound + flxml::attr_xmlns_unbound ); EXPECT_THROW( attr->xmlns(), - rapidxml::attr_xmlns_unbound + flxml::attr_xmlns_unbound ); } TEST(Parser, DuplicateAttrPrefix) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; doc.parse<0>(doc_text); @@ -89,13 +89,13 @@ TEST(Parser, DuplicateAttrPrefix) { assert(std::string("single-element") == node->name()); EXPECT_THROW( doc.validate(), - rapidxml::duplicate_attribute + flxml::duplicate_attribute ); } TEST(Parser, Xmlns) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; doc.parse<0>(doc_text); @@ -107,7 +107,7 @@ TEST(Parser, Xmlns) { } TEST(Parser, ChildXmlns) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; doc.parse<0>(doc_text); @@ -150,18 +150,18 @@ TEST(Parser, ChildXmlns) { } TEST(Parser, HandleEOF){ - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; EXPECT_THROW( doc.parse<0>(doc_text), - rapidxml::eof_error + flxml::eof_error ); } TEST(ParseOptions, Fastest) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; - doc.parse(doc_text); + doc.parse(doc_text); auto node = doc.first_node(); EXPECT_EQ("single", node->name()); @@ -180,9 +180,9 @@ TEST(ParseOptions, Fastest) { } TEST(ParseOptions, OpenOnly) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = ""; - doc.parse(doc_text); + doc.parse(doc_text); auto node = doc.first_node(); EXPECT_EQ("single", node->name()); @@ -192,9 +192,9 @@ TEST(ParseOptions, OpenOnly) { } TEST(ParseOptions, ParseOne) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = "Hello!"; - const char * text = doc.parse(doc_text); + const char * text = doc.parse(doc_text); { auto node = doc.first_node(); @@ -208,8 +208,8 @@ TEST(ParseOptions, ParseOne) { doc.validate(); unsigned counter = 0; while (*text) { - rapidxml::xml_document<> subdoc; - text = subdoc.parse(text, &doc); + flxml::xml_document<> subdoc; + text = subdoc.parse(text, &doc); auto node = subdoc.first_node(); ASSERT_NE(nullptr, node); switch(++counter) { @@ -229,9 +229,9 @@ TEST(ParseOptions, ParseOne) { } TEST(ParseOptions, OpenOnlyFastest) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; char doc_text[] = "Hello!"; - const char * text = doc.parse(doc_text); + const char * text = doc.parse(doc_text); { auto node = doc.first_node(); @@ -245,8 +245,8 @@ TEST(ParseOptions, OpenOnlyFastest) { doc.validate(); unsigned counter = 0; while (*text) { - rapidxml::xml_document<> subdoc; - text = subdoc.parse(text, &doc); + flxml::xml_document<> subdoc; + text = subdoc.parse(text, &doc); auto node = subdoc.first_node(); ASSERT_NE(nullptr, node); switch(++counter) { @@ -267,38 +267,38 @@ TEST(ParseOptions, OpenOnlyFastest) { TEST(Parser_Emoji, Single) { std::string foo{"'"}; - rapidxml::xml_document<> doc; - doc.parse(foo); + flxml::xml_document<> doc; + doc.parse(foo); EXPECT_EQ("'", doc.first_node()->value()); } TEST(Parser_Emoji, SingleUni) { std::string foo{"Ӓ"}; - rapidxml::xml_document<> doc; - doc.parse(foo); + flxml::xml_document<> doc; + doc.parse(foo); EXPECT_EQ("\xD3\x92", doc.first_node()->value()); } TEST(Parser_Emoji, SingleEmoji) { std::string foo{"😀"}; - rapidxml::xml_document<> doc; - doc.parse(foo); + flxml::xml_document<> doc; + doc.parse(foo); EXPECT_EQ("\xF0\x9F\x98\x80", doc.first_node()->value()); EXPECT_EQ(4, doc.first_node()->value().size()); } TEST(Parser_Emoji, SingleEmojiReuse) { std::string bar("Sir I bear a rhyme excelling in mystic verse and magic spelling 😀"); - rapidxml::xml_document<> doc; - rapidxml::xml_document<> parent_doc; - parent_doc.parse(""); - doc.parse(bar, &parent_doc); + flxml::xml_document<> doc; + flxml::xml_document<> parent_doc; + parent_doc.parse(""); + doc.parse(bar, &parent_doc); EXPECT_EQ("Sir I bear a rhyme excelling in mystic verse and magic spelling \xF0\x9F\x98\x80", doc.first_node()->value()); auto doc_a = doc.first_node()->document(); doc.first_node()->value(doc_a->allocate_string("Sausages are the loneliest fruit, and are but one of the strange things I have witnessed in my long and interesting life.")); EXPECT_EQ("Sausages are the loneliest fruit, and are but one of the strange things I have witnessed in my long and interesting life.", doc.first_node()->value()); bar = "😀"; - doc.parse(bar, &parent_doc); + doc.parse(bar, &parent_doc); EXPECT_EQ("\xF0\x9F\x98\x80", doc.first_node()->value()); EXPECT_EQ(4, doc.first_node()->value().size()); } diff --git a/test/src/perf.cpp b/test/src/perf.cpp index a29b95a..3560fe5 100644 --- a/test/src/perf.cpp +++ b/test/src/perf.cpp @@ -2,13 +2,13 @@ // Created by dave on 07/07/2024. // -#include -#include +#include +#include #include #include -#include "rapidxml_print.hpp" -#include "rapidxml_iterators.hpp" +#include "flxml/print.hpp" +#include "flxml/iterators.hpp" const auto xml_sample_file = "REC-xml-20081126.xml"; @@ -24,13 +24,13 @@ TEST(Perf, Parse) { using std::chrono::microseconds; PERF_TEST(); - rapidxml::file source(xml_sample_file); + flxml::file source(xml_sample_file); std::vector timings; for (auto i = 0; i != 1000; ++i) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; auto t1 = high_resolution_clock::now(); - doc.parse(source.data()); + doc.parse(source.data()); auto t2 = high_resolution_clock::now(); auto ms_int = duration_cast(t2 - t1); timings.push_back(ms_int.count()); @@ -48,14 +48,14 @@ TEST(Perf, Parse2) { using std::chrono::microseconds; PERF_TEST(); - rapidxml::file source(xml_sample_file); + flxml::file source(xml_sample_file); std::vector timings; for (auto i = 0; i != 1000; ++i) { - rapidxml::xml_document<> doc; + flxml::xml_document<> doc; std::string_view sv{source.data(), source.size() - 1}; // Drop the NUL. auto t1 = high_resolution_clock::now(); - doc.parse(sv); + doc.parse(sv); auto t2 = high_resolution_clock::now(); auto ms_int = duration_cast(t2 - t1); timings.push_back(ms_int.count()); @@ -73,15 +73,15 @@ TEST(Perf, PrintClean) { using std::chrono::microseconds; PERF_TEST(); - rapidxml::file source(xml_sample_file); + flxml::file source(xml_sample_file); std::vector timings; - rapidxml::xml_document<> doc; - doc.parse(source.data()); + flxml::xml_document<> doc; + doc.parse(source.data()); for (auto i = 0; i != 1000; ++i) { std::string output; auto t1 = high_resolution_clock::now(); - rapidxml::print(std::back_inserter(output), doc); + flxml::print(std::back_inserter(output), doc); auto t2 = high_resolution_clock::now(); auto ms_int = duration_cast(t2 - t1); timings.push_back(ms_int.count()); @@ -99,19 +99,19 @@ TEST(Perf, PrintDirty) { using std::chrono::microseconds; PERF_TEST(); - rapidxml::file source(xml_sample_file); + flxml::file source(xml_sample_file); std::vector timings; - rapidxml::xml_document<> doc_o; - doc_o.parse(source.data()); - rapidxml::xml_document<> doc; - for (auto & child : rapidxml::children(doc_o)) { + flxml::xml_document<> doc_o; + doc_o.parse(source.data()); + flxml::xml_document<> doc; + for (auto & child : flxml::children(doc_o)) { doc.append_node(doc.clone_node(&child, true)); } for (auto i = 0; i != 1000; ++i) { std::string output; auto t1 = high_resolution_clock::now(); - rapidxml::print(std::back_inserter(output), doc); + flxml::print(std::back_inserter(output), doc); auto t2 = high_resolution_clock::now(); auto ms_int = duration_cast(t2 - t1); timings.push_back(ms_int.count()); diff --git a/test/src/round-trips.cpp b/test/src/round-trips.cpp index 3e2b516..f69c13d 100644 --- a/test/src/round-trips.cpp +++ b/test/src/round-trips.cpp @@ -3,14 +3,13 @@ // #include -#include "rapidxml.hpp" -#include "rapidxml_print.hpp" -#include "rapidxml_iterators.hpp" +#include "flxml.hpp" +#include "flxml/print.hpp" namespace { - auto print(rapidxml::xml_document<> & doc) { + auto print(flxml::xml_document<> & doc) { std::string output; - rapidxml::print(std::back_inserter(output), doc, rapidxml::print_no_indenting); + flxml::print(std::back_inserter(output), doc, flxml::print_no_indenting); return output; } } @@ -18,8 +17,8 @@ namespace { TEST(RoundTrip, Simple) { const char input[] = ""; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); // Have we parsed correctly? EXPECT_EQ(input, output); @@ -30,8 +29,8 @@ TEST(RoundTrip, Simple) { TEST(RoundTrip, SimpleMod) { const char input[] = ""; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); // Have we parsed correctly? EXPECT_EQ(input, output); @@ -63,7 +62,7 @@ TEST(RoundTrip, SimpleMod) { EXPECT_EQ(check->name(), name); EXPECT_EQ(check->name().data(), name.data()); EXPECT_EQ(output3, "the otheranother'the otherthe otherthe otherlast time"); - rapidxml::xml_document<> doc2; + flxml::xml_document<> doc2; doc2.clone_node(doc.first_node(), true); auto output4 = print(doc); EXPECT_EQ(output3, output4); @@ -72,12 +71,12 @@ TEST(RoundTrip, SimpleMod) { TEST(RoundTrip, SimpleApos) { const char input[] = ""; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); // Have we parsed correctly? - rapidxml::xml_document<> doc2; - for (auto & child : rapidxml::children(doc)) { + flxml::xml_document<> doc2; + for (auto & child : flxml::children(doc)) { doc2.append_node(doc2.clone_node(&child, true)); } EXPECT_EQ(input, print(doc2)); @@ -90,13 +89,13 @@ TEST(RoundTrip, SimpleApos2) { const char input[] = ""; const char expected[] = ""; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); EXPECT_EQ(doc.first_node()->first_attribute()->value(), "'"); // Have we parsed correctly? - rapidxml::xml_document<> doc2; - for (auto & child : rapidxml::children(doc)) { + flxml::xml_document<> doc2; + for (auto & child : flxml::children(doc)) { doc2.append_node(doc2.clone_node(&child, true)); } EXPECT_EQ(expected, print(doc2)); @@ -109,14 +108,14 @@ TEST(RoundTrip, SimpleLtBody) { const char input[] = "<"; const char expected[] = "<"; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); EXPECT_EQ(doc.first_node()->value(), "<"); EXPECT_EQ(doc.first_node()->first_attribute()->value(), "'"); // Have we parsed correctly? - rapidxml::xml_document<> doc2; - for (auto & child : rapidxml::children(doc)) { + flxml::xml_document<> doc2; + for (auto & child : flxml::children(doc)) { doc2.append_node(doc2.clone_node(&child, true)); } EXPECT_EQ(expected, print(doc2)); @@ -130,8 +129,8 @@ TEST(RoundTrip, MutateBody) { const char expected[] = "<"; const char expected2[] = "new value"; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); EXPECT_EQ(expected, output); // Have we mutated the underlying buffer? @@ -146,11 +145,11 @@ TEST(RoundTrip, Everything) { const char input[] = ""; const char expected[] = ""; std::vector buffer{input, input + sizeof(input)}; - rapidxml::xml_document<> doc; - doc.parse(buffer.data()); + flxml::xml_document<> doc; + doc.parse(buffer.data()); auto output = print(doc); - rapidxml::xml_document<> doc2; - for (auto & child : rapidxml::children(doc)) { + flxml::xml_document<> doc2; + for (auto & child : flxml::children(doc)) { doc2.append_node(doc2.clone_node(&child, true)); } EXPECT_EQ(expected, print(doc2)); @@ -164,12 +163,12 @@ TEST(RoundTrip, EverythingStream) { const char input[] = ""; const char expected[] = "\n\n\n\t\n\n\n"; std::vector buffer{input, input + sizeof(input) - 1}; - rapidxml::xml_document<> doc; - doc.parse(buffer); + flxml::xml_document<> doc; + doc.parse(buffer); std::stringstream ss1; ss1 << doc; auto output = ss1.str(); - rapidxml::xml_document<> doc2; + flxml::xml_document<> doc2; for (auto & child : doc.children()) { doc2.append_node(doc2.clone_node(&child, true)); } diff --git a/test/src/xpath.cpp b/test/src/xpath.cpp index abe7bc7..7073d22 100644 --- a/test/src/xpath.cpp +++ b/test/src/xpath.cpp @@ -3,12 +3,12 @@ // #include -#include "rapidxml_predicates.hpp" +#include "flxml/predicates.hpp" TEST(XPath, parse) { std::string xpath_string = "//"; std::string_view sv{xpath_string}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); EXPECT_EQ(sv.length(), 0); EXPECT_NE(xp.get(), nullptr); EXPECT_EQ(xp->chain().size(), 1); @@ -17,7 +17,7 @@ TEST(XPath, parse) { TEST(XPath, parse2) { std::string xpath_string = "//child"; std::string_view sv{xpath_string}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); EXPECT_EQ(sv.length(), 0); EXPECT_NE(xp.get(), nullptr); EXPECT_EQ(xp->chain().size(), 2); @@ -26,7 +26,7 @@ TEST(XPath, parse2) { TEST(XPath, parse1) { std::string xpath_string = "/child"; std::string_view sv{xpath_string}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); EXPECT_EQ(sv.length(), 0); EXPECT_NE(xp.get(), nullptr); EXPECT_EQ(xp->chain().size(), 2); @@ -35,7 +35,7 @@ TEST(XPath, parse1) { TEST(XPath, parse3) { std::string xpath_string = "//child[another/element]/something"; std::string_view sv{xpath_string}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); EXPECT_EQ(sv.length(), 0); EXPECT_NE(xp.get(), nullptr); EXPECT_EQ(xp->chain().size(), 4); @@ -47,7 +47,7 @@ TEST(XPath, parse4) { std::string xpath_string = ""; std::string_view sv{xpath_string}; EXPECT_THROW( - rapidxml::xpath<>::parse(sv), + flxml::xpath<>::parse(sv), std::runtime_error ); } @@ -56,7 +56,7 @@ TEST(XPath, parse4) { TEST(XPath, parse_attr) { std::string xpath_string = "//child[@foo='bar']/something"; std::string_view sv{xpath_string}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); EXPECT_EQ(sv.length(), 0); EXPECT_NE(xp.get(), nullptr); EXPECT_EQ(xp->chain().size(), 4); @@ -67,7 +67,7 @@ TEST(XPath, parse_attr) { TEST(XPath, parse_text) { std::string xpath_string = "//child[text()='bar']/something"; std::string_view sv{xpath_string}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); EXPECT_EQ(sv.length(), 0); EXPECT_NE(xp.get(), nullptr); EXPECT_EQ(xp->chain().size(), 4); @@ -76,44 +76,44 @@ TEST(XPath, parse_text) { } TEST(XPathFirst, simple_all) { - rapidxml::xml_document<> doc; - doc.parse(""); + flxml::xml_document<> doc; + doc.parse(""); std::string xpath = "//"; std::string_view sv{xpath}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); auto r = xp->first(doc); ASSERT_TRUE(r); - EXPECT_EQ(r->type(), rapidxml::node_type::node_document); + EXPECT_EQ(r->type(), flxml::node_type::node_document); } TEST(XPathFirst, simple_any) { - rapidxml::xml_document<> doc; - doc.parse(""); + flxml::xml_document<> doc; + doc.parse(""); std::string xpath = "//child"; std::string_view sv{xpath}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "child"); } TEST(XPathFirst, simple_sub) { - rapidxml::xml_document<> doc; - doc.parse(""); + flxml::xml_document<> doc; + doc.parse(""); std::string xpath = "//[child]"; std::string_view sv{xpath}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "simple"); } TEST(XPathFirst, simple_attr) { - rapidxml::xml_document<> doc; - doc.parse("foobar"); + flxml::xml_document<> doc; + doc.parse("foobar"); std::string xpath = "//child[@attr='val2']"; std::string_view sv{xpath}; - auto xp = rapidxml::xpath<>::parse(sv); + auto xp = flxml::xpath<>::parse(sv); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "child"); @@ -121,9 +121,9 @@ TEST(XPathFirst, simple_attr) { } TEST(XPathFirst, simple_text) { - rapidxml::xml_document<> doc; - doc.parse("foobar"); - auto xp = rapidxml::xpath<>::parse("//child[text()='bar']"); + flxml::xml_document<> doc; + doc.parse("foobar"); + auto xp = flxml::xpath<>::parse("//child[text()='bar']"); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "child"); @@ -131,9 +131,9 @@ TEST(XPathFirst, simple_text) { } TEST(XPathNS, simple_text) { - rapidxml::xml_document<> doc; - doc.parse("foobar"); - auto xp = rapidxml::xpath<>::parse("//child[text()='bar']"); + flxml::xml_document<> doc; + doc.parse("foobar"); + auto xp = flxml::xpath<>::parse("//child[text()='bar']"); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "child"); @@ -141,13 +141,13 @@ TEST(XPathNS, simple_text) { } TEST(XPathNS, xmlns_text) { - rapidxml::xml_document<> doc; - doc.parse("foobar"); + flxml::xml_document<> doc; + doc.parse("foobar"); std::map xmlns = { {"x1", "p2"}, {"x2", "p1"} }; - auto xp = rapidxml::xpath<>::parse(xmlns,"//x1:child[text()='bar']"); + auto xp = flxml::xpath<>::parse(xmlns,"//x1:child[text()='bar']"); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "child"); @@ -155,13 +155,13 @@ TEST(XPathNS, xmlns_text) { } TEST(XPathNS, xmlns_both) { - rapidxml::xml_document<> doc; - doc.parse("foobar"); + flxml::xml_document<> doc; + doc.parse("foobar"); std::map xmlns = { {"x1", "p2"}, {"x2", "p1"} }; - auto xp = rapidxml::xpath<>::parse(xmlns,"//x1:child[text()='bar'][@attr='val2']"); + auto xp = flxml::xpath<>::parse(xmlns,"//x1:child[text()='bar'][@attr='val2']"); auto r = xp->first(doc); ASSERT_TRUE(r); EXPECT_EQ(r->name(), "child"); @@ -169,13 +169,13 @@ TEST(XPathNS, xmlns_both) { } TEST(XPathNS, xmlns_text_miss) { - rapidxml::xml_document<> doc; - doc.parse("foobar"); + flxml::xml_document<> doc; + doc.parse("foobar"); std::map xmlns = { {"x1", "p2"}, {"x2", "p1"} }; - auto xp = rapidxml::xpath<>::parse(xmlns,"//x2:child[text()='bar']"); + auto xp = flxml::xpath<>::parse(xmlns,"//x2:child[text()='bar']"); auto r = xp->first(doc); ASSERT_FALSE(r); } From 818b6cedbfd2e01b16f3a88ea6bfd18a14d38554 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Tue, 22 Apr 2025 13:12:40 +0100 Subject: [PATCH 20/21] More tidying, including hpp->h --- README.md | 22 +++++++++---------- conanfile.py | 1 + include/{flxml.hpp => flxml.h} | 6 ++--- include/flxml/{generator.hpp => generator.h} | 0 include/flxml/{iterators.hpp => iterators.h} | 2 +- .../flxml/{predicates.hpp => predicates.h} | 4 ++-- include/flxml/{print.hpp => print.h} | 2 +- include/flxml/{tables.hpp => tables.h} | 0 include/flxml/{utils.hpp => utils.h} | 0 include/flxml/{wrappers.hpp => wrappers.h} | 0 include/rapidxml.hpp | 2 ++ include/rapidxml_print.hpp | 2 ++ 12 files changed, 23 insertions(+), 18 deletions(-) rename include/{flxml.hpp => flxml.h} (99%) rename include/flxml/{generator.hpp => generator.h} (100%) rename include/flxml/{iterators.hpp => iterators.h} (99%) rename include/flxml/{predicates.hpp => predicates.h} (99%) rename include/flxml/{print.hpp => print.h} (99%) rename include/flxml/{tables.hpp => tables.h} (100%) rename include/flxml/{utils.hpp => utils.h} (100%) rename include/flxml/{wrappers.hpp => wrappers.h} (100%) diff --git a/README.md b/README.md index 6c64828..7a1c7d0 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -# RapidXML -## Dave's Version +# FLXML +## Or -- RapidXML, Dave's Version -Hey! This is RapidXML, an ancient C++ library for parsing XML quickly and flexibly. +Hey! This is a fork of RapidXML, an ancient C++ library for parsing XML quickly and flexibly. To distinguish, this version is called "FLXML", for "Fast/Light XML". Hey, it's a name. There's a lot of forks of this around, and I (Dave Cridland) didn't write the vast majority of this library - instead, it was written by someone called Marcin Kalicinski, and his copyright is dated 2009. ## Version 2, Breaking Changes -This is version 2.x. You might not want this. +This is version 2.x. You might not want this, since it introduces a number of breaking changes from rapidxml. The rapidxml-like library is available, with breaking changes, by including `rapidxml.hpp` as before, within the `rapidxml` namespace - however this is an alias to the `flxml` namespace defined in `flxml.h`. It has breaking changes, the largest of which are: * No more case insensitive option. Really, nobody should be using XML case insensitively anyway, but it was too difficult to keep around, sorry. @@ -28,12 +28,12 @@ Internal changes: New features: * Instead of the `doc->allocate_node` / `node->append_node` dance, you can now `node->append_element(name, value)`, where `name` can be either a `string` (or `string_view`, etc) or a tuple like {xmlns, local_name}, which will set an xmlns attribute if needed. -* There's a xpathish thing going on in `rapidxml_predicates`, which lets you search for (or iterate through) elements using a trivial subset of XPath. +* There's a xpathish thing going on in `flxml/predicates.h`, which lets you search for (or iterate through) elements using a trivial subset of XPath. * You can get access to containerish things in rapidxml_iterators by methods on nodes/documents, as `node.children()`, `node.attributes()` and a new `node.descendants()`. ### Fun -The rapidxml_iterators library is now included in rapidxml.hpp, and you can do amusing things like: +The rapidxml_iterators library is now included in `flxml.h`, and you can do amusing things like: ```c++ for (auto & child : node.children()) { @@ -46,7 +46,7 @@ More in [test/iterators.cpp](./test/iterators.cpp) Of course, in this case it might be simpler to: ```c++ -auto xpath = rapidxml::xpath::parse("/potato"); +auto xpath = flxml::xpath::parse("/potato"); for (auto & child : xp->all(node)) { scream_for(joy); } @@ -77,10 +77,10 @@ The other thing this fork added was a file of simple tests, which I've recently The original makes reference to an expansive test suite, but this was not included in the open source release. I'll expand these tests as and when I need to. -The tests use a driver which can optionally use Sentry for performance/error tracking; to enable, use the CMake option RAPIDXML_SENTRY, and clone the [sentry-native](https://github.com/getsentry/sentry-native) repository into the root, and when running `rapidxml-test`, set SENTRY_DSN in the environment. None of the submodules are needed, but it'll need libcurl, so `sudo apt install libcurl4-openssl-dev`. +The tests use a driver which can optionally use Sentry for performance/error tracking; to enable, use the CMake option RAPIDXML_SENTRY, and clone the [sentry-native](https://github.com/getsentry/sentry-native) repository into the root, and when running `rapidxml-test`, set SENTRY_DSN in the environment. -## Pull Requests +The tests are in a different Conan package, to keep things light and simple. -Erm. I didn't expect any, so never set up any of the infrastructure for them - this was really a fork-of-convenience for me. Not that they're unwelcome, of course, just entirely unexpected. +## Pull Requests -But yeah, go for it, just include an assurance you're happy with the licensing. \ No newline at end of file +Pull request are very welcome, but do ensure you're happy with the licensing first. \ No newline at end of file diff --git a/conanfile.py b/conanfile.py index fe93a36..b420226 100644 --- a/conanfile.py +++ b/conanfile.py @@ -8,6 +8,7 @@ class FLXML(ConanFile): def package(self): copy(self, "include/*.hpp", self.source_folder, self.package_folder) + copy(self, "include/*.h", self.source_folder, self.package_folder) def package_info(self): self.cpp_info.includedirs = ['include'] diff --git a/include/flxml.hpp b/include/flxml.h similarity index 99% rename from include/flxml.hpp rename to include/flxml.h index c2f743f..ead2dd2 100644 --- a/include/flxml.hpp +++ b/include/flxml.h @@ -6,8 +6,8 @@ // Revision $DateTime: 2009/05/13 01:46:17 $ //! \file rapidxml.hpp This file contains rapidxml parser and DOM implementation -#include "flxml/wrappers.hpp" -#include "flxml/tables.hpp" +#include "flxml/wrappers.h" +#include "flxml/tables.h" #include // For std::size_t #include // For assert @@ -2627,7 +2627,7 @@ namespace flxml } // Also include this now. -#include +#include // Undefine internal macros #undef FLXML_PARSE_ERROR diff --git a/include/flxml/generator.hpp b/include/flxml/generator.h similarity index 100% rename from include/flxml/generator.hpp rename to include/flxml/generator.h diff --git a/include/flxml/iterators.hpp b/include/flxml/iterators.h similarity index 99% rename from include/flxml/iterators.hpp rename to include/flxml/iterators.h index 5da9447..4bc9e3e 100644 --- a/include/flxml/iterators.hpp +++ b/include/flxml/iterators.h @@ -6,7 +6,7 @@ // Revision $DateTime: 2009/05/13 01:46:17 $ //! \file rapidxml_iterators.hpp This file contains rapidxml iterators -#include "flxml.hpp" +#include "flxml.h" namespace flxml { diff --git a/include/flxml/predicates.hpp b/include/flxml/predicates.h similarity index 99% rename from include/flxml/predicates.hpp rename to include/flxml/predicates.h index b254a81..36ee070 100644 --- a/include/flxml/predicates.hpp +++ b/include/flxml/predicates.h @@ -7,8 +7,8 @@ #include #include -#include "flxml/generator.hpp" -#include "flxml.hpp" +#include "flxml/generator.h" +#include "flxml.h" namespace flxml { template class xpath; diff --git a/include/flxml/print.hpp b/include/flxml/print.h similarity index 99% rename from include/flxml/print.hpp rename to include/flxml/print.h index 5d99054..2d940c3 100644 --- a/include/flxml/print.hpp +++ b/include/flxml/print.h @@ -6,7 +6,7 @@ // Revision $DateTime: 2009/05/13 01:46:17 $ //! \file rapidxml_print.hpp This file contains rapidxml printer implementation -#include "flxml.hpp" +#include "flxml.h" // Only include streams if not disabled #ifndef FLXML_NO_STREAMS diff --git a/include/flxml/tables.hpp b/include/flxml/tables.h similarity index 100% rename from include/flxml/tables.hpp rename to include/flxml/tables.h diff --git a/include/flxml/utils.hpp b/include/flxml/utils.h similarity index 100% rename from include/flxml/utils.hpp rename to include/flxml/utils.h diff --git a/include/flxml/wrappers.hpp b/include/flxml/wrappers.h similarity index 100% rename from include/flxml/wrappers.hpp rename to include/flxml/wrappers.h diff --git a/include/rapidxml.hpp b/include/rapidxml.hpp index 29d33c8..e70a5eb 100644 --- a/include/rapidxml.hpp +++ b/include/rapidxml.hpp @@ -5,6 +5,8 @@ #ifndef RAPIDXML_HPP #define RAPIDXML_HPP +#include + namespace rapidxml = flxml; #endif //RAPIDXML_HPP diff --git a/include/rapidxml_print.hpp b/include/rapidxml_print.hpp index ed0d709..89a5a60 100644 --- a/include/rapidxml_print.hpp +++ b/include/rapidxml_print.hpp @@ -5,6 +5,8 @@ #ifndef RAPIDXML_PRINT_HPP #define RAPIDXML_PRINT_HPP +#include + namespace rapidxml = flxml; #endif //RAPIDXML_PRINT_HPP From 1eaecc99416b5336c051737dc6fa20b31f850f9e Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Tue, 22 Apr 2025 13:27:11 +0100 Subject: [PATCH 21/21] More tidying, including hpp->h --- include/flxml.h | 4 ++-- include/flxml/iterators.h | 2 +- include/flxml/predicates.h | 4 ++-- include/flxml/print.h | 2 +- include/flxml/utils.h | 2 +- test/src/iterators.cpp | 2 +- test/src/low-level-parse.cpp | 2 +- test/src/manipulations.cpp | 4 ++-- test/src/parse-simple.cpp | 2 +- test/src/perf.cpp | 8 ++++---- test/src/round-trips.cpp | 4 ++-- test/src/xpath.cpp | 2 +- 12 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/flxml.h b/include/flxml.h index ead2dd2..56a3129 100644 --- a/include/flxml.h +++ b/include/flxml.h @@ -6,8 +6,8 @@ // Revision $DateTime: 2009/05/13 01:46:17 $ //! \file rapidxml.hpp This file contains rapidxml parser and DOM implementation -#include "flxml/wrappers.h" -#include "flxml/tables.h" +#include +#include #include // For std::size_t #include // For assert diff --git a/include/flxml/iterators.h b/include/flxml/iterators.h index 4bc9e3e..5abc662 100644 --- a/include/flxml/iterators.h +++ b/include/flxml/iterators.h @@ -6,7 +6,7 @@ // Revision $DateTime: 2009/05/13 01:46:17 $ //! \file rapidxml_iterators.hpp This file contains rapidxml iterators -#include "flxml.h" +#include namespace flxml { diff --git a/include/flxml/predicates.h b/include/flxml/predicates.h index 36ee070..ae88de4 100644 --- a/include/flxml/predicates.h +++ b/include/flxml/predicates.h @@ -7,8 +7,8 @@ #include #include -#include "flxml/generator.h" -#include "flxml.h" +#include +#include namespace flxml { template class xpath; diff --git a/include/flxml/print.h b/include/flxml/print.h index 2d940c3..5bd1239 100644 --- a/include/flxml/print.h +++ b/include/flxml/print.h @@ -6,7 +6,7 @@ // Revision $DateTime: 2009/05/13 01:46:17 $ //! \file rapidxml_print.hpp This file contains rapidxml printer implementation -#include "flxml.h" +#include // Only include streams if not disabled #ifndef FLXML_NO_STREAMS diff --git a/include/flxml/utils.h b/include/flxml/utils.h index df15b27..9b5d474 100644 --- a/include/flxml/utils.h +++ b/include/flxml/utils.h @@ -7,7 +7,7 @@ //! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful //! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective. -#include "flxml.hpp" +#include #include #include #include diff --git a/test/src/iterators.cpp b/test/src/iterators.cpp index 19ef0db..bb269b1 100644 --- a/test/src/iterators.cpp +++ b/test/src/iterators.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "flxml.hpp" +#include TEST(Iterators, Nodes) { std::string xml = ""; diff --git a/test/src/low-level-parse.cpp b/test/src/low-level-parse.cpp index 2845029..208a9b4 100644 --- a/test/src/low-level-parse.cpp +++ b/test/src/low-level-parse.cpp @@ -3,7 +3,7 @@ // #include -#include "flxml.hpp" +#include TEST(Constants, Empty) { flxml::xml_document<> doc; diff --git a/test/src/manipulations.cpp b/test/src/manipulations.cpp index 455c3b3..16105b0 100644 --- a/test/src/manipulations.cpp +++ b/test/src/manipulations.cpp @@ -4,8 +4,8 @@ #include -#include "flxml.hpp" -#include "flxml/print.hpp" +#include +#include namespace { auto print(flxml::xml_document<> & doc) { diff --git a/test/src/parse-simple.cpp b/test/src/parse-simple.cpp index 2b68270..7eea213 100644 --- a/test/src/parse-simple.cpp +++ b/test/src/parse-simple.cpp @@ -3,7 +3,7 @@ // #include -#include +#include TEST(Parser, SingleElement) { char doc_text[] = ""; diff --git a/test/src/perf.cpp b/test/src/perf.cpp index 3560fe5..c666590 100644 --- a/test/src/perf.cpp +++ b/test/src/perf.cpp @@ -2,13 +2,13 @@ // Created by dave on 07/07/2024. // -#include -#include +#include +#include #include #include -#include "flxml/print.hpp" -#include "flxml/iterators.hpp" +#include "flxml/print.h" +#include "flxml/iterators.h" const auto xml_sample_file = "REC-xml-20081126.xml"; diff --git a/test/src/round-trips.cpp b/test/src/round-trips.cpp index f69c13d..1381b38 100644 --- a/test/src/round-trips.cpp +++ b/test/src/round-trips.cpp @@ -3,8 +3,8 @@ // #include -#include "flxml.hpp" -#include "flxml/print.hpp" +#include +#include namespace { auto print(flxml::xml_document<> & doc) { diff --git a/test/src/xpath.cpp b/test/src/xpath.cpp index 7073d22..92c27b6 100644 --- a/test/src/xpath.cpp +++ b/test/src/xpath.cpp @@ -3,7 +3,7 @@ // #include -#include "flxml/predicates.hpp" +#include TEST(XPath, parse) { std::string xpath_string = "//";