]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/filesys.cpp
Add download rate to media progress bar (non http mode only!)
[dragonfireclient.git] / src / filesys.cpp
index 21ff199a874c804a03a04c27ef75b2b9cc2b2024..eda36c83307a39e93392e52048933b5185091f13 100644 (file)
@@ -18,11 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "filesys.h"
-#include "strfnd.h"
+#include "util/string.h"
 #include <iostream>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <fstream>
 #include "log.h"
 
 namespace fs
@@ -595,7 +596,7 @@ bool PathStartsWith(std::string path, std::string prefix)
                                        && !IsDirDelimiter(path[pathpos+len])
                                        && prefixpos+len < prefixsize
                                        && !IsDirDelimiter(
-                                               prefix[prefixsize+len]));
+                                               prefix[prefixpos+len]));
                        pathpos += len;
                        prefixpos += len;
                }
@@ -684,5 +685,28 @@ std::string RemoveRelativePathComponents(std::string path)
        return path.substr(0, pos);
 }
 
+bool safeWriteToFile(const std::string &path, const std::string &content)
+{
+       std::string tmp_file = path + ".~mt";
+
+       // Write to a tmp file
+       std::ofstream os(tmp_file.c_str(), std::ios::binary);
+       if (!os.good())
+               return false;
+       os << content;
+       os.flush();
+       os.close();
+       if (os.fail())
+               return false;
+
+       // Copy file
+#ifdef _WIN32
+       remove(path.c_str());
+       return (rename(tmp_file.c_str(), path.c_str()) == 0);
+#else
+       return (rename(tmp_file.c_str(), path.c_str()) == 0);
+#endif
+}
+
 } // namespace fs