void bomAdd(const FileUnit& file) { bf::ifstream strm; const auto result=bomUtf8Check(file, strm); if(strm.is_open()==false) return; if(result.second==false) { stdErr() << "utf8-invalid: " << file.total << std::endl; return; } if(result.first==true) { stdErr() << "utf8-BOM: " << file.total << std::endl; return; } strm.seekg(0); auto newFile=file.total; newFile += pathCreate(".new"); bf::ofstream out(newFile); out.write(reinterpret_cast<const char*>(utf8::bom), sizeof(utf8::bom)); std::copy(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out)); bf::rename(newFile, file.total); }
void bomRemove(const FileUnit& file) { bf::ifstream strm; const auto result=bomUtf8Check(file, strm); if(strm.is_open()==false) return; if(result.second==false) { stdErr() << "utf8-invalid: " << file.total << std::endl; return; } if(result.first==false) { stdErr() << "utf8-no-BOM: " << file.total << std::endl; return; } strm.seekg(3); auto newFile=file.total; newFile += pathCreate(".new"); bf::ofstream out(newFile); std::copy(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out)); bf::rename(newFile, file.total); }
xkern_return_t dgramtest_init( XObj self ) { static XObj firstServer; PState *ps; dgramtestprotocol = self; ps = pathAlloc(self->path, sizeof(PState)); if (!ps) { xError("dgramtest: couldn't allocate PState\n"); return XK_FAILURE; } self->state = ps; if ((ps->evdgram = evAlloc(self->path)) == 0) { pathFree(ps); return XK_FAILURE; } ps->mark = '.'; ps->loop = LOOP_DEFAULT; ps->delay = 0; ps->dgramtest_priority = DGRAMTEST_PRIORITY; ps->groupsize = ALL_TOGETHER; bcopy((char *)&defaultServer, (char *)&ps->server, sizeof(IPhost)); if ( findXObjRomOpts(self, dgramOpts, sizeof(dgramOpts)/sizeof(XObjRomOpt), ps) == XK_FAILURE ) { xError("dgramtest: romfile config errors, not running"); return XK_FAILURE; } { /* * Overload standard path with custom path */ Path p; XkThreadPolicy_s policy; int pri = ps->dgramtest_priority; xkern_return_t xkr; p = pathCreate(31, "test path"); policy.func = xkPolicyFixedFifo; policy.arg = &pri; policy.argLen = sizeof(int); xkr = pathEstablishPool(p, 10, 1, &policy, 0); if ( xkr != XK_SUCCESS ) { xError("pathEstablishPool fails"); } self->path = p; } #if !DIPC_XKERN dgramtesttrigger(); #endif /* DIPC_XKERN */ self->control = dgramtestControlProtl; return XK_SUCCESS; }
YipDirectory::YipDirectory(const std::string & prjPath, const Project * project) : m_Path(pathConcat(prjPath, ".yip")), m_Project(project) { pathCreate(m_Path); m_Path = pathMakeCanonical(m_Path); m_DB = std::make_shared<SQLiteDatabase>(pathConcat(m_Path, "db")); initDB(); }
int main(void) { Test test("FileSystem::Path"); #if defined(DUNE_OS_POSIX) Path root("/"); Path tmp("/tmp"); #elif defined(DUNE_OS_WINDOWS) Path root("c:/"); Path tmp("c:/"); #endif { Path a = root / "tmp"; test.boolean("Path::basename()", a.basename() == "tmp"); test.boolean("Path::dirname()", a.dirname() == root); } { Path a = root / "this is not a real path"; test.boolean("Path::root()", a.root() == root); test.boolean("Path::exists()", !a.exists()); } { Path a = root / "dir0" / ".." / "dir1"; test.boolean("Relative Path #0", a == (root / "dir1")); } { Path a = root / "dir0" / ".." / "dir1" / ".."; test.boolean("Relative Path #1", a == root); } { Path a = root / "dir0" / "dir1" / ".."; test.boolean("Relative Path #2", a == (root / "dir0")); } { Path a = root / "dir0" / "." / "dir1"; test.boolean("Relative Path #3", a == (root / "dir0" / "dir1")); } { Path a = tmp / "normal_level0"; test.boolean("Path::create(MODE_NORMAL)", pathCreate(a, Path::MODE_NORMAL)); test.boolean("Path::exists()", a.exists()); test.boolean("Path::remove(MODE_NORMAL)", pathRemove(a, Path::MODE_NORMAL)); test.boolean("!Path::exists()", !a.exists()); } { Path a = tmp / "recursive_level0" / "level1" / "level2" / "level3"; Path b = tmp / "recursive_level0"; test.boolean("Path::create(MODE_RECURSIVE)", pathCreate(a, Path::MODE_RECURSIVE)); test.boolean("Path::exists()", a.exists()); test.boolean("Path::remove(MODE_RECURSIVE)", pathRemove(b, Path::MODE_RECURSIVE)); test.boolean("!Path::exists()", !b.exists()); } { Path a(root); test.boolean("Path::storageAvailable()", Path::storageAvailable(a) > 0); test.boolean("Path::storageCapacity()", Path::storageCapacity(a) > 0); } { Path a = root / "dir0" / "dir1"; Path b = root / "dir0" / "dir1" / "dir2"; test.boolean("Path::suffix()", a.suffix(b) == "dir2"); } return 0; }
Path(const wchar_t* s) :bf::path(pathCreate(s).generic_string<bf::path::string_type>()) {}
Path(const std::wstring& s) :bf::path(pathCreate(s).generic_string<bf::path::string_type>()) {}
std::string YipDirectory::writeFile(const std::string & path, const std::string & data, bool * changed) { std::string file = pathSimplify(pathConcat(m_Path, path)); bool has_sha1 = false, write = true; std::string new_sha1; SQLiteTransaction transaction(m_DB); // Check whether file has changed if (pathIsExistent(file)) { // Canonicalize file path file = pathMakeCanonical(file); // Get information about file from the database bool found = false; size_t old_size = 0; time_t old_time = 0; std::string old_sha1; m_DB->select("SELECT size, time, sha1 FROM files WHERE path = ? LIMIT 1", { file }, [&found, &old_size, &old_time, &old_sha1](const SQLiteCursor & cursor) { found = true; old_size = cursor.toSizeT(0); old_time = cursor.toTimeT(1); old_sha1 = cursor.toString(2); } ); // Check whether file has been modified if (found && data.size() == old_size) { if (pathGetModificationTime(file) <= old_time) { new_sha1 = sha1(data); has_sha1 = true; if (new_sha1 == old_sha1) write = false; } } } // Do not overwrite file if it did not change if (!write) { std::cout << "keeping " << path << std::endl; if (changed) *changed = false; assert(has_sha1); m_DB->exec(fmt() << "REPLACE INTO files (path, size, time, sha1) VALUES (?, " << data.size() << ", " << time(nullptr) << ", ?)", { file, new_sha1 }); transaction.commit(); return file; } std::cout << "writing " << path << std::endl; if (changed) *changed = true; // Calculate SHA1 sum of the file if (!has_sha1) new_sha1 = sha1(data); // Create directory for the file std::string dir = pathGetDirectory(file); if (dir.length() > 0) pathCreate(dir); // Write the file ::writeFile(file, data); // Store information about file into the database m_DB->exec(fmt() << "REPLACE INTO files (path, size, time, sha1) VALUES (?, " << data.size() << ", " << time(nullptr) << ", ?)", { file, new_sha1 }); transaction.commit(); return file; }
ALERROR CGameSettings::Load (const CString &sFilespec, CString *retsError) // Load // // Load game settings from a file. If the file does not exist, then we // set settings to default values { ALERROR error; int i; // Initialize from defaults for (i = 0; i < OPTIONS_COUNT; i++) SetValue(i, CString(g_OptionData[i].pszDefaultValue, -1, true), true); // Look for a file in the current directory and see if it is writable. If // not, then look in AppData. We remember the place where we found a valid // file as our AppData root (and we base other directories off that). if (pathIsWritable(sFilespec)) { // AppData is current directory m_sAppData = NULL_STR; } else { m_sAppData = pathAddComponent(pathGetSpecialFolder(folderAppData), TRANSCENDENCE_APP_DATA); if (!pathCreate(m_sAppData) || !pathIsWritable(m_sAppData)) { *retsError = strPatternSubst(CONSTLIT("Unable to write to AppData folder: %s"), m_sAppData); return ERR_FAIL; } } // Settings file CString sSettingsFilespec = pathAddComponent(m_sAppData, sFilespec); // Load XML CFileReadBlock DataFile(sSettingsFilespec); CXMLElement *pData; CString sError; if (error = CXMLElement::ParseXML(&DataFile, &pData, retsError)) { // ERR_NOTFOUND means that we couldn't find the Settings.xml // file. In that case, initialize from defaults if (error == ERR_NOTFOUND) { LoadFromRegistry(); m_bModified = true; return NOERROR; } // Otherwise, it means that we got an error parsing the file. // Return the error, but leave the settings initialized to defaults // (We should be OK to continue, even with an error). else { m_bModified = false; return error; } } // Initialize to unmodified (as we load settings we might change this) m_bModified = false; // Loop over all elements for (i = 0; i < pData->GetContentElementCount(); i++) { CXMLElement *pItem = pData->GetContentElement(i); if (strEquals(pItem->GetTag(), OPTION_TAG)) { int iOption = FindOptionData(pItem->GetAttribute(NAME_ATTRIB)); if (iOption == -1) { kernelDebugLogMessage("Unknown option: %s", pItem->GetAttribute(NAME_ATTRIB)); continue; } SetValue(iOption, pItem->GetAttribute(VALUE_ATTRIB), true); } else if (strEquals(pItem->GetTag(), KEY_MAP_TAG)) { if (error = m_KeyMap.ReadFromXML(pItem)) return error; } else if (strEquals(pItem->GetTag(), EXTENSION_FOLDER_TAG)) { CString sFolder; if (pItem->FindAttribute(PATH_ATTRIB, &sFolder)) m_ExtensionFolders.Insert(sFolder); } else if (strEquals(pItem->GetTag(), EXTENSIONS_TAG)) { if (error = m_Extensions.ReadFromXML(pItem)) return error; } else if (m_pExtra) { bool bModified; if (error = m_pExtra->OnLoadSettings(pItem, &bModified)) return error; if (bModified) m_bModified = true; } } // Done delete pData; return NOERROR; }