// IN: String path core::Error createFile(const core::json::JsonRpcRequest& request, json::JsonRpcResponse* pResponse) { std::string path; Error error = json::readParam(request.params, 0, &path); if (error) return error ; // calculate file path FilePath filePath = module_context::resolveAliasedPath(path) ; if (filePath.exists()) { return fileExistsError(ERROR_LOCATION); } else { // create the file boost::shared_ptr<std::ostream> pOfs; error = filePath.open_w(&pOfs); if (error) return error; } return Success() ; }
Error writeStringToFile(const FilePath& filePath, const std::string& str, string_utils::LineEnding lineEnding) { using namespace boost::system::errc ; // open file boost::shared_ptr<std::ostream> pOfs; Error error = filePath.open_w(&pOfs); if (error) return error; try { // set exception mask (required for proper reporting of errors) pOfs->exceptions(std::ostream::failbit | std::ostream::badbit); // copy string to file std::string normalized = str; string_utils::convertLineEndings(&normalized, lineEnding); std::istringstream istr(normalized); boost::iostreams::copy(istr, *pOfs); // return success return Success(); } catch(const std::exception& e) { Error error = systemError(boost::system::errc::io_error, ERROR_LOCATION); error.addProperty("what", e.what()); error.addProperty("path", filePath.absolutePath()); return error; } }
Error ConnectionHistory::writeConnections(const json::Array& connectionsJson) { FilePath connectionListFile = connectionsDir_.childPath(kConnectionListFile); boost::shared_ptr<std::ostream> pStream; Error error = connectionListFile.open_w(&pStream); if (error) return error; json::writeFormatted(connectionsJson, *pStream); return Success(); }