/*!\file generator.cpp\brief Fast binary encoding generator implementation\author Ivan Shynkarenka\date 20.04.2018\copyright MIT License*/#include "generator.h"#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)#include <windows.h>#endifnamespace FBE {void Generator::Store(const CppCommon::Path& filename){std::string previous;// Try to read the previous file contentif (filename.IsExists()){CppCommon::File file = filename;file.Open(true, false);previous = file.ReadAllText();file.Close();}// Compare the buffer with previous content in order to store only new contentif (_buffer == previous)return;// Store the buffer into unique fileCppCommon::Path unique = filename.parent() / CppCommon::Path::unique();CppCommon::File::WriteAllText(unique, _buffer);#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)// Sometimes it happens that Visual Studio opens generated files on a fly to analyze.// It opens them with mapping into its process with CreateFileMapping/MapViewOfFile.// As the result files cannot be replaced or removed. Therefore we need to have some// retry attempts with small delay to give a change for the file to be created.// https://stackoverflow.com/questions/41844842/when-error-1224-error-user-mapped-file-occursconst int attempts = 1000;const int sleep = 100;for (int attempt = 0; attempt < attempts; ++attempt){try{// Rename the unique file inside a loop with retriesCppCommon::Path::Rename(unique, filename);return;}catch (const CppCommon::FileSystemException& ex){if ((ex.system_error() == ERROR_ACCESS_DENIED) || (ex.system_error() == ERROR_USER_MAPPED_FILE)){CppCommon::Thread::Sleep(sleep);continue;}throw;}}throwex CppCommon::FileSystemException("Cannot generate the output file!").Attach(filename);#else// Rename the unique file inside a loop with retriesCppCommon::Path::Rename(unique, filename);#endif}} // namespace FBE
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。