Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "3rdparty/json11/json11.hpp"
- using namespace json11;
- using namespace std;
- /**
- * Parse a JSON file stored in _file_to_saveload.
- * @returns The file parsed as a json11:Json object.
- */
- Json ParseJsonFromLoadedFile()
- {
- string err; ///< Holds the error message generated by the json11 parser.
- /* Load the GeoJSON file as a string initially. We'll parse it soon.. */
- size_t filesize;
- char *text{};
- FILE *handle = FioFOpenFile(_file_to_saveload.name, "rb", GEODATA_DIR, &filesize);
- if (handle == nullptr) return false;
- text = ReallocT(text, filesize);
- size_t read = fread(text, 1, filesize, handle);
- fclose(handle);
- if (read != filesize) return false;
- /* Add a trailing \0 to mark the end of the string */
- text = ReallocT(text, filesize + 1);
- text[filesize] = '\0';
- /* The parser doesn't like tabs or various newline characters, so we'll replace them with a space. */
- for (char *p = text; *p != '\0'; p++) {
- if (*p == '\t' || *p == '\r' || *p == '\n' || *p == '\r\n') *p = ' ';
- }
- /* Finally time to parse the string. */
- // TODO: Show errors from invalid JSON load to the player.
- return Json::parse(text, err);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement