From c2f7ae98d3c321a615cda6946443f5098d9d08bd Mon Sep 17 00:00:00 2001 From: Dan Sagunov Date: Sat, 2 Nov 2013 00:01:15 +0400 Subject: Adding renameFile function for NaCl --- src/utils/files.cpp | 29 +++++++++++++++++++++++++++++ src/utils/files.h | 2 ++ 2 files changed, 31 insertions(+) (limited to 'src/utils') diff --git a/src/utils/files.cpp b/src/utils/files.cpp index c06b3d892..fa69cd6af 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -100,4 +100,33 @@ void Files::extractZip(const std::string &zipName, const std::string &inDir, remove(zipName.c_str()); } +int Files::renameFile(const std::string &pFrom, const std::string &pTo) +{ + FILE *file = fopen(pFrom.c_str(), "rb"); + if (file == NULL) + return -1; + + fseek(file, 0, SEEK_END); + size_t sz = ftell(file); + fseek(file, 0, SEEK_SET); + + char *buf = (char *)malloc(sz + 1); + if (fread(buf, 1, sz, file) != sz) + return -1; + fclose(file); + buf[sz] = 0; + + file = fopen(pTo.c_str(), "w+b"); + if (file == NULL) + return -1; + + if (fwrite(buf, 1, sz, file) != sz) + return -1; + fclose(file); + + free(buf); + + return 0; +} + #endif // ANDROID __native_client__ diff --git a/src/utils/files.h b/src/utils/files.h index 519b7f520..4a44631cf 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -35,6 +35,8 @@ namespace Files void extractZip(const std::string &zipName, const std::string &inDir, const std::string &outDir); + + int renameFile(const std::string &pFrom, const std::string &pTo); } // namespace Files #endif // ANDROID __native_client__ -- cgit v1.2.3-70-g09d2