/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #pragma once #include #include #include #include #include #include #include #include #include #include //! Namespace for file functions. /*! The FileFunc namespace is where we put some higher level file reading and writing operations. */ namespace AzFramework { namespace FileFunc { struct WriteJsonSettings { int m_maxDecimalPlaces = -1; // -1 means use default }; /* * Read a string into a JSON document * * \param[in] jsonText The string of JSON to parse into a JSON document. * \return Outcome ReadJsonFromString(AZStd::string_view jsonText); /* * Read a text file into a JSON document * * \param[in] jsonFilePath The path to the JSON file path to open * \param[in] overrideFileIO Optional file IO instance to use. If null, use the Singleton instead * \param[in] maxFileSize The maximum size, in bytes, of the file to read. Defaults to 1 MB. * \return Outcome(rapidjson::Document) Returns a failure with error message if the content is not valid JSON. */ AZ::Outcome ReadJsonFile(const AZ::IO::Path& jsonFilePath, AZ::IO::FileIOBase* overrideFileIO = nullptr, size_t maxFileSize = AZ::Utils::DefaultMaxFileSize); /** * Write a JSON document to a string * * \param[in] jsonDoc The JSON document to write to a text file * \param[out] jsonFilePath The string that the JSON text will be written to. * \param[in] settings Settings to pass along to the JSON writer. * \return StringOutcome Saves the JSON document to text. Otherwise returns a failure with error message. */ AZ::Outcome WriteJsonToString(const rapidjson::Document& document, AZStd::string& jsonText, WriteJsonSettings settings = WriteJsonSettings{}); /** * Write a JSON document to a text file * * \param[in] jsonDoc The JSON document to write to a text file * \param[in] jsonFilePath The path to the JSON file path to write to * \param[in] settings Settings to pass along to the JSON writer. * \return StringOutcome Saves the JSON document to a file. Otherwise returns a failure with error message. */ AZ::Outcome WriteJsonFile(const rapidjson::Document& jsonDoc, const AZ::IO::Path& jsonFilePath, WriteJsonSettings settings = WriteJsonSettings{}); /** * Find all the files in a path based on an optional filter. Recurse if requested. * * \param[in] folder The folder to find the files in * \param[in] filter Optional file filter (i.e. wildcard *) to apply * \param[in] recurseSubFolders Option to recurse into any subfolders found in the search * \param[out] fileList List of files found from the search * \return true if the files where found, false if an error occurred. */ AZ::Outcome, AZStd::string> FindFilesInPath(const AZStd::string& folder, const AZStd::string& filter, bool recurseSubFolders); /** * Helper function to read a text file line by line and process each line through a function ptr input * \param[in] filePath The path to the config file * \param[in] perLineCallback Function ptr to a function that takes in a const char* as input, and returns false to stop processing or true to continue to the next line * \return true if the file was open and read successfully, false if not */ AZ::Outcome ReadTextFileByLine(const AZStd::string& filePath, AZStd::function perLineCallback); /** * Replaces a value in an ini-style file. * * \param[in] filePath The path to the config file to update * \param[in] updateRules The update rules (list of update strings, see below) to apply * * The replace string rule format follows the following: * ([header]/)[key]=[value] * * where: * header (optional) : Optional group title for the key * key : The key to lookup or create for the value * value : The value to update or create */ AZ::Outcome ReplaceInCfgFile(const AZStd::string& filePath, const AZStd::list& updateRules); /** * Replaces a value in an ini-style file. * * \param[in] filePath The path to the config file to update * \param[in] updateRule The update rule (list of update strings, see below) to apply * * The replace string rule format follows the following: * ([header]/)[key]=[value] * * where: * header (optional) : Optional group title for the key * key : The key to lookup or create for the value * value : The value to update or create */ AZ::Outcome ReplaceInCfgFile(const AZStd::string& filePath, const char* header, const AZStd::string& key, const AZStd::string& newValue); /** * Gets the value(s) for a key in an INI style config file. * * \param[in] filePath The path to the config file * \param[in] key The key of the value to find * * \returns Value(s) on success, error message on failure. */ AZ::Outcome GetValueForKeyInCfgFile(const AZStd::string& filePath, const char* key); /** * Gets the value(s) for a key in an INI style config file with the given contents. * * \param[in] configFileContents The contents of a config file in a string * \param[in] key The key of the value to find * * \returns Value(s) on success, error message on failure. */ AZ::Outcome GetValueForKeyInCfgFileContents(const AZStd::string& configFileContents, const char* key); /** * Gets the contents of an INI style config file as a string. * * \param[in] filePath The path to the config file * * \returns Contents of filePath on success, error message on failure. */ AZ::Outcome GetCfgFileContents(const AZStd::string& filePath); /** * Find a list of files using FileIO GetInstance * * \param[in] pathToStart The folder to start at * \param[in] pattern The wildcard pattern to match against * \param[in] recurse - whether to search directories underneath pathToStart recursively * \returns AZ::Success and a list of files of matches found, an error string on an empty list */ AZ::Outcome, AZStd::string> FindFileList(const AZStd::string& pathToStart, const char* pattern, bool recurse); } // namespace FileFunc } // namespace AzFramework