├── .gitignore ├── .travis.yml ├── .ycm_extra_conf.py ├── CHANGELOG.md ├── CMakeLists.txt ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── cmake ├── FindOsmium.cmake └── common.cmake ├── include └── rapidjson │ ├── allocators.h │ ├── document.h │ ├── encodedstream.h │ ├── encodings.h │ ├── error │ ├── en.h │ └── error.h │ ├── filereadstream.h │ ├── filewritestream.h │ ├── internal │ ├── biginteger.h │ ├── diyfp.h │ ├── dtoa.h │ ├── ieee754.h │ ├── itoa.h │ ├── meta.h │ ├── pow10.h │ ├── stack.h │ ├── strfunc.h │ └── strtod.h │ ├── memorybuffer.h │ ├── memorystream.h │ ├── msinttypes │ ├── inttypes.h │ └── stdint.h │ ├── pointer.h │ ├── prettywriter.h │ ├── rapidjson.h │ ├── reader.h │ ├── stringbuffer.h │ └── writer.h ├── json_feature.cpp ├── json_feature.hpp ├── json_handler.cpp ├── json_handler.hpp ├── minjur-generate-tilelist.cpp ├── minjur-mp.cpp ├── minjur.cpp ├── minjur_version.hpp └── scripts └── travis_script.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .ycm_extra_conf.pyc 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------- 2 | # 3 | # Configuration for continuous integration service at travis-ci.org 4 | # 5 | #----------------------------------------------------------------------------- 6 | 7 | language: cpp 8 | 9 | sudo: false 10 | 11 | matrix: 12 | include: 13 | - os: linux 14 | compiler: clang 15 | env: BUILD_TYPE=Dev 16 | - os: linux 17 | compiler: clang 18 | env: BUILD_TYPE=Release 19 | - os: linux 20 | compiler: gcc 21 | env: BUILD_TYPE=Dev 22 | - os: linux 23 | compiler: gcc 24 | env: BUILD_TYPE=Release 25 | - os: osx 26 | compiler: clang 27 | env: BUILD_TYPE=Dev 28 | - os: osx 29 | compiler: clang 30 | env: BUILD_TYPE=Release 31 | 32 | # http://docs.travis-ci.com/user/apt/ 33 | addons: 34 | apt: 35 | sources: 36 | - ubuntu-toolchain-r-test 37 | - boost-latest 38 | packages: 39 | - g++-4.8 40 | - gcc-4.8 41 | - libboost1.55-dev 42 | 43 | install: 44 | - git clone --quiet --depth 1 https://github.com/osmcode/libosmium.git ../libosmium 45 | 46 | script: 47 | - scripts/travis_script.sh 48 | 49 | -------------------------------------------------------------------------------- /.ycm_extra_conf.py: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------- 2 | # 3 | # Configuration for YouCompleteMe Vim plugin 4 | # 5 | # http://valloric.github.io/YouCompleteMe/ 6 | # 7 | #----------------------------------------------------------------------------- 8 | 9 | from os.path import realpath, dirname 10 | 11 | basedir = dirname(realpath(__file__)) 12 | 13 | # some default flags 14 | # for more information install clang-3.2-doc package and 15 | # check UsersManual.html 16 | flags = [ 17 | '-Werror', 18 | '-Wall', 19 | '-Wextra', 20 | '-pedantic', 21 | '-Wno-return-type', 22 | '-Wno-unused-parameter', 23 | '-Wno-unused-variable', 24 | 25 | '-std=c++11', 26 | 27 | # '-x' and 'c++' also required 28 | # use 'c' for C projects 29 | '-x', 30 | 'c++', 31 | 32 | # include dirs 33 | '-I%s' % basedir, 34 | '-I%s/include' % basedir, 35 | '-I%s/../libosmium/include' % basedir, 36 | 37 | ] 38 | 39 | # youcompleteme is calling this function to get flags 40 | # You can also set database for flags. Check: JSONCompilationDatabase.html in 41 | # clang-3.2-doc package 42 | def FlagsForFile( filename ): 43 | return { 44 | 'flags': flags, 45 | 'do_cache': True 46 | } 47 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # minjur changelog 2 | 3 | 4 | ## v0.1.0 5 | 6 | - First official release. Tagged from b4508a0. -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #---------------------------------------------------------------------- 2 | # 3 | # Single example osmium-contrib CMakeLists.txt 4 | # 5 | #---------------------------------------------------------------------- 6 | project(minjur) 7 | 8 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 9 | 10 | cmake_minimum_required(VERSION 2.8.5) 11 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") 12 | 13 | find_package(Boost REQUIRED) 14 | include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) 15 | 16 | find_package(Osmium REQUIRED COMPONENTS io) 17 | include_directories(SYSTEM ${OSMIUM_INCLUDE_DIRS}) 18 | 19 | include(common) 20 | 21 | #---------------------------------------------------------------------- 22 | 23 | include_directories(include) 24 | 25 | add_executable(minjur minjur.cpp json_feature.cpp json_handler.cpp) 26 | target_link_libraries(minjur ${OSMIUM_LIBRARIES}) 27 | 28 | add_executable(minjur-generate-tilelist minjur-generate-tilelist.cpp) 29 | target_link_libraries(minjur-generate-tilelist ${OSMIUM_LIBRARIES}) 30 | 31 | add_executable(minjur-mp minjur-mp.cpp json_feature.cpp json_handler.cpp) 32 | target_link_libraries(minjur-mp ${OSMIUM_LIBRARIES}) 33 | 34 | 35 | #----------------------------------------------------------------------------- 36 | # 37 | # Optional "cppcheck" target that checks C++ code 38 | # 39 | #----------------------------------------------------------------------------- 40 | message(STATUS "Looking for cppcheck") 41 | find_program(CPPCHECK cppcheck) 42 | 43 | if(CPPCHECK) 44 | message(STATUS "Looking for cppcheck - found") 45 | set(CPPCHECK_OPTIONS --enable=all) 46 | 47 | # cpp doesn't find system includes for some reason, suppress that report 48 | set(CPPCHECK_OPTIONS ${CPPCHECK_OPTIONS} --suppress=missingIncludeSystem) 49 | 50 | add_custom_target(cppcheck ${CPPCHECK} --std=c++11 ${CPPCHECK_OPTIONS} ${CMAKE_SOURCE_DIR}/*pp) 51 | else() 52 | message(STATUS "Looking for cppcheck - not found") 53 | message(STATUS " Build target 'cppcheck' will not be available") 54 | endif() 55 | 56 | 57 | #----------------------------------------------------------------------------- 58 | # 59 | # Optional "iwyu" target to check headers 60 | # http://include-what-you-use.org/ 61 | # 62 | #----------------------------------------------------------------------------- 63 | find_program(IWYU_TOOL iwyu_tool.py) 64 | 65 | if(IWYU_TOOL) 66 | message(STATUS "Looking for iwyu_tool.py - found") 67 | add_custom_target(iwyu ${IWYU_TOOL} -p ${CMAKE_BINARY_DIR}) 68 | else() 69 | message(STATUS "Looking for iwyu_tool.py - not found") 70 | message(STATUS " Make target 'iwyu' will not be available") 71 | endif() 72 | 73 | 74 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to minjur 2 | 3 | ## Releasing 4 | 5 | To release a new minjur version: 6 | 7 | - Make sure all tests are passing locally and on travis 8 | - Update version number in 9 | - minjur_version.hpp 10 | - README.md 11 | - Update CHANGELOG.md 12 | - Make a git tag: `git tag v${VERSION} -a -m "v${VERSION}" && git push --tags` 13 | - Go to https://github.com/mapbox/minjur/releases 14 | and edit the new release. Put "Version x.y.z" in title and 15 | cut-and-paste entry from CHANGELOG.md. 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2015, Mapbox 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any purpose 6 | with or without fee is hereby granted, provided that the above copyright notice 7 | and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 11 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 13 | OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 14 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 15 | THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # minjur 3 | 4 | **This program will not be developed any further. We recommend you use the 5 | [`export`](http://docs.osmcode.org/osmium/latest/osmium-export.html) 6 | command of [osmium-tool](http://osmcode.org/osmium-tool) instead.** 7 | 8 | Osmium-based converter of OSM data to GeoJSON. 9 | 10 | [![Build Status](https://secure.travis-ci.org/mapbox/minjur.png)](https://travis-ci.org/mapbox/minjur) 11 | 12 | ## Installing 13 | 14 | It is recommended that you install minjur from binaries using mason: 15 | 16 | ```bash 17 | $ mkdir mason && curl -sSfL https://github.com/mapbox/mason/archive/v0.6.0.tar.gz | tar --gunzip --extract --strip-components=1 --directory=./mason 18 | $ ./mason/mason install minjur 0.1.0 19 | $ ./mason/mason link minjur 0.1.0 20 | $ ./mason_packages/.link/bin/minjur --version 21 | 0.1.0 22 | ``` 23 | 24 | If you need to source compile then you'll need `libosmium` headers available. Then you build minjur with `cmake`: 25 | 26 | 27 | mkdir build 28 | cd build 29 | cmake .. 30 | make 31 | 32 | ## Run 33 | 34 | Run like this: 35 | 36 | minjur [OPTIONS] OSMFILE >out.geojson 37 | 38 | Call with `--help` to see command line options. 39 | 40 | Options: 41 | 42 | -d, --dump=FILE Dump location cache to file after run 43 | -e, --error-file=FILE Write errors to file 44 | -h, --help This help message 45 | -i, --with-id Add unique id to each feature 46 | -l, --location-store=TYPE Set location store 47 | -L, --list-location-stores Show available location stores 48 | -n, --nodes=sparse|dense Are node IDs sparse or dense? 49 | -p, --polygons Create polygons from closed ways 50 | -t, --tilefile=FILE File with tiles to filter 51 | -z, --zoom=ZOOM Zoom level for tiles (default: 15) 52 | -a, --attr-prefix=PREFIX Optional prefix for attributes, defaults to '@' 53 | 54 | ## Output 55 | 56 | The output will be a (possibly rather large file) with one GeoJSON object 57 | per line. 58 | 59 | Nodes without any tags will not appear in the output. Ways with zero or a 60 | single node will not appear in the output. 61 | 62 | All tags will appear as GeoJSON properties, the object `type`, `id`, `version`, 63 | `changeset`, `uid`, `user`, and `timestamp` will also be in the properties 64 | using a leading `@` in the key indicating that this is an attribute, not a 65 | tag. You can use the `--attr-prefix` or `-a` option to change this prefix. 66 | 67 | 68 | ## Working with updates 69 | 70 | If you are working with a planet file or very large extract (a large continent) 71 | set: 72 | 73 | export INDEX_TYPE=dense 74 | 75 | If you are working with a small (city-sized) to medium (country-sized) extract 76 | set: 77 | 78 | export INDEX_TYPE=sparse 79 | 80 | Then run like this on the old OSM data file: 81 | 82 | minjur -d locations.dump -n ${INDEX_TYPE} OLD_OSMFILE >out.geojson 83 | 84 | This will create a file `locations.dump` in addition to `out.geojson`. With 85 | the change file (usually with suffix `.osc.gz`) you run the following to 86 | create the tile list: 87 | 88 | minjur-generate-tilelist -l ${INDEX_TYPE}_file_array,locations.dump CHANGE_FILE >tiles.list 89 | 90 | Then run `minjur` again with the tile list to create a GeoJSON file with only 91 | the changes: 92 | 93 | minjur -d locations.dump -n ${INDEX_TYPE} -t tiles.list NEW_OSMFILE >changes.geojson 94 | 95 | Repeat the last two lines for every change file. 96 | 97 | For planet updates, you'll need at least 40GB RAM for the node location cache, 98 | on OS/X and Windows it could be twice that! 99 | 100 | ## minjur-generate-tilelist 101 | 102 | Run like this: 103 | 104 | minjur-generate-tilelist [OPTIONS] OSM-CHANGE-FILE 105 | 106 | Output is always to stdout. 107 | 108 | Options: 109 | 110 | -h, --help This help message 111 | -l, --location_store=TYPE Set location store 112 | -L, --list-location-stores Show available location stores 113 | -n, --nodes=sparse|dense Are node IDs sparse or dense? 114 | -z, --zoom=ZOOM Zoom level for tiles (default: 15) 115 | 116 | ## Experimental version with multipolygon support 117 | 118 | There is an experimental version called `minjur-mp` that has multipolygon 119 | support. It does not support updates, that's why it doesn't have all the 120 | options the `minjur` program has. 121 | 122 | Run like this: 123 | 124 | minjur-mp [OPTIONS] OSMFILE >out.geojson 125 | 126 | Call with `--help` to see command line options. 127 | 128 | Options: 129 | 130 | -e, --error-file=FILE Write errors to file 131 | -h, --help This help message 132 | -i, --with-id Add unique id to each feature 133 | -l, --location-store=TYPE Set location store 134 | -L, --list-location-stores Show available location stores 135 | -n, --nodes=sparse|dense Are node IDs sparse or dense? 136 | -a, --attr-prefix=PREFIX Optional prefix for attributes, defaults to '@' 137 | 138 | The output will have GeoJSON objects for all the tagged nodes first and then, 139 | in no particular order, the GeoJSON LineString objects (generated from ways) 140 | and the MultiPolygon objects (generated from multipolygon relations and closed 141 | ways). 142 | 143 | Note that this version of the program will run much longer, generating the 144 | multipolygons is rather slow. 145 | 146 | 147 | ## Name 148 | 149 | This project is named after the town of Minjur in India which I know nothing 150 | about. 151 | 152 | https://www.openstreetmap.org/#map=15/13.2781/80.2530 153 | 154 | -------------------------------------------------------------------------------- /cmake/FindOsmium.cmake: -------------------------------------------------------------------------------- 1 | #---------------------------------------------------------------------- 2 | # 3 | # FindOsmium.cmake 4 | # 5 | # Find the Libosmium headers and, optionally, several components needed for 6 | # different Libosmium functions. 7 | # 8 | #---------------------------------------------------------------------- 9 | # 10 | # Usage: 11 | # 12 | # Copy this file somewhere into your project directory, where cmake can 13 | # find it. Usually this will be a directory called "cmake" which you can 14 | # add to the CMake module search path with the following line in your 15 | # CMakeLists.txt: 16 | # 17 | # list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") 18 | # 19 | # Then add the following in your CMakeLists.txt: 20 | # 21 | # find_package(Osmium REQUIRED COMPONENTS ) 22 | # include_directories(SYSTEM ${OSMIUM_INCLUDE_DIRS}) 23 | # 24 | # For the substitute a space separated list of one or more of the 25 | # following components: 26 | # 27 | # pbf - include libraries needed for PBF input and output 28 | # xml - include libraries needed for XML input and output 29 | # io - include libraries needed for any type of input/output 30 | # geos - include if you want to use any of the GEOS functions 31 | # gdal - include if you want to use any of the OGR functions 32 | # proj - include if you want to use any of the Proj.4 functions 33 | # sparsehash - include if you use the sparsehash index 34 | # 35 | # You can check for success with something like this: 36 | # 37 | # if(NOT OSMIUM_FOUND) 38 | # message(WARNING "Libosmium not found!\n") 39 | # endif() 40 | # 41 | #---------------------------------------------------------------------- 42 | # 43 | # Variables: 44 | # 45 | # OSMIUM_FOUND - True if Osmium found. 46 | # OSMIUM_INCLUDE_DIRS - Where to find include files. 47 | # OSMIUM_XML_LIBRARIES - Libraries needed for XML I/O. 48 | # OSMIUM_PBF_LIBRARIES - Libraries needed for PBF I/O. 49 | # OSMIUM_IO_LIBRARIES - Libraries needed for XML or PBF I/O. 50 | # OSMIUM_LIBRARIES - All libraries Osmium uses somewhere. 51 | # 52 | #---------------------------------------------------------------------- 53 | 54 | # Look for the header file. 55 | find_path(OSMIUM_INCLUDE_DIR osmium/osm.hpp 56 | PATH_SUFFIXES include 57 | PATHS 58 | ../libosmium 59 | ~/Library/Frameworks 60 | /Library/Frameworks 61 | /opt/local # DarwinPorts 62 | /opt 63 | ) 64 | 65 | set(OSMIUM_INCLUDE_DIRS "${OSMIUM_INCLUDE_DIR}") 66 | 67 | #---------------------------------------------------------------------- 68 | # 69 | # Check for optional components 70 | # 71 | #---------------------------------------------------------------------- 72 | if(Osmium_FIND_COMPONENTS) 73 | foreach(_component ${Osmium_FIND_COMPONENTS}) 74 | string(TOUPPER ${_component} _component_uppercase) 75 | set(Osmium_USE_${_component_uppercase} TRUE) 76 | endforeach() 77 | endif() 78 | 79 | #---------------------------------------------------------------------- 80 | # Component 'io' is an alias for 'pbf' and 'xml' 81 | if(Osmium_USE_IO) 82 | set(Osmium_USE_PBF TRUE) 83 | set(Osmium_USE_XML TRUE) 84 | endif() 85 | 86 | #---------------------------------------------------------------------- 87 | # Component 'ogr' is an alias for 'gdal' 88 | if(Osmium_USE_OGR) 89 | set(Osmium_USE_GDAL TRUE) 90 | endif() 91 | 92 | #---------------------------------------------------------------------- 93 | # Component 'pbf' 94 | if(Osmium_USE_PBF) 95 | find_package(ZLIB) 96 | find_package(Threads) 97 | 98 | list(APPEND OSMIUM_EXTRA_FIND_VARS ZLIB_FOUND Threads_FOUND) 99 | if(ZLIB_FOUND AND Threads_FOUND) 100 | list(APPEND OSMIUM_PBF_LIBRARIES 101 | ${ZLIB_LIBRARIES} 102 | ${CMAKE_THREAD_LIBS_INIT} 103 | ) 104 | if(WIN32) 105 | list(APPEND OSMIUM_PBF_LIBRARIES ws2_32) 106 | endif() 107 | list(APPEND OSMIUM_INCLUDE_DIRS 108 | ${ZLIB_INCLUDE_DIR} 109 | ) 110 | else() 111 | message(WARNING "Osmium: Can not find some libraries for PBF input/output, please install them or configure the paths.") 112 | endif() 113 | endif() 114 | 115 | #---------------------------------------------------------------------- 116 | # Component 'xml' 117 | if(Osmium_USE_XML) 118 | find_package(EXPAT) 119 | find_package(BZip2) 120 | find_package(ZLIB) 121 | find_package(Threads) 122 | 123 | list(APPEND OSMIUM_EXTRA_FIND_VARS EXPAT_FOUND BZIP2_FOUND ZLIB_FOUND Threads_FOUND) 124 | if(EXPAT_FOUND AND BZIP2_FOUND AND ZLIB_FOUND AND Threads_FOUND) 125 | list(APPEND OSMIUM_XML_LIBRARIES 126 | ${EXPAT_LIBRARIES} 127 | ${BZIP2_LIBRARIES} 128 | ${ZLIB_LIBRARIES} 129 | ${CMAKE_THREAD_LIBS_INIT} 130 | ) 131 | list(APPEND OSMIUM_INCLUDE_DIRS 132 | ${EXPAT_INCLUDE_DIR} 133 | ${BZIP2_INCLUDE_DIR} 134 | ${ZLIB_INCLUDE_DIR} 135 | ) 136 | else() 137 | message(WARNING "Osmium: Can not find some libraries for XML input/output, please install them or configure the paths.") 138 | endif() 139 | endif() 140 | 141 | #---------------------------------------------------------------------- 142 | list(APPEND OSMIUM_IO_LIBRARIES 143 | ${OSMIUM_PBF_LIBRARIES} 144 | ${OSMIUM_XML_LIBRARIES} 145 | ) 146 | 147 | list(APPEND OSMIUM_LIBRARIES 148 | ${OSMIUM_IO_LIBRARIES} 149 | ) 150 | 151 | #---------------------------------------------------------------------- 152 | # Component 'geos' 153 | if(Osmium_USE_GEOS) 154 | find_path(GEOS_INCLUDE_DIR geos/geom.h) 155 | find_library(GEOS_LIBRARY NAMES geos) 156 | 157 | list(APPEND OSMIUM_EXTRA_FIND_VARS GEOS_INCLUDE_DIR GEOS_LIBRARY) 158 | if(GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 159 | SET(GEOS_FOUND 1) 160 | list(APPEND OSMIUM_LIBRARIES ${GEOS_LIBRARY}) 161 | list(APPEND OSMIUM_INCLUDE_DIRS ${GEOS_INCLUDE_DIR}) 162 | else() 163 | message(WARNING "Osmium: GEOS library is required but not found, please install it or configure the paths.") 164 | endif() 165 | endif() 166 | 167 | #---------------------------------------------------------------------- 168 | # Component 'gdal' (alias 'ogr') 169 | if(Osmium_USE_GDAL) 170 | find_package(GDAL) 171 | 172 | list(APPEND OSMIUM_EXTRA_FIND_VARS GDAL_FOUND) 173 | if(GDAL_FOUND) 174 | list(APPEND OSMIUM_LIBRARIES ${GDAL_LIBRARIES}) 175 | list(APPEND OSMIUM_INCLUDE_DIRS ${GDAL_INCLUDE_DIRS}) 176 | else() 177 | message(WARNING "Osmium: GDAL library is required but not found, please install it or configure the paths.") 178 | endif() 179 | endif() 180 | 181 | #---------------------------------------------------------------------- 182 | # Component 'proj' 183 | if(Osmium_USE_PROJ) 184 | find_path(PROJ_INCLUDE_DIR proj_api.h) 185 | find_library(PROJ_LIBRARY NAMES proj) 186 | 187 | list(APPEND OSMIUM_EXTRA_FIND_VARS PROJ_INCLUDE_DIR PROJ_LIBRARY) 188 | if(PROJ_INCLUDE_DIR AND PROJ_LIBRARY) 189 | set(PROJ_FOUND 1) 190 | list(APPEND OSMIUM_LIBRARIES ${PROJ_LIBRARY}) 191 | list(APPEND OSMIUM_INCLUDE_DIRS ${PROJ_INCLUDE_DIR}) 192 | else() 193 | message(WARNING "Osmium: PROJ.4 library is required but not found, please install it or configure the paths.") 194 | endif() 195 | endif() 196 | 197 | #---------------------------------------------------------------------- 198 | # Component 'sparsehash' 199 | if(Osmium_USE_SPARSEHASH) 200 | find_path(SPARSEHASH_INCLUDE_DIR google/sparsetable) 201 | 202 | list(APPEND OSMIUM_EXTRA_FIND_VARS SPARSEHASH_INCLUDE_DIR) 203 | if(SPARSEHASH_INCLUDE_DIR) 204 | # Find size of sparsetable::size_type. This does not work on older 205 | # CMake versions because they can do this check only in C, not in C++. 206 | if (NOT CMAKE_VERSION VERSION_LESS 3.0) 207 | include(CheckTypeSize) 208 | set(CMAKE_REQUIRED_INCLUDES ${SPARSEHASH_INCLUDE_DIR}) 209 | set(CMAKE_EXTRA_INCLUDE_FILES "google/sparsetable") 210 | check_type_size("google::sparsetable::size_type" SPARSETABLE_SIZE_TYPE LANGUAGE CXX) 211 | set(CMAKE_EXTRA_INCLUDE_FILES) 212 | set(CMAKE_REQUIRED_INCLUDES) 213 | else() 214 | set(SPARSETABLE_SIZE_TYPE ${CMAKE_SIZEOF_VOID_P}) 215 | endif() 216 | 217 | # Sparsetable::size_type must be at least 8 bytes (64bit), otherwise 218 | # OSM object IDs will not fit. 219 | if(SPARSETABLE_SIZE_TYPE GREATER 7) 220 | set(SPARSEHASH_FOUND 1) 221 | add_definitions(-DOSMIUM_WITH_SPARSEHASH=${SPARSEHASH_FOUND}) 222 | list(APPEND OSMIUM_INCLUDE_DIRS ${SPARSEHASH_INCLUDE_DIR}) 223 | else() 224 | message(WARNING "Osmium: Disabled Google SparseHash library on 32bit system (size_type=${SPARSETABLE_SIZE_TYPE}).") 225 | endif() 226 | else() 227 | message(WARNING "Osmium: Google SparseHash library is required but not found, please install it or configure the paths.") 228 | endif() 229 | endif() 230 | 231 | #---------------------------------------------------------------------- 232 | 233 | list(REMOVE_DUPLICATES OSMIUM_INCLUDE_DIRS) 234 | 235 | if(OSMIUM_XML_LIBRARIES) 236 | list(REMOVE_DUPLICATES OSMIUM_XML_LIBRARIES) 237 | endif() 238 | 239 | if(OSMIUM_PBF_LIBRARIES) 240 | list(REMOVE_DUPLICATES OSMIUM_PBF_LIBRARIES) 241 | endif() 242 | 243 | if(OSMIUM_IO_LIBRARIES) 244 | list(REMOVE_DUPLICATES OSMIUM_IO_LIBRARIES) 245 | endif() 246 | 247 | if(OSMIUM_LIBRARIES) 248 | list(REMOVE_DUPLICATES OSMIUM_LIBRARIES) 249 | endif() 250 | 251 | #---------------------------------------------------------------------- 252 | # 253 | # Check that all required libraries are available 254 | # 255 | #---------------------------------------------------------------------- 256 | if (OSMIUM_EXTRA_FIND_VARS) 257 | list(REMOVE_DUPLICATES OSMIUM_EXTRA_FIND_VARS) 258 | endif() 259 | # Handle the QUIETLY and REQUIRED arguments and set OSMIUM_FOUND to TRUE if 260 | # all listed variables are TRUE. 261 | include(FindPackageHandleStandardArgs) 262 | find_package_handle_standard_args(Osmium REQUIRED_VARS OSMIUM_INCLUDE_DIR ${OSMIUM_EXTRA_FIND_VARS}) 263 | unset(OSMIUM_EXTRA_FIND_VARS) 264 | 265 | #---------------------------------------------------------------------- 266 | # 267 | # Add compiler flags 268 | # 269 | #---------------------------------------------------------------------- 270 | add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64) 271 | 272 | if(MSVC) 273 | add_definitions(-wd4996) 274 | 275 | # Disable warning C4068: "unknown pragma" because we want it to ignore 276 | # pragmas for other compilers. 277 | add_definitions(-wd4068) 278 | 279 | # Disable warning C4715: "not all control paths return a value" because 280 | # it generates too many false positives. 281 | add_definitions(-wd4715) 282 | 283 | # Disable warning C4351: new behavior: elements of array '...' will be 284 | # default initialized. The new behaviour is correct and we don't support 285 | # old compilers anyway. 286 | add_definitions(-wd4351) 287 | 288 | add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_WARNINGS) 289 | endif() 290 | 291 | if(APPLE) 292 | # following only available from cmake 2.8.12: 293 | # add_compile_options(-stdlib=libc++) 294 | # so using this instead: 295 | add_definitions(-stdlib=libc++) 296 | set(LDFLAGS ${LDFLAGS} -stdlib=libc++) 297 | endif() 298 | 299 | #---------------------------------------------------------------------- 300 | 301 | # This is a set of recommended warning options that can be added when compiling 302 | # libosmium code. 303 | if(MSVC) 304 | set(OSMIUM_WARNING_OPTIONS "/W3 /wd4514" CACHE STRING "Recommended warning options for libosmium") 305 | else() 306 | set(OSMIUM_WARNING_OPTIONS "-Wall -Wextra -pedantic -Wredundant-decls -Wdisabled-optimization -Wctor-dtor-privacy -Wnon-virtual-dtor -Woverloaded-virtual -Wsign-promo -Wold-style-cast" CACHE STRING "Recommended warning options for libosmium") 307 | endif() 308 | 309 | set(OSMIUM_DRACONIC_CLANG_OPTIONS "-Wdocumentation -Wunused-exception-parameter -Wmissing-declarations -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-unused-macros -Wno-exit-time-destructors -Wno-global-constructors -Wno-padded -Wno-switch-enum -Wno-missing-prototypes -Wno-weak-vtables -Wno-cast-align -Wno-float-equal") 310 | 311 | if(Osmium_DEBUG) 312 | message(STATUS "OSMIUM_XML_LIBRARIES=" ${OSMIUM_XML_LIBRARIES}) 313 | message(STATUS "OSMIUM_PBF_LIBRARIES=" ${OSMIUM_PBF_LIBRARIES}) 314 | message(STATUS "OSMIUM_IO_LIBRARIES=" ${OSMIUM_IO_LIBRARIES}) 315 | message(STATUS "OSMIUM_LIBRARIES=" ${OSMIUM_LIBRARIES}) 316 | message(STATUS "OSMIUM_INCLUDE_DIRS=" ${OSMIUM_INCLUDE_DIRS}) 317 | endif() 318 | 319 | -------------------------------------------------------------------------------- /cmake/common.cmake: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------- 2 | # 3 | # CMake config used in all projects in this repository. 4 | # 5 | #----------------------------------------------------------------------------- 6 | 7 | #----------------------------------------------------------------------------- 8 | # 9 | # Decide which C++ version to use (Minimum/default: C++11). 10 | # 11 | #----------------------------------------------------------------------------- 12 | 13 | if(NOT MSVC) 14 | if(NOT USE_CPP_VERSION) 15 | set(USE_CPP_VERSION c++11) 16 | endif() 17 | message(STATUS "Use C++ version: ${USE_CPP_VERSION}") 18 | # following only available from cmake 2.8.12: 19 | # add_compile_options(-std=${USE_CPP_VERSION}) 20 | # so using this instead: 21 | add_definitions(-std=${USE_CPP_VERSION}) 22 | endif() 23 | 24 | 25 | #----------------------------------------------------------------------------- 26 | # 27 | # Compiler and Linker flags 28 | # 29 | #----------------------------------------------------------------------------- 30 | if(MSVC) 31 | set(USUAL_COMPILE_OPTIONS "/Ox") 32 | else() 33 | set(USUAL_COMPILE_OPTIONS "-O3 -g") 34 | endif() 35 | 36 | if(WIN32) 37 | add_definitions(-DWIN32 -D_WIN32 -DMSWIN32 -DBGDWIN32 38 | -DWINVER=0x0500 -D_WIN32_WINNT=0x0500 -D_WIN32_IE=0x0600) 39 | endif() 40 | 41 | set(CMAKE_CXX_FLAGS_DEV "${USUAL_COMPILE_OPTIONS}" 42 | CACHE STRING "Flags used by the compiler during developer builds." 43 | FORCE) 44 | 45 | set(CMAKE_EXE_LINKER_FLAGS_DEV "" 46 | CACHE STRING "Flags used by the linker during developer builds." 47 | FORCE) 48 | mark_as_advanced( 49 | CMAKE_CXX_FLAGS_DEV 50 | CMAKE_EXE_LINKER_FLAGS_DEV 51 | ) 52 | 53 | set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${USUAL_COMPILE_OPTIONS}" 54 | CACHE STRING "Flags used by the compiler during RELWITHDEBINFO builds." 55 | FORCE) 56 | 57 | 58 | #----------------------------------------------------------------------------- 59 | # 60 | # Build Type 61 | # 62 | #----------------------------------------------------------------------------- 63 | set(CMAKE_CONFIGURATION_TYPES "Debug Release RelWithDebInfo MinSizeRel Dev") 64 | 65 | # In 'Dev' mode: compile with very strict warnings and turn them into errors. 66 | if(CMAKE_BUILD_TYPE STREQUAL "Dev") 67 | if(NOT MSVC) 68 | add_definitions(-Werror) 69 | endif() 70 | add_definitions(${OSMIUM_WARNING_OPTIONS}) 71 | endif() 72 | 73 | # Force RelWithDebInfo build type if none was given 74 | if(CMAKE_BUILD_TYPE) 75 | set(build_type ${CMAKE_BUILD_TYPE}) 76 | else() 77 | set(build_type "RelWithDebInfo") 78 | endif() 79 | 80 | set(CMAKE_BUILD_TYPE ${build_type} 81 | CACHE STRING 82 | "Choose the type of build, options are: ${CMAKE_CONFIGURATION_TYPES}." 83 | FORCE) 84 | 85 | 86 | #----------------------------------------------------------------------------- 87 | -------------------------------------------------------------------------------- /include/rapidjson/allocators.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ALLOCATORS_H_ 16 | #define RAPIDJSON_ALLOCATORS_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | 22 | /////////////////////////////////////////////////////////////////////////////// 23 | // Allocator 24 | 25 | /*! \class rapidjson::Allocator 26 | \brief Concept for allocating, resizing and freeing memory block. 27 | 28 | Note that Malloc() and Realloc() are non-static but Free() is static. 29 | 30 | So if an allocator need to support Free(), it needs to put its pointer in 31 | the header of memory block. 32 | 33 | \code 34 | concept Allocator { 35 | static const bool kNeedFree; //!< Whether this allocator needs to call Free(). 36 | 37 | // Allocate a memory block. 38 | // \param size of the memory block in bytes. 39 | // \returns pointer to the memory block. 40 | void* Malloc(size_t size); 41 | 42 | // Resize a memory block. 43 | // \param originalPtr The pointer to current memory block. Null pointer is permitted. 44 | // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) 45 | // \param newSize the new size in bytes. 46 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); 47 | 48 | // Free a memory block. 49 | // \param pointer to the memory block. Null pointer is permitted. 50 | static void Free(void *ptr); 51 | }; 52 | \endcode 53 | */ 54 | 55 | /////////////////////////////////////////////////////////////////////////////// 56 | // CrtAllocator 57 | 58 | //! C-runtime library allocator. 59 | /*! This class is just wrapper for standard C library memory routines. 60 | \note implements Allocator concept 61 | */ 62 | class CrtAllocator { 63 | public: 64 | static const bool kNeedFree = true; 65 | void* Malloc(size_t size) { 66 | if (size) // behavior of malloc(0) is implementation defined. 67 | return std::malloc(size); 68 | else 69 | return NULL; // standardize to returning NULL. 70 | } 71 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { 72 | (void)originalSize; 73 | if (newSize == 0) { 74 | std::free(originalPtr); 75 | return NULL; 76 | } 77 | return std::realloc(originalPtr, newSize); 78 | } 79 | static void Free(void *ptr) { std::free(ptr); } 80 | }; 81 | 82 | /////////////////////////////////////////////////////////////////////////////// 83 | // MemoryPoolAllocator 84 | 85 | //! Default memory allocator used by the parser and DOM. 86 | /*! This allocator allocate memory blocks from pre-allocated memory chunks. 87 | 88 | It does not free memory blocks. And Realloc() only allocate new memory. 89 | 90 | The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. 91 | 92 | User may also supply a buffer as the first chunk. 93 | 94 | If the user-buffer is full then additional chunks are allocated by BaseAllocator. 95 | 96 | The user-buffer is not deallocated by this allocator. 97 | 98 | \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. 99 | \note implements Allocator concept 100 | */ 101 | template 102 | class MemoryPoolAllocator { 103 | public: 104 | static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) 105 | 106 | //! Constructor with chunkSize. 107 | /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. 108 | \param baseAllocator The allocator for allocating memory chunks. 109 | */ 110 | MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : 111 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) 112 | { 113 | } 114 | 115 | //! Constructor with user-supplied buffer. 116 | /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. 117 | 118 | The user buffer will not be deallocated when this allocator is destructed. 119 | 120 | \param buffer User supplied buffer. 121 | \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). 122 | \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. 123 | \param baseAllocator The allocator for allocating memory chunks. 124 | */ 125 | MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : 126 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) 127 | { 128 | RAPIDJSON_ASSERT(buffer != 0); 129 | RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); 130 | chunkHead_ = reinterpret_cast(buffer); 131 | chunkHead_->capacity = size - sizeof(ChunkHeader); 132 | chunkHead_->size = 0; 133 | chunkHead_->next = 0; 134 | } 135 | 136 | //! Destructor. 137 | /*! This deallocates all memory chunks, excluding the user-supplied buffer. 138 | */ 139 | ~MemoryPoolAllocator() { 140 | Clear(); 141 | RAPIDJSON_DELETE(ownBaseAllocator_); 142 | } 143 | 144 | //! Deallocates all memory chunks, excluding the user-supplied buffer. 145 | void Clear() { 146 | while (chunkHead_ && chunkHead_ != userBuffer_) { 147 | ChunkHeader* next = chunkHead_->next; 148 | baseAllocator_->Free(chunkHead_); 149 | chunkHead_ = next; 150 | } 151 | if (chunkHead_ && chunkHead_ == userBuffer_) 152 | chunkHead_->size = 0; // Clear user buffer 153 | } 154 | 155 | //! Computes the total capacity of allocated memory chunks. 156 | /*! \return total capacity in bytes. 157 | */ 158 | size_t Capacity() const { 159 | size_t capacity = 0; 160 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) 161 | capacity += c->capacity; 162 | return capacity; 163 | } 164 | 165 | //! Computes the memory blocks allocated. 166 | /*! \return total used bytes. 167 | */ 168 | size_t Size() const { 169 | size_t size = 0; 170 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) 171 | size += c->size; 172 | return size; 173 | } 174 | 175 | //! Allocates a memory block. (concept Allocator) 176 | void* Malloc(size_t size) { 177 | if (!size) 178 | return NULL; 179 | 180 | size = RAPIDJSON_ALIGN(size); 181 | if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) 182 | AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size); 183 | 184 | void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; 185 | chunkHead_->size += size; 186 | return buffer; 187 | } 188 | 189 | //! Resizes a memory block (concept Allocator) 190 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { 191 | if (originalPtr == 0) 192 | return Malloc(newSize); 193 | 194 | if (newSize == 0) 195 | return NULL; 196 | 197 | // Do not shrink if new size is smaller than original 198 | if (originalSize >= newSize) 199 | return originalPtr; 200 | 201 | // Simply expand it if it is the last allocation and there is sufficient space 202 | if (originalPtr == (char *)(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { 203 | size_t increment = static_cast(newSize - originalSize); 204 | increment = RAPIDJSON_ALIGN(increment); 205 | if (chunkHead_->size + increment <= chunkHead_->capacity) { 206 | chunkHead_->size += increment; 207 | return originalPtr; 208 | } 209 | } 210 | 211 | // Realloc process: allocate and copy memory, do not free original buffer. 212 | void* newBuffer = Malloc(newSize); 213 | RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly. 214 | if (originalSize) 215 | std::memcpy(newBuffer, originalPtr, originalSize); 216 | return newBuffer; 217 | } 218 | 219 | //! Frees a memory block (concept Allocator) 220 | static void Free(void *ptr) { (void)ptr; } // Do nothing 221 | 222 | private: 223 | //! Copy constructor is not permitted. 224 | MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; 225 | //! Copy assignment operator is not permitted. 226 | MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; 227 | 228 | //! Creates a new chunk. 229 | /*! \param capacity Capacity of the chunk in bytes. 230 | */ 231 | void AddChunk(size_t capacity) { 232 | if (!baseAllocator_) 233 | ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); 234 | ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity)); 235 | chunk->capacity = capacity; 236 | chunk->size = 0; 237 | chunk->next = chunkHead_; 238 | chunkHead_ = chunk; 239 | } 240 | 241 | static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. 242 | 243 | //! Chunk header for perpending to each chunk. 244 | /*! Chunks are stored as a singly linked list. 245 | */ 246 | struct ChunkHeader { 247 | size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). 248 | size_t size; //!< Current size of allocated memory in bytes. 249 | ChunkHeader *next; //!< Next chunk in the linked list. 250 | }; 251 | 252 | ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. 253 | size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. 254 | void *userBuffer_; //!< User supplied buffer. 255 | BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. 256 | BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. 257 | }; 258 | 259 | RAPIDJSON_NAMESPACE_END 260 | 261 | #endif // RAPIDJSON_ENCODINGS_H_ 262 | -------------------------------------------------------------------------------- /include/rapidjson/encodedstream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ENCODEDSTREAM_H_ 16 | #define RAPIDJSON_ENCODEDSTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | #ifdef __GNUC__ 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(effc++) 23 | #endif 24 | 25 | RAPIDJSON_NAMESPACE_BEGIN 26 | 27 | //! Input byte stream wrapper with a statically bound encoding. 28 | /*! 29 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. 30 | \tparam InputByteStream Type of input byte stream. For example, FileReadStream. 31 | */ 32 | template 33 | class EncodedInputStream { 34 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 35 | public: 36 | typedef typename Encoding::Ch Ch; 37 | 38 | EncodedInputStream(InputByteStream& is) : is_(is) { 39 | current_ = Encoding::TakeBOM(is_); 40 | } 41 | 42 | Ch Peek() const { return current_; } 43 | Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } 44 | size_t Tell() const { return is_.Tell(); } 45 | 46 | // Not implemented 47 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 48 | void Flush() { RAPIDJSON_ASSERT(false); } 49 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 50 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 51 | 52 | private: 53 | EncodedInputStream(const EncodedInputStream&); 54 | EncodedInputStream& operator=(const EncodedInputStream&); 55 | 56 | InputByteStream& is_; 57 | Ch current_; 58 | }; 59 | 60 | //! Output byte stream wrapper with statically bound encoding. 61 | /*! 62 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. 63 | \tparam InputByteStream Type of input byte stream. For example, FileWriteStream. 64 | */ 65 | template 66 | class EncodedOutputStream { 67 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 68 | public: 69 | typedef typename Encoding::Ch Ch; 70 | 71 | EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { 72 | if (putBOM) 73 | Encoding::PutBOM(os_); 74 | } 75 | 76 | void Put(Ch c) { Encoding::Put(os_, c); } 77 | void Flush() { os_.Flush(); } 78 | 79 | // Not implemented 80 | Ch Peek() const { RAPIDJSON_ASSERT(false); } 81 | Ch Take() { RAPIDJSON_ASSERT(false); } 82 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 83 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 84 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 85 | 86 | private: 87 | EncodedOutputStream(const EncodedOutputStream&); 88 | EncodedOutputStream& operator=(const EncodedOutputStream&); 89 | 90 | OutputByteStream& os_; 91 | }; 92 | 93 | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x 94 | 95 | //! Input stream wrapper with dynamically bound encoding and automatic encoding detection. 96 | /*! 97 | \tparam CharType Type of character for reading. 98 | \tparam InputByteStream type of input byte stream to be wrapped. 99 | */ 100 | template 101 | class AutoUTFInputStream { 102 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 103 | public: 104 | typedef CharType Ch; 105 | 106 | //! Constructor. 107 | /*! 108 | \param is input stream to be wrapped. 109 | \param type UTF encoding type if it is not detected from the stream. 110 | */ 111 | AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { 112 | RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); 113 | DetectType(); 114 | static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; 115 | takeFunc_ = f[type_]; 116 | current_ = takeFunc_(*is_); 117 | } 118 | 119 | UTFType GetType() const { return type_; } 120 | bool HasBOM() const { return hasBOM_; } 121 | 122 | Ch Peek() const { return current_; } 123 | Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } 124 | size_t Tell() const { return is_->Tell(); } 125 | 126 | // Not implemented 127 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 128 | void Flush() { RAPIDJSON_ASSERT(false); } 129 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 130 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 131 | 132 | private: 133 | AutoUTFInputStream(const AutoUTFInputStream&); 134 | AutoUTFInputStream& operator=(const AutoUTFInputStream&); 135 | 136 | // Detect encoding type with BOM or RFC 4627 137 | void DetectType() { 138 | // BOM (Byte Order Mark): 139 | // 00 00 FE FF UTF-32BE 140 | // FF FE 00 00 UTF-32LE 141 | // FE FF UTF-16BE 142 | // FF FE UTF-16LE 143 | // EF BB BF UTF-8 144 | 145 | const unsigned char* c = (const unsigned char *)is_->Peek4(); 146 | if (!c) 147 | return; 148 | 149 | unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24); 150 | hasBOM_ = false; 151 | if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } 152 | else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } 153 | else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } 154 | else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } 155 | else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } 156 | 157 | // RFC 4627: Section 3 158 | // "Since the first two characters of a JSON text will always be ASCII 159 | // characters [RFC0020], it is possible to determine whether an octet 160 | // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking 161 | // at the pattern of nulls in the first four octets." 162 | // 00 00 00 xx UTF-32BE 163 | // 00 xx 00 xx UTF-16BE 164 | // xx 00 00 00 UTF-32LE 165 | // xx 00 xx 00 UTF-16LE 166 | // xx xx xx xx UTF-8 167 | 168 | if (!hasBOM_) { 169 | unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); 170 | switch (pattern) { 171 | case 0x08: type_ = kUTF32BE; break; 172 | case 0x0A: type_ = kUTF16BE; break; 173 | case 0x01: type_ = kUTF32LE; break; 174 | case 0x05: type_ = kUTF16LE; break; 175 | case 0x0F: type_ = kUTF8; break; 176 | default: break; // Use type defined by user. 177 | } 178 | } 179 | 180 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. 181 | if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); 182 | if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); 183 | } 184 | 185 | typedef Ch (*TakeFunc)(InputByteStream& is); 186 | InputByteStream* is_; 187 | UTFType type_; 188 | Ch current_; 189 | TakeFunc takeFunc_; 190 | bool hasBOM_; 191 | }; 192 | 193 | //! Output stream wrapper with dynamically bound encoding and automatic encoding detection. 194 | /*! 195 | \tparam CharType Type of character for writing. 196 | \tparam InputByteStream type of output byte stream to be wrapped. 197 | */ 198 | template 199 | class AutoUTFOutputStream { 200 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 201 | public: 202 | typedef CharType Ch; 203 | 204 | //! Constructor. 205 | /*! 206 | \param os output stream to be wrapped. 207 | \param type UTF encoding type. 208 | \param putBOM Whether to write BOM at the beginning of the stream. 209 | */ 210 | AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { 211 | RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); 212 | 213 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. 214 | if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); 215 | if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); 216 | 217 | static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; 218 | putFunc_ = f[type_]; 219 | 220 | if (putBOM) 221 | PutBOM(); 222 | } 223 | 224 | UTFType GetType() const { return type_; } 225 | 226 | void Put(Ch c) { putFunc_(*os_, c); } 227 | void Flush() { os_->Flush(); } 228 | 229 | // Not implemented 230 | Ch Peek() const { RAPIDJSON_ASSERT(false); } 231 | Ch Take() { RAPIDJSON_ASSERT(false); } 232 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 233 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 234 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 235 | 236 | private: 237 | AutoUTFOutputStream(const AutoUTFOutputStream&); 238 | AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); 239 | 240 | void PutBOM() { 241 | typedef void (*PutBOMFunc)(OutputByteStream&); 242 | static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; 243 | f[type_](*os_); 244 | } 245 | 246 | typedef void (*PutFunc)(OutputByteStream&, Ch); 247 | 248 | OutputByteStream* os_; 249 | UTFType type_; 250 | PutFunc putFunc_; 251 | }; 252 | 253 | #undef RAPIDJSON_ENCODINGS_FUNC 254 | 255 | RAPIDJSON_NAMESPACE_END 256 | 257 | #ifdef __GNUC__ 258 | RAPIDJSON_DIAG_POP 259 | #endif 260 | 261 | #endif // RAPIDJSON_FILESTREAM_H_ 262 | -------------------------------------------------------------------------------- /include/rapidjson/error/en.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ERROR_EN_H__ 16 | #define RAPIDJSON_ERROR_EN_H__ 17 | 18 | #include "error.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | 22 | //! Maps error code of parsing into error message. 23 | /*! 24 | \ingroup RAPIDJSON_ERRORS 25 | \param parseErrorCode Error code obtained in parsing. 26 | \return the error message. 27 | \note User can make a copy of this function for localization. 28 | Using switch-case is safer for future modification of error codes. 29 | */ 30 | inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { 31 | switch (parseErrorCode) { 32 | case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); 33 | 34 | case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); 35 | case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values."); 36 | 37 | case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); 38 | 39 | case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); 40 | case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); 41 | case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); 42 | 43 | case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); 44 | 45 | case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); 46 | case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); 47 | case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); 48 | case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); 49 | case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); 50 | 51 | case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); 52 | case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); 53 | case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); 54 | 55 | case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); 56 | case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); 57 | 58 | default: 59 | return RAPIDJSON_ERROR_STRING("Unknown error."); 60 | } 61 | } 62 | 63 | RAPIDJSON_NAMESPACE_END 64 | 65 | #endif // RAPIDJSON_ERROR_EN_H__ 66 | -------------------------------------------------------------------------------- /include/rapidjson/error/error.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ERROR_ERROR_H__ 16 | #define RAPIDJSON_ERROR_ERROR_H__ 17 | 18 | #include "../rapidjson.h" 19 | 20 | /*! \file error.h */ 21 | 22 | /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ 23 | 24 | /////////////////////////////////////////////////////////////////////////////// 25 | // RAPIDJSON_ERROR_CHARTYPE 26 | 27 | //! Character type of error messages. 28 | /*! \ingroup RAPIDJSON_ERRORS 29 | The default character type is \c char. 30 | On Windows, user can define this macro as \c TCHAR for supporting both 31 | unicode/non-unicode settings. 32 | */ 33 | #ifndef RAPIDJSON_ERROR_CHARTYPE 34 | #define RAPIDJSON_ERROR_CHARTYPE char 35 | #endif 36 | 37 | /////////////////////////////////////////////////////////////////////////////// 38 | // RAPIDJSON_ERROR_STRING 39 | 40 | //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. 41 | /*! \ingroup RAPIDJSON_ERRORS 42 | By default this conversion macro does nothing. 43 | On Windows, user can define this macro as \c _T(x) for supporting both 44 | unicode/non-unicode settings. 45 | */ 46 | #ifndef RAPIDJSON_ERROR_STRING 47 | #define RAPIDJSON_ERROR_STRING(x) x 48 | #endif 49 | 50 | RAPIDJSON_NAMESPACE_BEGIN 51 | 52 | /////////////////////////////////////////////////////////////////////////////// 53 | // ParseErrorCode 54 | 55 | //! Error code of parsing. 56 | /*! \ingroup RAPIDJSON_ERRORS 57 | \see GenericReader::Parse, GenericReader::GetParseErrorCode 58 | */ 59 | enum ParseErrorCode { 60 | kParseErrorNone = 0, //!< No error. 61 | 62 | kParseErrorDocumentEmpty, //!< The document is empty. 63 | kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. 64 | 65 | kParseErrorValueInvalid, //!< Invalid value. 66 | 67 | kParseErrorObjectMissName, //!< Missing a name for object member. 68 | kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. 69 | kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. 70 | 71 | kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. 72 | 73 | kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. 74 | kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. 75 | kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. 76 | kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. 77 | kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. 78 | 79 | kParseErrorNumberTooBig, //!< Number too big to be stored in double. 80 | kParseErrorNumberMissFraction, //!< Miss fraction part in number. 81 | kParseErrorNumberMissExponent, //!< Miss exponent in number. 82 | 83 | kParseErrorTermination, //!< Parsing was terminated. 84 | kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. 85 | }; 86 | 87 | //! Result of parsing (wraps ParseErrorCode) 88 | /*! 89 | \ingroup RAPIDJSON_ERRORS 90 | \code 91 | Document doc; 92 | ParseResult ok = doc.Parse("[42]"); 93 | if (!ok) { 94 | fprintf(stderr, "JSON parse error: %s (%u)", 95 | GetParseError_En(ok.Code()), ok.Offset()); 96 | exit(EXIT_FAILURE); 97 | } 98 | \endcode 99 | \see GenericReader::Parse, GenericDocument::Parse 100 | */ 101 | struct ParseResult { 102 | 103 | //! Default constructor, no error. 104 | ParseResult() : code_(kParseErrorNone), offset_(0) {} 105 | //! Constructor to set an error. 106 | ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} 107 | 108 | //! Get the error code. 109 | ParseErrorCode Code() const { return code_; } 110 | //! Get the error offset, if \ref IsError(), 0 otherwise. 111 | size_t Offset() const { return offset_; } 112 | 113 | //! Conversion to \c bool, returns \c true, iff !\ref IsError(). 114 | operator bool() const { return !IsError(); } 115 | //! Whether the result is an error. 116 | bool IsError() const { return code_ != kParseErrorNone; } 117 | 118 | bool operator==(const ParseResult& that) const { return code_ == that.code_; } 119 | bool operator==(ParseErrorCode code) const { return code_ == code; } 120 | friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } 121 | 122 | //! Reset error code. 123 | void Clear() { Set(kParseErrorNone); } 124 | //! Update error code and offset. 125 | void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } 126 | 127 | private: 128 | ParseErrorCode code_; 129 | size_t offset_; 130 | }; 131 | 132 | //! Function pointer type of GetParseError(). 133 | /*! \ingroup RAPIDJSON_ERRORS 134 | 135 | This is the prototype for \c GetParseError_X(), where \c X is a locale. 136 | User can dynamically change locale in runtime, e.g.: 137 | \code 138 | GetParseErrorFunc GetParseError = GetParseError_En; // or whatever 139 | const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); 140 | \endcode 141 | */ 142 | typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); 143 | 144 | RAPIDJSON_NAMESPACE_END 145 | 146 | #endif // RAPIDJSON_ERROR_ERROR_H__ 147 | -------------------------------------------------------------------------------- /include/rapidjson/filereadstream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_FILEREADSTREAM_H_ 16 | #define RAPIDJSON_FILEREADSTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | #include 20 | 21 | RAPIDJSON_NAMESPACE_BEGIN 22 | 23 | //! File byte stream for input using fread(). 24 | /*! 25 | \note implements Stream concept 26 | */ 27 | class FileReadStream { 28 | public: 29 | typedef char Ch; //!< Character type (byte). 30 | 31 | //! Constructor. 32 | /*! 33 | \param fp File pointer opened for read. 34 | \param buffer user-supplied buffer. 35 | \param bufferSize size of buffer in bytes. Must >=4 bytes. 36 | */ 37 | FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { 38 | RAPIDJSON_ASSERT(fp_ != 0); 39 | RAPIDJSON_ASSERT(bufferSize >= 4); 40 | Read(); 41 | } 42 | 43 | Ch Peek() const { return *current_; } 44 | Ch Take() { Ch c = *current_; Read(); return c; } 45 | size_t Tell() const { return count_ + static_cast(current_ - buffer_); } 46 | 47 | // Not implemented 48 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 49 | void Flush() { RAPIDJSON_ASSERT(false); } 50 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 51 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 52 | 53 | // For encoding detection only. 54 | const Ch* Peek4() const { 55 | return (current_ + 4 <= bufferLast_) ? current_ : 0; 56 | } 57 | 58 | private: 59 | void Read() { 60 | if (current_ < bufferLast_) 61 | ++current_; 62 | else if (!eof_) { 63 | count_ += readCount_; 64 | readCount_ = fread(buffer_, 1, bufferSize_, fp_); 65 | bufferLast_ = buffer_ + readCount_ - 1; 66 | current_ = buffer_; 67 | 68 | if (readCount_ < bufferSize_) { 69 | buffer_[readCount_] = '\0'; 70 | ++bufferLast_; 71 | eof_ = true; 72 | } 73 | } 74 | } 75 | 76 | std::FILE* fp_; 77 | Ch *buffer_; 78 | size_t bufferSize_; 79 | Ch *bufferLast_; 80 | Ch *current_; 81 | size_t readCount_; 82 | size_t count_; //!< Number of characters read 83 | bool eof_; 84 | }; 85 | 86 | RAPIDJSON_NAMESPACE_END 87 | 88 | #endif // RAPIDJSON_FILESTREAM_H_ 89 | -------------------------------------------------------------------------------- /include/rapidjson/filewritestream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_FILEWRITESTREAM_H_ 16 | #define RAPIDJSON_FILEWRITESTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | #include 20 | 21 | RAPIDJSON_NAMESPACE_BEGIN 22 | 23 | //! Wrapper of C file stream for input using fread(). 24 | /*! 25 | \note implements Stream concept 26 | */ 27 | class FileWriteStream { 28 | public: 29 | typedef char Ch; //!< Character type. Only support char. 30 | 31 | FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { 32 | RAPIDJSON_ASSERT(fp_ != 0); 33 | } 34 | 35 | void Put(char c) { 36 | if (current_ >= bufferEnd_) 37 | Flush(); 38 | 39 | *current_++ = c; 40 | } 41 | 42 | void PutN(char c, size_t n) { 43 | size_t avail = static_cast(bufferEnd_ - current_); 44 | while (n > avail) { 45 | std::memset(current_, c, avail); 46 | current_ += avail; 47 | Flush(); 48 | n -= avail; 49 | avail = static_cast(bufferEnd_ - current_); 50 | } 51 | 52 | if (n > 0) { 53 | std::memset(current_, c, n); 54 | current_ += n; 55 | } 56 | } 57 | 58 | void Flush() { 59 | if (current_ != buffer_) { 60 | fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); 61 | current_ = buffer_; 62 | } 63 | } 64 | 65 | // Not implemented 66 | char Peek() const { RAPIDJSON_ASSERT(false); return 0; } 67 | char Take() { RAPIDJSON_ASSERT(false); return 0; } 68 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 69 | char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 70 | size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } 71 | 72 | private: 73 | // Prohibit copy constructor & assignment operator. 74 | FileWriteStream(const FileWriteStream&); 75 | FileWriteStream& operator=(const FileWriteStream&); 76 | 77 | std::FILE* fp_; 78 | char *buffer_; 79 | char *bufferEnd_; 80 | char *current_; 81 | }; 82 | 83 | //! Implement specialized version of PutN() with memset() for better performance. 84 | template<> 85 | inline void PutN(FileWriteStream& stream, char c, size_t n) { 86 | stream.PutN(c, n); 87 | } 88 | 89 | RAPIDJSON_NAMESPACE_END 90 | 91 | #endif // RAPIDJSON_FILESTREAM_H_ 92 | -------------------------------------------------------------------------------- /include/rapidjson/internal/biginteger.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_BIGINTEGER_H_ 16 | #define RAPIDJSON_BIGINTEGER_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | #if defined(_MSC_VER) && defined(_M_AMD64) 21 | #include // for _umul128 22 | #endif 23 | 24 | RAPIDJSON_NAMESPACE_BEGIN 25 | namespace internal { 26 | 27 | class BigInteger { 28 | public: 29 | typedef uint64_t Type; 30 | 31 | BigInteger(const BigInteger& rhs) : count_(rhs.count_) { 32 | std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); 33 | } 34 | 35 | explicit BigInteger(uint64_t u) : count_(1) { 36 | digits_[0] = u; 37 | } 38 | 39 | BigInteger(const char* decimals, size_t length) : count_(1) { 40 | RAPIDJSON_ASSERT(length > 0); 41 | digits_[0] = 0; 42 | size_t i = 0; 43 | const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 44 | while (length >= kMaxDigitPerIteration) { 45 | AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); 46 | length -= kMaxDigitPerIteration; 47 | i += kMaxDigitPerIteration; 48 | } 49 | 50 | if (length > 0) 51 | AppendDecimal64(decimals + i, decimals + i + length); 52 | } 53 | 54 | BigInteger& operator=(uint64_t u) { 55 | digits_[0] = u; 56 | count_ = 1; 57 | return *this; 58 | } 59 | 60 | BigInteger& operator+=(uint64_t u) { 61 | Type backup = digits_[0]; 62 | digits_[0] += u; 63 | for (size_t i = 0; i < count_ - 1; i++) { 64 | if (digits_[i] >= backup) 65 | return *this; // no carry 66 | backup = digits_[i + 1]; 67 | digits_[i + 1] += 1; 68 | } 69 | 70 | // Last carry 71 | if (digits_[count_ - 1] < backup) 72 | PushBack(1); 73 | 74 | return *this; 75 | } 76 | 77 | BigInteger& operator*=(uint64_t u) { 78 | if (u == 0) return *this = 0; 79 | if (u == 1) return *this; 80 | if (*this == 1) return *this = u; 81 | 82 | uint64_t k = 0; 83 | for (size_t i = 0; i < count_; i++) { 84 | uint64_t hi; 85 | digits_[i] = MulAdd64(digits_[i], u, k, &hi); 86 | k = hi; 87 | } 88 | 89 | if (k > 0) 90 | PushBack(k); 91 | 92 | return *this; 93 | } 94 | 95 | BigInteger& operator*=(uint32_t u) { 96 | if (u == 0) return *this = 0; 97 | if (u == 1) return *this; 98 | if (*this == 1) return *this = u; 99 | 100 | uint64_t k = 0; 101 | for (size_t i = 0; i < count_; i++) { 102 | const uint64_t c = digits_[i] >> 32; 103 | const uint64_t d = digits_[i] & 0xFFFFFFFF; 104 | const uint64_t uc = u * c; 105 | const uint64_t ud = u * d; 106 | const uint64_t p0 = ud + k; 107 | const uint64_t p1 = uc + (p0 >> 32); 108 | digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); 109 | k = p1 >> 32; 110 | } 111 | 112 | if (k > 0) 113 | PushBack(k); 114 | 115 | return *this; 116 | } 117 | 118 | BigInteger& operator<<=(size_t shift) { 119 | if (IsZero() || shift == 0) return *this; 120 | 121 | size_t offset = shift / kTypeBit; 122 | size_t interShift = shift % kTypeBit; 123 | RAPIDJSON_ASSERT(count_ + offset <= kCapacity); 124 | 125 | if (interShift == 0) { 126 | std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type)); 127 | count_ += offset; 128 | } 129 | else { 130 | digits_[count_] = 0; 131 | for (size_t i = count_; i > 0; i--) 132 | digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); 133 | digits_[offset] = digits_[0] << interShift; 134 | count_ += offset; 135 | if (digits_[count_]) 136 | count_++; 137 | } 138 | 139 | std::memset(digits_, 0, offset * sizeof(Type)); 140 | 141 | return *this; 142 | } 143 | 144 | bool operator==(const BigInteger& rhs) const { 145 | return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; 146 | } 147 | 148 | bool operator==(const Type rhs) const { 149 | return count_ == 1 && digits_[0] == rhs; 150 | } 151 | 152 | BigInteger& MultiplyPow5(unsigned exp) { 153 | static const uint32_t kPow5[12] = { 154 | 5, 155 | 5 * 5, 156 | 5 * 5 * 5, 157 | 5 * 5 * 5 * 5, 158 | 5 * 5 * 5 * 5 * 5, 159 | 5 * 5 * 5 * 5 * 5 * 5, 160 | 5 * 5 * 5 * 5 * 5 * 5 * 5, 161 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 162 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 163 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 164 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 165 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 166 | }; 167 | if (exp == 0) return *this; 168 | for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 169 | for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 170 | if (exp > 0) *this *= kPow5[exp - 1]; 171 | return *this; 172 | } 173 | 174 | // Compute absolute difference of this and rhs. 175 | // Assume this != rhs 176 | bool Difference(const BigInteger& rhs, BigInteger* out) const { 177 | int cmp = Compare(rhs); 178 | RAPIDJSON_ASSERT(cmp != 0); 179 | const BigInteger *a, *b; // Makes a > b 180 | bool ret; 181 | if (cmp < 0) { a = &rhs; b = this; ret = true; } 182 | else { a = this; b = &rhs; ret = false; } 183 | 184 | Type borrow = 0; 185 | for (size_t i = 0; i < a->count_; i++) { 186 | Type d = a->digits_[i] - borrow; 187 | if (i < b->count_) 188 | d -= b->digits_[i]; 189 | borrow = (d > a->digits_[i]) ? 1 : 0; 190 | out->digits_[i] = d; 191 | if (d != 0) 192 | out->count_ = i + 1; 193 | } 194 | 195 | return ret; 196 | } 197 | 198 | int Compare(const BigInteger& rhs) const { 199 | if (count_ != rhs.count_) 200 | return count_ < rhs.count_ ? -1 : 1; 201 | 202 | for (size_t i = count_; i-- > 0;) 203 | if (digits_[i] != rhs.digits_[i]) 204 | return digits_[i] < rhs.digits_[i] ? -1 : 1; 205 | 206 | return 0; 207 | } 208 | 209 | size_t GetCount() const { return count_; } 210 | Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } 211 | bool IsZero() const { return count_ == 1 && digits_[0] == 0; } 212 | 213 | private: 214 | void AppendDecimal64(const char* begin, const char* end) { 215 | uint64_t u = ParseUint64(begin, end); 216 | if (IsZero()) 217 | *this = u; 218 | else { 219 | unsigned exp = static_cast(end - begin); 220 | (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u 221 | } 222 | } 223 | 224 | void PushBack(Type digit) { 225 | RAPIDJSON_ASSERT(count_ < kCapacity); 226 | digits_[count_++] = digit; 227 | } 228 | 229 | static uint64_t ParseUint64(const char* begin, const char* end) { 230 | uint64_t r = 0; 231 | for (const char* p = begin; p != end; ++p) { 232 | RAPIDJSON_ASSERT(*p >= '0' && *p <= '9'); 233 | r = r * 10 + (*p - '0'); 234 | } 235 | return r; 236 | } 237 | 238 | // Assume a * b + k < 2^128 239 | static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { 240 | #if defined(_MSC_VER) && defined(_M_AMD64) 241 | uint64_t low = _umul128(a, b, outHigh) + k; 242 | if (low < k) 243 | (*outHigh)++; 244 | return low; 245 | #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) 246 | __extension__ typedef unsigned __int128 uint128; 247 | uint128 p = static_cast(a) * static_cast(b); 248 | p += k; 249 | *outHigh = static_cast(p >> 64); 250 | return static_cast(p); 251 | #else 252 | const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; 253 | uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; 254 | x1 += (x0 >> 32); // can't give carry 255 | x1 += x2; 256 | if (x1 < x2) 257 | x3 += (static_cast(1) << 32); 258 | uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); 259 | uint64_t hi = x3 + (x1 >> 32); 260 | 261 | lo += k; 262 | if (lo < k) 263 | hi++; 264 | *outHigh = hi; 265 | return lo; 266 | #endif 267 | } 268 | 269 | static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 270 | static const size_t kCapacity = kBitCount / sizeof(Type); 271 | static const size_t kTypeBit = sizeof(Type) * 8; 272 | 273 | Type digits_[kCapacity]; 274 | size_t count_; 275 | }; 276 | 277 | } // namespace internal 278 | RAPIDJSON_NAMESPACE_END 279 | 280 | #endif // RAPIDJSON_BIGINTEGER_H_ 281 | -------------------------------------------------------------------------------- /include/rapidjson/internal/diyfp.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | // This is a C++ header-only implementation of Grisu2 algorithm from the publication: 16 | // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with 17 | // integers." ACM Sigplan Notices 45.6 (2010): 233-243. 18 | 19 | #ifndef RAPIDJSON_DIYFP_H_ 20 | #define RAPIDJSON_DIYFP_H_ 21 | 22 | #include "../rapidjson.h" 23 | 24 | #if defined(_MSC_VER) && defined(_M_AMD64) 25 | #include 26 | #pragma intrinsic(_BitScanReverse64) 27 | #endif 28 | 29 | RAPIDJSON_NAMESPACE_BEGIN 30 | namespace internal { 31 | 32 | #ifdef __GNUC__ 33 | RAPIDJSON_DIAG_PUSH 34 | RAPIDJSON_DIAG_OFF(effc++) 35 | #endif 36 | 37 | struct DiyFp { 38 | DiyFp() {} 39 | 40 | DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} 41 | 42 | explicit DiyFp(double d) { 43 | union { 44 | double d; 45 | uint64_t u64; 46 | } u = { d }; 47 | 48 | int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); 49 | uint64_t significand = (u.u64 & kDpSignificandMask); 50 | if (biased_e != 0) { 51 | f = significand + kDpHiddenBit; 52 | e = biased_e - kDpExponentBias; 53 | } 54 | else { 55 | f = significand; 56 | e = kDpMinExponent + 1; 57 | } 58 | } 59 | 60 | DiyFp operator-(const DiyFp& rhs) const { 61 | return DiyFp(f - rhs.f, e); 62 | } 63 | 64 | DiyFp operator*(const DiyFp& rhs) const { 65 | #if defined(_MSC_VER) && defined(_M_AMD64) 66 | uint64_t h; 67 | uint64_t l = _umul128(f, rhs.f, &h); 68 | if (l & (uint64_t(1) << 63)) // rounding 69 | h++; 70 | return DiyFp(h, e + rhs.e + 64); 71 | #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) 72 | __extension__ typedef unsigned __int128 uint128; 73 | uint128 p = static_cast(f) * static_cast(rhs.f); 74 | uint64_t h = static_cast(p >> 64); 75 | uint64_t l = static_cast(p); 76 | if (l & (uint64_t(1) << 63)) // rounding 77 | h++; 78 | return DiyFp(h, e + rhs.e + 64); 79 | #else 80 | const uint64_t M32 = 0xFFFFFFFF; 81 | const uint64_t a = f >> 32; 82 | const uint64_t b = f & M32; 83 | const uint64_t c = rhs.f >> 32; 84 | const uint64_t d = rhs.f & M32; 85 | const uint64_t ac = a * c; 86 | const uint64_t bc = b * c; 87 | const uint64_t ad = a * d; 88 | const uint64_t bd = b * d; 89 | uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); 90 | tmp += 1U << 31; /// mult_round 91 | return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); 92 | #endif 93 | } 94 | 95 | DiyFp Normalize() const { 96 | #if defined(_MSC_VER) && defined(_M_AMD64) 97 | unsigned long index; 98 | _BitScanReverse64(&index, f); 99 | return DiyFp(f << (63 - index), e - (63 - index)); 100 | #elif defined(__GNUC__) && __GNUC__ >= 4 101 | int s = __builtin_clzll(f); 102 | return DiyFp(f << s, e - s); 103 | #else 104 | DiyFp res = *this; 105 | while (!(res.f & (static_cast(1) << 63))) { 106 | res.f <<= 1; 107 | res.e--; 108 | } 109 | return res; 110 | #endif 111 | } 112 | 113 | DiyFp NormalizeBoundary() const { 114 | DiyFp res = *this; 115 | while (!(res.f & (kDpHiddenBit << 1))) { 116 | res.f <<= 1; 117 | res.e--; 118 | } 119 | res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); 120 | res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); 121 | return res; 122 | } 123 | 124 | void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { 125 | DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); 126 | DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); 127 | mi.f <<= mi.e - pl.e; 128 | mi.e = pl.e; 129 | *plus = pl; 130 | *minus = mi; 131 | } 132 | 133 | double ToDouble() const { 134 | union { 135 | double d; 136 | uint64_t u64; 137 | }u; 138 | const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : 139 | static_cast(e + kDpExponentBias); 140 | u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); 141 | return u.d; 142 | } 143 | 144 | static const int kDiySignificandSize = 64; 145 | static const int kDpSignificandSize = 52; 146 | static const int kDpExponentBias = 0x3FF + kDpSignificandSize; 147 | static const int kDpMaxExponent = 0x7FF - kDpExponentBias; 148 | static const int kDpMinExponent = -kDpExponentBias; 149 | static const int kDpDenormalExponent = -kDpExponentBias + 1; 150 | static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); 151 | static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); 152 | static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); 153 | 154 | uint64_t f; 155 | int e; 156 | }; 157 | 158 | inline DiyFp GetCachedPowerByIndex(size_t index) { 159 | // 10^-348, 10^-340, ..., 10^340 160 | static const uint64_t kCachedPowers_F[] = { 161 | RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), 162 | RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), 163 | RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), 164 | RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), 165 | RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), 166 | RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), 167 | RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), 168 | RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), 169 | RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), 170 | RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), 171 | RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), 172 | RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), 173 | RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), 174 | RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), 175 | RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), 176 | RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), 177 | RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), 178 | RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), 179 | RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), 180 | RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), 181 | RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), 182 | RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), 183 | RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), 184 | RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), 185 | RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), 186 | RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), 187 | RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), 188 | RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), 189 | RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), 190 | RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), 191 | RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), 192 | RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), 193 | RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), 194 | RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), 195 | RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), 196 | RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), 197 | RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), 198 | RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), 199 | RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), 200 | RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), 201 | RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), 202 | RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), 203 | RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), 204 | RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) 205 | }; 206 | static const int16_t kCachedPowers_E[] = { 207 | -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, 208 | -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, 209 | -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, 210 | -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, 211 | -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, 212 | 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, 213 | 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, 214 | 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, 215 | 907, 933, 960, 986, 1013, 1039, 1066 216 | }; 217 | return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); 218 | } 219 | 220 | inline DiyFp GetCachedPower(int e, int* K) { 221 | 222 | //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; 223 | double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive 224 | int k = static_cast(dk); 225 | if (dk - k > 0.0) 226 | k++; 227 | 228 | unsigned index = static_cast((k >> 3) + 1); 229 | *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table 230 | 231 | return GetCachedPowerByIndex(index); 232 | } 233 | 234 | inline DiyFp GetCachedPower10(int exp, int *outExp) { 235 | unsigned index = (exp + 348) / 8; 236 | *outExp = -348 + index * 8; 237 | return GetCachedPowerByIndex(index); 238 | } 239 | 240 | #ifdef __GNUC__ 241 | RAPIDJSON_DIAG_POP 242 | #endif 243 | 244 | } // namespace internal 245 | RAPIDJSON_NAMESPACE_END 246 | 247 | #endif // RAPIDJSON_DIYFP_H_ 248 | -------------------------------------------------------------------------------- /include/rapidjson/internal/dtoa.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | // This is a C++ header-only implementation of Grisu2 algorithm from the publication: 16 | // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with 17 | // integers." ACM Sigplan Notices 45.6 (2010): 233-243. 18 | 19 | #ifndef RAPIDJSON_DTOA_ 20 | #define RAPIDJSON_DTOA_ 21 | 22 | #include "itoa.h" // GetDigitsLut() 23 | #include "diyfp.h" 24 | #include "ieee754.h" 25 | 26 | RAPIDJSON_NAMESPACE_BEGIN 27 | namespace internal { 28 | 29 | #ifdef __GNUC__ 30 | RAPIDJSON_DIAG_PUSH 31 | RAPIDJSON_DIAG_OFF(effc++) 32 | #endif 33 | 34 | inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { 35 | while (rest < wp_w && delta - rest >= ten_kappa && 36 | (rest + ten_kappa < wp_w || /// closer 37 | wp_w - rest > rest + ten_kappa - wp_w)) { 38 | buffer[len - 1]--; 39 | rest += ten_kappa; 40 | } 41 | } 42 | 43 | inline unsigned CountDecimalDigit32(uint32_t n) { 44 | // Simple pure C++ implementation was faster than __builtin_clz version in this situation. 45 | if (n < 10) return 1; 46 | if (n < 100) return 2; 47 | if (n < 1000) return 3; 48 | if (n < 10000) return 4; 49 | if (n < 100000) return 5; 50 | if (n < 1000000) return 6; 51 | if (n < 10000000) return 7; 52 | if (n < 100000000) return 8; 53 | // Will not reach 10 digits in DigitGen() 54 | //if (n < 1000000000) return 9; 55 | //return 10; 56 | return 9; 57 | } 58 | 59 | inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { 60 | static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; 61 | const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); 62 | const DiyFp wp_w = Mp - W; 63 | uint32_t p1 = static_cast(Mp.f >> -one.e); 64 | uint64_t p2 = Mp.f & (one.f - 1); 65 | int kappa = CountDecimalDigit32(p1); // kappa in [0, 9] 66 | *len = 0; 67 | 68 | while (kappa > 0) { 69 | uint32_t d = 0; 70 | switch (kappa) { 71 | case 9: d = p1 / 100000000; p1 %= 100000000; break; 72 | case 8: d = p1 / 10000000; p1 %= 10000000; break; 73 | case 7: d = p1 / 1000000; p1 %= 1000000; break; 74 | case 6: d = p1 / 100000; p1 %= 100000; break; 75 | case 5: d = p1 / 10000; p1 %= 10000; break; 76 | case 4: d = p1 / 1000; p1 %= 1000; break; 77 | case 3: d = p1 / 100; p1 %= 100; break; 78 | case 2: d = p1 / 10; p1 %= 10; break; 79 | case 1: d = p1; p1 = 0; break; 80 | default:; 81 | } 82 | if (d || *len) 83 | buffer[(*len)++] = static_cast('0' + static_cast(d)); 84 | kappa--; 85 | uint64_t tmp = (static_cast(p1) << -one.e) + p2; 86 | if (tmp <= delta) { 87 | *K += kappa; 88 | GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f); 89 | return; 90 | } 91 | } 92 | 93 | // kappa = 0 94 | for (;;) { 95 | p2 *= 10; 96 | delta *= 10; 97 | char d = static_cast(p2 >> -one.e); 98 | if (d || *len) 99 | buffer[(*len)++] = static_cast('0' + d); 100 | p2 &= one.f - 1; 101 | kappa--; 102 | if (p2 < delta) { 103 | *K += kappa; 104 | GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]); 105 | return; 106 | } 107 | } 108 | } 109 | 110 | inline void Grisu2(double value, char* buffer, int* length, int* K) { 111 | const DiyFp v(value); 112 | DiyFp w_m, w_p; 113 | v.NormalizedBoundaries(&w_m, &w_p); 114 | 115 | const DiyFp c_mk = GetCachedPower(w_p.e, K); 116 | const DiyFp W = v.Normalize() * c_mk; 117 | DiyFp Wp = w_p * c_mk; 118 | DiyFp Wm = w_m * c_mk; 119 | Wm.f++; 120 | Wp.f--; 121 | DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); 122 | } 123 | 124 | inline char* WriteExponent(int K, char* buffer) { 125 | if (K < 0) { 126 | *buffer++ = '-'; 127 | K = -K; 128 | } 129 | 130 | if (K >= 100) { 131 | *buffer++ = static_cast('0' + static_cast(K / 100)); 132 | K %= 100; 133 | const char* d = GetDigitsLut() + K * 2; 134 | *buffer++ = d[0]; 135 | *buffer++ = d[1]; 136 | } 137 | else if (K >= 10) { 138 | const char* d = GetDigitsLut() + K * 2; 139 | *buffer++ = d[0]; 140 | *buffer++ = d[1]; 141 | } 142 | else 143 | *buffer++ = static_cast('0' + static_cast(K)); 144 | 145 | return buffer; 146 | } 147 | 148 | inline char* Prettify(char* buffer, int length, int k) { 149 | const int kk = length + k; // 10^(kk-1) <= v < 10^kk 150 | 151 | if (length <= kk && kk <= 21) { 152 | // 1234e7 -> 12340000000 153 | for (int i = length; i < kk; i++) 154 | buffer[i] = '0'; 155 | buffer[kk] = '.'; 156 | buffer[kk + 1] = '0'; 157 | return &buffer[kk + 2]; 158 | } 159 | else if (0 < kk && kk <= 21) { 160 | // 1234e-2 -> 12.34 161 | std::memmove(&buffer[kk + 1], &buffer[kk], length - kk); 162 | buffer[kk] = '.'; 163 | return &buffer[length + 1]; 164 | } 165 | else if (-6 < kk && kk <= 0) { 166 | // 1234e-6 -> 0.001234 167 | const int offset = 2 - kk; 168 | std::memmove(&buffer[offset], &buffer[0], length); 169 | buffer[0] = '0'; 170 | buffer[1] = '.'; 171 | for (int i = 2; i < offset; i++) 172 | buffer[i] = '0'; 173 | return &buffer[length + offset]; 174 | } 175 | else if (length == 1) { 176 | // 1e30 177 | buffer[1] = 'e'; 178 | return WriteExponent(kk - 1, &buffer[2]); 179 | } 180 | else { 181 | // 1234e30 -> 1.234e33 182 | std::memmove(&buffer[2], &buffer[1], length - 1); 183 | buffer[1] = '.'; 184 | buffer[length + 1] = 'e'; 185 | return WriteExponent(kk - 1, &buffer[0 + length + 2]); 186 | } 187 | } 188 | 189 | inline char* dtoa(double value, char* buffer) { 190 | Double d(value); 191 | if (d.IsZero()) { 192 | if (d.Sign()) 193 | *buffer++ = '-'; // -0.0, Issue #289 194 | buffer[0] = '0'; 195 | buffer[1] = '.'; 196 | buffer[2] = '0'; 197 | return &buffer[3]; 198 | } 199 | else { 200 | if (value < 0) { 201 | *buffer++ = '-'; 202 | value = -value; 203 | } 204 | int length, K; 205 | Grisu2(value, buffer, &length, &K); 206 | return Prettify(buffer, length, K); 207 | } 208 | } 209 | 210 | #ifdef __GNUC__ 211 | RAPIDJSON_DIAG_POP 212 | #endif 213 | 214 | } // namespace internal 215 | RAPIDJSON_NAMESPACE_END 216 | 217 | #endif // RAPIDJSON_DTOA_ 218 | -------------------------------------------------------------------------------- /include/rapidjson/internal/ieee754.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_IEEE754_ 16 | #define RAPIDJSON_IEEE754_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | class Double { 24 | public: 25 | Double() {} 26 | Double(double d) : d_(d) {} 27 | Double(uint64_t u) : u_(u) {} 28 | 29 | double Value() const { return d_; } 30 | uint64_t Uint64Value() const { return u_; } 31 | 32 | double NextPositiveDouble() const { 33 | RAPIDJSON_ASSERT(!Sign()); 34 | return Double(u_ + 1).Value(); 35 | } 36 | 37 | bool Sign() const { return (u_ & kSignMask) != 0; } 38 | uint64_t Significand() const { return u_ & kSignificandMask; } 39 | int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } 40 | 41 | bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } 42 | bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } 43 | bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } 44 | bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } 45 | 46 | uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } 47 | int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } 48 | uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } 49 | 50 | static unsigned EffectiveSignificandSize(int order) { 51 | if (order >= -1021) 52 | return 53; 53 | else if (order <= -1074) 54 | return 0; 55 | else 56 | return order + 1074; 57 | } 58 | 59 | private: 60 | static const int kSignificandSize = 52; 61 | static const int kExponentBias = 0x3FF; 62 | static const int kDenormalExponent = 1 - kExponentBias; 63 | static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); 64 | static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); 65 | static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); 66 | static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); 67 | 68 | union { 69 | double d_; 70 | uint64_t u_; 71 | }; 72 | }; 73 | 74 | } // namespace internal 75 | RAPIDJSON_NAMESPACE_END 76 | 77 | #endif // RAPIDJSON_IEEE754_ 78 | -------------------------------------------------------------------------------- /include/rapidjson/internal/itoa.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ITOA_ 16 | #define RAPIDJSON_ITOA_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | inline const char* GetDigitsLut() { 24 | static const char cDigitsLut[200] = { 25 | '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', 26 | '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', 27 | '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', 28 | '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', 29 | '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', 30 | '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', 31 | '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', 32 | '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', 33 | '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', 34 | '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' 35 | }; 36 | return cDigitsLut; 37 | } 38 | 39 | inline char* u32toa(uint32_t value, char* buffer) { 40 | const char* cDigitsLut = GetDigitsLut(); 41 | 42 | if (value < 10000) { 43 | const uint32_t d1 = (value / 100) << 1; 44 | const uint32_t d2 = (value % 100) << 1; 45 | 46 | if (value >= 1000) 47 | *buffer++ = cDigitsLut[d1]; 48 | if (value >= 100) 49 | *buffer++ = cDigitsLut[d1 + 1]; 50 | if (value >= 10) 51 | *buffer++ = cDigitsLut[d2]; 52 | *buffer++ = cDigitsLut[d2 + 1]; 53 | } 54 | else if (value < 100000000) { 55 | // value = bbbbcccc 56 | const uint32_t b = value / 10000; 57 | const uint32_t c = value % 10000; 58 | 59 | const uint32_t d1 = (b / 100) << 1; 60 | const uint32_t d2 = (b % 100) << 1; 61 | 62 | const uint32_t d3 = (c / 100) << 1; 63 | const uint32_t d4 = (c % 100) << 1; 64 | 65 | if (value >= 10000000) 66 | *buffer++ = cDigitsLut[d1]; 67 | if (value >= 1000000) 68 | *buffer++ = cDigitsLut[d1 + 1]; 69 | if (value >= 100000) 70 | *buffer++ = cDigitsLut[d2]; 71 | *buffer++ = cDigitsLut[d2 + 1]; 72 | 73 | *buffer++ = cDigitsLut[d3]; 74 | *buffer++ = cDigitsLut[d3 + 1]; 75 | *buffer++ = cDigitsLut[d4]; 76 | *buffer++ = cDigitsLut[d4 + 1]; 77 | } 78 | else { 79 | // value = aabbbbcccc in decimal 80 | 81 | const uint32_t a = value / 100000000; // 1 to 42 82 | value %= 100000000; 83 | 84 | if (a >= 10) { 85 | const unsigned i = a << 1; 86 | *buffer++ = cDigitsLut[i]; 87 | *buffer++ = cDigitsLut[i + 1]; 88 | } 89 | else 90 | *buffer++ = static_cast('0' + static_cast(a)); 91 | 92 | const uint32_t b = value / 10000; // 0 to 9999 93 | const uint32_t c = value % 10000; // 0 to 9999 94 | 95 | const uint32_t d1 = (b / 100) << 1; 96 | const uint32_t d2 = (b % 100) << 1; 97 | 98 | const uint32_t d3 = (c / 100) << 1; 99 | const uint32_t d4 = (c % 100) << 1; 100 | 101 | *buffer++ = cDigitsLut[d1]; 102 | *buffer++ = cDigitsLut[d1 + 1]; 103 | *buffer++ = cDigitsLut[d2]; 104 | *buffer++ = cDigitsLut[d2 + 1]; 105 | *buffer++ = cDigitsLut[d3]; 106 | *buffer++ = cDigitsLut[d3 + 1]; 107 | *buffer++ = cDigitsLut[d4]; 108 | *buffer++ = cDigitsLut[d4 + 1]; 109 | } 110 | return buffer; 111 | } 112 | 113 | inline char* i32toa(int32_t value, char* buffer) { 114 | uint32_t u = static_cast(value); 115 | if (value < 0) { 116 | *buffer++ = '-'; 117 | u = ~u + 1; 118 | } 119 | 120 | return u32toa(u, buffer); 121 | } 122 | 123 | inline char* u64toa(uint64_t value, char* buffer) { 124 | const char* cDigitsLut = GetDigitsLut(); 125 | const uint64_t kTen8 = 100000000; 126 | const uint64_t kTen9 = kTen8 * 10; 127 | const uint64_t kTen10 = kTen8 * 100; 128 | const uint64_t kTen11 = kTen8 * 1000; 129 | const uint64_t kTen12 = kTen8 * 10000; 130 | const uint64_t kTen13 = kTen8 * 100000; 131 | const uint64_t kTen14 = kTen8 * 1000000; 132 | const uint64_t kTen15 = kTen8 * 10000000; 133 | const uint64_t kTen16 = kTen8 * kTen8; 134 | 135 | if (value < kTen8) { 136 | uint32_t v = static_cast(value); 137 | if (v < 10000) { 138 | const uint32_t d1 = (v / 100) << 1; 139 | const uint32_t d2 = (v % 100) << 1; 140 | 141 | if (v >= 1000) 142 | *buffer++ = cDigitsLut[d1]; 143 | if (v >= 100) 144 | *buffer++ = cDigitsLut[d1 + 1]; 145 | if (v >= 10) 146 | *buffer++ = cDigitsLut[d2]; 147 | *buffer++ = cDigitsLut[d2 + 1]; 148 | } 149 | else { 150 | // value = bbbbcccc 151 | const uint32_t b = v / 10000; 152 | const uint32_t c = v % 10000; 153 | 154 | const uint32_t d1 = (b / 100) << 1; 155 | const uint32_t d2 = (b % 100) << 1; 156 | 157 | const uint32_t d3 = (c / 100) << 1; 158 | const uint32_t d4 = (c % 100) << 1; 159 | 160 | if (value >= 10000000) 161 | *buffer++ = cDigitsLut[d1]; 162 | if (value >= 1000000) 163 | *buffer++ = cDigitsLut[d1 + 1]; 164 | if (value >= 100000) 165 | *buffer++ = cDigitsLut[d2]; 166 | *buffer++ = cDigitsLut[d2 + 1]; 167 | 168 | *buffer++ = cDigitsLut[d3]; 169 | *buffer++ = cDigitsLut[d3 + 1]; 170 | *buffer++ = cDigitsLut[d4]; 171 | *buffer++ = cDigitsLut[d4 + 1]; 172 | } 173 | } 174 | else if (value < kTen16) { 175 | const uint32_t v0 = static_cast(value / kTen8); 176 | const uint32_t v1 = static_cast(value % kTen8); 177 | 178 | const uint32_t b0 = v0 / 10000; 179 | const uint32_t c0 = v0 % 10000; 180 | 181 | const uint32_t d1 = (b0 / 100) << 1; 182 | const uint32_t d2 = (b0 % 100) << 1; 183 | 184 | const uint32_t d3 = (c0 / 100) << 1; 185 | const uint32_t d4 = (c0 % 100) << 1; 186 | 187 | const uint32_t b1 = v1 / 10000; 188 | const uint32_t c1 = v1 % 10000; 189 | 190 | const uint32_t d5 = (b1 / 100) << 1; 191 | const uint32_t d6 = (b1 % 100) << 1; 192 | 193 | const uint32_t d7 = (c1 / 100) << 1; 194 | const uint32_t d8 = (c1 % 100) << 1; 195 | 196 | if (value >= kTen15) 197 | *buffer++ = cDigitsLut[d1]; 198 | if (value >= kTen14) 199 | *buffer++ = cDigitsLut[d1 + 1]; 200 | if (value >= kTen13) 201 | *buffer++ = cDigitsLut[d2]; 202 | if (value >= kTen12) 203 | *buffer++ = cDigitsLut[d2 + 1]; 204 | if (value >= kTen11) 205 | *buffer++ = cDigitsLut[d3]; 206 | if (value >= kTen10) 207 | *buffer++ = cDigitsLut[d3 + 1]; 208 | if (value >= kTen9) 209 | *buffer++ = cDigitsLut[d4]; 210 | if (value >= kTen8) 211 | *buffer++ = cDigitsLut[d4 + 1]; 212 | 213 | *buffer++ = cDigitsLut[d5]; 214 | *buffer++ = cDigitsLut[d5 + 1]; 215 | *buffer++ = cDigitsLut[d6]; 216 | *buffer++ = cDigitsLut[d6 + 1]; 217 | *buffer++ = cDigitsLut[d7]; 218 | *buffer++ = cDigitsLut[d7 + 1]; 219 | *buffer++ = cDigitsLut[d8]; 220 | *buffer++ = cDigitsLut[d8 + 1]; 221 | } 222 | else { 223 | const uint32_t a = static_cast(value / kTen16); // 1 to 1844 224 | value %= kTen16; 225 | 226 | if (a < 10) 227 | *buffer++ = static_cast('0' + static_cast(a)); 228 | else if (a < 100) { 229 | const uint32_t i = a << 1; 230 | *buffer++ = cDigitsLut[i]; 231 | *buffer++ = cDigitsLut[i + 1]; 232 | } 233 | else if (a < 1000) { 234 | *buffer++ = static_cast('0' + static_cast(a / 100)); 235 | 236 | const uint32_t i = (a % 100) << 1; 237 | *buffer++ = cDigitsLut[i]; 238 | *buffer++ = cDigitsLut[i + 1]; 239 | } 240 | else { 241 | const uint32_t i = (a / 100) << 1; 242 | const uint32_t j = (a % 100) << 1; 243 | *buffer++ = cDigitsLut[i]; 244 | *buffer++ = cDigitsLut[i + 1]; 245 | *buffer++ = cDigitsLut[j]; 246 | *buffer++ = cDigitsLut[j + 1]; 247 | } 248 | 249 | const uint32_t v0 = static_cast(value / kTen8); 250 | const uint32_t v1 = static_cast(value % kTen8); 251 | 252 | const uint32_t b0 = v0 / 10000; 253 | const uint32_t c0 = v0 % 10000; 254 | 255 | const uint32_t d1 = (b0 / 100) << 1; 256 | const uint32_t d2 = (b0 % 100) << 1; 257 | 258 | const uint32_t d3 = (c0 / 100) << 1; 259 | const uint32_t d4 = (c0 % 100) << 1; 260 | 261 | const uint32_t b1 = v1 / 10000; 262 | const uint32_t c1 = v1 % 10000; 263 | 264 | const uint32_t d5 = (b1 / 100) << 1; 265 | const uint32_t d6 = (b1 % 100) << 1; 266 | 267 | const uint32_t d7 = (c1 / 100) << 1; 268 | const uint32_t d8 = (c1 % 100) << 1; 269 | 270 | *buffer++ = cDigitsLut[d1]; 271 | *buffer++ = cDigitsLut[d1 + 1]; 272 | *buffer++ = cDigitsLut[d2]; 273 | *buffer++ = cDigitsLut[d2 + 1]; 274 | *buffer++ = cDigitsLut[d3]; 275 | *buffer++ = cDigitsLut[d3 + 1]; 276 | *buffer++ = cDigitsLut[d4]; 277 | *buffer++ = cDigitsLut[d4 + 1]; 278 | *buffer++ = cDigitsLut[d5]; 279 | *buffer++ = cDigitsLut[d5 + 1]; 280 | *buffer++ = cDigitsLut[d6]; 281 | *buffer++ = cDigitsLut[d6 + 1]; 282 | *buffer++ = cDigitsLut[d7]; 283 | *buffer++ = cDigitsLut[d7 + 1]; 284 | *buffer++ = cDigitsLut[d8]; 285 | *buffer++ = cDigitsLut[d8 + 1]; 286 | } 287 | 288 | return buffer; 289 | } 290 | 291 | inline char* i64toa(int64_t value, char* buffer) { 292 | uint64_t u = static_cast(value); 293 | if (value < 0) { 294 | *buffer++ = '-'; 295 | u = ~u + 1; 296 | } 297 | 298 | return u64toa(u, buffer); 299 | } 300 | 301 | } // namespace internal 302 | RAPIDJSON_NAMESPACE_END 303 | 304 | #endif // RAPIDJSON_ITOA_ 305 | -------------------------------------------------------------------------------- /include/rapidjson/internal/meta.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_INTERNAL_META_H_ 16 | #define RAPIDJSON_INTERNAL_META_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | #ifdef __GNUC__ 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(effc++) 23 | #endif 24 | #if defined(_MSC_VER) 25 | RAPIDJSON_DIAG_PUSH 26 | RAPIDJSON_DIAG_OFF(6334) 27 | #endif 28 | 29 | #if RAPIDJSON_HAS_CXX11_TYPETRAITS 30 | #include 31 | #endif 32 | 33 | //@cond RAPIDJSON_INTERNAL 34 | RAPIDJSON_NAMESPACE_BEGIN 35 | namespace internal { 36 | 37 | // Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching 38 | template struct Void { typedef void Type; }; 39 | 40 | /////////////////////////////////////////////////////////////////////////////// 41 | // BoolType, TrueType, FalseType 42 | // 43 | template struct BoolType { 44 | static const bool Value = Cond; 45 | typedef BoolType Type; 46 | }; 47 | typedef BoolType TrueType; 48 | typedef BoolType FalseType; 49 | 50 | 51 | /////////////////////////////////////////////////////////////////////////////// 52 | // SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr 53 | // 54 | 55 | template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; 56 | template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; 57 | template struct SelectIfCond : SelectIfImpl::template Apply {}; 58 | template struct SelectIf : SelectIfCond {}; 59 | 60 | template struct AndExprCond : FalseType {}; 61 | template <> struct AndExprCond : TrueType {}; 62 | template struct OrExprCond : TrueType {}; 63 | template <> struct OrExprCond : FalseType {}; 64 | 65 | template struct BoolExpr : SelectIf::Type {}; 66 | template struct NotExpr : SelectIf::Type {}; 67 | template struct AndExpr : AndExprCond::Type {}; 68 | template struct OrExpr : OrExprCond::Type {}; 69 | 70 | 71 | /////////////////////////////////////////////////////////////////////////////// 72 | // AddConst, MaybeAddConst, RemoveConst 73 | template struct AddConst { typedef const T Type; }; 74 | template struct MaybeAddConst : SelectIfCond {}; 75 | template struct RemoveConst { typedef T Type; }; 76 | template struct RemoveConst { typedef T Type; }; 77 | 78 | 79 | /////////////////////////////////////////////////////////////////////////////// 80 | // IsSame, IsConst, IsMoreConst, IsPointer 81 | // 82 | template struct IsSame : FalseType {}; 83 | template struct IsSame : TrueType {}; 84 | 85 | template struct IsConst : FalseType {}; 86 | template struct IsConst : TrueType {}; 87 | 88 | template 89 | struct IsMoreConst 90 | : AndExpr::Type, typename RemoveConst::Type>, 91 | BoolType::Value >= IsConst::Value> >::Type {}; 92 | 93 | template struct IsPointer : FalseType {}; 94 | template struct IsPointer : TrueType {}; 95 | 96 | /////////////////////////////////////////////////////////////////////////////// 97 | // IsBaseOf 98 | // 99 | #if RAPIDJSON_HAS_CXX11_TYPETRAITS 100 | 101 | template struct IsBaseOf 102 | : BoolType< ::std::is_base_of::value> {}; 103 | 104 | #else // simplified version adopted from Boost 105 | 106 | template struct IsBaseOfImpl { 107 | RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); 108 | RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); 109 | 110 | typedef char (&Yes)[1]; 111 | typedef char (&No) [2]; 112 | 113 | template 114 | static Yes Check(const D*, T); 115 | static No Check(const B*, int); 116 | 117 | struct Host { 118 | operator const B*() const; 119 | operator const D*(); 120 | }; 121 | 122 | enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; 123 | }; 124 | 125 | template struct IsBaseOf 126 | : OrExpr, BoolExpr > >::Type {}; 127 | 128 | #endif // RAPIDJSON_HAS_CXX11_TYPETRAITS 129 | 130 | 131 | ////////////////////////////////////////////////////////////////////////// 132 | // EnableIf / DisableIf 133 | // 134 | template struct EnableIfCond { typedef T Type; }; 135 | template struct EnableIfCond { /* empty */ }; 136 | 137 | template struct DisableIfCond { typedef T Type; }; 138 | template struct DisableIfCond { /* empty */ }; 139 | 140 | template 141 | struct EnableIf : EnableIfCond {}; 142 | 143 | template 144 | struct DisableIf : DisableIfCond {}; 145 | 146 | // SFINAE helpers 147 | struct SfinaeTag {}; 148 | template struct RemoveSfinaeTag; 149 | template struct RemoveSfinaeTag { typedef T Type; }; 150 | 151 | #define RAPIDJSON_REMOVEFPTR_(type) \ 152 | typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ 153 | < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type 154 | 155 | #define RAPIDJSON_ENABLEIF(cond) \ 156 | typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ 157 | ::Type * = NULL 158 | 159 | #define RAPIDJSON_DISABLEIF(cond) \ 160 | typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ 161 | ::Type * = NULL 162 | 163 | #define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ 164 | typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ 165 | ::Type 167 | 168 | #define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ 169 | typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ 170 | ::Type 172 | 173 | } // namespace internal 174 | RAPIDJSON_NAMESPACE_END 175 | //@endcond 176 | 177 | #if defined(__GNUC__) || defined(_MSC_VER) 178 | RAPIDJSON_DIAG_POP 179 | #endif 180 | 181 | #endif // RAPIDJSON_INTERNAL_META_H_ 182 | -------------------------------------------------------------------------------- /include/rapidjson/internal/pow10.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_POW10_ 16 | #define RAPIDJSON_POW10_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | //! Computes integer powers of 10 in double (10.0^n). 24 | /*! This function uses lookup table for fast and accurate results. 25 | \param n non-negative exponent. Must <= 308. 26 | \return 10.0^n 27 | */ 28 | inline double Pow10(int n) { 29 | static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes 30 | 1e+0, 31 | 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, 32 | 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, 33 | 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, 34 | 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, 35 | 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, 36 | 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, 37 | 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, 38 | 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, 39 | 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, 40 | 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, 41 | 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, 42 | 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, 43 | 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, 44 | 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, 45 | 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, 46 | 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 47 | }; 48 | RAPIDJSON_ASSERT(n >= 0 && n <= 308); 49 | return e[n]; 50 | } 51 | 52 | } // namespace internal 53 | RAPIDJSON_NAMESPACE_END 54 | 55 | #endif // RAPIDJSON_POW10_ 56 | -------------------------------------------------------------------------------- /include/rapidjson/internal/stack.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_INTERNAL_STACK_H_ 16 | #define RAPIDJSON_INTERNAL_STACK_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | /////////////////////////////////////////////////////////////////////////////// 24 | // Stack 25 | 26 | //! A type-unsafe stack for storing different types of data. 27 | /*! \tparam Allocator Allocator for allocating stack memory. 28 | */ 29 | template 30 | class Stack { 31 | public: 32 | // Optimization note: Do not allocate memory for stack_ in constructor. 33 | // Do it lazily when first Push() -> Expand() -> Resize(). 34 | Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { 35 | RAPIDJSON_ASSERT(stackCapacity > 0); 36 | } 37 | 38 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 39 | Stack(Stack&& rhs) 40 | : allocator_(rhs.allocator_), 41 | ownAllocator_(rhs.ownAllocator_), 42 | stack_(rhs.stack_), 43 | stackTop_(rhs.stackTop_), 44 | stackEnd_(rhs.stackEnd_), 45 | initialCapacity_(rhs.initialCapacity_) 46 | { 47 | rhs.allocator_ = 0; 48 | rhs.ownAllocator_ = 0; 49 | rhs.stack_ = 0; 50 | rhs.stackTop_ = 0; 51 | rhs.stackEnd_ = 0; 52 | rhs.initialCapacity_ = 0; 53 | } 54 | #endif 55 | 56 | ~Stack() { 57 | Destroy(); 58 | } 59 | 60 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 61 | Stack& operator=(Stack&& rhs) { 62 | if (&rhs != this) 63 | { 64 | Destroy(); 65 | 66 | allocator_ = rhs.allocator_; 67 | ownAllocator_ = rhs.ownAllocator_; 68 | stack_ = rhs.stack_; 69 | stackTop_ = rhs.stackTop_; 70 | stackEnd_ = rhs.stackEnd_; 71 | initialCapacity_ = rhs.initialCapacity_; 72 | 73 | rhs.allocator_ = 0; 74 | rhs.ownAllocator_ = 0; 75 | rhs.stack_ = 0; 76 | rhs.stackTop_ = 0; 77 | rhs.stackEnd_ = 0; 78 | rhs.initialCapacity_ = 0; 79 | } 80 | return *this; 81 | } 82 | #endif 83 | 84 | void Clear() { stackTop_ = stack_; } 85 | 86 | void ShrinkToFit() { 87 | if (Empty()) { 88 | // If the stack is empty, completely deallocate the memory. 89 | Allocator::Free(stack_); 90 | stack_ = 0; 91 | stackTop_ = 0; 92 | stackEnd_ = 0; 93 | } 94 | else 95 | Resize(GetSize()); 96 | } 97 | 98 | // Optimization note: try to minimize the size of this function for force inline. 99 | // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. 100 | template 101 | RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { 102 | // Expand the stack if needed 103 | if (stackTop_ + sizeof(T) * count >= stackEnd_) 104 | Expand(count); 105 | 106 | T* ret = reinterpret_cast(stackTop_); 107 | stackTop_ += sizeof(T) * count; 108 | return ret; 109 | } 110 | 111 | template 112 | T* Pop(size_t count) { 113 | RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); 114 | stackTop_ -= count * sizeof(T); 115 | return reinterpret_cast(stackTop_); 116 | } 117 | 118 | template 119 | T* Top() { 120 | RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); 121 | return reinterpret_cast(stackTop_ - sizeof(T)); 122 | } 123 | 124 | template 125 | T* Bottom() { return (T*)stack_; } 126 | 127 | Allocator& GetAllocator() { return *allocator_; } 128 | bool Empty() const { return stackTop_ == stack_; } 129 | size_t GetSize() const { return static_cast(stackTop_ - stack_); } 130 | size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } 131 | 132 | private: 133 | template 134 | void Expand(size_t count) { 135 | // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. 136 | size_t newCapacity; 137 | if (stack_ == 0) { 138 | if (!allocator_) 139 | ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); 140 | newCapacity = initialCapacity_; 141 | } else { 142 | newCapacity = GetCapacity(); 143 | newCapacity += (newCapacity + 1) / 2; 144 | } 145 | size_t newSize = GetSize() + sizeof(T) * count; 146 | if (newCapacity < newSize) 147 | newCapacity = newSize; 148 | 149 | Resize(newCapacity); 150 | } 151 | 152 | void Resize(size_t newCapacity) { 153 | const size_t size = GetSize(); // Backup the current size 154 | stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity); 155 | stackTop_ = stack_ + size; 156 | stackEnd_ = stack_ + newCapacity; 157 | } 158 | 159 | void Destroy() { 160 | Allocator::Free(stack_); 161 | RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack 162 | } 163 | 164 | // Prohibit copy constructor & assignment operator. 165 | Stack(const Stack&); 166 | Stack& operator=(const Stack&); 167 | 168 | Allocator* allocator_; 169 | Allocator* ownAllocator_; 170 | char *stack_; 171 | char *stackTop_; 172 | char *stackEnd_; 173 | size_t initialCapacity_; 174 | }; 175 | 176 | } // namespace internal 177 | RAPIDJSON_NAMESPACE_END 178 | 179 | #endif // RAPIDJSON_STACK_H_ 180 | -------------------------------------------------------------------------------- /include/rapidjson/internal/strfunc.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ 16 | #define RAPIDJSON_INTERNAL_STRFUNC_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | //! Custom strlen() which works on different character types. 24 | /*! \tparam Ch Character type (e.g. char, wchar_t, short) 25 | \param s Null-terminated input string. 26 | \return Number of characters in the string. 27 | \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. 28 | */ 29 | template 30 | inline SizeType StrLen(const Ch* s) { 31 | const Ch* p = s; 32 | while (*p) ++p; 33 | return SizeType(p - s); 34 | } 35 | 36 | } // namespace internal 37 | RAPIDJSON_NAMESPACE_END 38 | 39 | #endif // RAPIDJSON_INTERNAL_STRFUNC_H_ 40 | -------------------------------------------------------------------------------- /include/rapidjson/internal/strtod.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_STRTOD_ 16 | #define RAPIDJSON_STRTOD_ 17 | 18 | #include "../rapidjson.h" 19 | #include "ieee754.h" 20 | #include "biginteger.h" 21 | #include "diyfp.h" 22 | #include "pow10.h" 23 | 24 | RAPIDJSON_NAMESPACE_BEGIN 25 | namespace internal { 26 | 27 | inline double FastPath(double significand, int exp) { 28 | if (exp < -308) 29 | return 0.0; 30 | else if (exp >= 0) 31 | return significand * internal::Pow10(exp); 32 | else 33 | return significand / internal::Pow10(-exp); 34 | } 35 | 36 | inline double StrtodNormalPrecision(double d, int p) { 37 | if (p < -308) { 38 | // Prevent expSum < -308, making Pow10(p) = 0 39 | d = FastPath(d, -308); 40 | d = FastPath(d, p + 308); 41 | } 42 | else 43 | d = FastPath(d, p); 44 | return d; 45 | } 46 | 47 | template 48 | inline T Min3(T a, T b, T c) { 49 | T m = a; 50 | if (m > b) m = b; 51 | if (m > c) m = c; 52 | return m; 53 | } 54 | 55 | inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { 56 | const Double db(b); 57 | const uint64_t bInt = db.IntegerSignificand(); 58 | const int bExp = db.IntegerExponent(); 59 | const int hExp = bExp - 1; 60 | 61 | int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; 62 | 63 | // Adjust for decimal exponent 64 | if (dExp >= 0) { 65 | dS_Exp2 += dExp; 66 | dS_Exp5 += dExp; 67 | } 68 | else { 69 | bS_Exp2 -= dExp; 70 | bS_Exp5 -= dExp; 71 | hS_Exp2 -= dExp; 72 | hS_Exp5 -= dExp; 73 | } 74 | 75 | // Adjust for binary exponent 76 | if (bExp >= 0) 77 | bS_Exp2 += bExp; 78 | else { 79 | dS_Exp2 -= bExp; 80 | hS_Exp2 -= bExp; 81 | } 82 | 83 | // Adjust for half ulp exponent 84 | if (hExp >= 0) 85 | hS_Exp2 += hExp; 86 | else { 87 | dS_Exp2 -= hExp; 88 | bS_Exp2 -= hExp; 89 | } 90 | 91 | // Remove common power of two factor from all three scaled values 92 | int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); 93 | dS_Exp2 -= common_Exp2; 94 | bS_Exp2 -= common_Exp2; 95 | hS_Exp2 -= common_Exp2; 96 | 97 | BigInteger dS = d; 98 | dS.MultiplyPow5(dS_Exp5) <<= dS_Exp2; 99 | 100 | BigInteger bS(bInt); 101 | bS.MultiplyPow5(bS_Exp5) <<= bS_Exp2; 102 | 103 | BigInteger hS(1); 104 | hS.MultiplyPow5(hS_Exp5) <<= hS_Exp2; 105 | 106 | BigInteger delta(0); 107 | dS.Difference(bS, &delta); 108 | 109 | return delta.Compare(hS); 110 | } 111 | 112 | inline bool StrtodFast(double d, int p, double* result) { 113 | // Use fast path for string-to-double conversion if possible 114 | // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ 115 | if (p > 22 && p < 22 + 16) { 116 | // Fast Path Cases In Disguise 117 | d *= internal::Pow10(p - 22); 118 | p = 22; 119 | } 120 | 121 | if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 122 | *result = FastPath(d, p); 123 | return true; 124 | } 125 | else 126 | return false; 127 | } 128 | 129 | // Compute an approximation and see if it is within 1/2 ULP 130 | inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) { 131 | uint64_t significand = 0; 132 | size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 133 | for (; i < length; i++) { 134 | if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || 135 | (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5')) 136 | break; 137 | significand = significand * 10 + (decimals[i] - '0'); 138 | } 139 | 140 | if (i < length && decimals[i] >= '5') // Rounding 141 | significand++; 142 | 143 | size_t remaining = length - i; 144 | const unsigned kUlpShift = 3; 145 | const unsigned kUlp = 1 << kUlpShift; 146 | int error = (remaining == 0) ? 0 : kUlp / 2; 147 | 148 | DiyFp v(significand, 0); 149 | v = v.Normalize(); 150 | error <<= -v.e; 151 | 152 | const int dExp = (int)decimalPosition - (int)i + exp; 153 | 154 | int actualExp; 155 | DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); 156 | if (actualExp != dExp) { 157 | static const DiyFp kPow10[] = { 158 | DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 159 | DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 160 | DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 161 | DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 162 | DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 163 | DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 164 | DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 165 | }; 166 | int adjustment = dExp - actualExp - 1; 167 | RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); 168 | v = v * kPow10[adjustment]; 169 | if (length + adjustment > 19) // has more digits than decimal digits in 64-bit 170 | error += kUlp / 2; 171 | } 172 | 173 | v = v * cachedPower; 174 | 175 | error += kUlp + (error == 0 ? 0 : 1); 176 | 177 | const int oldExp = v.e; 178 | v = v.Normalize(); 179 | error <<= oldExp - v.e; 180 | 181 | const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); 182 | unsigned precisionSize = 64 - effectiveSignificandSize; 183 | if (precisionSize + kUlpShift >= 64) { 184 | unsigned scaleExp = (precisionSize + kUlpShift) - 63; 185 | v.f >>= scaleExp; 186 | v.e += scaleExp; 187 | error = (error >> scaleExp) + 1 + kUlp; 188 | precisionSize -= scaleExp; 189 | } 190 | 191 | DiyFp rounded(v.f >> precisionSize, v.e + precisionSize); 192 | const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; 193 | const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; 194 | if (precisionBits >= halfWay + error) { 195 | rounded.f++; 196 | if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) 197 | rounded.f >>= 1; 198 | rounded.e++; 199 | } 200 | } 201 | 202 | *result = rounded.ToDouble(); 203 | 204 | return halfWay - error >= precisionBits || precisionBits >= halfWay + error; 205 | } 206 | 207 | inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) { 208 | const BigInteger dInt(decimals, length); 209 | const int dExp = (int)decimalPosition - (int)length + exp; 210 | Double a(approx); 211 | int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); 212 | if (cmp < 0) 213 | return a.Value(); // within half ULP 214 | else if (cmp == 0) { 215 | // Round towards even 216 | if (a.Significand() & 1) 217 | return a.NextPositiveDouble(); 218 | else 219 | return a.Value(); 220 | } 221 | else // adjustment 222 | return a.NextPositiveDouble(); 223 | } 224 | 225 | inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) { 226 | RAPIDJSON_ASSERT(d >= 0.0); 227 | RAPIDJSON_ASSERT(length >= 1); 228 | 229 | double result; 230 | if (StrtodFast(d, p, &result)) 231 | return result; 232 | 233 | // Trim leading zeros 234 | while (*decimals == '0' && length > 1) { 235 | length--; 236 | decimals++; 237 | decimalPosition--; 238 | } 239 | 240 | // Trim trailing zeros 241 | while (decimals[length - 1] == '0' && length > 1) { 242 | length--; 243 | decimalPosition--; 244 | exp++; 245 | } 246 | 247 | // Trim right-most digits 248 | const int kMaxDecimalDigit = 780; 249 | if ((int)length > kMaxDecimalDigit) { 250 | int delta = (int(length) - kMaxDecimalDigit); 251 | exp += delta; 252 | decimalPosition -= delta; 253 | length = kMaxDecimalDigit; 254 | } 255 | 256 | // If too small, underflow to zero 257 | if (int(length) + exp < -324) 258 | return 0.0; 259 | 260 | if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) 261 | return result; 262 | 263 | // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison 264 | return StrtodBigInteger(result, decimals, length, decimalPosition, exp); 265 | } 266 | 267 | } // namespace internal 268 | RAPIDJSON_NAMESPACE_END 269 | 270 | #endif // RAPIDJSON_STRTOD_ 271 | -------------------------------------------------------------------------------- /include/rapidjson/memorybuffer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_MEMORYBUFFER_H_ 16 | #define RAPIDJSON_MEMORYBUFFER_H_ 17 | 18 | #include "rapidjson.h" 19 | #include "internal/stack.h" 20 | 21 | RAPIDJSON_NAMESPACE_BEGIN 22 | 23 | //! Represents an in-memory output byte stream. 24 | /*! 25 | This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. 26 | 27 | It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. 28 | 29 | Differences between MemoryBuffer and StringBuffer: 30 | 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. 31 | 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. 32 | 33 | \tparam Allocator type for allocating memory buffer. 34 | \note implements Stream concept 35 | */ 36 | template 37 | struct GenericMemoryBuffer { 38 | typedef char Ch; // byte 39 | 40 | GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} 41 | 42 | void Put(Ch c) { *stack_.template Push() = c; } 43 | void Flush() {} 44 | 45 | void Clear() { stack_.Clear(); } 46 | void ShrinkToFit() { stack_.ShrinkToFit(); } 47 | Ch* Push(size_t count) { return stack_.template Push(count); } 48 | void Pop(size_t count) { stack_.template Pop(count); } 49 | 50 | const Ch* GetBuffer() const { 51 | return stack_.template Bottom(); 52 | } 53 | 54 | size_t GetSize() const { return stack_.GetSize(); } 55 | 56 | static const size_t kDefaultCapacity = 256; 57 | mutable internal::Stack stack_; 58 | }; 59 | 60 | typedef GenericMemoryBuffer<> MemoryBuffer; 61 | 62 | //! Implement specialized version of PutN() with memset() for better performance. 63 | template<> 64 | inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { 65 | std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); 66 | } 67 | 68 | RAPIDJSON_NAMESPACE_END 69 | 70 | #endif // RAPIDJSON_MEMORYBUFFER_H_ 71 | -------------------------------------------------------------------------------- /include/rapidjson/memorystream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_MEMORYSTREAM_H_ 16 | #define RAPIDJSON_MEMORYSTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | 22 | //! Represents an in-memory input byte stream. 23 | /*! 24 | This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. 25 | 26 | It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. 27 | 28 | Differences between MemoryStream and StringStream: 29 | 1. StringStream has encoding but MemoryStream is a byte stream. 30 | 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. 31 | 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). 32 | \note implements Stream concept 33 | */ 34 | struct MemoryStream { 35 | typedef char Ch; // byte 36 | 37 | MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} 38 | 39 | Ch Peek() const { return (src_ == end_) ? '\0' : *src_; } 40 | Ch Take() { return (src_ == end_) ? '\0' : *src_++; } 41 | size_t Tell() const { return static_cast(src_ - begin_); } 42 | 43 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 44 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 45 | void Flush() { RAPIDJSON_ASSERT(false); } 46 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 47 | 48 | // For encoding detection only. 49 | const Ch* Peek4() const { 50 | return Tell() + 4 <= size_ ? src_ : 0; 51 | } 52 | 53 | const Ch* src_; //!< Current read position. 54 | const Ch* begin_; //!< Original head of the string. 55 | const Ch* end_; //!< End of stream. 56 | size_t size_; //!< Size of the stream. 57 | }; 58 | 59 | RAPIDJSON_NAMESPACE_END 60 | 61 | #endif // RAPIDJSON_MEMORYBUFFER_H_ 62 | -------------------------------------------------------------------------------- /include/rapidjson/msinttypes/inttypes.h: -------------------------------------------------------------------------------- 1 | // ISO C9x compliant inttypes.h for Microsoft Visual Studio 2 | // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 3 | // 4 | // Copyright (c) 2006-2013 Alexander Chemeris 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are met: 8 | // 9 | // 1. Redistributions of source code must retain the above copyright notice, 10 | // this list of conditions and the following disclaimer. 11 | // 12 | // 2. Redistributions in binary form must reproduce the above copyright 13 | // notice, this list of conditions and the following disclaimer in the 14 | // documentation and/or other materials provided with the distribution. 15 | // 16 | // 3. Neither the name of the product nor the names of its contributors may 17 | // be used to endorse or promote products derived from this software 18 | // without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 21 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 23 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | // 31 | /////////////////////////////////////////////////////////////////////////////// 32 | 33 | // The above software in this distribution may have been modified by 34 | // THL A29 Limited ("Tencent Modifications"). 35 | // All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. 36 | 37 | #ifndef _MSC_VER // [ 38 | #error "Use this header only with Microsoft Visual C++ compilers!" 39 | #endif // _MSC_VER ] 40 | 41 | #ifndef _MSC_INTTYPES_H_ // [ 42 | #define _MSC_INTTYPES_H_ 43 | 44 | #if _MSC_VER > 1000 45 | #pragma once 46 | #endif 47 | 48 | #include "stdint.h" 49 | 50 | // miloyip: VC supports inttypes.h since VC2013 51 | #if _MSC_VER >= 1800 52 | #include 53 | #else 54 | 55 | // 7.8 Format conversion of integer types 56 | 57 | typedef struct { 58 | intmax_t quot; 59 | intmax_t rem; 60 | } imaxdiv_t; 61 | 62 | // 7.8.1 Macros for format specifiers 63 | 64 | #if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198 65 | 66 | // The fprintf macros for signed integers are: 67 | #define PRId8 "d" 68 | #define PRIi8 "i" 69 | #define PRIdLEAST8 "d" 70 | #define PRIiLEAST8 "i" 71 | #define PRIdFAST8 "d" 72 | #define PRIiFAST8 "i" 73 | 74 | #define PRId16 "hd" 75 | #define PRIi16 "hi" 76 | #define PRIdLEAST16 "hd" 77 | #define PRIiLEAST16 "hi" 78 | #define PRIdFAST16 "hd" 79 | #define PRIiFAST16 "hi" 80 | 81 | #define PRId32 "I32d" 82 | #define PRIi32 "I32i" 83 | #define PRIdLEAST32 "I32d" 84 | #define PRIiLEAST32 "I32i" 85 | #define PRIdFAST32 "I32d" 86 | #define PRIiFAST32 "I32i" 87 | 88 | #define PRId64 "I64d" 89 | #define PRIi64 "I64i" 90 | #define PRIdLEAST64 "I64d" 91 | #define PRIiLEAST64 "I64i" 92 | #define PRIdFAST64 "I64d" 93 | #define PRIiFAST64 "I64i" 94 | 95 | #define PRIdMAX "I64d" 96 | #define PRIiMAX "I64i" 97 | 98 | #define PRIdPTR "Id" 99 | #define PRIiPTR "Ii" 100 | 101 | // The fprintf macros for unsigned integers are: 102 | #define PRIo8 "o" 103 | #define PRIu8 "u" 104 | #define PRIx8 "x" 105 | #define PRIX8 "X" 106 | #define PRIoLEAST8 "o" 107 | #define PRIuLEAST8 "u" 108 | #define PRIxLEAST8 "x" 109 | #define PRIXLEAST8 "X" 110 | #define PRIoFAST8 "o" 111 | #define PRIuFAST8 "u" 112 | #define PRIxFAST8 "x" 113 | #define PRIXFAST8 "X" 114 | 115 | #define PRIo16 "ho" 116 | #define PRIu16 "hu" 117 | #define PRIx16 "hx" 118 | #define PRIX16 "hX" 119 | #define PRIoLEAST16 "ho" 120 | #define PRIuLEAST16 "hu" 121 | #define PRIxLEAST16 "hx" 122 | #define PRIXLEAST16 "hX" 123 | #define PRIoFAST16 "ho" 124 | #define PRIuFAST16 "hu" 125 | #define PRIxFAST16 "hx" 126 | #define PRIXFAST16 "hX" 127 | 128 | #define PRIo32 "I32o" 129 | #define PRIu32 "I32u" 130 | #define PRIx32 "I32x" 131 | #define PRIX32 "I32X" 132 | #define PRIoLEAST32 "I32o" 133 | #define PRIuLEAST32 "I32u" 134 | #define PRIxLEAST32 "I32x" 135 | #define PRIXLEAST32 "I32X" 136 | #define PRIoFAST32 "I32o" 137 | #define PRIuFAST32 "I32u" 138 | #define PRIxFAST32 "I32x" 139 | #define PRIXFAST32 "I32X" 140 | 141 | #define PRIo64 "I64o" 142 | #define PRIu64 "I64u" 143 | #define PRIx64 "I64x" 144 | #define PRIX64 "I64X" 145 | #define PRIoLEAST64 "I64o" 146 | #define PRIuLEAST64 "I64u" 147 | #define PRIxLEAST64 "I64x" 148 | #define PRIXLEAST64 "I64X" 149 | #define PRIoFAST64 "I64o" 150 | #define PRIuFAST64 "I64u" 151 | #define PRIxFAST64 "I64x" 152 | #define PRIXFAST64 "I64X" 153 | 154 | #define PRIoMAX "I64o" 155 | #define PRIuMAX "I64u" 156 | #define PRIxMAX "I64x" 157 | #define PRIXMAX "I64X" 158 | 159 | #define PRIoPTR "Io" 160 | #define PRIuPTR "Iu" 161 | #define PRIxPTR "Ix" 162 | #define PRIXPTR "IX" 163 | 164 | // The fscanf macros for signed integers are: 165 | #define SCNd8 "d" 166 | #define SCNi8 "i" 167 | #define SCNdLEAST8 "d" 168 | #define SCNiLEAST8 "i" 169 | #define SCNdFAST8 "d" 170 | #define SCNiFAST8 "i" 171 | 172 | #define SCNd16 "hd" 173 | #define SCNi16 "hi" 174 | #define SCNdLEAST16 "hd" 175 | #define SCNiLEAST16 "hi" 176 | #define SCNdFAST16 "hd" 177 | #define SCNiFAST16 "hi" 178 | 179 | #define SCNd32 "ld" 180 | #define SCNi32 "li" 181 | #define SCNdLEAST32 "ld" 182 | #define SCNiLEAST32 "li" 183 | #define SCNdFAST32 "ld" 184 | #define SCNiFAST32 "li" 185 | 186 | #define SCNd64 "I64d" 187 | #define SCNi64 "I64i" 188 | #define SCNdLEAST64 "I64d" 189 | #define SCNiLEAST64 "I64i" 190 | #define SCNdFAST64 "I64d" 191 | #define SCNiFAST64 "I64i" 192 | 193 | #define SCNdMAX "I64d" 194 | #define SCNiMAX "I64i" 195 | 196 | #ifdef _WIN64 // [ 197 | # define SCNdPTR "I64d" 198 | # define SCNiPTR "I64i" 199 | #else // _WIN64 ][ 200 | # define SCNdPTR "ld" 201 | # define SCNiPTR "li" 202 | #endif // _WIN64 ] 203 | 204 | // The fscanf macros for unsigned integers are: 205 | #define SCNo8 "o" 206 | #define SCNu8 "u" 207 | #define SCNx8 "x" 208 | #define SCNX8 "X" 209 | #define SCNoLEAST8 "o" 210 | #define SCNuLEAST8 "u" 211 | #define SCNxLEAST8 "x" 212 | #define SCNXLEAST8 "X" 213 | #define SCNoFAST8 "o" 214 | #define SCNuFAST8 "u" 215 | #define SCNxFAST8 "x" 216 | #define SCNXFAST8 "X" 217 | 218 | #define SCNo16 "ho" 219 | #define SCNu16 "hu" 220 | #define SCNx16 "hx" 221 | #define SCNX16 "hX" 222 | #define SCNoLEAST16 "ho" 223 | #define SCNuLEAST16 "hu" 224 | #define SCNxLEAST16 "hx" 225 | #define SCNXLEAST16 "hX" 226 | #define SCNoFAST16 "ho" 227 | #define SCNuFAST16 "hu" 228 | #define SCNxFAST16 "hx" 229 | #define SCNXFAST16 "hX" 230 | 231 | #define SCNo32 "lo" 232 | #define SCNu32 "lu" 233 | #define SCNx32 "lx" 234 | #define SCNX32 "lX" 235 | #define SCNoLEAST32 "lo" 236 | #define SCNuLEAST32 "lu" 237 | #define SCNxLEAST32 "lx" 238 | #define SCNXLEAST32 "lX" 239 | #define SCNoFAST32 "lo" 240 | #define SCNuFAST32 "lu" 241 | #define SCNxFAST32 "lx" 242 | #define SCNXFAST32 "lX" 243 | 244 | #define SCNo64 "I64o" 245 | #define SCNu64 "I64u" 246 | #define SCNx64 "I64x" 247 | #define SCNX64 "I64X" 248 | #define SCNoLEAST64 "I64o" 249 | #define SCNuLEAST64 "I64u" 250 | #define SCNxLEAST64 "I64x" 251 | #define SCNXLEAST64 "I64X" 252 | #define SCNoFAST64 "I64o" 253 | #define SCNuFAST64 "I64u" 254 | #define SCNxFAST64 "I64x" 255 | #define SCNXFAST64 "I64X" 256 | 257 | #define SCNoMAX "I64o" 258 | #define SCNuMAX "I64u" 259 | #define SCNxMAX "I64x" 260 | #define SCNXMAX "I64X" 261 | 262 | #ifdef _WIN64 // [ 263 | # define SCNoPTR "I64o" 264 | # define SCNuPTR "I64u" 265 | # define SCNxPTR "I64x" 266 | # define SCNXPTR "I64X" 267 | #else // _WIN64 ][ 268 | # define SCNoPTR "lo" 269 | # define SCNuPTR "lu" 270 | # define SCNxPTR "lx" 271 | # define SCNXPTR "lX" 272 | #endif // _WIN64 ] 273 | 274 | #endif // __STDC_FORMAT_MACROS ] 275 | 276 | // 7.8.2 Functions for greatest-width integer types 277 | 278 | // 7.8.2.1 The imaxabs function 279 | #define imaxabs _abs64 280 | 281 | // 7.8.2.2 The imaxdiv function 282 | 283 | // This is modified version of div() function from Microsoft's div.c found 284 | // in %MSVC.NET%\crt\src\div.c 285 | #ifdef STATIC_IMAXDIV // [ 286 | static 287 | #else // STATIC_IMAXDIV ][ 288 | _inline 289 | #endif // STATIC_IMAXDIV ] 290 | imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) 291 | { 292 | imaxdiv_t result; 293 | 294 | result.quot = numer / denom; 295 | result.rem = numer % denom; 296 | 297 | if (numer < 0 && result.rem > 0) { 298 | // did division wrong; must fix up 299 | ++result.quot; 300 | result.rem -= denom; 301 | } 302 | 303 | return result; 304 | } 305 | 306 | // 7.8.2.3 The strtoimax and strtoumax functions 307 | #define strtoimax _strtoi64 308 | #define strtoumax _strtoui64 309 | 310 | // 7.8.2.4 The wcstoimax and wcstoumax functions 311 | #define wcstoimax _wcstoi64 312 | #define wcstoumax _wcstoui64 313 | 314 | #endif // _MSC_VER >= 1800 315 | 316 | #endif // _MSC_INTTYPES_H_ ] 317 | -------------------------------------------------------------------------------- /include/rapidjson/msinttypes/stdint.h: -------------------------------------------------------------------------------- 1 | // ISO C9x compliant stdint.h for Microsoft Visual Studio 2 | // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 3 | // 4 | // Copyright (c) 2006-2013 Alexander Chemeris 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are met: 8 | // 9 | // 1. Redistributions of source code must retain the above copyright notice, 10 | // this list of conditions and the following disclaimer. 11 | // 12 | // 2. Redistributions in binary form must reproduce the above copyright 13 | // notice, this list of conditions and the following disclaimer in the 14 | // documentation and/or other materials provided with the distribution. 15 | // 16 | // 3. Neither the name of the product nor the names of its contributors may 17 | // be used to endorse or promote products derived from this software 18 | // without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 21 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 23 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | // 31 | /////////////////////////////////////////////////////////////////////////////// 32 | 33 | // The above software in this distribution may have been modified by 34 | // THL A29 Limited ("Tencent Modifications"). 35 | // All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. 36 | 37 | #ifndef _MSC_VER // [ 38 | #error "Use this header only with Microsoft Visual C++ compilers!" 39 | #endif // _MSC_VER ] 40 | 41 | #ifndef _MSC_STDINT_H_ // [ 42 | #define _MSC_STDINT_H_ 43 | 44 | #if _MSC_VER > 1000 45 | #pragma once 46 | #endif 47 | 48 | // miloyip: Originally Visual Studio 2010 uses its own stdint.h. However it generates warning with INT64_C(), so change to use this file for vs2010. 49 | #if _MSC_VER >= 1600 // [ 50 | #include 51 | 52 | #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 53 | 54 | #undef INT8_C 55 | #undef INT16_C 56 | #undef INT32_C 57 | #undef INT64_C 58 | #undef UINT8_C 59 | #undef UINT16_C 60 | #undef UINT32_C 61 | #undef UINT64_C 62 | 63 | // 7.18.4.1 Macros for minimum-width integer constants 64 | 65 | #define INT8_C(val) val##i8 66 | #define INT16_C(val) val##i16 67 | #define INT32_C(val) val##i32 68 | #define INT64_C(val) val##i64 69 | 70 | #define UINT8_C(val) val##ui8 71 | #define UINT16_C(val) val##ui16 72 | #define UINT32_C(val) val##ui32 73 | #define UINT64_C(val) val##ui64 74 | 75 | // 7.18.4.2 Macros for greatest-width integer constants 76 | // These #ifndef's are needed to prevent collisions with . 77 | // Check out Issue 9 for the details. 78 | #ifndef INTMAX_C // [ 79 | # define INTMAX_C INT64_C 80 | #endif // INTMAX_C ] 81 | #ifndef UINTMAX_C // [ 82 | # define UINTMAX_C UINT64_C 83 | #endif // UINTMAX_C ] 84 | 85 | #endif // __STDC_CONSTANT_MACROS ] 86 | 87 | #else // ] _MSC_VER >= 1700 [ 88 | 89 | #include 90 | 91 | // For Visual Studio 6 in C++ mode and for many Visual Studio versions when 92 | // compiling for ARM we should wrap include with 'extern "C++" {}' 93 | // or compiler give many errors like this: 94 | // error C2733: second C linkage of overloaded function 'wmemchr' not allowed 95 | #ifdef __cplusplus 96 | extern "C" { 97 | #endif 98 | # include 99 | #ifdef __cplusplus 100 | } 101 | #endif 102 | 103 | // Define _W64 macros to mark types changing their size, like intptr_t. 104 | #ifndef _W64 105 | # if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 106 | # define _W64 __w64 107 | # else 108 | # define _W64 109 | # endif 110 | #endif 111 | 112 | 113 | // 7.18.1 Integer types 114 | 115 | // 7.18.1.1 Exact-width integer types 116 | 117 | // Visual Studio 6 and Embedded Visual C++ 4 doesn't 118 | // realize that, e.g. char has the same size as __int8 119 | // so we give up on __intX for them. 120 | #if (_MSC_VER < 1300) 121 | typedef signed char int8_t; 122 | typedef signed short int16_t; 123 | typedef signed int int32_t; 124 | typedef unsigned char uint8_t; 125 | typedef unsigned short uint16_t; 126 | typedef unsigned int uint32_t; 127 | #else 128 | typedef signed __int8 int8_t; 129 | typedef signed __int16 int16_t; 130 | typedef signed __int32 int32_t; 131 | typedef unsigned __int8 uint8_t; 132 | typedef unsigned __int16 uint16_t; 133 | typedef unsigned __int32 uint32_t; 134 | #endif 135 | typedef signed __int64 int64_t; 136 | typedef unsigned __int64 uint64_t; 137 | 138 | 139 | // 7.18.1.2 Minimum-width integer types 140 | typedef int8_t int_least8_t; 141 | typedef int16_t int_least16_t; 142 | typedef int32_t int_least32_t; 143 | typedef int64_t int_least64_t; 144 | typedef uint8_t uint_least8_t; 145 | typedef uint16_t uint_least16_t; 146 | typedef uint32_t uint_least32_t; 147 | typedef uint64_t uint_least64_t; 148 | 149 | // 7.18.1.3 Fastest minimum-width integer types 150 | typedef int8_t int_fast8_t; 151 | typedef int16_t int_fast16_t; 152 | typedef int32_t int_fast32_t; 153 | typedef int64_t int_fast64_t; 154 | typedef uint8_t uint_fast8_t; 155 | typedef uint16_t uint_fast16_t; 156 | typedef uint32_t uint_fast32_t; 157 | typedef uint64_t uint_fast64_t; 158 | 159 | // 7.18.1.4 Integer types capable of holding object pointers 160 | #ifdef _WIN64 // [ 161 | typedef signed __int64 intptr_t; 162 | typedef unsigned __int64 uintptr_t; 163 | #else // _WIN64 ][ 164 | typedef _W64 signed int intptr_t; 165 | typedef _W64 unsigned int uintptr_t; 166 | #endif // _WIN64 ] 167 | 168 | // 7.18.1.5 Greatest-width integer types 169 | typedef int64_t intmax_t; 170 | typedef uint64_t uintmax_t; 171 | 172 | 173 | // 7.18.2 Limits of specified-width integer types 174 | 175 | #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 176 | 177 | // 7.18.2.1 Limits of exact-width integer types 178 | #define INT8_MIN ((int8_t)_I8_MIN) 179 | #define INT8_MAX _I8_MAX 180 | #define INT16_MIN ((int16_t)_I16_MIN) 181 | #define INT16_MAX _I16_MAX 182 | #define INT32_MIN ((int32_t)_I32_MIN) 183 | #define INT32_MAX _I32_MAX 184 | #define INT64_MIN ((int64_t)_I64_MIN) 185 | #define INT64_MAX _I64_MAX 186 | #define UINT8_MAX _UI8_MAX 187 | #define UINT16_MAX _UI16_MAX 188 | #define UINT32_MAX _UI32_MAX 189 | #define UINT64_MAX _UI64_MAX 190 | 191 | // 7.18.2.2 Limits of minimum-width integer types 192 | #define INT_LEAST8_MIN INT8_MIN 193 | #define INT_LEAST8_MAX INT8_MAX 194 | #define INT_LEAST16_MIN INT16_MIN 195 | #define INT_LEAST16_MAX INT16_MAX 196 | #define INT_LEAST32_MIN INT32_MIN 197 | #define INT_LEAST32_MAX INT32_MAX 198 | #define INT_LEAST64_MIN INT64_MIN 199 | #define INT_LEAST64_MAX INT64_MAX 200 | #define UINT_LEAST8_MAX UINT8_MAX 201 | #define UINT_LEAST16_MAX UINT16_MAX 202 | #define UINT_LEAST32_MAX UINT32_MAX 203 | #define UINT_LEAST64_MAX UINT64_MAX 204 | 205 | // 7.18.2.3 Limits of fastest minimum-width integer types 206 | #define INT_FAST8_MIN INT8_MIN 207 | #define INT_FAST8_MAX INT8_MAX 208 | #define INT_FAST16_MIN INT16_MIN 209 | #define INT_FAST16_MAX INT16_MAX 210 | #define INT_FAST32_MIN INT32_MIN 211 | #define INT_FAST32_MAX INT32_MAX 212 | #define INT_FAST64_MIN INT64_MIN 213 | #define INT_FAST64_MAX INT64_MAX 214 | #define UINT_FAST8_MAX UINT8_MAX 215 | #define UINT_FAST16_MAX UINT16_MAX 216 | #define UINT_FAST32_MAX UINT32_MAX 217 | #define UINT_FAST64_MAX UINT64_MAX 218 | 219 | // 7.18.2.4 Limits of integer types capable of holding object pointers 220 | #ifdef _WIN64 // [ 221 | # define INTPTR_MIN INT64_MIN 222 | # define INTPTR_MAX INT64_MAX 223 | # define UINTPTR_MAX UINT64_MAX 224 | #else // _WIN64 ][ 225 | # define INTPTR_MIN INT32_MIN 226 | # define INTPTR_MAX INT32_MAX 227 | # define UINTPTR_MAX UINT32_MAX 228 | #endif // _WIN64 ] 229 | 230 | // 7.18.2.5 Limits of greatest-width integer types 231 | #define INTMAX_MIN INT64_MIN 232 | #define INTMAX_MAX INT64_MAX 233 | #define UINTMAX_MAX UINT64_MAX 234 | 235 | // 7.18.3 Limits of other integer types 236 | 237 | #ifdef _WIN64 // [ 238 | # define PTRDIFF_MIN _I64_MIN 239 | # define PTRDIFF_MAX _I64_MAX 240 | #else // _WIN64 ][ 241 | # define PTRDIFF_MIN _I32_MIN 242 | # define PTRDIFF_MAX _I32_MAX 243 | #endif // _WIN64 ] 244 | 245 | #define SIG_ATOMIC_MIN INT_MIN 246 | #define SIG_ATOMIC_MAX INT_MAX 247 | 248 | #ifndef SIZE_MAX // [ 249 | # ifdef _WIN64 // [ 250 | # define SIZE_MAX _UI64_MAX 251 | # else // _WIN64 ][ 252 | # define SIZE_MAX _UI32_MAX 253 | # endif // _WIN64 ] 254 | #endif // SIZE_MAX ] 255 | 256 | // WCHAR_MIN and WCHAR_MAX are also defined in 257 | #ifndef WCHAR_MIN // [ 258 | # define WCHAR_MIN 0 259 | #endif // WCHAR_MIN ] 260 | #ifndef WCHAR_MAX // [ 261 | # define WCHAR_MAX _UI16_MAX 262 | #endif // WCHAR_MAX ] 263 | 264 | #define WINT_MIN 0 265 | #define WINT_MAX _UI16_MAX 266 | 267 | #endif // __STDC_LIMIT_MACROS ] 268 | 269 | 270 | // 7.18.4 Limits of other integer types 271 | 272 | #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 273 | 274 | // 7.18.4.1 Macros for minimum-width integer constants 275 | 276 | #define INT8_C(val) val##i8 277 | #define INT16_C(val) val##i16 278 | #define INT32_C(val) val##i32 279 | #define INT64_C(val) val##i64 280 | 281 | #define UINT8_C(val) val##ui8 282 | #define UINT16_C(val) val##ui16 283 | #define UINT32_C(val) val##ui32 284 | #define UINT64_C(val) val##ui64 285 | 286 | // 7.18.4.2 Macros for greatest-width integer constants 287 | // These #ifndef's are needed to prevent collisions with . 288 | // Check out Issue 9 for the details. 289 | #ifndef INTMAX_C // [ 290 | # define INTMAX_C INT64_C 291 | #endif // INTMAX_C ] 292 | #ifndef UINTMAX_C // [ 293 | # define UINTMAX_C UINT64_C 294 | #endif // UINTMAX_C ] 295 | 296 | #endif // __STDC_CONSTANT_MACROS ] 297 | 298 | #endif // _MSC_VER >= 1600 ] 299 | 300 | #endif // _MSC_STDINT_H_ ] 301 | -------------------------------------------------------------------------------- /include/rapidjson/prettywriter.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_PRETTYWRITER_H_ 16 | #define RAPIDJSON_PRETTYWRITER_H_ 17 | 18 | #include "writer.h" 19 | 20 | #ifdef __GNUC__ 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(effc++) 23 | #endif 24 | 25 | RAPIDJSON_NAMESPACE_BEGIN 26 | 27 | //! Writer with indentation and spacing. 28 | /*! 29 | \tparam OutputStream Type of ouptut os. 30 | \tparam SourceEncoding Encoding of source string. 31 | \tparam TargetEncoding Encoding of output stream. 32 | \tparam StackAllocator Type of allocator for allocating memory of stack. 33 | */ 34 | template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator> 35 | class PrettyWriter : public Writer { 36 | public: 37 | typedef Writer Base; 38 | typedef typename Base::Ch Ch; 39 | 40 | //! Constructor 41 | /*! \param os Output stream. 42 | \param allocator User supplied allocator. If it is null, it will create a private one. 43 | \param levelDepth Initial capacity of stack. 44 | */ 45 | PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : 46 | Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} 47 | 48 | //! Set custom indentation. 49 | /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). 50 | \param indentCharCount Number of indent characters for each indentation level. 51 | \note The default indentation is 4 spaces. 52 | */ 53 | PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { 54 | RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); 55 | indentChar_ = indentChar; 56 | indentCharCount_ = indentCharCount; 57 | return *this; 58 | } 59 | 60 | /*! @name Implementation of Handler 61 | \see Handler 62 | */ 63 | //@{ 64 | 65 | bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } 66 | bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } 67 | bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } 68 | bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } 69 | bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } 70 | bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } 71 | bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } 72 | 73 | bool String(const Ch* str, SizeType length, bool copy = false) { 74 | (void)copy; 75 | PrettyPrefix(kStringType); 76 | return Base::WriteString(str, length); 77 | } 78 | 79 | #if RAPIDJSON_HAS_STDSTRING 80 | bool String(const std::basic_string& str) { 81 | return String(str.data(), SizeType(str.size())); 82 | } 83 | #endif 84 | 85 | bool StartObject() { 86 | PrettyPrefix(kObjectType); 87 | new (Base::level_stack_.template Push()) typename Base::Level(false); 88 | return Base::WriteStartObject(); 89 | } 90 | 91 | bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } 92 | 93 | bool EndObject(SizeType memberCount = 0) { 94 | (void)memberCount; 95 | RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); 96 | RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); 97 | bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; 98 | 99 | if (!empty) { 100 | Base::os_->Put('\n'); 101 | WriteIndent(); 102 | } 103 | bool ret = Base::WriteEndObject(); 104 | (void)ret; 105 | RAPIDJSON_ASSERT(ret == true); 106 | if (Base::level_stack_.Empty()) // end of json text 107 | Base::os_->Flush(); 108 | return true; 109 | } 110 | 111 | bool StartArray() { 112 | PrettyPrefix(kArrayType); 113 | new (Base::level_stack_.template Push()) typename Base::Level(true); 114 | return Base::WriteStartArray(); 115 | } 116 | 117 | bool EndArray(SizeType memberCount = 0) { 118 | (void)memberCount; 119 | RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); 120 | RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); 121 | bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; 122 | 123 | if (!empty) { 124 | Base::os_->Put('\n'); 125 | WriteIndent(); 126 | } 127 | bool ret = Base::WriteEndArray(); 128 | (void)ret; 129 | RAPIDJSON_ASSERT(ret == true); 130 | if (Base::level_stack_.Empty()) // end of json text 131 | Base::os_->Flush(); 132 | return true; 133 | } 134 | 135 | //@} 136 | 137 | /*! @name Convenience extensions */ 138 | //@{ 139 | 140 | //! Simpler but slower overload. 141 | bool String(const Ch* str) { return String(str, internal::StrLen(str)); } 142 | bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } 143 | 144 | //@} 145 | protected: 146 | void PrettyPrefix(Type type) { 147 | (void)type; 148 | if (Base::level_stack_.GetSize() != 0) { // this value is not at root 149 | typename Base::Level* level = Base::level_stack_.template Top(); 150 | 151 | if (level->inArray) { 152 | if (level->valueCount > 0) { 153 | Base::os_->Put(','); // add comma if it is not the first element in array 154 | Base::os_->Put('\n'); 155 | } 156 | else 157 | Base::os_->Put('\n'); 158 | WriteIndent(); 159 | } 160 | else { // in object 161 | if (level->valueCount > 0) { 162 | if (level->valueCount % 2 == 0) { 163 | Base::os_->Put(','); 164 | Base::os_->Put('\n'); 165 | } 166 | else { 167 | Base::os_->Put(':'); 168 | Base::os_->Put(' '); 169 | } 170 | } 171 | else 172 | Base::os_->Put('\n'); 173 | 174 | if (level->valueCount % 2 == 0) 175 | WriteIndent(); 176 | } 177 | if (!level->inArray && level->valueCount % 2 == 0) 178 | RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name 179 | level->valueCount++; 180 | } 181 | else { 182 | RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. 183 | Base::hasRoot_ = true; 184 | } 185 | } 186 | 187 | void WriteIndent() { 188 | size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; 189 | PutN(*Base::os_, indentChar_, count); 190 | } 191 | 192 | Ch indentChar_; 193 | unsigned indentCharCount_; 194 | 195 | private: 196 | // Prohibit copy constructor & assignment operator. 197 | PrettyWriter(const PrettyWriter&); 198 | PrettyWriter& operator=(const PrettyWriter&); 199 | }; 200 | 201 | RAPIDJSON_NAMESPACE_END 202 | 203 | #ifdef __GNUC__ 204 | RAPIDJSON_DIAG_POP 205 | #endif 206 | 207 | #endif // RAPIDJSON_RAPIDJSON_H_ 208 | -------------------------------------------------------------------------------- /include/rapidjson/stringbuffer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_STRINGBUFFER_H_ 16 | #define RAPIDJSON_STRINGBUFFER_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 21 | #include // std::move 22 | #endif 23 | 24 | #include "internal/stack.h" 25 | 26 | RAPIDJSON_NAMESPACE_BEGIN 27 | 28 | //! Represents an in-memory output stream. 29 | /*! 30 | \tparam Encoding Encoding of the stream. 31 | \tparam Allocator type for allocating memory buffer. 32 | \note implements Stream concept 33 | */ 34 | template 35 | class GenericStringBuffer { 36 | public: 37 | typedef typename Encoding::Ch Ch; 38 | 39 | GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} 40 | 41 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 42 | GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} 43 | GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { 44 | if (&rhs != this) 45 | stack_ = std::move(rhs.stack_); 46 | return *this; 47 | } 48 | #endif 49 | 50 | void Put(Ch c) { *stack_.template Push() = c; } 51 | void Flush() {} 52 | 53 | void Clear() { stack_.Clear(); } 54 | void ShrinkToFit() { 55 | // Push and pop a null terminator. This is safe. 56 | *stack_.template Push() = '\0'; 57 | stack_.ShrinkToFit(); 58 | stack_.template Pop(1); 59 | } 60 | Ch* Push(size_t count) { return stack_.template Push(count); } 61 | void Pop(size_t count) { stack_.template Pop(count); } 62 | 63 | const Ch* GetString() const { 64 | // Push and pop a null terminator. This is safe. 65 | *stack_.template Push() = '\0'; 66 | stack_.template Pop(1); 67 | 68 | return stack_.template Bottom(); 69 | } 70 | 71 | size_t GetSize() const { return stack_.GetSize(); } 72 | 73 | static const size_t kDefaultCapacity = 256; 74 | mutable internal::Stack stack_; 75 | 76 | private: 77 | // Prohibit copy constructor & assignment operator. 78 | GenericStringBuffer(const GenericStringBuffer&); 79 | GenericStringBuffer& operator=(const GenericStringBuffer&); 80 | }; 81 | 82 | //! String buffer with UTF8 encoding 83 | typedef GenericStringBuffer > StringBuffer; 84 | 85 | //! Implement specialized version of PutN() with memset() for better performance. 86 | template<> 87 | inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { 88 | std::memset(stream.stack_.Push(n), c, n * sizeof(c)); 89 | } 90 | 91 | RAPIDJSON_NAMESPACE_END 92 | 93 | #endif // RAPIDJSON_STRINGBUFFER_H_ 94 | -------------------------------------------------------------------------------- /include/rapidjson/writer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_WRITER_H_ 16 | #define RAPIDJSON_WRITER_H_ 17 | 18 | #include "rapidjson.h" 19 | #include "internal/stack.h" 20 | #include "internal/strfunc.h" 21 | #include "internal/dtoa.h" 22 | #include "internal/itoa.h" 23 | #include "stringbuffer.h" 24 | #include // placement new 25 | 26 | #if RAPIDJSON_HAS_STDSTRING 27 | #include 28 | #endif 29 | 30 | #ifdef _MSC_VER 31 | RAPIDJSON_DIAG_PUSH 32 | RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant 33 | #endif 34 | 35 | RAPIDJSON_NAMESPACE_BEGIN 36 | 37 | //! JSON writer 38 | /*! Writer implements the concept Handler. 39 | It generates JSON text by events to an output os. 40 | 41 | User may programmatically calls the functions of a writer to generate JSON text. 42 | 43 | On the other side, a writer can also be passed to objects that generates events, 44 | 45 | for example Reader::Parse() and Document::Accept(). 46 | 47 | \tparam OutputStream Type of output stream. 48 | \tparam SourceEncoding Encoding of source string. 49 | \tparam TargetEncoding Encoding of output stream. 50 | \tparam StackAllocator Type of allocator for allocating memory of stack. 51 | \note implements Handler concept 52 | */ 53 | template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator> 54 | class Writer { 55 | public: 56 | typedef typename SourceEncoding::Ch Ch; 57 | 58 | //! Constructor 59 | /*! \param os Output stream. 60 | \param stackAllocator User supplied allocator. If it is null, it will create a private one. 61 | \param levelDepth Initial capacity of stack. 62 | */ 63 | explicit 64 | Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : 65 | os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), hasRoot_(false) {} 66 | 67 | explicit 68 | Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : 69 | os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {} 70 | 71 | //! Reset the writer with a new stream. 72 | /*! 73 | This function reset the writer with a new stream and default settings, 74 | in order to make a Writer object reusable for output multiple JSONs. 75 | 76 | \param os New output stream. 77 | \code 78 | Writer writer(os1); 79 | writer.StartObject(); 80 | // ... 81 | writer.EndObject(); 82 | 83 | writer.Reset(os2); 84 | writer.StartObject(); 85 | // ... 86 | writer.EndObject(); 87 | \endcode 88 | */ 89 | void Reset(OutputStream& os) { 90 | os_ = &os; 91 | hasRoot_ = false; 92 | level_stack_.Clear(); 93 | } 94 | 95 | //! Checks whether the output is a complete JSON. 96 | /*! 97 | A complete JSON has a complete root object or array. 98 | */ 99 | bool IsComplete() const { 100 | return hasRoot_ && level_stack_.Empty(); 101 | } 102 | 103 | /*!@name Implementation of Handler 104 | \see Handler 105 | */ 106 | //@{ 107 | 108 | bool Null() { Prefix(kNullType); return WriteNull(); } 109 | bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); } 110 | bool Int(int i) { Prefix(kNumberType); return WriteInt(i); } 111 | bool Uint(unsigned u) { Prefix(kNumberType); return WriteUint(u); } 112 | bool Int64(int64_t i64) { Prefix(kNumberType); return WriteInt64(i64); } 113 | bool Uint64(uint64_t u64) { Prefix(kNumberType); return WriteUint64(u64); } 114 | 115 | //! Writes the given \c double value to the stream 116 | /*! 117 | \param d The value to be written. 118 | \return Whether it is succeed. 119 | */ 120 | bool Double(double d) { Prefix(kNumberType); return WriteDouble(d); } 121 | 122 | bool String(const Ch* str, SizeType length, bool copy = false) { 123 | (void)copy; 124 | Prefix(kStringType); 125 | return WriteString(str, length); 126 | } 127 | 128 | #if RAPIDJSON_HAS_STDSTRING 129 | bool String(const std::basic_string& str) { 130 | return String(str.data(), SizeType(str.size())); 131 | } 132 | #endif 133 | 134 | bool StartObject() { 135 | Prefix(kObjectType); 136 | new (level_stack_.template Push()) Level(false); 137 | return WriteStartObject(); 138 | } 139 | 140 | bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } 141 | 142 | bool EndObject(SizeType memberCount = 0) { 143 | (void)memberCount; 144 | RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); 145 | RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); 146 | level_stack_.template Pop(1); 147 | bool ret = WriteEndObject(); 148 | if (level_stack_.Empty()) // end of json text 149 | os_->Flush(); 150 | return ret; 151 | } 152 | 153 | bool StartArray() { 154 | Prefix(kArrayType); 155 | new (level_stack_.template Push()) Level(true); 156 | return WriteStartArray(); 157 | } 158 | 159 | bool EndArray(SizeType elementCount = 0) { 160 | (void)elementCount; 161 | RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); 162 | RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); 163 | level_stack_.template Pop(1); 164 | bool ret = WriteEndArray(); 165 | if (level_stack_.Empty()) // end of json text 166 | os_->Flush(); 167 | return ret; 168 | } 169 | //@} 170 | 171 | /*! @name Convenience extensions */ 172 | //@{ 173 | 174 | //! Simpler but slower overload. 175 | bool String(const Ch* str) { return String(str, internal::StrLen(str)); } 176 | bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } 177 | 178 | //@} 179 | 180 | protected: 181 | //! Information for each nested level 182 | struct Level { 183 | Level(bool inArray_) : valueCount(0), inArray(inArray_) {} 184 | size_t valueCount; //!< number of values in this level 185 | bool inArray; //!< true if in array, otherwise in object 186 | }; 187 | 188 | static const size_t kDefaultLevelDepth = 32; 189 | 190 | bool WriteNull() { 191 | os_->Put('n'); os_->Put('u'); os_->Put('l'); os_->Put('l'); return true; 192 | } 193 | 194 | bool WriteBool(bool b) { 195 | if (b) { 196 | os_->Put('t'); os_->Put('r'); os_->Put('u'); os_->Put('e'); 197 | } 198 | else { 199 | os_->Put('f'); os_->Put('a'); os_->Put('l'); os_->Put('s'); os_->Put('e'); 200 | } 201 | return true; 202 | } 203 | 204 | bool WriteInt(int i) { 205 | char buffer[11]; 206 | const char* end = internal::i32toa(i, buffer); 207 | for (const char* p = buffer; p != end; ++p) 208 | os_->Put(*p); 209 | return true; 210 | } 211 | 212 | bool WriteUint(unsigned u) { 213 | char buffer[10]; 214 | const char* end = internal::u32toa(u, buffer); 215 | for (const char* p = buffer; p != end; ++p) 216 | os_->Put(*p); 217 | return true; 218 | } 219 | 220 | bool WriteInt64(int64_t i64) { 221 | char buffer[21]; 222 | const char* end = internal::i64toa(i64, buffer); 223 | for (const char* p = buffer; p != end; ++p) 224 | os_->Put(*p); 225 | return true; 226 | } 227 | 228 | bool WriteUint64(uint64_t u64) { 229 | char buffer[20]; 230 | char* end = internal::u64toa(u64, buffer); 231 | for (char* p = buffer; p != end; ++p) 232 | os_->Put(*p); 233 | return true; 234 | } 235 | 236 | bool WriteDouble(double d) { 237 | char buffer[25]; 238 | char* end = internal::dtoa(d, buffer); 239 | for (char* p = buffer; p != end; ++p) 240 | os_->Put(*p); 241 | return true; 242 | } 243 | 244 | bool WriteString(const Ch* str, SizeType length) { 245 | static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 246 | static const char escape[256] = { 247 | #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 248 | //0 1 2 3 4 5 6 7 8 9 A B C D E F 249 | 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 250 | 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 251 | 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 252 | Z16, Z16, // 30~4F 253 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 254 | Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF 255 | #undef Z16 256 | }; 257 | 258 | os_->Put('\"'); 259 | GenericStringStream is(str); 260 | while (is.Tell() < length) { 261 | const Ch c = is.Peek(); 262 | if (!TargetEncoding::supportUnicode && (unsigned)c >= 0x80) { 263 | // Unicode escaping 264 | unsigned codepoint; 265 | if (!SourceEncoding::Decode(is, &codepoint)) 266 | return false; 267 | os_->Put('\\'); 268 | os_->Put('u'); 269 | if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { 270 | os_->Put(hexDigits[(codepoint >> 12) & 15]); 271 | os_->Put(hexDigits[(codepoint >> 8) & 15]); 272 | os_->Put(hexDigits[(codepoint >> 4) & 15]); 273 | os_->Put(hexDigits[(codepoint ) & 15]); 274 | } 275 | else { 276 | RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); 277 | // Surrogate pair 278 | unsigned s = codepoint - 0x010000; 279 | unsigned lead = (s >> 10) + 0xD800; 280 | unsigned trail = (s & 0x3FF) + 0xDC00; 281 | os_->Put(hexDigits[(lead >> 12) & 15]); 282 | os_->Put(hexDigits[(lead >> 8) & 15]); 283 | os_->Put(hexDigits[(lead >> 4) & 15]); 284 | os_->Put(hexDigits[(lead ) & 15]); 285 | os_->Put('\\'); 286 | os_->Put('u'); 287 | os_->Put(hexDigits[(trail >> 12) & 15]); 288 | os_->Put(hexDigits[(trail >> 8) & 15]); 289 | os_->Put(hexDigits[(trail >> 4) & 15]); 290 | os_->Put(hexDigits[(trail ) & 15]); 291 | } 292 | } 293 | else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) { 294 | is.Take(); 295 | os_->Put('\\'); 296 | os_->Put(escape[(unsigned char)c]); 297 | if (escape[(unsigned char)c] == 'u') { 298 | os_->Put('0'); 299 | os_->Put('0'); 300 | os_->Put(hexDigits[(unsigned char)c >> 4]); 301 | os_->Put(hexDigits[(unsigned char)c & 0xF]); 302 | } 303 | } 304 | else 305 | if (!Transcoder::Transcode(is, *os_)) 306 | return false; 307 | } 308 | os_->Put('\"'); 309 | return true; 310 | } 311 | 312 | bool WriteStartObject() { os_->Put('{'); return true; } 313 | bool WriteEndObject() { os_->Put('}'); return true; } 314 | bool WriteStartArray() { os_->Put('['); return true; } 315 | bool WriteEndArray() { os_->Put(']'); return true; } 316 | 317 | void Prefix(Type type) { 318 | (void)type; 319 | if (level_stack_.GetSize() != 0) { // this value is not at root 320 | Level* level = level_stack_.template Top(); 321 | if (level->valueCount > 0) { 322 | if (level->inArray) 323 | os_->Put(','); // add comma if it is not the first element in array 324 | else // in object 325 | os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); 326 | } 327 | if (!level->inArray && level->valueCount % 2 == 0) 328 | RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name 329 | level->valueCount++; 330 | } 331 | else { 332 | RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. 333 | hasRoot_ = true; 334 | } 335 | } 336 | 337 | OutputStream* os_; 338 | internal::Stack level_stack_; 339 | bool hasRoot_; 340 | 341 | private: 342 | // Prohibit copy constructor & assignment operator. 343 | Writer(const Writer&); 344 | Writer& operator=(const Writer&); 345 | }; 346 | 347 | // Full specialization for StringStream to prevent memory copying 348 | 349 | template<> 350 | inline bool Writer::WriteInt(int i) { 351 | char *buffer = os_->Push(11); 352 | const char* end = internal::i32toa(i, buffer); 353 | os_->Pop(11 - (end - buffer)); 354 | return true; 355 | } 356 | 357 | template<> 358 | inline bool Writer::WriteUint(unsigned u) { 359 | char *buffer = os_->Push(10); 360 | const char* end = internal::u32toa(u, buffer); 361 | os_->Pop(10 - (end - buffer)); 362 | return true; 363 | } 364 | 365 | template<> 366 | inline bool Writer::WriteInt64(int64_t i64) { 367 | char *buffer = os_->Push(21); 368 | const char* end = internal::i64toa(i64, buffer); 369 | os_->Pop(21 - (end - buffer)); 370 | return true; 371 | } 372 | 373 | template<> 374 | inline bool Writer::WriteUint64(uint64_t u) { 375 | char *buffer = os_->Push(20); 376 | const char* end = internal::u64toa(u, buffer); 377 | os_->Pop(20 - (end - buffer)); 378 | return true; 379 | } 380 | 381 | template<> 382 | inline bool Writer::WriteDouble(double d) { 383 | char *buffer = os_->Push(25); 384 | char* end = internal::dtoa(d, buffer); 385 | os_->Pop(25 - (end - buffer)); 386 | return true; 387 | } 388 | 389 | RAPIDJSON_NAMESPACE_END 390 | 391 | #ifdef _MSC_VER 392 | RAPIDJSON_DIAG_POP 393 | #endif 394 | 395 | #endif // RAPIDJSON_RAPIDJSON_H_ 396 | -------------------------------------------------------------------------------- /json_feature.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | #include "json_feature.hpp" 7 | 8 | void JSONFeature::add_id(const std::string& prefix, osmium::object_id_type id) { 9 | m_writer.Key("id"); 10 | m_writer.String(prefix + std::to_string(id)); 11 | } 12 | 13 | void JSONFeature::add_properties(const osmium::OSMObject& object) { 14 | m_writer.Key("properties"); 15 | 16 | m_writer.StartObject(); 17 | 18 | m_writer.String(m_attr_names.id); 19 | m_writer.Int64(object.type() == osmium::item_type::area ? osmium::area_id_to_object_id(object.id()) : object.id()); 20 | 21 | m_writer.String(m_attr_names.type); 22 | if (object.type() == osmium::item_type::area) { 23 | if (static_cast(object).from_way()) { 24 | m_writer.String("way"); 25 | } else { 26 | m_writer.String("relation"); 27 | } 28 | } else { 29 | m_writer.String(osmium::item_type_to_name(object.type())); 30 | } 31 | 32 | m_writer.String(m_attr_names.version); 33 | m_writer.Int(object.version()); 34 | 35 | m_writer.String(m_attr_names.changeset); 36 | m_writer.Int(object.changeset()); 37 | 38 | m_writer.String(m_attr_names.uid); 39 | m_writer.Int(object.uid()); 40 | 41 | m_writer.String(m_attr_names.user); 42 | m_writer.String(object.user()); 43 | 44 | m_writer.String(m_attr_names.timestamp); 45 | m_writer.Int(object.timestamp().seconds_since_epoch()); 46 | 47 | for (const auto& tag : object.tags()) { 48 | m_writer.String(tag.key()); 49 | m_writer.String(tag.value()); 50 | } 51 | m_writer.EndObject(); 52 | } 53 | 54 | void JSONFeature::append_to(std::string& buffer) { 55 | m_writer.EndObject(); 56 | 57 | buffer.append(m_stream.GetString(), m_stream.GetSize()); 58 | buffer.append(1, '\n'); 59 | } 60 | 61 | -------------------------------------------------------------------------------- /json_feature.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #pragma GCC diagnostic push 6 | #pragma GCC diagnostic ignored "-Wold-style-cast" 7 | #define RAPIDJSON_HAS_STDSTRING 1 8 | #include 9 | #include 10 | #pragma GCC diagnostic pop 11 | 12 | #include 13 | #include 14 | 15 | struct attribute_names { 16 | 17 | std::string id; 18 | std::string type; 19 | std::string version; 20 | std::string changeset; 21 | std::string uid; 22 | std::string user; 23 | std::string timestamp; 24 | 25 | attribute_names(const std::string& prefix) : 26 | id(prefix + "id"), 27 | type(prefix + "type"), 28 | version(prefix + "version"), 29 | changeset(prefix + "changeset"), 30 | uid(prefix + "uid"), 31 | user(prefix + "user"), 32 | timestamp(prefix + "timestamp") { 33 | } 34 | 35 | }; // struct attribute_names 36 | 37 | 38 | using writer_type = rapidjson::Writer; 39 | 40 | class JSONFeature { 41 | 42 | rapidjson::StringBuffer m_stream; 43 | writer_type m_writer; 44 | osmium::geom::RapidGeoJSONFactory m_factory; 45 | const attribute_names& m_attr_names; 46 | 47 | public: 48 | 49 | JSONFeature(const attribute_names& attr_names) : 50 | m_stream(), 51 | m_writer(m_stream), 52 | m_factory(m_writer), 53 | m_attr_names(attr_names) { 54 | m_writer.StartObject(); 55 | m_writer.Key("type"); 56 | m_writer.String("Feature"); 57 | } 58 | 59 | void add_point(const osmium::Node& node) { 60 | m_factory.create_point(node); 61 | } 62 | 63 | void add_linestring(const osmium::Way& way) { 64 | m_factory.create_linestring(way); 65 | } 66 | 67 | void add_polygon(const osmium::Way& way) { 68 | m_factory.create_polygon(way); 69 | } 70 | 71 | void add_multipolygon(const osmium::Area& area) { 72 | m_factory.create_multipolygon(area); 73 | } 74 | 75 | void add_id(const std::string& prefix, osmium::object_id_type id); 76 | 77 | void add_properties(const osmium::OSMObject& object); 78 | 79 | void append_to(std::string& buffer); 80 | 81 | }; // class JSONFeature 82 | 83 | -------------------------------------------------------------------------------- /json_handler.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include "json_handler.hpp" 8 | 9 | void JSONHandler::flush_to_output() { 10 | const auto written = write(1, m_buffer.data(), m_buffer.size()); 11 | assert(written == long(m_buffer.size())); 12 | m_buffer.clear(); 13 | } 14 | 15 | void JSONHandler::report_geometry_problem(const osmium::OSMObject& object, const char* error) { 16 | ++m_geometry_error_count; 17 | if (m_error_stream) { 18 | *m_error_stream << osmium::item_type_to_char(object.type()) << object.id() << ":" << error << "\n"; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /json_handler.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "json_feature.hpp" 10 | 11 | namespace osmium { 12 | class OSMObject; 13 | } 14 | 15 | class JSONHandler : public osmium::handler::Handler { 16 | 17 | std::string m_buffer; 18 | attribute_names m_attr_names; 19 | std::unique_ptr m_error_stream; 20 | int m_geometry_error_count; 21 | bool m_with_id; 22 | 23 | void flush_to_output(); 24 | 25 | protected: 26 | 27 | std::string& buffer() noexcept { 28 | return m_buffer; 29 | } 30 | 31 | const attribute_names& attr_names() const noexcept { 32 | return m_attr_names; 33 | } 34 | 35 | bool with_id() const noexcept { 36 | return m_with_id; 37 | } 38 | 39 | void maybe_flush() { 40 | if (m_buffer.size() > 1024*1024) { 41 | flush_to_output(); 42 | } 43 | } 44 | 45 | void report_geometry_problem(const osmium::OSMObject& object, const char* error); 46 | 47 | JSONHandler(const std::string& error_file, const std::string& attr_prefix, bool with_id) : 48 | m_buffer(), 49 | m_attr_names(attr_prefix), 50 | m_error_stream(nullptr), 51 | m_geometry_error_count(0), 52 | m_with_id(with_id) { 53 | if (!error_file.empty()) { 54 | m_error_stream.reset(new std::ofstream(error_file)); 55 | } 56 | } 57 | 58 | ~JSONHandler() { 59 | flush_to_output(); 60 | } 61 | 62 | public: 63 | 64 | int geometry_error_count() const { 65 | return m_geometry_error_count; 66 | } 67 | 68 | }; // class JSONHandler 69 | 70 | -------------------------------------------------------------------------------- /minjur-generate-tilelist.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using index_type = osmium::index::map::Map; 20 | using location_handler_type = osmium::handler::NodeLocationsForWays; 21 | 22 | class TileDiffHandler : public osmium::handler::Handler { 23 | 24 | int m_zoom; 25 | index_type& m_old_index; 26 | index_type& m_tmp_index; 27 | 28 | std::set m_dirty_tiles; 29 | 30 | void add_location(const osmium::Location& location) { 31 | if (location.valid()) { 32 | m_dirty_tiles.emplace(m_zoom, location); 33 | } 34 | } 35 | 36 | public: 37 | 38 | TileDiffHandler(int zoom, index_type& old_index, index_type& tmp_index) : 39 | m_zoom(zoom), 40 | m_old_index(old_index), 41 | m_tmp_index(tmp_index) { 42 | } 43 | 44 | void node(const osmium::Node& node) { 45 | try { 46 | add_location(m_old_index.get(node.id())); 47 | } catch (...) { 48 | } 49 | try { 50 | add_location(node.location()); 51 | } catch (...) { 52 | } 53 | } 54 | 55 | void way(const osmium::Way& way) { 56 | for (const auto& node_ref : way.nodes()) { 57 | try { 58 | add_location(m_old_index.get(node_ref.ref())); 59 | } catch (...) { 60 | } 61 | try { 62 | add_location(m_tmp_index.get(node_ref.ref())); 63 | } catch (...) { 64 | } 65 | } 66 | } 67 | 68 | void dump_tiles() const { 69 | for (const auto& tile : m_dirty_tiles) { 70 | std::cout << tile.z << " " << tile.x << " " << tile.y << "\n"; 71 | } 72 | } 73 | 74 | }; // class TileDiffHandler 75 | 76 | void print_help() { 77 | std::cout << "minjur-generate-tilelist [OPTIONS] OSM-CHANGE-FILE\n\n" \ 78 | << "Output is always to stdout.\n" \ 79 | << "\nOptions:\n" \ 80 | << " -h, --help This help message\n" \ 81 | << " -l, --location_store=TYPE Set location store\n" \ 82 | << " -L, --list-location-stores Show available location stores\n" \ 83 | << " -n, --nodes=sparse|dense Are node IDs sparse or dense?\n" \ 84 | << " -z, --zoom=ZOOM Zoom level for tiles (default: 15)\n"; 85 | } 86 | 87 | int main(int argc, char* argv[]) { 88 | const auto& map_factory = osmium::index::MapFactory::instance(); 89 | 90 | static struct option long_options[] = { 91 | {"help", no_argument, 0, 'h'}, 92 | {"location_store", required_argument, 0, 'l'}, 93 | {"list_location_stores", no_argument, 0, 'L'}, 94 | {"nodes", required_argument, 0, 'n'}, 95 | {"zoom", required_argument, 0, 'z'}, 96 | {0, 0, 0, 0} 97 | }; 98 | 99 | std::string input_filename = "-"; 100 | std::string location_store = "sparse_file_array,locations.dump"; 101 | std::string locations_dump_file; 102 | bool nodes_dense = false; 103 | int zoom = 15; 104 | 105 | while (true) { 106 | int c = getopt_long(argc, argv, "hl:Ln:z", long_options, 0); 107 | if (c == -1) { 108 | break; 109 | } 110 | 111 | switch (c) { 112 | case 'h': 113 | print_help(); 114 | std::exit(0); 115 | case 'l': 116 | location_store = optarg; 117 | break; 118 | case 'L': 119 | std::cout << "Available map types:\n"; 120 | for (const auto& map_type : map_factory.map_types()) { 121 | std::cout << " " << map_type << "\n"; 122 | } 123 | std::exit(0); 124 | case 'n': 125 | if (!std::strcmp(optarg, "sparse")) { 126 | nodes_dense = false; 127 | } else if (!std::strcmp(optarg, "dense")) { 128 | nodes_dense = true; 129 | } else { 130 | std::cerr << "Set --nodes, -n to 'sparse' or 'dense'\n"; 131 | std::exit(1); 132 | } 133 | break; 134 | case 'z': 135 | zoom = std::atoi(optarg); 136 | break; 137 | default: 138 | std::exit(1); 139 | } 140 | } 141 | 142 | if (location_store.empty()) { 143 | location_store = nodes_dense ? "dense" : "sparse"; 144 | location_store.append("_file_array,locations.dump"); 145 | } 146 | 147 | std::cerr << "Using the '" << location_store << "' location store. Use -l or -n to change this.\n"; 148 | 149 | const int remaining_args = argc - optind; 150 | if (remaining_args > 1) { 151 | std::cerr << "Usage: " << argv[0] << " [OPTIONS] OSM-CHANGE-FILE\n"; 152 | std::exit(1); 153 | } else if (remaining_args == 1) { 154 | input_filename = argv[optind]; 155 | std::cerr << "Reading from '" << input_filename << "'...\n"; 156 | } else { 157 | input_filename = "-"; 158 | std::cerr << "Reading from STDIN...\n"; 159 | } 160 | 161 | osmium::io::Reader reader{input_filename}; 162 | 163 | std::unique_ptr old_index = map_factory.create_map(location_store); 164 | std::unique_ptr tmp_index = map_factory.create_map("sparse_mem_array"); 165 | location_handler_type location_handler{*tmp_index}; 166 | location_handler.ignore_errors(); 167 | 168 | TileDiffHandler tile_diff_handler{zoom, *old_index, *tmp_index}; 169 | 170 | osmium::apply(reader, location_handler, tile_diff_handler); 171 | reader.close(); 172 | 173 | tile_diff_handler.dump_tiles(); 174 | } 175 | 176 | -------------------------------------------------------------------------------- /minjur-mp.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | // these must be include in this order 19 | #include 20 | #include 21 | 22 | #include "minjur_version.hpp" 23 | #include "json_feature.hpp" 24 | #include "json_handler.hpp" 25 | 26 | using index_type = osmium::index::map::Map; 27 | using location_handler_type = osmium::handler::NodeLocationsForWays; 28 | 29 | 30 | class JSONAreaHandler : public JSONHandler { 31 | 32 | public: 33 | 34 | JSONAreaHandler(const std::string& error_file, const std::string& attr_prefix, bool with_id) : 35 | JSONHandler(error_file, attr_prefix, with_id) { 36 | } 37 | 38 | void node(const osmium::Node& node) { 39 | if (node.tags().empty()) { 40 | return; 41 | } 42 | 43 | try { 44 | JSONFeature feature{attr_names()}; 45 | if (with_id()) { 46 | feature.add_id("n", node.id()); 47 | } 48 | feature.add_point(node); 49 | feature.add_properties(node); 50 | feature.append_to(buffer()); 51 | } catch (const osmium::geometry_error&) { 52 | report_geometry_problem(node, "geometry_error"); 53 | } catch (const osmium::invalid_location&) { 54 | report_geometry_problem(node, "invalid_location"); 55 | } 56 | 57 | maybe_flush(); 58 | } 59 | 60 | void way(const osmium::Way& way) { 61 | if (way.nodes().size() <= 1) { 62 | return; 63 | } 64 | try { 65 | JSONFeature feature{attr_names()}; 66 | if (with_id()) { 67 | feature.add_id("w", way.id()); 68 | } 69 | feature.add_linestring(way); 70 | feature.add_properties(way); 71 | feature.append_to(buffer()); 72 | } catch (const osmium::geometry_error&) { 73 | report_geometry_problem(way, "geometry_error"); 74 | } catch (const osmium::invalid_location&) { 75 | report_geometry_problem(way, "invalid_location"); 76 | } 77 | 78 | maybe_flush(); 79 | } 80 | 81 | void area(const osmium::Area& area) { 82 | try { 83 | JSONFeature feature{attr_names()}; 84 | if (with_id()) { 85 | feature.add_id("a", area.id()); 86 | } 87 | feature.add_multipolygon(area); 88 | feature.add_properties(area); 89 | feature.append_to(buffer()); 90 | } catch (const osmium::geometry_error&) { 91 | report_geometry_problem(area, "geometry_error"); 92 | } catch (const osmium::invalid_location&) { 93 | report_geometry_problem(area, "invalid_location"); 94 | } 95 | 96 | maybe_flush(); 97 | } 98 | 99 | }; // class JSONAreaHandler 100 | 101 | /* ================================================== */ 102 | 103 | void print_help() { 104 | std::cout << "minjur-mp [OPTIONS] INFILE\n\n" 105 | << "Output is always to stdout.\n" 106 | << "\nOptions:\n" 107 | << " -e, --error-file=FILE Write errors to file\n" 108 | << " -h, --help This help message\n" 109 | << " -v, --version Display version\n" 110 | << " -i, --with-id Add unique id to each feature\n" 111 | << " -l, --location-store=TYPE Set location store\n" 112 | << " -L, --list-location-stores Show available location stores\n" 113 | << " -n, --nodes=sparse|dense Are node IDs sparse or dense?\n" 114 | << " -a, --attr-prefix=PREFIX Optional prefix for attributes, defaults to '@'\n"; 115 | } 116 | 117 | void print_version() { 118 | std::cout << MINJUR_VERSION_STRING << "\n"; 119 | } 120 | 121 | int main(int argc, char* argv[]) { 122 | const auto& map_factory = osmium::index::MapFactory::instance(); 123 | 124 | static struct option long_options[] = { 125 | {"error-file", required_argument, 0, 'e'}, 126 | {"help", no_argument, 0, 'h'}, 127 | {"version", no_argument, 0, 'v'}, 128 | {"with-id", no_argument, 0, 'i'}, 129 | {"location-store", required_argument, 0, 'l'}, 130 | {"list-location-stores", no_argument, 0, 'L'}, 131 | {"nodes", required_argument, 0, 'n'}, 132 | {"attr-prefix", required_argument, 0, 'a'}, 133 | {0, 0, 0, 0} 134 | }; 135 | 136 | std::string location_store; 137 | std::string error_file; 138 | std::string attr_prefix = "@"; 139 | bool nodes_dense = false; 140 | bool with_id = false; 141 | 142 | while (true) { 143 | int c = getopt_long(argc, argv, "e:hivl:Ln:a:", long_options, 0); 144 | if (c == -1) { 145 | break; 146 | } 147 | 148 | switch (c) { 149 | case 'e': 150 | error_file = optarg; 151 | break; 152 | case 'h': 153 | print_help(); 154 | std::exit(0); 155 | case 'v': 156 | print_version(); 157 | std::exit(0); 158 | case 'i': 159 | with_id = true; 160 | break; 161 | case 'l': 162 | location_store = optarg; 163 | break; 164 | case 'L': 165 | std::cout << "Available map types:\n"; 166 | for (const auto& map_type : map_factory.map_types()) { 167 | std::cout << " " << map_type << "\n"; 168 | } 169 | std::exit(0); 170 | case 'n': 171 | if (!std::strcmp(optarg, "sparse")) { 172 | nodes_dense = false; 173 | } else if (!std::strcmp(optarg, "dense")) { 174 | nodes_dense = true; 175 | } else { 176 | std::cerr << "Set --nodes, -n to 'sparse' or 'dense'\n"; 177 | std::exit(1); 178 | } 179 | break; 180 | case 'a': 181 | attr_prefix = optarg; 182 | break; 183 | default: 184 | std::exit(1); 185 | } 186 | } 187 | 188 | if (location_store.empty()) { 189 | location_store = nodes_dense ? "dense" : "sparse"; 190 | 191 | if (map_factory.has_map_type(location_store + "_mmap_array")) { 192 | location_store.append("_mmap_array"); 193 | } else { 194 | location_store.append("_mem_array"); 195 | } 196 | } 197 | std::cerr << "Using the '" << location_store << "' location store. Use -l or -n to change this.\n"; 198 | 199 | std::string input_filename; 200 | const int remaining_args = argc - optind; 201 | if (remaining_args == 1) { 202 | input_filename = argv[optind]; 203 | std::cerr << "Reading from '" << input_filename << "'...\n"; 204 | } else { 205 | std::cerr << "Usage: " << argv[0] << " [OPTIONS] INFILE\n"; 206 | std::exit(1); 207 | } 208 | 209 | osmium::area::Assembler::config_type assembler_config; 210 | osmium::area::MultipolygonCollector collector{assembler_config}; 211 | 212 | std::cerr << "Pass 1...\n"; 213 | osmium::io::Reader reader1{input_filename, osmium::osm_entity_bits::relation}; 214 | collector.read_relations(reader1); 215 | reader1.close(); 216 | std::cerr << "Pass 1 done\n"; 217 | 218 | 219 | std::unique_ptr index = map_factory.create_map(location_store); 220 | location_handler_type location_handler{*index}; 221 | location_handler.ignore_errors(); 222 | 223 | JSONAreaHandler json_handler{error_file, attr_prefix, with_id}; 224 | osmium::handler::CheckOrder check_order_handler; 225 | 226 | std::cerr << "Pass 2...\n"; 227 | osmium::io::Reader reader2{input_filename}; 228 | osmium::apply(reader2, check_order_handler, location_handler, json_handler, collector.handler([&json_handler](osmium::memory::Buffer&& buffer) { 229 | osmium::apply(buffer, json_handler); 230 | })); 231 | reader2.close(); 232 | std::cerr << "Pass 2 done\n"; 233 | 234 | 235 | if (json_handler.geometry_error_count()) { 236 | std::cerr << "Number of geometry errors (not written to output): " << json_handler.geometry_error_count() << "\n"; 237 | } 238 | 239 | std::cerr << "Done.\n"; 240 | } 241 | 242 | -------------------------------------------------------------------------------- /minjur_version.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /// Version number as string 4 | #define MINJUR_VERSION_STRING "0.1.0" 5 | -------------------------------------------------------------------------------- /scripts/travis_script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # travis_script.sh 4 | # 5 | 6 | mkdir build 7 | cd build 8 | 9 | # GCC ignores the pragmas in the code that disable the "return-type" warning 10 | # selectively, so use this workaround. 11 | if [ "${CXX}" = "g++" ]; then 12 | WORKAROUND="-DCMAKE_CXX_FLAGS=-Wno-return-type" 13 | else 14 | WORKAROUND="" 15 | fi 16 | 17 | if [ "${CXX}" = "g++" ]; then 18 | CXX=g++-4.8 19 | CC=gcc-4.8 20 | fi 21 | 22 | echo "travis_fold:start:cmake\nRunning cmake..." 23 | cmake -LA \ 24 | -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ 25 | ${WORKAROUND} \ 26 | .. 27 | echo "travis_fold:end:cmake" 28 | 29 | echo "travis_fold:start:make\nRunning make..." 30 | make VERBOSE=1 31 | echo "travis_fold:end:make" 32 | 33 | #echo "travis_fold:start:ctest\nRunning ctest..." 34 | #ctest --output-on-failure 35 | #echo "travis_fold:end:ctest" 36 | 37 | --------------------------------------------------------------------------------