Commit 8385b896 authored by Chen Bill's avatar Chen Bill

myfilesystem: use std functions

parent d536b9a9
#ifndef FILESYSTEM_H #ifndef FILESYSTEM_H
#define FILESYSTEM_H #define FILESYSTEM_H
#include <string.h> #include <cstdio>
#include <functional> #include <functional>
#include "bufferio.h" #include "bufferio.h"
...@@ -14,14 +14,13 @@ ...@@ -14,14 +14,13 @@
#ifdef _WIN32 #ifdef _WIN32
#include <wchar.h>
#define NOMINMAX #define NOMINMAX
#include <Windows.h> #include <Windows.h>
class FileSystem { class FileSystem {
public: public:
static void SafeFileName(wchar_t* wfile) { static void SafeFileName(wchar_t* wfile) {
while((wfile = wcspbrk(wfile, L"<>:\"/\\|?*")) != NULL) while((wfile = std::wcspbrk(wfile, L"<>:\"/\\|?*")) != nullptr)
*wfile++ = '_'; *wfile++ = '_';
} }
...@@ -71,8 +70,8 @@ public: ...@@ -71,8 +70,8 @@ public:
static bool DeleteDir(const wchar_t* wdir) { static bool DeleteDir(const wchar_t* wdir) {
wchar_t pdir[256]; wchar_t pdir[256];
BufferIO::CopyWideString(wdir, pdir); int len = BufferIO::CopyWStr(wdir, pdir, sizeof pdir / sizeof pdir[0]);
pdir[wcslen(wdir) + 1] = 0; pdir[len + 1] = 0;
SHFILEOPSTRUCTW lpFileOp; SHFILEOPSTRUCTW lpFileOp;
lpFileOp.hwnd = NULL; lpFileOp.hwnd = NULL;
lpFileOp.wFunc = FO_DELETE; lpFileOp.wFunc = FO_DELETE;
...@@ -90,7 +89,7 @@ public: ...@@ -90,7 +89,7 @@ public:
static void TraversalDir(const wchar_t* wpath, const std::function<void(const wchar_t*, bool)>& cb) { static void TraversalDir(const wchar_t* wpath, const std::function<void(const wchar_t*, bool)>& cb) {
wchar_t findstr[1024]; wchar_t findstr[1024];
swprintf(findstr, 1024, L"%s/*", wpath); std::swprintf(findstr, sizeof findstr / sizeof findstr[0], L"%s/*", wpath);
WIN32_FIND_DATAW fdataw; WIN32_FIND_DATAW fdataw;
HANDLE fh = FindFirstFileW(findstr, &fdataw); HANDLE fh = FindFirstFileW(findstr, &fdataw);
if(fh == INVALID_HANDLE_VALUE) if(fh == INVALID_HANDLE_VALUE)
...@@ -118,7 +117,7 @@ public: ...@@ -118,7 +117,7 @@ public:
class FileSystem { class FileSystem {
public: public:
static void SafeFileName(wchar_t* wfile) { static void SafeFileName(wchar_t* wfile) {
while((wfile = wcspbrk(wfile, L"/")) != NULL) while((wfile = std::wcspbrk(wfile, L"/")) != nullptr)
*wfile++ = '_'; *wfile++ = '_';
} }
...@@ -208,9 +207,7 @@ public: ...@@ -208,9 +207,7 @@ public:
while((dirp = readdir(dir)) != nullptr) { while((dirp = readdir(dir)) != nullptr) {
file_unit funit; file_unit funit;
char fname[1024]; char fname[1024];
strcpy(fname, path); std::snprintf(fname, sizeof fname, "%s/%s", path, dirp->d_name);
strcat(fname, "/");
strcat(fname, dirp->d_name);
stat(fname, &fileStat); stat(fname, &fileStat);
funit.filename = std::string(dirp->d_name); funit.filename = std::string(dirp->d_name);
funit.is_dir = S_ISDIR(fileStat.st_mode); funit.is_dir = S_ISDIR(fileStat.st_mode);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment