BOOL is_empty_dir(const char* dir) { HANDLE hFind; WIN32_FIND_DATA finddata; char szCurrDir[_MAX_PATH]; BOOL fOk, fEmpty = TRUE;; if (!is_directory(dir)) { // 不是目录,返回TRUE return TRUE; } GetCurrentDirectory(_MAX_PATH, szCurrDir); SetCurrentDirectory(appendbackslash(dir)); hFind = FindFirstFile("*.*", &finddata); fOk = (hFind != INVALID_HANDLE_VALUE); while (fOk) { if ((strcmp(finddata.cFileName, ".") != 0) && (strcmp(finddata.cFileName, "..") != 0)) { fEmpty = FALSE; break; } fOk = FindNextFile(hFind, &finddata); } if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); } SetCurrentDirectory(szCurrDir); return fEmpty; }
void OnLSBt(BOOL checkrunning) { ULARGE_INTEGER ullFreeBytesAvailable, ullTotalNumberOfBytes; char text[_MAX_PATH]; HRESULT hr = S_OK; HWND hctl = GetDlgItem(gdmgr._hdlg_ddesc, IDC_TV_DDESC_EXPLORER); // 1. 删除Treeview中所有项 TreeView_DeleteAllItems(hctl); // 2. 地址栏编辑框 Edit_SetText(GetDlgItem(gdmgr._hdlg_ddesc, IDC_ET_DDESC_WWWROOT), appendbackslash(game_config::path.c_str())); // 3. 向TreeView添加一级内容 gdmgr._htvroot = TreeView_AddLeaf(hctl, TVI_ROOT); strcpy(text, basename(game_config::path.c_str())); // 这里一定要设TVIF_CHILDREN, 否则接下折叠后将判断出其cChildren为0, 再不能展开 TreeView_SetItem1(hctl, gdmgr._htvroot, TVIF_TEXT | TVIF_PARAM | TVIF_CHILDREN, (LPARAM)gdmgr._htvroot, 0, 0, is_empty_dir(game_config::path.c_str())? 0: 1, text); dir_2_tv(hctl, gdmgr._htvroot, game_config::path.c_str(), 0); // 4. 底下状态 strncpy(text, game_config::path.c_str(), 2); text[2] = '\\'; text[3] = 0; GetDiskFreeSpaceEx(text, &ullFreeBytesAvailable, &ullTotalNumberOfBytes, NULL); strcpy(text, format_space_u64n(ullTotalNumberOfBytes.QuadPart)); Edit_SetText(GetDlgItem(gdmgr._hdlg_ddesc, IDC_ET_DDESC_SUBAREA), formatstr("%s (Avail Space %s)", text, format_space_u64n(ullFreeBytesAvailable.QuadPart))); return; }
static void cb_walk_dir_empty(char *name, uint32_t flags, uint64_t len, int64_t lastWriteTime, walkdirempty_t *wdepy) { char path[_MAX_PATH]; if (wdepy->fn) { wdepy->fn(name, flags, len, lastWriteTime, wdepy->ctx); } sprintf(path, "%s\\%s", wdepy->path, name); if (flags & FILE_ATTRIBUTE_DIRECTORY) { RemoveDirectory(appendbackslash(path)); DWORD err = GetLastError(); } else { delfile(path); } return; }
BOOL copy_root_files(char* src, char* dst) { char szCurrDir[_MAX_PATH], text1[_MAX_PATH], text2[_MAX_PATH]; HANDLE hFind; WIN32_FIND_DATA finddata; BOOL fok, fret = TRUE; if (!src || !src[0] || !dst || !dst[0]) { return FALSE; } GetCurrentDirectory(_MAX_PATH, szCurrDir); SetCurrentDirectory(appendbackslash(src)); hFind = FindFirstFile("*.*", &finddata); fok = (hFind != INVALID_HANDLE_VALUE); while (fok) { if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 目录 } else { // 文件 sprintf(text1, "%s\\%s", src, finddata.cFileName); sprintf(text2, "%s\\%s", dst, finddata.cFileName); fret = CopyFile(text1, text2, FALSE); if (!fret) { posix_print_mb("copy file from %s to %s fail", text1, text2); break; } } fok = FindNextFile(hFind, &finddata); } if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); } SetCurrentDirectory(szCurrDir); return fret; }
// @rootdir: 欲遍历的根目录 // @subfolders: 是否要遍历子目录. 0: 不遍历 // @deepen: 遍历, 调用fn时给的参数是, 1: 浅到深, 0: 由深到浅 // 注: // 1.靠这种FindFirstFile方法删除目录是不行的.当检测到是目时,调用RemoveDirectory将返回总是失败(错误码:32,指示另有进行在使用) // FindClose后才可以,而这时close时不可能的事.要删目录改调用SHFileOperation. BOOL walk_dir_win32(const char* rootdir, int subfolders, int deepen, fn_walk_dir fn, uint32_t *ctx, int del) { char szCurrDir[_MAX_PATH], text[_MAX_PATH]; HANDLE hFind; WIN32_FIND_DATA finddata; BOOL fOk, fRet = TRUE; int64_t timestamp = 0; GetCurrentDirectory(_MAX_PATH, szCurrDir); SetCurrentDirectory(appendbackslash(rootdir)); hFind = FindFirstFile("*.*", &finddata); fOk = (hFind != INVALID_HANDLE_VALUE); while (fOk) { timestamp = FileTimeToUnixTime(finddata.ftLastWriteTime); if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 目录 if ((strcmp(finddata.cFileName, ".") != 0) && (strcmp(finddata.cFileName, "..") != 0)) { if (deepen && fn) { // 由浅到深: 是先调用fn,再去在该目上递归 if (!fn(finddata.cFileName, FILE_ATTRIBUTE_DIRECTORY, posix_mku64(finddata.nFileSizeLow, finddata.nFileSizeHigh), timestamp, ctx)) { fRet = FALSE; break; } } sprintf(text, "%s\\%s\\", rootdir, finddata.cFileName); if (subfolders) { SetCurrentDirectory(text); walk_subdir_win32(rootdir, finddata.cFileName, deepen, fn, ctx); } if (!deepen && fn) { // 由深到浅: 是先在该目上递归,再去调动fn if (!fn(finddata.cFileName, FILE_ATTRIBUTE_DIRECTORY, posix_mku64(finddata.nFileSizeLow, finddata.nFileSizeHigh), timestamp, ctx)) { fRet = FALSE; break; } } if (del) { RemoveDirectory(text); DWORD err = GetLastError(); } } } else { // 文件 if (fn) { if (!fn(finddata.cFileName, 0, posix_mku64(finddata.nFileSizeLow, finddata.nFileSizeHigh), timestamp, ctx)) { fRet = FALSE; break; } } if (del) { sprintf(text, "%s\\%s", rootdir, finddata.cFileName); delfile(text); } } fOk = FindNextFile(hFind, &finddata); } if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); } SetCurrentDirectory(szCurrDir); return fRet; }
void On_DlgDDescCommand(HWND hdlgP, int id, HWND hwndCtrl, UINT codeNotify) { char *ptr = NULL; int retval; char text[_MAX_PATH]; BOOL fok; std::string str; std::stringstream strstr; switch (id) { case IDC_BT_DDESC_BROWSE: ptr = GetBrowseFilePath(hdlgP); if (!ptr) { break; } strcpy(text, appendbackslash(ptr)); if (text[strlen(text) - 1] == '\\') { text[strlen(text) - 1] = 0; } if (!check_wok_root_folder(text)) { posix_print_mb("%s,不是有效的kingdom-src-x.x.x源数据目录,请重新选择", appendbackslash(text)); break; } if (strcasecmp(ptr, game_config::path.c_str())) { game_config::path = ptr; gdmgr.heros_.set_path(game_config::path.c_str()); Edit_SetText(GetDlgItem(hdlgP, IDC_ET_DDESC_WWWROOT), appendbackslash(ptr)); OnLSBt(TRUE); if (gdmgr._da != da_sync) { title_select(da_sync); } else { sync_enter_ui(); } update_locale_dir(); } break; case IDM_NEW_EXTRAINSDIST: sprintf(text, "Do you want to extract install-material to C:\\kingdom-ins?"); retval = MessageBox(hdlgP, text, "Confirm Generate", MB_YESNO | MB_DEFBUTTON2); if (retval == IDYES) { fok = extra_kingdom_ins_disk(gdmgr._menu_text, "c:\\kingdom-ins", "c:\\kingdom-ins-android\\com.freeors.kingdom"); posix_print_mb("Extract install-material from %s to C:\\kingdom-ins, %s!!", gdmgr._menu_text, fok? "Success": "Fail"); } break; case IDM_NEW_CAMPAIGN: if (campaign_new()) { // title_select(da_sync); } break; case IDM_EXPLORER_WML: if (!_stricmp(gdmgr._menu_text, "hero.dat")) { if (gdmgr._da != da_wgen) { title_select(da_wgen); } else { wgen_enter_ui(); } } else if (!_stricmp(gdmgr._menu_text, "tb.dat")) { editor_config::type = BIN_BUILDINGRULE; if (gdmgr._da != da_visual) { title_select(da_visual); } else { visual_enter_ui(); } } else if (!_stricmp(extname(gdmgr._menu_text), "bin")) { if (wml_checksum_from_file(std::string(gdmgr._menu_text))) { editor_config::type = BIN_WML; if (gdmgr._da != da_visual) { title_select(da_visual); } else { visual_enter_ui(); } } } break; case IDM_DELETE_ITEM0: // 册除指定的文件/目录 strstr.str(""); str = gdmgr._menu_text; strstr << str.substr(0, str.rfind("\\xwml")); strstr << "\\data\\campaigns\\" << offextname(basename(gdmgr._menu_text)); strcpy(text, strstr.str().c_str()); strstr.str(""); strstr << "您想删除文件:" << gdmgr._menu_text << ",以及目录:"; strstr << text << "?"; // Confirm Multiple File Delete retval = MessageBox(hdlgP, strstr.str().c_str(), "确认删除", MB_YESNO | MB_DEFBUTTON2); if (retval == IDYES) { if (delfile1(gdmgr._menu_text)) { TreeView_DeleteItem(GetDlgItem(hdlgP, IDC_TV_DDESC_EXPLORER), (HTREEITEM)(UINT_PTR)gdmgr._menu_lparam); TreeView_SetChilerenByPath(GetDlgItem(hdlgP, IDC_TV_DDESC_EXPLORER), (HTREEITEM)(UINT_PTR)gdmgr._menu_lparam, dirname(gdmgr._menu_text)); } else { posix_print_mb("Failed delete %s !", gdmgr._menu_text); } if (!delfile1(text)) { posix_print_mb("Failed delete %s !", text); } sync_refresh_sync(); } break; case IDM_DELETE_ITEM1: strstr.str(""); strstr << "将产生不同步,您想删除文件:" << gdmgr._menu_text << "?"; // Confirm Multiple File Delete retval = MessageBox(hdlgP, strstr.str().c_str(), "确认删除", MB_YESNO | MB_DEFBUTTON2); if (retval == IDYES) { if (delfile1(gdmgr._menu_text)) { TreeView_DeleteItem(GetDlgItem(hdlgP, IDC_TV_DDESC_EXPLORER), (HTREEITEM)(UINT_PTR)gdmgr._menu_lparam); TreeView_SetChilerenByPath(GetDlgItem(hdlgP, IDC_TV_DDESC_EXPLORER), (HTREEITEM)(UINT_PTR)gdmgr._menu_lparam, dirname(gdmgr._menu_text)); } else { posix_print_mb("Failed delete %s !", gdmgr._menu_text); } // inform user, there is non sync. if (gdmgr._da != da_sync) { title_select(da_sync); } else { // forbid refresh sync_enter_ui(); } } break; default: break; } return; }