├── .gitignore
├── .vs
└── DeepGTAV
│ └── v15
│ ├── .suo
│ ├── Browse.VC.db
│ └── ipch
│ └── AutoPCH
│ └── fcee33426b875baa
│ └── SCENARIO.ipch
├── DeepGTAV.filters
├── DeepGTAV.sdf
├── DeepGTAV.v12.suo
├── DeepGTAV.vcxproj
├── DeepGTAV.vcxproj.user
├── LICENSE
├── README.md
├── Rewarders
├── GeneralRewarder.cpp
├── GeneralRewarder.h
├── LaneRewarder.cpp
├── LaneRewarder.h
├── Rewarder.h
├── SpeedRewarder.cpp
└── SpeedRewarder.h
├── Scenario.cpp
├── Scenario.h
├── ScreenCapturer.cpp
├── ScreenCapturer.h
├── Server.cpp
├── Server.h
├── bin
├── Release
│ ├── DeepGTAV.asi
│ ├── DeepGTAV.iobj
│ ├── DeepGTAV.ipdb
│ ├── GTAVLauncherBypass.asi
│ ├── GTAVLauncherBypass.ini
│ ├── ScriptHookV.dll
│ └── dinput8.dll
└── SaveGame
│ ├── SGTA50015
│ ├── SGTA50015.bak
│ ├── cfg.dat
│ └── pc_settings.bin
├── defaults.h
├── lib
├── ScriptHookV.lib
├── enums.h
├── main.cpp
├── main.h
├── nativeCaller.h
├── natives.h
├── rapidjson
│ ├── allocators.h
│ ├── document.h
│ ├── encodedstream.h
│ ├── encodings.h
│ ├── error
│ │ ├── en.h
│ │ └── error.h
│ ├── filereadstream.h
│ ├── filewritestream.h
│ ├── fwd.h
│ ├── internal
│ │ ├── biginteger.h
│ │ ├── diyfp.h
│ │ ├── dtoa.h
│ │ ├── ieee754.h
│ │ ├── itoa.h
│ │ ├── meta.h
│ │ ├── pow10.h
│ │ ├── regex.h
│ │ ├── stack.h
│ │ ├── strfunc.h
│ │ ├── strtod.h
│ │ └── swap.h
│ ├── istreamwrapper.h
│ ├── memorybuffer.h
│ ├── memorystream.h
│ ├── msinttypes
│ │ ├── inttypes.h
│ │ └── stdint.h
│ ├── ostreamwrapper.h
│ ├── pointer.h
│ ├── prettywriter.h
│ ├── rapidjson.h
│ ├── reader.h
│ ├── schema.h
│ ├── stream.h
│ ├── stringbuffer.h
│ └── writer.h
├── script.h
├── tinyxml2.cpp
├── tinyxml2.h
├── types.h
├── utils.cpp
└── utils.h
└── script.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | tmp/
2 | bin/Release/paths.xml
--------------------------------------------------------------------------------
/.vs/DeepGTAV/v15/.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/.vs/DeepGTAV/v15/.suo
--------------------------------------------------------------------------------
/.vs/DeepGTAV/v15/Browse.VC.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/.vs/DeepGTAV/v15/Browse.VC.db
--------------------------------------------------------------------------------
/.vs/DeepGTAV/v15/ipch/AutoPCH/fcee33426b875baa/SCENARIO.ipch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/.vs/DeepGTAV/v15/ipch/AutoPCH/fcee33426b875baa/SCENARIO.ipch
--------------------------------------------------------------------------------
/DeepGTAV.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | include
11 |
12 |
13 | include
14 |
15 |
16 | include
17 |
18 |
19 |
20 |
21 | include
22 |
23 |
24 |
25 |
26 | {5d58942b-ebbf-4d0a-9526-359e3a556796}
27 |
28 |
29 |
--------------------------------------------------------------------------------
/DeepGTAV.sdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/DeepGTAV.sdf
--------------------------------------------------------------------------------
/DeepGTAV.v12.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/DeepGTAV.v12.suo
--------------------------------------------------------------------------------
/DeepGTAV.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Release
6 | x64
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | {8D82F34A-1D64-465B-84B1-37F89AD3D20B}
36 | Win32Proj
37 | DeepGTAV
38 | DeepGTAV
39 | 10.0.15063.0
40 |
41 |
42 |
43 | DynamicLibrary
44 | false
45 | v141
46 | true
47 | MultiByte
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | false
58 | .asi
59 | bin\$(Configuration)\
60 | tmp\$(Configuration)\
61 |
62 |
63 |
64 | Level3
65 |
66 |
67 | MaxSpeed
68 | true
69 | true
70 | _WINSOCKAPI_;WIN32;NDEBUG;_WINDOWS;_USRDLL;NativeSpeedo_EXPORTS;%(PreprocessorDefinitions)
71 | MultiThreaded
72 | Fast
73 |
74 |
75 | Windows
76 | false
77 | true
78 | true
79 | lib\ScriptHookV.lib %(AdditionalOptions)
80 |
81 |
82 | copy "bin\Release\*" "C:\Program Files\Rockstar Games\Grand Theft Auto V\*"
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/DeepGTAV.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | true
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DeepGTAV v2
2 |
3 | *A plugin for GTAV that transforms it into a vision-based self-driving car research environment.*
4 |
5 |
6 |
7 | ## Installation
8 | 1. Make sure GTAV is on version 1.0.1180.2 or below
9 | 2. Copy-paste the contents of *bin/Release* under your GTAV installation directory
10 | 3. Replace your saved game data in *Documents/Rockstar Games/GTA V/Profiles/* with the contents of *bin/SaveGame*
11 | 4. Download *[paths.xml](https://drive.google.com/file/d/0B6pR5O2YrmHnNU9EMDBSSFpMV00/view?usp=sharing)* and store it also in the GTAV installation directory.
12 |
13 | ## Recommendations
14 |
15 | 1. Under your game settings, set your screen to windowed mode
16 | 2. Bypass the menu screen by configuring GTAV to start directly into Story Mode
17 | 3. To avoid the Rockstar updates, start the game using GTA5.exe, otherwise use GTAVLauncher.exe or PlayGTAV.exe
18 |
19 | ## How it works
20 |
21 | If installation was successful, GTAV will automatically load the DeepGTAV plugin. Once the game starts DeepGTAV will wait for TCP clients to connect on port 8000.
22 |
23 | Clients connected to DeepGTAV are allowed to send messages to GTAV to start and configure the research environment (*Start* and *Config* messages), send driving commands to control the vehicle (*Commands* message) and to stop the environment to fallback to normal gameplay (*Stop* message).
24 |
25 | When the environment has been started with the *Start* message, DeepGTAV will start sending the data gathered from the game back to the client in JSON format, so the client can use it to store a dataset, run it through a self-driving agent...
26 |
27 | The data sent back to the client and intitial conditions will depend on the parameters sent by the client with the *Start* or *Config* messages. Things that can be controlled for instance are: rate of transmission, frame width and height, weather, time, type of vehicle, driving style, wether to get surrounding vehicles or peds, type of reward function and a large etc...
28 |
29 | [VPilot](https://github.com/ai-tor/VPilot) provides a nice interface and examples written in Python to use DeepGTAV for your self-driving car research.
30 |
31 | The following chapters describe the purpose and contents of each message.
32 |
33 | ## Messages from the client to DeepGTAV
34 |
35 | Messages must be always be sent in two steps: First send the JSON message length in bytes and then send the message itself, this is done so DeepGTAV knows when to stop reading a message.
36 |
37 | ### Start
38 |
39 | This is the message that needs to be sent to start DeepGTAV, any other message sent prior to this won't make any effect. Along with this message, several fields can be set to start DeepGTAV with the desired initial conditions and requested *Data* transmission.
40 |
41 | When this message is sent the environment starts, the game camera is set to the front center of the vehicle and *Data* starts being sent back to the client until the client is disconnected or an *Stop* message is received
42 |
43 | Here follows an example of the *Start* message:
44 | ```json
45 | {"start": {
46 | "scenario": {
47 | "location": [1015.6, 736.8],
48 | "time": [22, null],
49 | "weather": "RAIN",
50 | "vehicle": null,
51 | "drivingMode": [1074528293, 15.0]
52 | },
53 | "dataset": {
54 | "rate": 20,
55 | "frame": [227, 227],
56 | "vehicles": true,
57 | "peds": false,
58 | "trafficSigns": null,
59 | "direction": [1234.8, 354.3, 0],
60 | "reward": [15.0, 0.5],
61 | "throttle": true,
62 | "brake": true,
63 | "steering": true,
64 | "speed": null,
65 | "yawRate": false,
66 | "drivingMode": null,
67 | "location": null,
68 | "time": false
69 | }
70 | }}
71 | ```
72 | The scenario field specifies the desired intitial conditions for the environment. If any of its fields or itself is null or invalid the relevant configuration will be randomly set.
73 |
74 | The dataset field specifies the data we want back from the game. If any of its fields or itself is null or invalid, the relevant *Data* field will be deactivated, except for the frame rate and dimensions, which will be set to 10 Hz and 320x160 by default.
75 |
76 | ### Config
77 |
78 | This message allows to change at any moment during DeepGTAV's execution, the initial configuration set by the *Start* message.
79 |
80 | Here follows an example of the *Config* message (identical to the *Start* message):
81 | ```json
82 | {"start": {
83 | "scenario": {
84 | "location": [1015.6, 736.8],
85 | "time": null,
86 | "weather": "SUNNY",
87 | "vehicle": "voltic",
88 | "drivingMode": -1
89 | },
90 | "dataset": {
91 | "rate": null,
92 | "frame": null,
93 | "vehicles": false,
94 | "peds": true,
95 | "trafficSigns": null,
96 | "direction": null,
97 | "reward": null,
98 | "throttle": null,
99 | "brake": false,
100 | "steering": false,
101 | "speed": true,
102 | "yawRate": null,
103 | "drivingMode": null,
104 | "location": null,
105 | "time": true
106 | }
107 | }}
108 | ```
109 | In this case, if any field is null or invalid, the previous configuration is kept. Otherwise the configuration is overrided.
110 |
111 | ### Commands
112 |
113 | As simple as it seems, this message can be sent at any moment during DeepGTAV's execution to control the vehicle. Note that you will only be able to control the vehicle if *drivingMode* is set to manual.
114 |
115 | Here follows an example of the *Commands* message:
116 | ```json
117 | {"commands": {
118 | "throttle": 1.0,
119 | "brake": 0.0,
120 | "steering": -0.5
121 | }}
122 | ```
123 |
124 | ### Stop
125 |
126 | Stops the environment and allows the user to go back to the normal gameplay. Simply disconnecting the client produces the same effect.
127 |
128 | Here follows an example of the *Stop* message:
129 | ```json
130 | {"stop": {}}
131 | ```
132 | ## Messages from DeepGTAV to the client
133 |
134 | DeepGTAV will always send the messages in two steps: First it will send the message length in bytes and then it will send the message itself, so the client can know when to stop reading it.
135 |
136 | The messages rate and content will depend on the configuration set by the *Start* or *Config* messages, and are always sent consecutively (Frame, Data).
137 |
138 | ### Frame
139 |
140 | This is a byte array containing the RGB values of the current GTAV screen (resized to the specified width and length). Make sure the window is not minimized, otherwise the values will be all zeroes (black).
141 |
142 | ### Data
143 |
144 | ## Reporting issues and TODOs
145 |
146 | DeepGTAV generates a useful log file named *deepgtav.log* in GTAV's installation directory. Please attach it in your issue when reporting any bug.
147 |
148 | __TODO:__
149 | * Improve code quality (a lot!)
150 | * Add support for traffic signs detection
151 | * Add support for driving mode override
152 | * General bug fixing
153 |
--------------------------------------------------------------------------------
/Rewarders/GeneralRewarder.cpp:
--------------------------------------------------------------------------------
1 | #include "GeneralRewarder.h"
2 |
3 | GeneralRewarder::GeneralRewarder(const char* pathsfile, float setSpeed, float _agressivity): laneRewarder(pathsfile), speedRewarder(setSpeed){
4 | agressivity = _agressivity;
5 | }
6 |
7 | float GeneralRewarder::computeReward(Vehicle vehicle) {
8 | if (ENTITY::HAS_ENTITY_COLLIDED_WITH_ANYTHING(vehicle)) return -1.0;
9 | return agressivity*speedRewarder.computeReward(vehicle) + (1.0f - agressivity)*laneRewarder.computeReward(vehicle);
10 | }
--------------------------------------------------------------------------------
/Rewarders/GeneralRewarder.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Rewarder.h"
4 | #include "LaneRewarder.h"
5 | #include "SpeedRewarder.h"
6 |
7 | class GeneralRewarder : public Rewarder {
8 | private:
9 | LaneRewarder laneRewarder;
10 | SpeedRewarder speedRewarder;
11 | float agressivity;
12 | public:
13 | GeneralRewarder(const char* pathsfile, float setSpeed, float _agressivity);
14 | float computeReward(Vehicle vehicle);
15 |
16 | };
--------------------------------------------------------------------------------
/Rewarders/LaneRewarder.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "LaneRewarder.h"
3 | #include "../lib/tinyxml2.h"
4 |
5 | LaneRewarder::LaneRewarder(const char* pathsfile) {
6 | while (nodes.size() == 0) {
7 | if (PATHFIND::_0xF7B79A50B905A30D(-8192.0f, 8192.0f, -8192.0f, 8192.0f)) {
8 | populateNodes(pathsfile);
9 | }
10 | WAIT(0);
11 | }
12 | }
13 |
14 | LaneRewarder::~LaneRewarder(){
15 | nodes.clear();
16 | }
17 |
18 | float LaneRewarder::computeReward(Vehicle vehicle) {
19 | std::vector pointPair;
20 | Vector3 laneCenter, currentPosition, forwardVector;
21 | float d, a, direction, reward;
22 | int nodeID;
23 | tNode node;
24 | tLink link;
25 |
26 | currentPosition = ENTITY::GET_ENTITY_COORDS(vehicle, FALSE);
27 | if (!PATHFIND::IS_POINT_ON_ROAD(currentPosition.x, currentPosition.y, currentPosition.z, 0)) return -1.0;
28 |
29 | for (int i = 1; i <= 6; i++) {
30 | nodeID = PATHFIND::GET_NTH_CLOSEST_VEHICLE_NODE_ID(currentPosition.x, currentPosition.y, currentPosition.z, i, 1, 300, 300);
31 | node = nodes[nodeID];
32 | if (node.attr.special > 0) continue;
33 | for (int j = 0; j < node.links.size(); j++){
34 | link = node.links.at(j);
35 | if (link.attr.shortcut || link.attr.width == -1) continue;
36 | pointPair = getCurrentLanePoints(link, currentPosition);
37 | if (pointPair.size() == 2) break;
38 | }
39 | if (pointPair.size() == 2) break;
40 | }
41 |
42 | if (pointPair.size() != 2) return -1.0;
43 |
44 | forwardVector = ENTITY::GET_ENTITY_FORWARD_VECTOR(vehicle);
45 | direction = forwardVector.x*link.direction.x + forwardVector.y*link.direction.y;
46 |
47 | laneCenter.x = (pointPair.at(0).coord.x + pointPair.at(1).coord.x) / 2.0f;
48 | laneCenter.y = (pointPair.at(0).coord.y + pointPair.at(1).coord.y) / 2.0f;
49 |
50 | a = GAMEPLAY::GET_ANGLE_BETWEEN_2D_VECTORS(currentPosition.x - laneCenter.x, currentPosition.y - laneCenter.y, pointPair.at(0).coord.x - laneCenter.x, pointPair.at(0).coord.y - laneCenter.y);
51 | d = SYSTEM::VDIST(currentPosition.x, currentPosition.y, 0, laneCenter.x, laneCenter.y, 0);
52 | reward = 1.0f - (d*abs(SYSTEM::COS(a))) / SYSTEM::VDIST(pointPair.at(0).coord.x, pointPair.at(0).coord.y, 0, laneCenter.x, laneCenter.y, 0);
53 |
54 | if (pointPair.at(0).laneIn || pointPair.at(1).laneIn) {
55 | if (direction < 0.0) {
56 | reward = -reward;
57 | }
58 | }
59 | else {
60 | if (direction > 0.0) {
61 | reward = -reward;
62 | }
63 | }
64 |
65 | return reward;
66 | }
67 |
68 |
69 | //helpers
70 | void LaneRewarder::populateNodes(const char* pathsfile){
71 | std::unordered_map tmpnodes;
72 | tLink *tmplinks = (tLink*)malloc(80592 * sizeof(*tmplinks)); //Too large for the stack, need to store in the heap
73 | int i = 0;
74 |
75 | tinyxml2::XMLDocument doc;
76 | tinyxml2::XMLElement* object;
77 | tinyxml2::XMLElement* coords;
78 | tinyxml2::XMLElement* attr;
79 | tinyxml2::XMLElement* ref1;
80 | tinyxml2::XMLElement* ref2;
81 |
82 | doc.LoadFile(pathsfile);
83 | object = doc.FirstChildElement()->FirstChildElement()->FirstChildElement();
84 | for (object; object; object = object->NextSiblingElement()) {
85 | if (object->Attribute("class", "vehiclenode")) {
86 | tNode node = { 0 };
87 | node.attr.speed = 4;
88 | node.attr.density = -1;
89 |
90 | coords = object->FirstChildElement()->FirstChildElement()->FirstChildElement();
91 | node.coord.x = coords->FloatAttribute("x");
92 | node.coord.y = coords->FloatAttribute("y");
93 | node.coord.z = coords->FloatAttribute("z");
94 |
95 | attr = object->FirstChildElement()->NextSiblingElement()->FirstChildElement();
96 | if (attr != NULL) {
97 | for (attr; attr; attr = attr->NextSiblingElement()) {
98 | if (attr->Attribute("name", "Disabled")) {
99 | node.attr.disabled = attr->BoolAttribute("value");
100 | }
101 | else if (attr->Attribute("name", "Water")) {
102 | node.attr.water = attr->BoolAttribute("value");
103 | }
104 | else if (attr->Attribute("name", "Speed")) {
105 | node.attr.speed = attr->IntAttribute("value");
106 | }
107 | else if (attr->Attribute("name", "Special")) {
108 | node.attr.special = attr->IntAttribute("value");
109 | }
110 | else if (attr->Attribute("name", "Density")) {
111 | node.attr.density = attr->IntAttribute("value");
112 | }
113 | else if (attr->Attribute("name", "Highway")) {
114 | node.attr.highway = attr->BoolAttribute("value");
115 | }
116 | else if (attr->Attribute("name", "NoGps")) {
117 | node.attr.noGPS = attr->BoolAttribute("value");
118 | }
119 | else if (attr->Attribute("name", "Tunnel")) {
120 | node.attr.tunnel = attr->BoolAttribute("value");
121 | }
122 | else if (attr->Attribute("name", "Cannot Go Left")) {
123 | node.attr.cantGoLeft = attr->BoolAttribute("value");
124 | }
125 | else if (attr->Attribute("name", "Left Turns Only")) {
126 | node.attr.leftTurnsOnly = attr->BoolAttribute("value");
127 | }
128 | else if (attr->Attribute("name", "Off Road")) {
129 | node.attr.offRoad = attr->BoolAttribute("value");
130 | }
131 | else if (attr->Attribute("name", "Cannot Go Right")) {
132 | node.attr.cantGoRight = attr->BoolAttribute("value");
133 | }
134 | else if (attr->Attribute("name", "No Big Vehicles")) {
135 | node.attr.noBigVehicles = attr->BoolAttribute("value");
136 | }
137 | else if (attr->Attribute("name", "Indicate Keep Left")) {
138 | node.attr.indicateKeepLeft = attr->BoolAttribute("value");
139 | }
140 | else if (attr->Attribute("name", "Indicate Keep Right")) {
141 | node.attr.indicateKeepRight = attr->BoolAttribute("value");
142 | }
143 | else if (attr->Attribute("name", "Slip Lane")) {
144 | node.attr.slipLane = attr->BoolAttribute("value");
145 | }
146 |
147 | }
148 | }
149 | node.id = PATHFIND::GET_NTH_CLOSEST_VEHICLE_NODE_ID(node.coord.x, node.coord.y, node.coord.z, 1, 1, 300, 300);
150 | //PATHFIND::GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(node.coord.x, node.coord.y, node.coord.z, &dummy, &(node.heading), nodetype, 300, 300);
151 | //node.nodeID = PATHFIND::GET_NTH_CLOSEST_VEHICLE_NODE_ID_WITH_HEADING(node.coord.x, node.coord.y, node.coord.z, 1, &dummy, &(node.heading), nodetype, 300, 300);
152 | tmpnodes[std::string(object->Attribute("guid"))] = node;
153 | }
154 | else if (object->Attribute("class", "vehiclelink")) {
155 | tLink link = { 0 };
156 | link.attr.lanesIn = 1;
157 | link.attr.lanesOut = 1;
158 |
159 | coords = object->FirstChildElement()->FirstChildElement()->FirstChildElement();
160 | link.coord.x = coords->FloatAttribute("x");
161 | link.coord.y = coords->FloatAttribute("y");
162 | link.coord.z = coords->FloatAttribute("z");
163 |
164 | attr = object->FirstChildElement()->NextSiblingElement()->FirstChildElement();
165 | if (attr != NULL) {
166 | for (attr; attr; attr = attr->NextSiblingElement()) {
167 | if (attr->Attribute("name", "Width")) {
168 | link.attr.width = attr->IntAttribute("value");
169 | }
170 | else if (attr->Attribute("name", "Lanes In")) {
171 | link.attr.lanesIn = attr->IntAttribute("value");
172 | }
173 | else if (attr->Attribute("name", "Lanes Out")) {
174 | link.attr.lanesOut = attr->IntAttribute("value");
175 | }
176 | else if (attr->Attribute("name", "Narrowroad")) {
177 | link.attr.narrowRoad = attr->BoolAttribute("value");
178 | }
179 | else if (attr->Attribute("name", "GpsBothWays")) {
180 | link.attr.gpsBothWays = attr->BoolAttribute("value");
181 | }
182 | else if (attr->Attribute("name", "Block If No Lanes")) {
183 | link.attr.blockIfNoLanes = attr->BoolAttribute("value");
184 | }
185 | else if (attr->Attribute("name", "Shortcut")) {
186 | link.attr.shortcut = attr->BoolAttribute("value");
187 | }
188 | else if (attr->Attribute("name", "Dont Use For Navigation")) {
189 | link.attr.dontUseForNavigation = attr->BoolAttribute("value");
190 | }
191 | }
192 | }
193 |
194 | ref1 = object->FirstChildElement()->NextSiblingElement()->NextSiblingElement()->NextSiblingElement()->FirstChildElement();
195 | ref2 = ref1->NextSiblingElement();
196 |
197 | link._ref1 = std::string(ref1->Attribute("guid"));
198 | link._ref2 = std::string(ref2->Attribute("guid"));
199 | tmplinks[i] = link;
200 | i++;
201 | }
202 | }
203 |
204 | //Puts everything into place for fast searching by nodeID
205 | tNode node1;
206 | tNode node2;
207 | tLink link;
208 | float m;
209 | for (i = 0; i < 80592; i++){
210 | link = tmplinks[i];
211 | node1 = tmpnodes[link._ref1];
212 | node2 = tmpnodes[link._ref2];
213 | m = GAMEPLAY::GET_DISTANCE_BETWEEN_COORDS(node1.coord.x, node1.coord.y, 0, node2.coord.x, node2.coord.y, 0, FALSE);
214 | link.direction.x = (node2.coord.x - node1.coord.x) / m; //Unitary vector pointing in the direction of the road
215 | link.direction.y = (node2.coord.y - node1.coord.y) / m;
216 |
217 | try {
218 | node1 = nodes.at(node1.id);
219 | node2 = nodes.at(node2.id);
220 | }
221 | catch (const std::out_of_range) {
222 | //Empty on purpose
223 | }
224 |
225 | setLinePoints(&node1, link);
226 | setLinePoints(&node2, link);
227 | nodes[node1.id] = node1;
228 | nodes[node2.id] = node2;
229 | }
230 | free(tmplinks);
231 | }
232 |
233 | void LaneRewarder::setLinePoints(tNode* node, tLink link){
234 |
235 | tLinePoint linePoint;
236 | int linesIn, linesOut, starti;
237 | float laneWidth, lineOffset;
238 |
239 | linesIn = link.attr.lanesIn + 1;
240 | linesOut = link.attr.lanesOut + 1;
241 |
242 | if (node->attr.highway) laneWidth = 6.0;
243 | else if (link.attr.narrowRoad) laneWidth = 4.2;
244 | else laneWidth = 5.5;
245 |
246 |
247 | if (link.attr.width > 0) { //Usually urban scenarios
248 | lineOffset = laneWidth / 2;
249 | starti = 0;
250 | }
251 | else if (link.attr.lanesOut == 0){ //Usually interurban scenarios TODO
252 |
253 | if ((link.attr.lanesIn % 2) == 0) {
254 | lineOffset = 0;
255 | linesIn = (linesIn / 2) + 1;
256 | starti = -(linesIn - 1);
257 | }
258 | else {
259 | lineOffset = laneWidth / 2;
260 | linesIn = linesIn / 2;
261 | starti = -linesIn;
262 | }
263 |
264 | linesOut = starti; //Skips linesOut loop
265 | }
266 | else { //Urban and interurban scenarios
267 | lineOffset = 0;
268 | starti = 1;
269 |
270 | linePoint.coord = node->coord;
271 | linePoint.laneIn = false;
272 | link.linePoints.push_back(linePoint);
273 | }
274 |
275 | linePoint.laneIn = true;
276 | for (int i = starti; i < linesIn; i++) {
277 | linePoint.coord.x = node->coord.x + link.direction.y*(lineOffset + laneWidth*i);
278 | linePoint.coord.y = node->coord.y - link.direction.x*(lineOffset + laneWidth*i);
279 | linePoint.coord.z = node->coord.z;
280 | link.linePoints.push_back(linePoint);
281 | }
282 |
283 | linePoint.laneIn = false;
284 | for (int i = starti; i < linesOut; i++) {
285 | linePoint.coord.x = node->coord.x - link.direction.y*(lineOffset + laneWidth*i);
286 | linePoint.coord.y = node->coord.y + link.direction.x*(lineOffset + laneWidth*i);
287 | linePoint.coord.z = node->coord.z;
288 | link.linePoints.push_back(linePoint);
289 | }
290 |
291 | node->links.push_back(link);
292 | }
293 |
294 | std::vector LaneRewarder::getCurrentLanePoints(tLink link, Vector3 currentPosition) {
295 | std::multimap nearestLines;
296 | std::vector pointPair;
297 | tLinePoint linePoint1, linePoint2;
298 | float d1, d2, pos1, pos2;
299 |
300 | for (int i = 0; i < link.linePoints.size(); i++) {
301 | linePoint1 = link.linePoints.at(i);
302 | nearestLines.emplace(SYSTEM::VDIST(currentPosition.x, currentPosition.y, 0, linePoint1.coord.x, linePoint1.coord.y, 0), linePoint1);
303 | }
304 |
305 | std::multimap::const_iterator it = nearestLines.begin();
306 | d1 = it->first; linePoint1 = it->second; it++;
307 | d2 = it->first; linePoint2 = it->second;
308 |
309 | pos1 = (link.direction.x)*(currentPosition.y - linePoint1.coord.y) - (link.direction.y)*(currentPosition.x - linePoint1.coord.x);
310 | pos2 = (link.direction.x)*(currentPosition.y - linePoint2.coord.y) - (link.direction.y)*(currentPosition.x - linePoint2.coord.x);
311 |
312 | if (pos1*pos2 < 0.0) {
313 | pointPair.push_back(linePoint1);
314 | pointPair.push_back(linePoint2);
315 | }
316 |
317 | return pointPair;
318 | }
319 |
--------------------------------------------------------------------------------
/Rewarders/LaneRewarder.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Rewarder.h"
4 | #include
5 |
6 | typedef struct {
7 | bool disabled; //Disabled (0)
8 | bool water; //Water (0)
9 | int speed; //Speed (4)
10 | int special; //Special (0)
11 | int density; //Density (-1)
12 | bool highway; //Highway (0)
13 | bool noGPS; //NoGps (0)
14 | bool tunnel; //Tunnel (0)
15 | bool cantGoLeft; //Cannot Go Left (0)
16 | bool leftTurnsOnly; //Left Turns Only (0)
17 | bool offRoad; //Off Road (0)
18 | bool cantGoRight; //Cannot Go Right (0)
19 | bool noBigVehicles; //No Big Vehicles (0)
20 | bool indicateKeepLeft; //Indicate Keep Left (0)
21 | bool indicateKeepRight; //Indicate Keep Right (0)
22 | bool slipLane; //Slip Lane (0)
23 | } tNodeAttr;
24 |
25 | typedef struct {
26 | int width; //Width (0)
27 | int lanesIn; //Lanes In (1)
28 | int lanesOut; //Lanes Out (1)
29 | bool narrowRoad; //Narrowroad (0)
30 | bool gpsBothWays; //GpsBothWays (0)
31 | bool blockIfNoLanes; //Block If No Lanes (0)
32 | bool shortcut; //Shortcut (0)
33 | bool dontUseForNavigation; //Dont Use For Navigation (0)
34 | } tLinkAttr;
35 |
36 | typedef struct {
37 | Vector3 coord;
38 | bool laneIn;
39 | } tLinePoint;
40 |
41 | typedef struct{
42 | Vector3 coord;
43 | Vector3 direction;
44 | tLinkAttr attr;
45 | std::vector linePoints;
46 |
47 | std::string _ref1;
48 | std::string _ref2;
49 | } tLink;
50 |
51 | typedef struct{
52 | int id;
53 | Vector3 coord;
54 | tNodeAttr attr;
55 | std::vector links;
56 | } tNode;
57 |
58 | class LaneRewarder : public Rewarder {
59 | private:
60 | std::unordered_map nodes;
61 |
62 | //helpers
63 | void populateNodes(const char* pathsfile);
64 | void setLinePoints(tNode* node, tLink link);
65 | std::vector getCurrentLanePoints(tLink link, Vector3 currentPosition);
66 | public:
67 | LaneRewarder(const char* pathsfile);
68 | ~LaneRewarder();
69 | float computeReward(Vehicle vehicle);
70 | };
--------------------------------------------------------------------------------
/Rewarders/Rewarder.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "../lib/script.h"
4 |
5 | class Rewarder {
6 | public:
7 | virtual float computeReward(Vehicle vehicle) { return 0.0; };
8 | };
--------------------------------------------------------------------------------
/Rewarders/SpeedRewarder.cpp:
--------------------------------------------------------------------------------
1 | #include "SpeedRewarder.h"
2 |
3 | SpeedRewarder::SpeedRewarder(float _setSpeed) {
4 | setSpeed = _setSpeed;
5 | }
6 |
7 | float SpeedRewarder::computeReward(Vehicle vehicle) {
8 | float reward = ENTITY::GET_ENTITY_SPEED(vehicle) / setSpeed;
9 | if (reward > 1.0f) reward = (1.0f - reward);
10 | if (reward < -1.0f) reward = -1.0f;
11 | return reward;
12 | }
--------------------------------------------------------------------------------
/Rewarders/SpeedRewarder.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Rewarder.h"
4 |
5 | class SpeedRewarder : public Rewarder {
6 | private:
7 | float setSpeed;
8 | public:
9 | SpeedRewarder(float _setSpeed);
10 | float computeReward(Vehicle vehicle);
11 | };
--------------------------------------------------------------------------------
/Scenario.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #include "lib/script.h"
7 | #include "lib/utils.h"
8 |
9 | #include "lib/rapidjson/document.h"
10 | #include "lib/rapidjson/stringbuffer.h"
11 |
12 | #include "ScreenCapturer.h"
13 | #include "Rewarders\Rewarder.h"
14 |
15 | using namespace rapidjson;
16 |
17 | //#define DEBUG 1
18 |
19 | class Scenario {
20 | private:
21 | static char* weatherList[14];
22 | static char* vehicleList[3];
23 |
24 | Vehicle vehicle = NULL;
25 | Player player = NULL;
26 | Ped ped = NULL;
27 | Cam camera = NULL;
28 | Vector3 dir;
29 |
30 | float x, y;
31 | int hour, minute;
32 | const char* _weather;
33 | const char* _vehicle;
34 | int width, height;
35 |
36 | bool vehicles;
37 | bool peds;
38 | bool trafficSigns; //TODO
39 | bool direction;
40 | bool reward;
41 | bool throttle;
42 | bool brake;
43 | bool steering;
44 | bool speed;
45 | bool yawRate;
46 | bool drivingMode; //TODO
47 | bool location;
48 | bool time;
49 |
50 | float currentThrottle = 0.0;
51 | float currentBrake = 0.0;
52 | float currentSteering = 0.0;
53 |
54 | Rewarder* rewarder;
55 | std::clock_t lastSafetyCheck;
56 | int _drivingMode;
57 | float _setSpeed;
58 |
59 | bool running = false;
60 | Document d;
61 |
62 | public:
63 | int rate;
64 |
65 | void start(const Value& sc, const Value& dc);
66 | void stop();
67 | void config(const Value& sc, const Value& dc);
68 | void setCommands(float throttle, float brake, float steering);
69 | void run();
70 |
71 | ScreenCapturer* screenCapturer;
72 | StringBuffer generateMessage();
73 |
74 | private:
75 | void parseScenarioConfig(const Value& sc, bool setDefaults);
76 | void parseDatasetConfig(const Value& dc, bool setDefaults);
77 | void buildScenario();
78 |
79 | void setVehiclesList();
80 | void setPedsList();
81 | void setTrafficSignsList();
82 | void setDirection();
83 | void setReward();
84 | void setThrottle();
85 | void setBrake();
86 | void setSteering();
87 | void setSpeed();
88 | void setYawRate();
89 | void setDrivingMode();
90 | void setLocation();
91 | void setTime();
92 | };
--------------------------------------------------------------------------------
/ScreenCapturer.cpp:
--------------------------------------------------------------------------------
1 | #include "ScreenCapturer.h"
2 | #include "lib/natives.h"
3 | #include
4 |
5 | ScreenCapturer::ScreenCapturer(int frameWidth, int frameHeight){
6 | imageWidth = frameWidth;
7 | imageHeight = frameHeight;
8 |
9 | // Round up the scan line size to a multiple of 4
10 | length = ((imageWidth * 3 + 3) / 4 * 4) * imageHeight;
11 |
12 | //Screen capture buffer
13 | GRAPHICS::_GET_SCREEN_ACTIVE_RESOLUTION(&windowWidth, &windowHeight);
14 | //hWnd = ::FindWindow(NULL, "Grand Theft Auto V");
15 | hWindowDC = GetDC(NULL);
16 | hCaptureDC = CreateCompatibleDC(hWindowDC);
17 | hCaptureBitmap = CreateCompatibleBitmap(hWindowDC, imageWidth, imageHeight);
18 | SelectObject(hCaptureDC, hCaptureBitmap);
19 | SetStretchBltMode(hCaptureDC, COLORONCOLOR);
20 |
21 | pixels = (UINT8*)malloc(length);
22 | info.biSize = sizeof(BITMAPINFOHEADER);
23 | info.biPlanes = 1;
24 | info.biBitCount = 24;
25 | info.biWidth = imageWidth;
26 | info.biHeight = -imageHeight;
27 | info.biCompression = BI_RGB;
28 | info.biSizeImage = 0;
29 | }
30 |
31 | ScreenCapturer::~ScreenCapturer(){
32 | free(pixels);
33 | ReleaseDC(hWnd, hWindowDC);
34 | DeleteDC(hCaptureDC);
35 | DeleteObject(hCaptureBitmap);
36 | }
37 |
38 | void ScreenCapturer::capture() {
39 | StretchBlt(hCaptureDC, 0, 0, imageWidth, imageHeight, hWindowDC, 0, 0, windowWidth, windowHeight, SRCCOPY);
40 | GetDIBits(hCaptureDC, hCaptureBitmap, 0, imageHeight, pixels, (BITMAPINFO*)&info, DIB_RGB_COLORS);
41 | }
--------------------------------------------------------------------------------
/ScreenCapturer.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #define WIN32_LEAN_AND_MEAN
4 | #include
5 |
6 | class ScreenCapturer {
7 | private:
8 | int windowWidth;
9 | int windowHeight;
10 |
11 | HWND hWnd;
12 | HDC hWindowDC;
13 | HDC hCaptureDC;
14 | HBITMAP hCaptureBitmap;
15 | BITMAPINFOHEADER info;
16 | public:
17 | int imageWidth;
18 | int imageHeight;
19 | int length;
20 |
21 | UINT8* pixels;
22 |
23 |
24 | ScreenCapturer(int frameWidth, int frameHeight);
25 | ~ScreenCapturer();
26 | void capture();
27 |
28 | };
--------------------------------------------------------------------------------
/Server.cpp:
--------------------------------------------------------------------------------
1 | #include "Server.h"
2 | #include
3 | #include "lib/rapidjson/document.h"
4 | #include "lib/rapidjson/stringbuffer.h"
5 | #include "lib/main.h"
6 |
7 | using namespace rapidjson;
8 |
9 | Server::Server(unsigned int port) {
10 | struct sockaddr_in server;
11 | freopen("deepgtav.log", "w", stdout);
12 |
13 | printf("\nInitializing Winsock...");
14 | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
15 | printf("Failed. Error Code: %d", WSAGetLastError());
16 | }
17 | printf("Initialized.\n");
18 |
19 | if ((ServerSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
20 | printf("Could not create socket: %d", WSAGetLastError());
21 | }
22 | printf("Socket created.\n");
23 |
24 | server.sin_family = AF_INET;
25 | server.sin_addr.s_addr = INADDR_ANY;
26 | server.sin_port = htons(port);
27 |
28 | if (bind(ServerSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) {
29 | printf("Bind failed with error code: %d", WSAGetLastError());
30 | }
31 | printf("Bind done.\n");
32 |
33 | printf("Listening...\n");
34 | if (listen(ServerSocket, 1) == SOCKET_ERROR) {
35 | printf("Could not listen: %d", WSAGetLastError());
36 | }
37 |
38 | if (ioctlsocket(ServerSocket, FIONBIO, &iMode) != NO_ERROR) {
39 | printf("Server ioctlsocket failed");
40 | }
41 |
42 | }
43 |
44 | void Server::checkClient(){
45 | SOCKET tmpSocket = SOCKET_ERROR;
46 | tmpSocket = accept(ServerSocket, NULL, NULL);
47 | if (tmpSocket != SOCKET_ERROR) {
48 | printf("Connection accepted.\n");
49 | ClientSocket = tmpSocket;
50 | if (ioctlsocket(ClientSocket, FIONBIO, &iMode) != NO_ERROR) {
51 | printf("Client ioctlsocket failed");
52 | return;
53 | }
54 | clientConnected = true;
55 | }
56 | }
57 |
58 | void Server::checkRecvMessage() {
59 | int result;
60 | Document d;
61 | int error;
62 |
63 | if (recvMessageLen == 0) {
64 | recv(ClientSocket, (char*)&recvMessageLen, 4, 0); //Receive message len first
65 | error = WSAGetLastError();
66 | if (error == WSAEWOULDBLOCK) return;
67 | if (error != 0) {
68 | printf("\nError receiving message length: %d", error);
69 | resetState();
70 | return;
71 | }
72 | }
73 |
74 | while (bytesRead < recvMessageLen){
75 | result = recv(ClientSocket, json + bytesRead, recvMessageLen - bytesRead, 0);
76 | error = WSAGetLastError();
77 | if (error == WSAEWOULDBLOCK) return;
78 | if (error != 0 || result < 1) {
79 | printf("\nError receiving message: %d", error);
80 | resetState();
81 | return;
82 | }
83 | bytesRead = bytesRead + result;
84 | }
85 |
86 | json[bytesRead] = '\0';
87 | bytesRead = 0;
88 | recvMessageLen = 0;
89 |
90 | d.Parse(json);
91 |
92 | if (d.HasMember("commands")) {
93 | printf("Commands received\n");
94 | const Value& commands = d["commands"];
95 | scenario.setCommands(commands["throttle"].GetFloat(), commands["brake"].GetFloat(), commands["steering"].GetFloat());
96 | }
97 | else if (d.HasMember("config")) {
98 | //Change the message values and keep the others the same
99 | printf("Config received\n");
100 | const Value& config = d["config"];
101 | const Value& sc = config["scenario"];
102 | const Value& dc = config["dataset"];
103 | scenario.config(sc, dc);
104 | }
105 | else if (d.HasMember("start")) {
106 | //Set the message values and randomize the others. Start sending the messages
107 | printf("Start received\n");
108 | const Value& config = d["start"];
109 | const Value& sc = config["scenario"];
110 | const Value& dc = config["dataset"];
111 | scenario.start(sc, dc);
112 | sendOutputs = true;
113 | }
114 | else if (d.HasMember("stop")) {
115 | //Stop sendig messages, keep client connected
116 | printf("Stop received\n");
117 | sendOutputs = false;
118 | scenario.stop();
119 | }
120 | else {
121 | return; //Invalid message
122 | }
123 |
124 | }
125 |
126 | void Server::checkSendMessage() {
127 | int error;
128 | int r;
129 |
130 | if (sendOutputs && (((float)(std::clock() - lastSentMessage) / CLOCKS_PER_SEC) > (1.0 / scenario.rate))) {
131 | if (messageSize == 0) {
132 | message = scenario.generateMessage();
133 | chmessage = message.GetString();
134 | messageSize = message.GetSize();
135 | }
136 |
137 | if (!frameSent) {
138 | if (!readyToSend) {
139 | send(ClientSocket, (const char*)&scenario.screenCapturer->length, sizeof(scenario.screenCapturer->length), 0);
140 | error = WSAGetLastError();
141 | if (error == WSAEWOULDBLOCK) return;
142 | if (error != 0) {
143 | printf("\nError sending frame length: %d", error);
144 | resetState();
145 | return;
146 | }
147 | readyToSend = true;
148 | sendMessageLen = 0;
149 | }
150 |
151 | while (readyToSend && (sendMessageLen < scenario.screenCapturer->length)) {
152 | r = send(ClientSocket, (const char*)(scenario.screenCapturer->pixels + sendMessageLen), scenario.screenCapturer->length - sendMessageLen, 0);
153 | error = WSAGetLastError();
154 | if (error == WSAEWOULDBLOCK) return;
155 | if (error != 0 || r <= 1) {
156 | printf("\nError sending frame: %d", error);
157 | resetState();
158 | return;
159 | }
160 | sendMessageLen = sendMessageLen + r;
161 | }
162 | readyToSend = false;
163 | frameSent = true;
164 | }
165 |
166 | if (frameSent) {
167 | if (!readyToSend) {
168 | send(ClientSocket, (const char*)&messageSize, sizeof(messageSize), 0);
169 | error = WSAGetLastError();
170 | if (error == WSAEWOULDBLOCK) return;
171 | if (error != 0) {
172 | printf("\nError sending message length: %d", error);
173 | resetState();
174 | return;
175 | }
176 | readyToSend = true;
177 | sendMessageLen = 0;
178 | }
179 |
180 | while (readyToSend && (sendMessageLen < messageSize)) {
181 | r = send(ClientSocket, (const char*)(chmessage + sendMessageLen), messageSize - sendMessageLen, 0);
182 | error = WSAGetLastError();
183 | if (error == WSAEWOULDBLOCK) return;
184 | if (error != 0 || r <= 1) {
185 | printf("\nError sending message: %d", error);
186 | resetState();
187 | return;
188 | }
189 | sendMessageLen = sendMessageLen + r;
190 | }
191 | readyToSend = false;
192 | messageSize = 0;
193 | frameSent = false;
194 | }
195 | lastSentMessage = std::clock();
196 | }
197 | }
198 |
199 | void Server::resetState() {
200 | shutdown(ClientSocket, SD_SEND);
201 | closesocket(ClientSocket);
202 |
203 | clientConnected = false;
204 | sendOutputs = false;
205 | bytesRead = 0;
206 | recvMessageLen = 0;
207 | sendMessageLen = 0;
208 | readyToSend = false;
209 | frameSent = false;
210 | messageSize = 0;
211 |
212 | scenario.stop();
213 | }
--------------------------------------------------------------------------------
/Server.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Scenario.h"
4 |
5 | #include
6 | #include
7 |
8 | // Need to link with Ws2_32.lib
9 | #pragma comment (lib, "ws2_32")
10 |
11 | class Server {
12 | private:
13 | WSADATA wsaData;
14 | u_long iMode = 1; //non-blocking socket
15 | SOCKET ServerSocket = INVALID_SOCKET;
16 | SOCKET ClientSocket = INVALID_SOCKET;
17 |
18 | bool sendOutputs = false;
19 | int bytesRead = 0;
20 | int recvMessageLen = 0;
21 | int sendMessageLen = 0;
22 | bool readyToSend = false;
23 | bool frameSent = false;
24 |
25 | char json[4096];
26 | StringBuffer message;
27 | const char* chmessage;
28 | int messageSize = 0;
29 | std::clock_t lastSentMessage = std::clock();
30 |
31 | void resetState();
32 |
33 | public:
34 | bool clientConnected = false;
35 | Scenario scenario;
36 |
37 | Server(unsigned int port);
38 | void checkRecvMessage();
39 | void checkSendMessage();
40 | void checkClient();
41 | };
--------------------------------------------------------------------------------
/bin/Release/DeepGTAV.asi:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/Release/DeepGTAV.asi
--------------------------------------------------------------------------------
/bin/Release/DeepGTAV.iobj:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/Release/DeepGTAV.iobj
--------------------------------------------------------------------------------
/bin/Release/DeepGTAV.ipdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/Release/DeepGTAV.ipdb
--------------------------------------------------------------------------------
/bin/Release/GTAVLauncherBypass.asi:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/Release/GTAVLauncherBypass.asi
--------------------------------------------------------------------------------
/bin/Release/GTAVLauncherBypass.ini:
--------------------------------------------------------------------------------
1 | ; Settings for GTAVLauncherBypass.asi | (C) Unknown Modder
2 |
3 | [GLOBAL]
4 | bypass_launcher_check = true
5 | bypass_intro_video = true
6 | bypass_legal_message = true
--------------------------------------------------------------------------------
/bin/Release/ScriptHookV.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/Release/ScriptHookV.dll
--------------------------------------------------------------------------------
/bin/Release/dinput8.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/Release/dinput8.dll
--------------------------------------------------------------------------------
/bin/SaveGame/SGTA50015:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/SaveGame/SGTA50015
--------------------------------------------------------------------------------
/bin/SaveGame/SGTA50015.bak:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/SaveGame/SGTA50015.bak
--------------------------------------------------------------------------------
/bin/SaveGame/cfg.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/SaveGame/cfg.dat
--------------------------------------------------------------------------------
/bin/SaveGame/pc_settings.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/bin/SaveGame/pc_settings.bin
--------------------------------------------------------------------------------
/defaults.h:
--------------------------------------------------------------------------------
1 | //DATASET
2 | #define _RATE_ 10
3 | #define _WIDTH_ 320
4 | #define _HEIGHT_ 160
5 | #define _VEHICLES_ false
6 | #define _PEDS_ false
7 | #define _TRAFFIC_SIGNS_ false
8 | #define _DIRECTION_ false
9 | #define _REWARD_ false
10 | #define _THROTTLE_ false
11 | #define _BRAKE_ false
12 | #define _STEERING_ false
13 | #define _SPEED_ false
14 | #define _YAW_RATE_ false
15 | #define _DRIVING_MODE_ false
16 | #define _LOCATION_ false
17 | #define _TIME_ false
18 |
--------------------------------------------------------------------------------
/lib/ScriptHookV.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aitorzip/DeepGTAV/b1dc8759300c6651706a0035a88d53d4cfd60b43/lib/ScriptHookV.lib
--------------------------------------------------------------------------------
/lib/main.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE IS A PART OF GTA V SCRIPT HOOK SDK
3 | http://dev-c.com
4 | (C) Alexander Blade 2015
5 | */
6 |
7 | #include "main.h"
8 | #include "script.h"
9 |
10 | BOOL APIENTRY DllMain(HMODULE hInstance, DWORD reason, LPVOID lpReserved)
11 | {
12 | switch (reason)
13 | {
14 | case DLL_PROCESS_ATTACH:
15 | scriptRegister(hInstance, ScriptMain);
16 | break;
17 | case DLL_PROCESS_DETACH:
18 | scriptUnregister(hInstance);
19 | break;
20 | }
21 | return TRUE;
22 | }
--------------------------------------------------------------------------------
/lib/main.h:
--------------------------------------------------------------------------------
1 | /*
2 | THIS FILE IS A PART OF GTA V SCRIPT HOOK SDK
3 | http://dev-c.com
4 | (C) Alexander Blade 2015-2016
5 | */
6 |
7 | #pragma once
8 |
9 | #include
10 |
11 | #define IMPORT __declspec(dllimport)
12 |
13 | /* textures */
14 |
15 | // Create texture
16 | // texFileName - texture file name, it's best to specify full texture path and use PNG textures
17 | // returns internal texture id
18 | // Texture deletion is performed automatically when game reloads scripts
19 | // Can be called only in the same thread as natives
20 |
21 | IMPORT int createTexture(const char *texFileName);
22 |
23 | // Draw texture
24 | // id - texture id recieved from createTexture()
25 | // index - each texture can have up to 64 different instances on screen at one time
26 | // level - draw level, being used in global draw order, texture instance with least level draws first
27 | // time - how much time (ms) texture instance will stay on screen, the amount of time should be enough
28 | // for it to stay on screen until the next corresponding drawTexture() call
29 | // sizeX,Y - size in screen space, should be in the range from 0.0 to 1.0, e.g setting this to 0.2 means that
30 | // texture instance will take 20% of the screen space
31 | // centerX,Y - center position in texture space, e.g. 0.5 means real texture center
32 | // posX,Y - position in screen space, [0.0, 0.0] - top left corner, [1.0, 1.0] - bottom right,
33 | // texture instance is positioned according to it's center
34 | // rotation - should be in the range from 0.0 to 1.0
35 | // screenHeightScaleFactor - screen aspect ratio, used for texture size correction, you can get it using natives
36 | // r,g,b,a - color, should be in the range from 0.0 to 1.0
37 | //
38 | // Texture instance draw parameters are updated each time script performs corresponding call to drawTexture()
39 | // You should always check your textures layout for 16:9, 16:10 and 4:3 screen aspects, for ex. in 1280x720,
40 | // 1440x900 and 1024x768 screen resolutions, use windowed mode for this
41 | // Can be called only in the same thread as natives
42 |
43 | IMPORT void drawTexture(int id, int index, int level, int time,
44 | float sizeX, float sizeY, float centerX, float centerY,
45 | float posX, float posY, float rotation, float screenHeightScaleFactor,
46 | float r, float g, float b, float a);
47 |
48 | // IDXGISwapChain::Present callback
49 | // Called right before the actual Present method call, render test calls don't trigger callbacks
50 | // When the game uses DX10 it actually uses DX11 with DX10 feature level
51 | // Remember that you can't call natives inside
52 | // void OnPresent(IDXGISwapChain *swapChain);
53 | typedef void(*PresentCallback)(void *);
54 |
55 | // Register IDXGISwapChain::Present callback
56 | // must be called on dll attach
57 | IMPORT void presentCallbackRegister(PresentCallback cb);
58 |
59 | // Unregister IDXGISwapChain::Present callback
60 | // must be called on dll detach
61 | IMPORT void presentCallbackUnregister(PresentCallback cb);
62 |
63 | /* keyboard */
64 |
65 | // DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore, BOOL isUpNow
66 | typedef void(*KeyboardHandler)(DWORD, WORD, BYTE, BOOL, BOOL, BOOL, BOOL);
67 |
68 | // Register keyboard handler
69 | // must be called on dll attach
70 | IMPORT void keyboardHandlerRegister(KeyboardHandler handler);
71 |
72 | // Unregister keyboard handler
73 | // must be called on dll detach
74 | IMPORT void keyboardHandlerUnregister(KeyboardHandler handler);
75 |
76 | /* scripts */
77 |
78 | IMPORT void scriptWait(DWORD time);
79 | IMPORT void scriptRegister(HMODULE module, void(*LP_SCRIPT_MAIN)());
80 | IMPORT void scriptRegisterAdditionalThread(HMODULE module, void(*LP_SCRIPT_MAIN)());
81 | IMPORT void scriptUnregister(HMODULE module);
82 | IMPORT void scriptUnregister(void(*LP_SCRIPT_MAIN)()); // deprecated
83 |
84 | IMPORT void nativeInit(UINT64 hash);
85 | IMPORT void nativePush64(UINT64 val);
86 | IMPORT PUINT64 nativeCall();
87 |
88 | static void WAIT(DWORD time) { scriptWait(time); }
89 | static void TERMINATE() { WAIT(MAXDWORD); }
90 |
91 | // Returns pointer to global variable
92 | // make sure that you check game version before accessing globals because
93 | // ids may differ between patches
94 | IMPORT UINT64 *getGlobalPtr(int globalId);
95 |
96 | /* world */
97 |
98 | // Get entities from internal pools
99 | // return value represents filled array elements count
100 | // can be called only in the same thread as natives
101 | IMPORT int worldGetAllVehicles(int *arr, int arrSize);
102 | IMPORT int worldGetAllPeds(int *arr, int arrSize);
103 | IMPORT int worldGetAllObjects(int *arr, int arrSize);
104 | IMPORT int worldGetAllPickups(int *arr, int arrSize);
105 |
106 | /* misc */
107 |
108 | // Returns base object pointer using it's script handle
109 | // make sure that you check game version before accessing object fields because
110 | // offsets may differ between patches
111 | IMPORT BYTE *getScriptHandleBaseAddress(int handle);
112 |
113 | enum eGameVersion : int
114 | {
115 | VER_1_0_335_2_STEAM,
116 | VER_1_0_335_2_NOSTEAM,
117 |
118 | VER_1_0_350_1_STEAM,
119 | VER_1_0_350_2_NOSTEAM,
120 |
121 | VER_1_0_372_2_STEAM,
122 | VER_1_0_372_2_NOSTEAM,
123 |
124 | VER_1_0_393_2_STEAM,
125 | VER_1_0_393_2_NOSTEAM,
126 |
127 | VER_1_0_393_4_STEAM,
128 | VER_1_0_393_4_NOSTEAM,
129 |
130 | VER_1_0_463_1_STEAM,
131 | VER_1_0_463_1_NOSTEAM,
132 |
133 | VER_1_0_505_2_STEAM,
134 | VER_1_0_505_2_NOSTEAM,
135 |
136 | VER_1_0_573_1_STEAM,
137 | VER_1_0_573_1_NOSTEAM,
138 |
139 | VER_1_0_617_1_STEAM,
140 | VER_1_0_617_1_NOSTEAM,
141 |
142 | VER_SIZE,
143 | VER_UNK = -1
144 | };
145 |
146 | IMPORT eGameVersion getGameVersion();
147 |
--------------------------------------------------------------------------------
/lib/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 | if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
183 | return NULL;
184 |
185 | void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
186 | chunkHead_->size += size;
187 | return buffer;
188 | }
189 |
190 | //! Resizes a memory block (concept Allocator)
191 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
192 | if (originalPtr == 0)
193 | return Malloc(newSize);
194 |
195 | if (newSize == 0)
196 | return NULL;
197 |
198 | originalSize = RAPIDJSON_ALIGN(originalSize);
199 | newSize = RAPIDJSON_ALIGN(newSize);
200 |
201 | // Do not shrink if new size is smaller than original
202 | if (originalSize >= newSize)
203 | return originalPtr;
204 |
205 | // Simply expand it if it is the last allocation and there is sufficient space
206 | if (originalPtr == reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
207 | size_t increment = static_cast(newSize - originalSize);
208 | if (chunkHead_->size + increment <= chunkHead_->capacity) {
209 | chunkHead_->size += increment;
210 | return originalPtr;
211 | }
212 | }
213 |
214 | // Realloc process: allocate and copy memory, do not free original buffer.
215 | if (void* newBuffer = Malloc(newSize)) {
216 | if (originalSize)
217 | std::memcpy(newBuffer, originalPtr, originalSize);
218 | return newBuffer;
219 | }
220 | else
221 | return NULL;
222 | }
223 |
224 | //! Frees a memory block (concept Allocator)
225 | static void Free(void *ptr) { (void)ptr; } // Do nothing
226 |
227 | private:
228 | //! Copy constructor is not permitted.
229 | MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
230 | //! Copy assignment operator is not permitted.
231 | MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
232 |
233 | //! Creates a new chunk.
234 | /*! \param capacity Capacity of the chunk in bytes.
235 | \return true if success.
236 | */
237 | bool AddChunk(size_t capacity) {
238 | if (!baseAllocator_)
239 | ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
240 | if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
241 | chunk->capacity = capacity;
242 | chunk->size = 0;
243 | chunk->next = chunkHead_;
244 | chunkHead_ = chunk;
245 | return true;
246 | }
247 | else
248 | return false;
249 | }
250 |
251 | static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
252 |
253 | //! Chunk header for perpending to each chunk.
254 | /*! Chunks are stored as a singly linked list.
255 | */
256 | struct ChunkHeader {
257 | size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself).
258 | size_t size; //!< Current size of allocated memory in bytes.
259 | ChunkHeader *next; //!< Next chunk in the linked list.
260 | };
261 |
262 | ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation.
263 | size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated.
264 | void *userBuffer_; //!< User supplied buffer.
265 | BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks.
266 | BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object.
267 | };
268 |
269 | RAPIDJSON_NAMESPACE_END
270 |
271 | #endif // RAPIDJSON_ENCODINGS_H_
272 |
--------------------------------------------------------------------------------
/lib/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 "stream.h"
19 | #include "memorystream.h"
20 |
21 | #ifdef __GNUC__
22 | RAPIDJSON_DIAG_PUSH
23 | RAPIDJSON_DIAG_OFF(effc++)
24 | #endif
25 |
26 | #ifdef __clang__
27 | RAPIDJSON_DIAG_PUSH
28 | RAPIDJSON_DIAG_OFF(padded)
29 | #endif
30 |
31 | RAPIDJSON_NAMESPACE_BEGIN
32 |
33 | //! Input byte stream wrapper with a statically bound encoding.
34 | /*!
35 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
36 | \tparam InputByteStream Type of input byte stream. For example, FileReadStream.
37 | */
38 | template
39 | class EncodedInputStream {
40 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
41 | public:
42 | typedef typename Encoding::Ch Ch;
43 |
44 | EncodedInputStream(InputByteStream& is) : is_(is) {
45 | current_ = Encoding::TakeBOM(is_);
46 | }
47 |
48 | Ch Peek() const { return current_; }
49 | Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; }
50 | size_t Tell() const { return is_.Tell(); }
51 |
52 | // Not implemented
53 | void Put(Ch) { RAPIDJSON_ASSERT(false); }
54 | void Flush() { RAPIDJSON_ASSERT(false); }
55 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
56 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
57 |
58 | private:
59 | EncodedInputStream(const EncodedInputStream&);
60 | EncodedInputStream& operator=(const EncodedInputStream&);
61 |
62 | InputByteStream& is_;
63 | Ch current_;
64 | };
65 |
66 | //! Specialized for UTF8 MemoryStream.
67 | template <>
68 | class EncodedInputStream, MemoryStream> {
69 | public:
70 | typedef UTF8<>::Ch Ch;
71 |
72 | EncodedInputStream(MemoryStream& is) : is_(is) {
73 | if (static_cast(is_.Peek()) == 0xEFu) is_.Take();
74 | if (static_cast(is_.Peek()) == 0xBBu) is_.Take();
75 | if (static_cast(is_.Peek()) == 0xBFu) is_.Take();
76 | }
77 | Ch Peek() const { return is_.Peek(); }
78 | Ch Take() { return is_.Take(); }
79 | size_t Tell() const { return is_.Tell(); }
80 |
81 | // Not implemented
82 | void Put(Ch) {}
83 | void Flush() {}
84 | Ch* PutBegin() { return 0; }
85 | size_t PutEnd(Ch*) { return 0; }
86 |
87 | MemoryStream& is_;
88 |
89 | private:
90 | EncodedInputStream(const EncodedInputStream&);
91 | EncodedInputStream& operator=(const EncodedInputStream&);
92 | };
93 |
94 | //! Output byte stream wrapper with statically bound encoding.
95 | /*!
96 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
97 | \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream.
98 | */
99 | template
100 | class EncodedOutputStream {
101 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
102 | public:
103 | typedef typename Encoding::Ch Ch;
104 |
105 | EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) {
106 | if (putBOM)
107 | Encoding::PutBOM(os_);
108 | }
109 |
110 | void Put(Ch c) { Encoding::Put(os_, c); }
111 | void Flush() { os_.Flush(); }
112 |
113 | // Not implemented
114 | Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;}
115 | Ch Take() { RAPIDJSON_ASSERT(false); return 0;}
116 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
117 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
118 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
119 |
120 | private:
121 | EncodedOutputStream(const EncodedOutputStream&);
122 | EncodedOutputStream& operator=(const EncodedOutputStream&);
123 |
124 | OutputByteStream& os_;
125 | };
126 |
127 | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x
128 |
129 | //! Input stream wrapper with dynamically bound encoding and automatic encoding detection.
130 | /*!
131 | \tparam CharType Type of character for reading.
132 | \tparam InputByteStream type of input byte stream to be wrapped.
133 | */
134 | template
135 | class AutoUTFInputStream {
136 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
137 | public:
138 | typedef CharType Ch;
139 |
140 | //! Constructor.
141 | /*!
142 | \param is input stream to be wrapped.
143 | \param type UTF encoding type if it is not detected from the stream.
144 | */
145 | AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) {
146 | RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
147 | DetectType();
148 | static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
149 | takeFunc_ = f[type_];
150 | current_ = takeFunc_(*is_);
151 | }
152 |
153 | UTFType GetType() const { return type_; }
154 | bool HasBOM() const { return hasBOM_; }
155 |
156 | Ch Peek() const { return current_; }
157 | Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; }
158 | size_t Tell() const { return is_->Tell(); }
159 |
160 | // Not implemented
161 | void Put(Ch) { RAPIDJSON_ASSERT(false); }
162 | void Flush() { RAPIDJSON_ASSERT(false); }
163 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
164 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
165 |
166 | private:
167 | AutoUTFInputStream(const AutoUTFInputStream&);
168 | AutoUTFInputStream& operator=(const AutoUTFInputStream&);
169 |
170 | // Detect encoding type with BOM or RFC 4627
171 | void DetectType() {
172 | // BOM (Byte Order Mark):
173 | // 00 00 FE FF UTF-32BE
174 | // FF FE 00 00 UTF-32LE
175 | // FE FF UTF-16BE
176 | // FF FE UTF-16LE
177 | // EF BB BF UTF-8
178 |
179 | const unsigned char* c = reinterpret_cast(is_->Peek4());
180 | if (!c)
181 | return;
182 |
183 | unsigned bom = static_cast(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
184 | hasBOM_ = false;
185 | if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
186 | else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
187 | else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); }
188 | else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); }
189 | else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); }
190 |
191 | // RFC 4627: Section 3
192 | // "Since the first two characters of a JSON text will always be ASCII
193 | // characters [RFC0020], it is possible to determine whether an octet
194 | // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
195 | // at the pattern of nulls in the first four octets."
196 | // 00 00 00 xx UTF-32BE
197 | // 00 xx 00 xx UTF-16BE
198 | // xx 00 00 00 UTF-32LE
199 | // xx 00 xx 00 UTF-16LE
200 | // xx xx xx xx UTF-8
201 |
202 | if (!hasBOM_) {
203 | int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
204 | switch (pattern) {
205 | case 0x08: type_ = kUTF32BE; break;
206 | case 0x0A: type_ = kUTF16BE; break;
207 | case 0x01: type_ = kUTF32LE; break;
208 | case 0x05: type_ = kUTF16LE; break;
209 | case 0x0F: type_ = kUTF8; break;
210 | default: break; // Use type defined by user.
211 | }
212 | }
213 |
214 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
215 | if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
216 | if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
217 | }
218 |
219 | typedef Ch (*TakeFunc)(InputByteStream& is);
220 | InputByteStream* is_;
221 | UTFType type_;
222 | Ch current_;
223 | TakeFunc takeFunc_;
224 | bool hasBOM_;
225 | };
226 |
227 | //! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
228 | /*!
229 | \tparam CharType Type of character for writing.
230 | \tparam OutputByteStream type of output byte stream to be wrapped.
231 | */
232 | template
233 | class AutoUTFOutputStream {
234 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
235 | public:
236 | typedef CharType Ch;
237 |
238 | //! Constructor.
239 | /*!
240 | \param os output stream to be wrapped.
241 | \param type UTF encoding type.
242 | \param putBOM Whether to write BOM at the beginning of the stream.
243 | */
244 | AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) {
245 | RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
246 |
247 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
248 | if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
249 | if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
250 |
251 | static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
252 | putFunc_ = f[type_];
253 |
254 | if (putBOM)
255 | PutBOM();
256 | }
257 |
258 | UTFType GetType() const { return type_; }
259 |
260 | void Put(Ch c) { putFunc_(*os_, c); }
261 | void Flush() { os_->Flush(); }
262 |
263 | // Not implemented
264 | Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;}
265 | Ch Take() { RAPIDJSON_ASSERT(false); return 0;}
266 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
267 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
268 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
269 |
270 | private:
271 | AutoUTFOutputStream(const AutoUTFOutputStream&);
272 | AutoUTFOutputStream& operator=(const AutoUTFOutputStream&);
273 |
274 | void PutBOM() {
275 | typedef void (*PutBOMFunc)(OutputByteStream&);
276 | static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
277 | f[type_](*os_);
278 | }
279 |
280 | typedef void (*PutFunc)(OutputByteStream&, Ch);
281 |
282 | OutputByteStream* os_;
283 | UTFType type_;
284 | PutFunc putFunc_;
285 | };
286 |
287 | #undef RAPIDJSON_ENCODINGS_FUNC
288 |
289 | RAPIDJSON_NAMESPACE_END
290 |
291 | #ifdef __clang__
292 | RAPIDJSON_DIAG_POP
293 | #endif
294 |
295 | #ifdef __GNUC__
296 | RAPIDJSON_DIAG_POP
297 | #endif
298 |
299 | #endif // RAPIDJSON_FILESTREAM_H_
300 |
--------------------------------------------------------------------------------
/lib/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 | #ifdef __clang__
21 | RAPIDJSON_DIAG_PUSH
22 | RAPIDJSON_DIAG_OFF(switch-enum)
23 | RAPIDJSON_DIAG_OFF(covered-switch-default)
24 | #endif
25 |
26 | RAPIDJSON_NAMESPACE_BEGIN
27 |
28 | //! Maps error code of parsing into error message.
29 | /*!
30 | \ingroup RAPIDJSON_ERRORS
31 | \param parseErrorCode Error code obtained in parsing.
32 | \return the error message.
33 | \note User can make a copy of this function for localization.
34 | Using switch-case is safer for future modification of error codes.
35 | */
36 | inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) {
37 | switch (parseErrorCode) {
38 | case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error.");
39 |
40 | case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty.");
41 | case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values.");
42 |
43 | case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value.");
44 |
45 | case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member.");
46 | case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member.");
47 | case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member.");
48 |
49 | case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element.");
50 |
51 | case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string.");
52 | case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid.");
53 | case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string.");
54 | case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string.");
55 | case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string.");
56 |
57 | case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double.");
58 | case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number.");
59 | case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number.");
60 |
61 | case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error.");
62 | case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error.");
63 |
64 | default: return RAPIDJSON_ERROR_STRING("Unknown error.");
65 | }
66 | }
67 |
68 | RAPIDJSON_NAMESPACE_END
69 |
70 | #ifdef __clang__
71 | RAPIDJSON_DIAG_POP
72 | #endif
73 |
74 | #endif // RAPIDJSON_ERROR_EN_H_
75 |
--------------------------------------------------------------------------------
/lib/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 | #ifdef __clang__
21 | RAPIDJSON_DIAG_PUSH
22 | RAPIDJSON_DIAG_OFF(padded)
23 | #endif
24 |
25 | /*! \file error.h */
26 |
27 | /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
28 |
29 | ///////////////////////////////////////////////////////////////////////////////
30 | // RAPIDJSON_ERROR_CHARTYPE
31 |
32 | //! Character type of error messages.
33 | /*! \ingroup RAPIDJSON_ERRORS
34 | The default character type is \c char.
35 | On Windows, user can define this macro as \c TCHAR for supporting both
36 | unicode/non-unicode settings.
37 | */
38 | #ifndef RAPIDJSON_ERROR_CHARTYPE
39 | #define RAPIDJSON_ERROR_CHARTYPE char
40 | #endif
41 |
42 | ///////////////////////////////////////////////////////////////////////////////
43 | // RAPIDJSON_ERROR_STRING
44 |
45 | //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
46 | /*! \ingroup RAPIDJSON_ERRORS
47 | By default this conversion macro does nothing.
48 | On Windows, user can define this macro as \c _T(x) for supporting both
49 | unicode/non-unicode settings.
50 | */
51 | #ifndef RAPIDJSON_ERROR_STRING
52 | #define RAPIDJSON_ERROR_STRING(x) x
53 | #endif
54 |
55 | RAPIDJSON_NAMESPACE_BEGIN
56 |
57 | ///////////////////////////////////////////////////////////////////////////////
58 | // ParseErrorCode
59 |
60 | //! Error code of parsing.
61 | /*! \ingroup RAPIDJSON_ERRORS
62 | \see GenericReader::Parse, GenericReader::GetParseErrorCode
63 | */
64 | enum ParseErrorCode {
65 | kParseErrorNone = 0, //!< No error.
66 |
67 | kParseErrorDocumentEmpty, //!< The document is empty.
68 | kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
69 |
70 | kParseErrorValueInvalid, //!< Invalid value.
71 |
72 | kParseErrorObjectMissName, //!< Missing a name for object member.
73 | kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
74 | kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
75 |
76 | kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
77 |
78 | kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
79 | kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
80 | kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
81 | kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
82 | kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
83 |
84 | kParseErrorNumberTooBig, //!< Number too big to be stored in double.
85 | kParseErrorNumberMissFraction, //!< Miss fraction part in number.
86 | kParseErrorNumberMissExponent, //!< Miss exponent in number.
87 |
88 | kParseErrorTermination, //!< Parsing was terminated.
89 | kParseErrorUnspecificSyntaxError //!< Unspecific syntax error.
90 | };
91 |
92 | //! Result of parsing (wraps ParseErrorCode)
93 | /*!
94 | \ingroup RAPIDJSON_ERRORS
95 | \code
96 | Document doc;
97 | ParseResult ok = doc.Parse("[42]");
98 | if (!ok) {
99 | fprintf(stderr, "JSON parse error: %s (%u)",
100 | GetParseError_En(ok.Code()), ok.Offset());
101 | exit(EXIT_FAILURE);
102 | }
103 | \endcode
104 | \see GenericReader::Parse, GenericDocument::Parse
105 | */
106 | struct ParseResult {
107 | public:
108 | //! Default constructor, no error.
109 | ParseResult() : code_(kParseErrorNone), offset_(0) {}
110 | //! Constructor to set an error.
111 | ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
112 |
113 | //! Get the error code.
114 | ParseErrorCode Code() const { return code_; }
115 | //! Get the error offset, if \ref IsError(), 0 otherwise.
116 | size_t Offset() const { return offset_; }
117 |
118 | //! Conversion to \c bool, returns \c true, iff !\ref IsError().
119 | operator bool() const { return !IsError(); }
120 | //! Whether the result is an error.
121 | bool IsError() const { return code_ != kParseErrorNone; }
122 |
123 | bool operator==(const ParseResult& that) const { return code_ == that.code_; }
124 | bool operator==(ParseErrorCode code) const { return code_ == code; }
125 | friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
126 |
127 | //! Reset error code.
128 | void Clear() { Set(kParseErrorNone); }
129 | //! Update error code and offset.
130 | void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
131 |
132 | private:
133 | ParseErrorCode code_;
134 | size_t offset_;
135 | };
136 |
137 | //! Function pointer type of GetParseError().
138 | /*! \ingroup RAPIDJSON_ERRORS
139 |
140 | This is the prototype for \c GetParseError_X(), where \c X is a locale.
141 | User can dynamically change locale in runtime, e.g.:
142 | \code
143 | GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
144 | const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
145 | \endcode
146 | */
147 | typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
148 |
149 | RAPIDJSON_NAMESPACE_END
150 |
151 | #ifdef __clang__
152 | RAPIDJSON_DIAG_POP
153 | #endif
154 |
155 | #endif // RAPIDJSON_ERROR_ERROR_H_
156 |
--------------------------------------------------------------------------------
/lib/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 "stream.h"
19 | #include
20 |
21 | #ifdef __clang__
22 | RAPIDJSON_DIAG_PUSH
23 | RAPIDJSON_DIAG_OFF(padded)
24 | RAPIDJSON_DIAG_OFF(unreachable-code)
25 | RAPIDJSON_DIAG_OFF(missing-noreturn)
26 | #endif
27 |
28 | RAPIDJSON_NAMESPACE_BEGIN
29 |
30 | //! File byte stream for input using fread().
31 | /*!
32 | \note implements Stream concept
33 | */
34 | class FileReadStream {
35 | public:
36 | typedef char Ch; //!< Character type (byte).
37 |
38 | //! Constructor.
39 | /*!
40 | \param fp File pointer opened for read.
41 | \param buffer user-supplied buffer.
42 | \param bufferSize size of buffer in bytes. Must >=4 bytes.
43 | */
44 | 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) {
45 | RAPIDJSON_ASSERT(fp_ != 0);
46 | RAPIDJSON_ASSERT(bufferSize >= 4);
47 | Read();
48 | }
49 |
50 | Ch Peek() const { return *current_; }
51 | Ch Take() { Ch c = *current_; Read(); return c; }
52 | size_t Tell() const { return count_ + static_cast(current_ - buffer_); }
53 |
54 | // Not implemented
55 | void Put(Ch) { RAPIDJSON_ASSERT(false); }
56 | void Flush() { RAPIDJSON_ASSERT(false); }
57 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
58 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
59 |
60 | // For encoding detection only.
61 | const Ch* Peek4() const {
62 | return (current_ + 4 <= bufferLast_) ? current_ : 0;
63 | }
64 |
65 | private:
66 | void Read() {
67 | if (current_ < bufferLast_)
68 | ++current_;
69 | else if (!eof_) {
70 | count_ += readCount_;
71 | readCount_ = fread(buffer_, 1, bufferSize_, fp_);
72 | bufferLast_ = buffer_ + readCount_ - 1;
73 | current_ = buffer_;
74 |
75 | if (readCount_ < bufferSize_) {
76 | buffer_[readCount_] = '\0';
77 | ++bufferLast_;
78 | eof_ = true;
79 | }
80 | }
81 | }
82 |
83 | std::FILE* fp_;
84 | Ch *buffer_;
85 | size_t bufferSize_;
86 | Ch *bufferLast_;
87 | Ch *current_;
88 | size_t readCount_;
89 | size_t count_; //!< Number of characters read
90 | bool eof_;
91 | };
92 |
93 | RAPIDJSON_NAMESPACE_END
94 |
95 | #ifdef __clang__
96 | RAPIDJSON_DIAG_POP
97 | #endif
98 |
99 | #endif // RAPIDJSON_FILESTREAM_H_
100 |
--------------------------------------------------------------------------------
/lib/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 "stream.h"
19 | #include
20 |
21 | #ifdef __clang__
22 | RAPIDJSON_DIAG_PUSH
23 | RAPIDJSON_DIAG_OFF(unreachable-code)
24 | #endif
25 |
26 | RAPIDJSON_NAMESPACE_BEGIN
27 |
28 | //! Wrapper of C file stream for input using fread().
29 | /*!
30 | \note implements Stream concept
31 | */
32 | class FileWriteStream {
33 | public:
34 | typedef char Ch; //!< Character type. Only support char.
35 |
36 | FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
37 | RAPIDJSON_ASSERT(fp_ != 0);
38 | }
39 |
40 | void Put(char c) {
41 | if (current_ >= bufferEnd_)
42 | Flush();
43 |
44 | *current_++ = c;
45 | }
46 |
47 | void PutN(char c, size_t n) {
48 | size_t avail = static_cast(bufferEnd_ - current_);
49 | while (n > avail) {
50 | std::memset(current_, c, avail);
51 | current_ += avail;
52 | Flush();
53 | n -= avail;
54 | avail = static_cast(bufferEnd_ - current_);
55 | }
56 |
57 | if (n > 0) {
58 | std::memset(current_, c, n);
59 | current_ += n;
60 | }
61 | }
62 |
63 | void Flush() {
64 | if (current_ != buffer_) {
65 | size_t result = fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_);
66 | if (result < static_cast(current_ - buffer_)) {
67 | // failure deliberately ignored at this time
68 | // added to avoid warn_unused_result build errors
69 | }
70 | current_ = buffer_;
71 | }
72 | }
73 |
74 | // Not implemented
75 | char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
76 | char Take() { RAPIDJSON_ASSERT(false); return 0; }
77 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
78 | char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
79 | size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
80 |
81 | private:
82 | // Prohibit copy constructor & assignment operator.
83 | FileWriteStream(const FileWriteStream&);
84 | FileWriteStream& operator=(const FileWriteStream&);
85 |
86 | std::FILE* fp_;
87 | char *buffer_;
88 | char *bufferEnd_;
89 | char *current_;
90 | };
91 |
92 | //! Implement specialized version of PutN() with memset() for better performance.
93 | template<>
94 | inline void PutN(FileWriteStream& stream, char c, size_t n) {
95 | stream.PutN(c, n);
96 | }
97 |
98 | RAPIDJSON_NAMESPACE_END
99 |
100 | #ifdef __clang__
101 | RAPIDJSON_DIAG_POP
102 | #endif
103 |
104 | #endif // RAPIDJSON_FILESTREAM_H_
105 |
--------------------------------------------------------------------------------
/lib/rapidjson/fwd.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_FWD_H_
16 | #define RAPIDJSON_FWD_H_
17 |
18 | #include "rapidjson.h"
19 |
20 | RAPIDJSON_NAMESPACE_BEGIN
21 |
22 | // encodings.h
23 |
24 | template struct UTF8;
25 | template struct UTF16;
26 | template struct UTF16BE;
27 | template struct UTF16LE;
28 | template struct UTF32;
29 | template struct UTF32BE;
30 | template struct UTF32LE;
31 | template struct ASCII;
32 | template struct AutoUTF;
33 |
34 | template
35 | struct Transcoder;
36 |
37 | // allocators.h
38 |
39 | class CrtAllocator;
40 |
41 | template
42 | class MemoryPoolAllocator;
43 |
44 | // stream.h
45 |
46 | template
47 | struct GenericStringStream;
48 |
49 | typedef GenericStringStream > StringStream;
50 |
51 | template
52 | struct GenericInsituStringStream;
53 |
54 | typedef GenericInsituStringStream > InsituStringStream;
55 |
56 | // stringbuffer.h
57 |
58 | template
59 | class GenericStringBuffer;
60 |
61 | typedef GenericStringBuffer, CrtAllocator> StringBuffer;
62 |
63 | // filereadstream.h
64 |
65 | class FileReadStream;
66 |
67 | // filewritestream.h
68 |
69 | class FileWriteStream;
70 |
71 | // memorybuffer.h
72 |
73 | template
74 | struct GenericMemoryBuffer;
75 |
76 | typedef GenericMemoryBuffer MemoryBuffer;
77 |
78 | // memorystream.h
79 |
80 | struct MemoryStream;
81 |
82 | // reader.h
83 |
84 | template
85 | struct BaseReaderHandler;
86 |
87 | template
88 | class GenericReader;
89 |
90 | typedef GenericReader, UTF8, CrtAllocator> Reader;
91 |
92 | // writer.h
93 |
94 | template
95 | class Writer;
96 |
97 | // prettywriter.h
98 |
99 | template
100 | class PrettyWriter;
101 |
102 | // document.h
103 |
104 | template
105 | struct GenericMember;
106 |
107 | template
108 | class GenericMemberIterator;
109 |
110 | template
111 | struct GenericStringRef;
112 |
113 | template
114 | class GenericValue;
115 |
116 | typedef GenericValue, MemoryPoolAllocator > Value;
117 |
118 | template
119 | class GenericDocument;
120 |
121 | typedef GenericDocument, MemoryPoolAllocator, CrtAllocator> Document;
122 |
123 | // pointer.h
124 |
125 | template
126 | class GenericPointer;
127 |
128 | typedef GenericPointer Pointer;
129 |
130 | // schema.h
131 |
132 | template
133 | class IGenericRemoteSchemaDocumentProvider;
134 |
135 | template
136 | class GenericSchemaDocument;
137 |
138 | typedef GenericSchemaDocument SchemaDocument;
139 | typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider;
140 |
141 | template <
142 | typename SchemaDocumentType,
143 | typename OutputHandler,
144 | typename StateAllocator>
145 | class GenericSchemaValidator;
146 |
147 | typedef GenericSchemaValidator, void>, CrtAllocator> SchemaValidator;
148 |
149 | RAPIDJSON_NAMESPACE_END
150 |
151 | #endif // RAPIDJSON_RAPIDJSONFWD_H_
152 |
--------------------------------------------------------------------------------
/lib/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 | #pragma intrinsic(_umul128)
23 | #endif
24 |
25 | RAPIDJSON_NAMESPACE_BEGIN
26 | namespace internal {
27 |
28 | class BigInteger {
29 | public:
30 | typedef uint64_t Type;
31 |
32 | BigInteger(const BigInteger& rhs) : count_(rhs.count_) {
33 | std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
34 | }
35 |
36 | explicit BigInteger(uint64_t u) : count_(1) {
37 | digits_[0] = u;
38 | }
39 |
40 | BigInteger(const char* decimals, size_t length) : count_(1) {
41 | RAPIDJSON_ASSERT(length > 0);
42 | digits_[0] = 0;
43 | size_t i = 0;
44 | const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
45 | while (length >= kMaxDigitPerIteration) {
46 | AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
47 | length -= kMaxDigitPerIteration;
48 | i += kMaxDigitPerIteration;
49 | }
50 |
51 | if (length > 0)
52 | AppendDecimal64(decimals + i, decimals + i + length);
53 | }
54 |
55 | BigInteger& operator=(const BigInteger &rhs)
56 | {
57 | if (this != &rhs) {
58 | count_ = rhs.count_;
59 | std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
60 | }
61 | return *this;
62 | }
63 |
64 | BigInteger& operator=(uint64_t u) {
65 | digits_[0] = u;
66 | count_ = 1;
67 | return *this;
68 | }
69 |
70 | BigInteger& operator+=(uint64_t u) {
71 | Type backup = digits_[0];
72 | digits_[0] += u;
73 | for (size_t i = 0; i < count_ - 1; i++) {
74 | if (digits_[i] >= backup)
75 | return *this; // no carry
76 | backup = digits_[i + 1];
77 | digits_[i + 1] += 1;
78 | }
79 |
80 | // Last carry
81 | if (digits_[count_ - 1] < backup)
82 | PushBack(1);
83 |
84 | return *this;
85 | }
86 |
87 | BigInteger& operator*=(uint64_t u) {
88 | if (u == 0) return *this = 0;
89 | if (u == 1) return *this;
90 | if (*this == 1) return *this = u;
91 |
92 | uint64_t k = 0;
93 | for (size_t i = 0; i < count_; i++) {
94 | uint64_t hi;
95 | digits_[i] = MulAdd64(digits_[i], u, k, &hi);
96 | k = hi;
97 | }
98 |
99 | if (k > 0)
100 | PushBack(k);
101 |
102 | return *this;
103 | }
104 |
105 | BigInteger& operator*=(uint32_t u) {
106 | if (u == 0) return *this = 0;
107 | if (u == 1) return *this;
108 | if (*this == 1) return *this = u;
109 |
110 | uint64_t k = 0;
111 | for (size_t i = 0; i < count_; i++) {
112 | const uint64_t c = digits_[i] >> 32;
113 | const uint64_t d = digits_[i] & 0xFFFFFFFF;
114 | const uint64_t uc = u * c;
115 | const uint64_t ud = u * d;
116 | const uint64_t p0 = ud + k;
117 | const uint64_t p1 = uc + (p0 >> 32);
118 | digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
119 | k = p1 >> 32;
120 | }
121 |
122 | if (k > 0)
123 | PushBack(k);
124 |
125 | return *this;
126 | }
127 |
128 | BigInteger& operator<<=(size_t shift) {
129 | if (IsZero() || shift == 0) return *this;
130 |
131 | size_t offset = shift / kTypeBit;
132 | size_t interShift = shift % kTypeBit;
133 | RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
134 |
135 | if (interShift == 0) {
136 | std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type));
137 | count_ += offset;
138 | }
139 | else {
140 | digits_[count_] = 0;
141 | for (size_t i = count_; i > 0; i--)
142 | digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
143 | digits_[offset] = digits_[0] << interShift;
144 | count_ += offset;
145 | if (digits_[count_])
146 | count_++;
147 | }
148 |
149 | std::memset(digits_, 0, offset * sizeof(Type));
150 |
151 | return *this;
152 | }
153 |
154 | bool operator==(const BigInteger& rhs) const {
155 | return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
156 | }
157 |
158 | bool operator==(const Type rhs) const {
159 | return count_ == 1 && digits_[0] == rhs;
160 | }
161 |
162 | BigInteger& MultiplyPow5(unsigned exp) {
163 | static const uint32_t kPow5[12] = {
164 | 5,
165 | 5 * 5,
166 | 5 * 5 * 5,
167 | 5 * 5 * 5 * 5,
168 | 5 * 5 * 5 * 5 * 5,
169 | 5 * 5 * 5 * 5 * 5 * 5,
170 | 5 * 5 * 5 * 5 * 5 * 5 * 5,
171 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
172 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
173 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
174 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
175 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
176 | };
177 | if (exp == 0) return *this;
178 | for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
179 | for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13
180 | if (exp > 0) *this *= kPow5[exp - 1];
181 | return *this;
182 | }
183 |
184 | // Compute absolute difference of this and rhs.
185 | // Assume this != rhs
186 | bool Difference(const BigInteger& rhs, BigInteger* out) const {
187 | int cmp = Compare(rhs);
188 | RAPIDJSON_ASSERT(cmp != 0);
189 | const BigInteger *a, *b; // Makes a > b
190 | bool ret;
191 | if (cmp < 0) { a = &rhs; b = this; ret = true; }
192 | else { a = this; b = &rhs; ret = false; }
193 |
194 | Type borrow = 0;
195 | for (size_t i = 0; i < a->count_; i++) {
196 | Type d = a->digits_[i] - borrow;
197 | if (i < b->count_)
198 | d -= b->digits_[i];
199 | borrow = (d > a->digits_[i]) ? 1 : 0;
200 | out->digits_[i] = d;
201 | if (d != 0)
202 | out->count_ = i + 1;
203 | }
204 |
205 | return ret;
206 | }
207 |
208 | int Compare(const BigInteger& rhs) const {
209 | if (count_ != rhs.count_)
210 | return count_ < rhs.count_ ? -1 : 1;
211 |
212 | for (size_t i = count_; i-- > 0;)
213 | if (digits_[i] != rhs.digits_[i])
214 | return digits_[i] < rhs.digits_[i] ? -1 : 1;
215 |
216 | return 0;
217 | }
218 |
219 | size_t GetCount() const { return count_; }
220 | Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
221 | bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
222 |
223 | private:
224 | void AppendDecimal64(const char* begin, const char* end) {
225 | uint64_t u = ParseUint64(begin, end);
226 | if (IsZero())
227 | *this = u;
228 | else {
229 | unsigned exp = static_cast(end - begin);
230 | (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
231 | }
232 | }
233 |
234 | void PushBack(Type digit) {
235 | RAPIDJSON_ASSERT(count_ < kCapacity);
236 | digits_[count_++] = digit;
237 | }
238 |
239 | static uint64_t ParseUint64(const char* begin, const char* end) {
240 | uint64_t r = 0;
241 | for (const char* p = begin; p != end; ++p) {
242 | RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
243 | r = r * 10u + static_cast(*p - '0');
244 | }
245 | return r;
246 | }
247 |
248 | // Assume a * b + k < 2^128
249 | static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
250 | #if defined(_MSC_VER) && defined(_M_AMD64)
251 | uint64_t low = _umul128(a, b, outHigh) + k;
252 | if (low < k)
253 | (*outHigh)++;
254 | return low;
255 | #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
256 | __extension__ typedef unsigned __int128 uint128;
257 | uint128 p = static_cast(a) * static_cast(b);
258 | p += k;
259 | *outHigh = static_cast(p >> 64);
260 | return static_cast(p);
261 | #else
262 | const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
263 | uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
264 | x1 += (x0 >> 32); // can't give carry
265 | x1 += x2;
266 | if (x1 < x2)
267 | x3 += (static_cast(1) << 32);
268 | uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
269 | uint64_t hi = x3 + (x1 >> 32);
270 |
271 | lo += k;
272 | if (lo < k)
273 | hi++;
274 | *outHigh = hi;
275 | return lo;
276 | #endif
277 | }
278 |
279 | static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000
280 | static const size_t kCapacity = kBitCount / sizeof(Type);
281 | static const size_t kTypeBit = sizeof(Type) * 8;
282 |
283 | Type digits_[kCapacity];
284 | size_t count_;
285 | };
286 |
287 | } // namespace internal
288 | RAPIDJSON_NAMESPACE_END
289 |
290 | #endif // RAPIDJSON_BIGINTEGER_H_
291 |
--------------------------------------------------------------------------------
/lib/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 | RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
33 | #endif
34 |
35 | inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
36 | while (rest < wp_w && delta - rest >= ten_kappa &&
37 | (rest + ten_kappa < wp_w || /// closer
38 | wp_w - rest > rest + ten_kappa - wp_w)) {
39 | buffer[len - 1]--;
40 | rest += ten_kappa;
41 | }
42 | }
43 |
44 | inline int CountDecimalDigit32(uint32_t n) {
45 | // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
46 | if (n < 10) return 1;
47 | if (n < 100) return 2;
48 | if (n < 1000) return 3;
49 | if (n < 10000) return 4;
50 | if (n < 100000) return 5;
51 | if (n < 1000000) return 6;
52 | if (n < 10000000) return 7;
53 | if (n < 100000000) return 8;
54 | // Will not reach 10 digits in DigitGen()
55 | //if (n < 1000000000) return 9;
56 | //return 10;
57 | return 9;
58 | }
59 |
60 | inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
61 | static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
62 | const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
63 | const DiyFp wp_w = Mp - W;
64 | uint32_t p1 = static_cast(Mp.f >> -one.e);
65 | uint64_t p2 = Mp.f & (one.f - 1);
66 | int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
67 | *len = 0;
68 |
69 | while (kappa > 0) {
70 | uint32_t d = 0;
71 | switch (kappa) {
72 | case 9: d = p1 / 100000000; p1 %= 100000000; break;
73 | case 8: d = p1 / 10000000; p1 %= 10000000; break;
74 | case 7: d = p1 / 1000000; p1 %= 1000000; break;
75 | case 6: d = p1 / 100000; p1 %= 100000; break;
76 | case 5: d = p1 / 10000; p1 %= 10000; break;
77 | case 4: d = p1 / 1000; p1 %= 1000; break;
78 | case 3: d = p1 / 100; p1 %= 100; break;
79 | case 2: d = p1 / 10; p1 %= 10; break;
80 | case 1: d = p1; p1 = 0; break;
81 | default:;
82 | }
83 | if (d || *len)
84 | buffer[(*len)++] = static_cast('0' + static_cast(d));
85 | kappa--;
86 | uint64_t tmp = (static_cast(p1) << -one.e) + p2;
87 | if (tmp <= delta) {
88 | *K += kappa;
89 | GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f);
90 | return;
91 | }
92 | }
93 |
94 | // kappa = 0
95 | for (;;) {
96 | p2 *= 10;
97 | delta *= 10;
98 | char d = static_cast(p2 >> -one.e);
99 | if (d || *len)
100 | buffer[(*len)++] = static_cast('0' + d);
101 | p2 &= one.f - 1;
102 | kappa--;
103 | if (p2 < delta) {
104 | *K += kappa;
105 | int index = -kappa;
106 | GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[index] : 0));
107 | return;
108 | }
109 | }
110 | }
111 |
112 | inline void Grisu2(double value, char* buffer, int* length, int* K) {
113 | const DiyFp v(value);
114 | DiyFp w_m, w_p;
115 | v.NormalizedBoundaries(&w_m, &w_p);
116 |
117 | const DiyFp c_mk = GetCachedPower(w_p.e, K);
118 | const DiyFp W = v.Normalize() * c_mk;
119 | DiyFp Wp = w_p * c_mk;
120 | DiyFp Wm = w_m * c_mk;
121 | Wm.f++;
122 | Wp.f--;
123 | DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
124 | }
125 |
126 | inline char* WriteExponent(int K, char* buffer) {
127 | if (K < 0) {
128 | *buffer++ = '-';
129 | K = -K;
130 | }
131 |
132 | if (K >= 100) {
133 | *buffer++ = static_cast('0' + static_cast(K / 100));
134 | K %= 100;
135 | const char* d = GetDigitsLut() + K * 2;
136 | *buffer++ = d[0];
137 | *buffer++ = d[1];
138 | }
139 | else if (K >= 10) {
140 | const char* d = GetDigitsLut() + K * 2;
141 | *buffer++ = d[0];
142 | *buffer++ = d[1];
143 | }
144 | else
145 | *buffer++ = static_cast('0' + static_cast(K));
146 |
147 | return buffer;
148 | }
149 |
150 | inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
151 | const int kk = length + k; // 10^(kk-1) <= v < 10^kk
152 |
153 | if (0 <= k && kk <= 21) {
154 | // 1234e7 -> 12340000000
155 | for (int i = length; i < kk; i++)
156 | buffer[i] = '0';
157 | buffer[kk] = '.';
158 | buffer[kk + 1] = '0';
159 | return &buffer[kk + 2];
160 | }
161 | else if (0 < kk && kk <= 21) {
162 | // 1234e-2 -> 12.34
163 | std::memmove(&buffer[kk + 1], &buffer[kk], static_cast(length - kk));
164 | buffer[kk] = '.';
165 | if (0 > k + maxDecimalPlaces) {
166 | // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
167 | // Remove extra trailing zeros (at least one) after truncation.
168 | for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)
169 | if (buffer[i] != '0')
170 | return &buffer[i + 1];
171 | return &buffer[kk + 2]; // Reserve one zero
172 | }
173 | else
174 | return &buffer[length + 1];
175 | }
176 | else if (-6 < kk && kk <= 0) {
177 | // 1234e-6 -> 0.001234
178 | const int offset = 2 - kk;
179 | std::memmove(&buffer[offset], &buffer[0], static_cast(length));
180 | buffer[0] = '0';
181 | buffer[1] = '.';
182 | for (int i = 2; i < offset; i++)
183 | buffer[i] = '0';
184 | if (length - kk > maxDecimalPlaces) {
185 | // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
186 | // Remove extra trailing zeros (at least one) after truncation.
187 | for (int i = maxDecimalPlaces + 1; i > 2; i--)
188 | if (buffer[i] != '0')
189 | return &buffer[i + 1];
190 | return &buffer[3]; // Reserve one zero
191 | }
192 | else
193 | return &buffer[length + offset];
194 | }
195 | else if (kk < -maxDecimalPlaces) {
196 | // Truncate to zero
197 | buffer[0] = '0';
198 | buffer[1] = '.';
199 | buffer[2] = '0';
200 | return &buffer[3];
201 | }
202 | else if (length == 1) {
203 | // 1e30
204 | buffer[1] = 'e';
205 | return WriteExponent(kk - 1, &buffer[2]);
206 | }
207 | else {
208 | // 1234e30 -> 1.234e33
209 | std::memmove(&buffer[2], &buffer[1], static_cast(length - 1));
210 | buffer[1] = '.';
211 | buffer[length + 1] = 'e';
212 | return WriteExponent(kk - 1, &buffer[0 + length + 2]);
213 | }
214 | }
215 |
216 | inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
217 | RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
218 | Double d(value);
219 | if (d.IsZero()) {
220 | if (d.Sign())
221 | *buffer++ = '-'; // -0.0, Issue #289
222 | buffer[0] = '0';
223 | buffer[1] = '.';
224 | buffer[2] = '0';
225 | return &buffer[3];
226 | }
227 | else {
228 | if (value < 0) {
229 | *buffer++ = '-';
230 | value = -value;
231 | }
232 | int length, K;
233 | Grisu2(value, buffer, &length, &K);
234 | return Prettify(buffer, length, K, maxDecimalPlaces);
235 | }
236 | }
237 |
238 | #ifdef __GNUC__
239 | RAPIDJSON_DIAG_POP
240 | #endif
241 |
242 | } // namespace internal
243 | RAPIDJSON_NAMESPACE_END
244 |
245 | #endif // RAPIDJSON_DTOA_
246 |
--------------------------------------------------------------------------------
/lib/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 IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
44 | bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
45 | bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
46 |
47 | uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); }
48 | int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
49 | uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
50 |
51 | static int EffectiveSignificandSize(int order) {
52 | if (order >= -1021)
53 | return 53;
54 | else if (order <= -1074)
55 | return 0;
56 | else
57 | return order + 1074;
58 | }
59 |
60 | private:
61 | static const int kSignificandSize = 52;
62 | static const int kExponentBias = 0x3FF;
63 | static const int kDenormalExponent = 1 - kExponentBias;
64 | static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
65 | static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
66 | static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
67 | static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
68 |
69 | union {
70 | double d_;
71 | uint64_t u_;
72 | };
73 | };
74 |
75 | } // namespace internal
76 | RAPIDJSON_NAMESPACE_END
77 |
78 | #endif // RAPIDJSON_IEEE754_
79 |
--------------------------------------------------------------------------------
/lib/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 |
--------------------------------------------------------------------------------
/lib/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 |
--------------------------------------------------------------------------------
/lib/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 |
--------------------------------------------------------------------------------
/lib/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 "../allocators.h"
19 | #include "swap.h"
20 |
21 | #if defined(__clang__)
22 | RAPIDJSON_DIAG_PUSH
23 | RAPIDJSON_DIAG_OFF(c++98-compat)
24 | #endif
25 |
26 | RAPIDJSON_NAMESPACE_BEGIN
27 | namespace internal {
28 |
29 | ///////////////////////////////////////////////////////////////////////////////
30 | // Stack
31 |
32 | //! A type-unsafe stack for storing different types of data.
33 | /*! \tparam Allocator Allocator for allocating stack memory.
34 | */
35 | template
36 | class Stack {
37 | public:
38 | // Optimization note: Do not allocate memory for stack_ in constructor.
39 | // Do it lazily when first Push() -> Expand() -> Resize().
40 | Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
41 | }
42 |
43 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
44 | Stack(Stack&& rhs)
45 | : allocator_(rhs.allocator_),
46 | ownAllocator_(rhs.ownAllocator_),
47 | stack_(rhs.stack_),
48 | stackTop_(rhs.stackTop_),
49 | stackEnd_(rhs.stackEnd_),
50 | initialCapacity_(rhs.initialCapacity_)
51 | {
52 | rhs.allocator_ = 0;
53 | rhs.ownAllocator_ = 0;
54 | rhs.stack_ = 0;
55 | rhs.stackTop_ = 0;
56 | rhs.stackEnd_ = 0;
57 | rhs.initialCapacity_ = 0;
58 | }
59 | #endif
60 |
61 | ~Stack() {
62 | Destroy();
63 | }
64 |
65 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
66 | Stack& operator=(Stack&& rhs) {
67 | if (&rhs != this)
68 | {
69 | Destroy();
70 |
71 | allocator_ = rhs.allocator_;
72 | ownAllocator_ = rhs.ownAllocator_;
73 | stack_ = rhs.stack_;
74 | stackTop_ = rhs.stackTop_;
75 | stackEnd_ = rhs.stackEnd_;
76 | initialCapacity_ = rhs.initialCapacity_;
77 |
78 | rhs.allocator_ = 0;
79 | rhs.ownAllocator_ = 0;
80 | rhs.stack_ = 0;
81 | rhs.stackTop_ = 0;
82 | rhs.stackEnd_ = 0;
83 | rhs.initialCapacity_ = 0;
84 | }
85 | return *this;
86 | }
87 | #endif
88 |
89 | void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
90 | internal::Swap(allocator_, rhs.allocator_);
91 | internal::Swap(ownAllocator_, rhs.ownAllocator_);
92 | internal::Swap(stack_, rhs.stack_);
93 | internal::Swap(stackTop_, rhs.stackTop_);
94 | internal::Swap(stackEnd_, rhs.stackEnd_);
95 | internal::Swap(initialCapacity_, rhs.initialCapacity_);
96 | }
97 |
98 | void Clear() { stackTop_ = stack_; }
99 |
100 | void ShrinkToFit() {
101 | if (Empty()) {
102 | // If the stack is empty, completely deallocate the memory.
103 | Allocator::Free(stack_);
104 | stack_ = 0;
105 | stackTop_ = 0;
106 | stackEnd_ = 0;
107 | }
108 | else
109 | Resize(GetSize());
110 | }
111 |
112 | // Optimization note: try to minimize the size of this function for force inline.
113 | // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
114 | template
115 | RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
116 | // Expand the stack if needed
117 | if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_))
118 | Expand(count);
119 | }
120 |
121 | template
122 | RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
123 | Reserve(count);
124 | return PushUnsafe(count);
125 | }
126 |
127 | template
128 | RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
129 | RAPIDJSON_ASSERT(stackTop_);
130 | RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
131 | T* ret = reinterpret_cast(stackTop_);
132 | stackTop_ += sizeof(T) * count;
133 | return ret;
134 | }
135 |
136 | template
137 | T* Pop(size_t count) {
138 | RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
139 | stackTop_ -= count * sizeof(T);
140 | return reinterpret_cast(stackTop_);
141 | }
142 |
143 | template
144 | T* Top() {
145 | RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
146 | return reinterpret_cast(stackTop_ - sizeof(T));
147 | }
148 |
149 | template
150 | const T* Top() const {
151 | RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
152 | return reinterpret_cast(stackTop_ - sizeof(T));
153 | }
154 |
155 | template
156 | T* End() { return reinterpret_cast(stackTop_); }
157 |
158 | template
159 | const T* End() const { return reinterpret_cast(stackTop_); }
160 |
161 | template
162 | T* Bottom() { return reinterpret_cast(stack_); }
163 |
164 | template
165 | const T* Bottom() const { return reinterpret_cast(stack_); }
166 |
167 | bool HasAllocator() const {
168 | return allocator_ != 0;
169 | }
170 |
171 | Allocator& GetAllocator() {
172 | RAPIDJSON_ASSERT(allocator_);
173 | return *allocator_;
174 | }
175 |
176 | bool Empty() const { return stackTop_ == stack_; }
177 | size_t GetSize() const { return static_cast(stackTop_ - stack_); }
178 | size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); }
179 |
180 | private:
181 | template
182 | void Expand(size_t count) {
183 | // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
184 | size_t newCapacity;
185 | if (stack_ == 0) {
186 | if (!allocator_)
187 | ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
188 | newCapacity = initialCapacity_;
189 | } else {
190 | newCapacity = GetCapacity();
191 | newCapacity += (newCapacity + 1) / 2;
192 | }
193 | size_t newSize = GetSize() + sizeof(T) * count;
194 | if (newCapacity < newSize)
195 | newCapacity = newSize;
196 |
197 | Resize(newCapacity);
198 | }
199 |
200 | void Resize(size_t newCapacity) {
201 | const size_t size = GetSize(); // Backup the current size
202 | stack_ = static_cast(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
203 | stackTop_ = stack_ + size;
204 | stackEnd_ = stack_ + newCapacity;
205 | }
206 |
207 | void Destroy() {
208 | Allocator::Free(stack_);
209 | RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
210 | }
211 |
212 | // Prohibit copy constructor & assignment operator.
213 | Stack(const Stack&);
214 | Stack& operator=(const Stack&);
215 |
216 | Allocator* allocator_;
217 | Allocator* ownAllocator_;
218 | char *stack_;
219 | char *stackTop_;
220 | char *stackEnd_;
221 | size_t initialCapacity_;
222 | };
223 |
224 | } // namespace internal
225 | RAPIDJSON_NAMESPACE_END
226 |
227 | #if defined(__clang__)
228 | RAPIDJSON_DIAG_POP
229 | #endif
230 |
231 | #endif // RAPIDJSON_STACK_H_
232 |
--------------------------------------------------------------------------------
/lib/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 "../stream.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 | RAPIDJSON_ASSERT(s != 0);
32 | const Ch* p = s;
33 | while (*p) ++p;
34 | return SizeType(p - s);
35 | }
36 |
37 | //! Returns number of code points in a encoded string.
38 | template
39 | bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
40 | RAPIDJSON_ASSERT(s != 0);
41 | RAPIDJSON_ASSERT(outCount != 0);
42 | GenericStringStream is(s);
43 | const typename Encoding::Ch* end = s + length;
44 | SizeType count = 0;
45 | while (is.src_ < end) {
46 | unsigned codepoint;
47 | if (!Encoding::Decode(is, &codepoint))
48 | return false;
49 | count++;
50 | }
51 | *outCount = count;
52 | return true;
53 | }
54 |
55 | } // namespace internal
56 | RAPIDJSON_NAMESPACE_END
57 |
58 | #endif // RAPIDJSON_INTERNAL_STRFUNC_H_
59 |
--------------------------------------------------------------------------------
/lib/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 "ieee754.h"
19 | #include "biginteger.h"
20 | #include "diyfp.h"
21 | #include "pow10.h"
22 |
23 | RAPIDJSON_NAMESPACE_BEGIN
24 | namespace internal {
25 |
26 | inline double FastPath(double significand, int exp) {
27 | if (exp < -308)
28 | return 0.0;
29 | else if (exp >= 0)
30 | return significand * internal::Pow10(exp);
31 | else
32 | return significand / internal::Pow10(-exp);
33 | }
34 |
35 | inline double StrtodNormalPrecision(double d, int p) {
36 | if (p < -308) {
37 | // Prevent expSum < -308, making Pow10(p) = 0
38 | d = FastPath(d, -308);
39 | d = FastPath(d, p + 308);
40 | }
41 | else
42 | d = FastPath(d, p);
43 | return d;
44 | }
45 |
46 | template
47 | inline T Min3(T a, T b, T c) {
48 | T m = a;
49 | if (m > b) m = b;
50 | if (m > c) m = c;
51 | return m;
52 | }
53 |
54 | inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) {
55 | const Double db(b);
56 | const uint64_t bInt = db.IntegerSignificand();
57 | const int bExp = db.IntegerExponent();
58 | const int hExp = bExp - 1;
59 |
60 | int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
61 |
62 | // Adjust for decimal exponent
63 | if (dExp >= 0) {
64 | dS_Exp2 += dExp;
65 | dS_Exp5 += dExp;
66 | }
67 | else {
68 | bS_Exp2 -= dExp;
69 | bS_Exp5 -= dExp;
70 | hS_Exp2 -= dExp;
71 | hS_Exp5 -= dExp;
72 | }
73 |
74 | // Adjust for binary exponent
75 | if (bExp >= 0)
76 | bS_Exp2 += bExp;
77 | else {
78 | dS_Exp2 -= bExp;
79 | hS_Exp2 -= bExp;
80 | }
81 |
82 | // Adjust for half ulp exponent
83 | if (hExp >= 0)
84 | hS_Exp2 += hExp;
85 | else {
86 | dS_Exp2 -= hExp;
87 | bS_Exp2 -= hExp;
88 | }
89 |
90 | // Remove common power of two factor from all three scaled values
91 | int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
92 | dS_Exp2 -= common_Exp2;
93 | bS_Exp2 -= common_Exp2;
94 | hS_Exp2 -= common_Exp2;
95 |
96 | BigInteger dS = d;
97 | dS.MultiplyPow5(static_cast(dS_Exp5)) <<= static_cast(dS_Exp2);
98 |
99 | BigInteger bS(bInt);
100 | bS.MultiplyPow5(static_cast(bS_Exp5)) <<= static_cast(bS_Exp2);
101 |
102 | BigInteger hS(1);
103 | hS.MultiplyPow5(static_cast(hS_Exp5)) <<= static_cast(hS_Exp2);
104 |
105 | BigInteger delta(0);
106 | dS.Difference(bS, &delta);
107 |
108 | return delta.Compare(hS);
109 | }
110 |
111 | inline bool StrtodFast(double d, int p, double* result) {
112 | // Use fast path for string-to-double conversion if possible
113 | // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
114 | if (p > 22 && p < 22 + 16) {
115 | // Fast Path Cases In Disguise
116 | d *= internal::Pow10(p - 22);
117 | p = 22;
118 | }
119 |
120 | if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1
121 | *result = FastPath(d, p);
122 | return true;
123 | }
124 | else
125 | return false;
126 | }
127 |
128 | // Compute an approximation and see if it is within 1/2 ULP
129 | inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) {
130 | uint64_t significand = 0;
131 | size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999
132 | for (; i < length; i++) {
133 | if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
134 | (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5'))
135 | break;
136 | significand = significand * 10u + static_cast(decimals[i] - '0');
137 | }
138 |
139 | if (i < length && decimals[i] >= '5') // Rounding
140 | significand++;
141 |
142 | size_t remaining = length - i;
143 | const int kUlpShift = 3;
144 | const int kUlp = 1 << kUlpShift;
145 | int64_t error = (remaining == 0) ? 0 : kUlp / 2;
146 |
147 | DiyFp v(significand, 0);
148 | v = v.Normalize();
149 | error <<= -v.e;
150 |
151 | const int dExp = static_cast(decimalPosition) - static_cast(i) + exp;
152 |
153 | int actualExp;
154 | DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
155 | if (actualExp != dExp) {
156 | static const DiyFp kPow10[] = {
157 | DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1
158 | DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2
159 | DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3
160 | DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4
161 | DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5
162 | DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6
163 | DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7
164 | };
165 | int adjustment = dExp - actualExp - 1;
166 | RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7);
167 | v = v * kPow10[adjustment];
168 | if (length + static_cast(adjustment)> 19u) // has more digits than decimal digits in 64-bit
169 | error += kUlp / 2;
170 | }
171 |
172 | v = v * cachedPower;
173 |
174 | error += kUlp + (error == 0 ? 0 : 1);
175 |
176 | const int oldExp = v.e;
177 | v = v.Normalize();
178 | error <<= oldExp - v.e;
179 |
180 | const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
181 | int precisionSize = 64 - effectiveSignificandSize;
182 | if (precisionSize + kUlpShift >= 64) {
183 | int scaleExp = (precisionSize + kUlpShift) - 63;
184 | v.f >>= scaleExp;
185 | v.e += scaleExp;
186 | error = (error >> scaleExp) + 1 + kUlp;
187 | precisionSize -= scaleExp;
188 | }
189 |
190 | DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
191 | const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
192 | const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
193 | if (precisionBits >= halfWay + static_cast(error)) {
194 | rounded.f++;
195 | if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
196 | rounded.f >>= 1;
197 | rounded.e++;
198 | }
199 | }
200 |
201 | *result = rounded.ToDouble();
202 |
203 | return halfWay - static_cast(error) >= precisionBits || precisionBits >= halfWay + static_cast(error);
204 | }
205 |
206 | inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) {
207 | const BigInteger dInt(decimals, length);
208 | const int dExp = static_cast(decimalPosition) - static_cast(length) + exp;
209 | Double a(approx);
210 | int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
211 | if (cmp < 0)
212 | return a.Value(); // within half ULP
213 | else if (cmp == 0) {
214 | // Round towards even
215 | if (a.Significand() & 1)
216 | return a.NextPositiveDouble();
217 | else
218 | return a.Value();
219 | }
220 | else // adjustment
221 | return a.NextPositiveDouble();
222 | }
223 |
224 | inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) {
225 | RAPIDJSON_ASSERT(d >= 0.0);
226 | RAPIDJSON_ASSERT(length >= 1);
227 |
228 | double result;
229 | if (StrtodFast(d, p, &result))
230 | return result;
231 |
232 | // Trim leading zeros
233 | while (*decimals == '0' && length > 1) {
234 | length--;
235 | decimals++;
236 | decimalPosition--;
237 | }
238 |
239 | // Trim trailing zeros
240 | while (decimals[length - 1] == '0' && length > 1) {
241 | length--;
242 | decimalPosition--;
243 | exp++;
244 | }
245 |
246 | // Trim right-most digits
247 | const int kMaxDecimalDigit = 780;
248 | if (static_cast(length) > kMaxDecimalDigit) {
249 | int delta = (static_cast(length) - kMaxDecimalDigit);
250 | exp += delta;
251 | decimalPosition -= static_cast(delta);
252 | length = kMaxDecimalDigit;
253 | }
254 |
255 | // If too small, underflow to zero
256 | if (int(length) + exp < -324)
257 | return 0.0;
258 |
259 | if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result))
260 | return result;
261 |
262 | // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison
263 | return StrtodBigInteger(result, decimals, length, decimalPosition, exp);
264 | }
265 |
266 | } // namespace internal
267 | RAPIDJSON_NAMESPACE_END
268 |
269 | #endif // RAPIDJSON_STRTOD_
270 |
--------------------------------------------------------------------------------
/lib/rapidjson/internal/swap.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_SWAP_H_
16 | #define RAPIDJSON_INTERNAL_SWAP_H_
17 |
18 | #include "../rapidjson.h"
19 |
20 | #if defined(__clang__)
21 | RAPIDJSON_DIAG_PUSH
22 | RAPIDJSON_DIAG_OFF(c++98-compat)
23 | #endif
24 |
25 | RAPIDJSON_NAMESPACE_BEGIN
26 | namespace internal {
27 |
28 | //! Custom swap() to avoid dependency on C++ header
29 | /*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only.
30 | \note This has the same semantics as std::swap().
31 | */
32 | template
33 | inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT {
34 | T tmp = a;
35 | a = b;
36 | b = tmp;
37 | }
38 |
39 | } // namespace internal
40 | RAPIDJSON_NAMESPACE_END
41 |
42 | #if defined(__clang__)
43 | RAPIDJSON_DIAG_POP
44 | #endif
45 |
46 | #endif // RAPIDJSON_INTERNAL_SWAP_H_
47 |
--------------------------------------------------------------------------------
/lib/rapidjson/istreamwrapper.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_ISTREAMWRAPPER_H_
16 | #define RAPIDJSON_ISTREAMWRAPPER_H_
17 |
18 | #include "stream.h"
19 | #include
20 |
21 | #ifdef __clang__
22 | RAPIDJSON_DIAG_PUSH
23 | RAPIDJSON_DIAG_OFF(padded)
24 | #endif
25 |
26 | #ifdef _MSC_VER
27 | RAPIDJSON_DIAG_PUSH
28 | RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
29 | #endif
30 |
31 | RAPIDJSON_NAMESPACE_BEGIN
32 |
33 | //! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
34 | /*!
35 | The classes can be wrapped including but not limited to:
36 |
37 | - \c std::istringstream
38 | - \c std::stringstream
39 | - \c std::wistringstream
40 | - \c std::wstringstream
41 | - \c std::ifstream
42 | - \c std::fstream
43 | - \c std::wifstream
44 | - \c std::wfstream
45 |
46 | \tparam StreamType Class derived from \c std::basic_istream.
47 | */
48 |
49 | template
50 | class BasicIStreamWrapper {
51 | public:
52 | typedef typename StreamType::char_type Ch;
53 | BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
54 |
55 | Ch Peek() const {
56 | typename StreamType::int_type c = stream_.peek();
57 | return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast(c) : static_cast('\0');
58 | }
59 |
60 | Ch Take() {
61 | typename StreamType::int_type c = stream_.get();
62 | if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
63 | count_++;
64 | return static_cast(c);
65 | }
66 | else
67 | return '\0';
68 | }
69 |
70 | // tellg() may return -1 when failed. So we count by ourself.
71 | size_t Tell() const { return count_; }
72 |
73 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
74 | void Put(Ch) { RAPIDJSON_ASSERT(false); }
75 | void Flush() { RAPIDJSON_ASSERT(false); }
76 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
77 |
78 | // For encoding detection only.
79 | const Ch* Peek4() const {
80 | RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
81 | int i;
82 | bool hasError = false;
83 | for (i = 0; i < 4; ++i) {
84 | typename StreamType::int_type c = stream_.get();
85 | if (c == StreamType::traits_type::eof()) {
86 | hasError = true;
87 | stream_.clear();
88 | break;
89 | }
90 | peekBuffer_[i] = static_cast(c);
91 | }
92 | for (--i; i >= 0; --i)
93 | stream_.putback(peekBuffer_[i]);
94 | return !hasError ? peekBuffer_ : 0;
95 | }
96 |
97 | private:
98 | BasicIStreamWrapper(const BasicIStreamWrapper&);
99 | BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
100 |
101 | StreamType& stream_;
102 | size_t count_; //!< Number of characters read. Note:
103 | mutable Ch peekBuffer_[4];
104 | };
105 |
106 | typedef BasicIStreamWrapper IStreamWrapper;
107 | typedef BasicIStreamWrapper