| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/newui_filedlg.cpp $ |
| 21 | * $Revision: 22 $ |
| 22 | * $Date: 10/25/99 10:46a $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * File Dialog using NewUI class |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/newui_filedlg.cpp $ |
| 28 | * |
| 29 | * 22 10/25/99 10:46a Matt |
| 30 | * Code from Duane that allows users to double-click on a directory in the |
| 31 | * file dialog to open that folder. |
| 32 | * |
| 33 | * 21 9/07/99 4:33p Jeff |
| 34 | * fixed crash due to not setting a pointer to NULL |
| 35 | * |
| 36 | * 20 5/10/99 10:23p Ardussi |
| 37 | * changes to compile on Mac |
| 38 | * |
| 39 | * 19 5/10/99 9:21p Samir |
| 40 | * Ardussi pointed out this small but significant error when checking for |
| 41 | * valid path. |
| 42 | * |
| 43 | * 18 4/20/99 3:11p Jeff |
| 44 | * handle invalid initial directory |
| 45 | * |
| 46 | * 17 4/20/99 3:07p Jeff |
| 47 | * save last directory |
| 48 | * |
| 49 | * 16 4/20/99 11:46a Samir |
| 50 | * numerous ui 'feel' fixes. |
| 51 | * |
| 52 | * 15 4/14/99 3:56a Jeff |
| 53 | * fixed case mismatch in #includes |
| 54 | * |
| 55 | * 14 2/28/99 6:14p Jeff |
| 56 | * use UID_OK and UID_CANCEL |
| 57 | * |
| 58 | * 13 2/28/99 2:57a Jeff |
| 59 | * got rid of useless hotspots |
| 60 | * |
| 61 | * 12 2/28/99 2:23a Jeff |
| 62 | * use new ui |
| 63 | * |
| 64 | * 11 1/29/99 5:22p Jeff |
| 65 | * localization |
| 66 | * |
| 67 | * 10 1/27/99 5:47p Jeff |
| 68 | * put up a message that files are being gathered...ease the fear of crash |
| 69 | * |
| 70 | * 9 1/21/99 11:15p Jeff |
| 71 | * pulled out some structs and defines from header files and moved them |
| 72 | * into seperate header files so that multiplayer dlls don't require major |
| 73 | * game headers, just those new headers. Side effect is a shorter build |
| 74 | * time. Also cleaned up some header file #includes that weren't needed. |
| 75 | * This affected polymodel.h, object.h, player.h, vecmat.h, room.h, |
| 76 | * manage.h and multi.h |
| 77 | * |
| 78 | * 8 10/08/98 4:23p Kevin |
| 79 | * Changed code to comply with memory library usage. Always use mem_malloc |
| 80 | * , mem_free and mem_strdup |
| 81 | * |
| 82 | * 7 9/28/98 4:35p Jeff |
| 83 | * general UI changes and improvements |
| 84 | * |
| 85 | * 6 9/23/98 6:19p Jeff |
| 86 | * finished up (hopefully) updating the config/ui dialogs to meet our |
| 87 | * standard. Keyboard/joystick config still needs some work |
| 88 | * |
| 89 | * 5 9/02/98 2:54p Jeff |
| 90 | * added defines for text colors to be used throughout the game...fixed up |
| 91 | * buddy menu too |
| 92 | * |
| 93 | * 4 8/19/98 2:18p Jeff |
| 94 | * changed ddio_CleanPath |
| 95 | * |
| 96 | * 3 7/30/98 12:31p Jeff |
| 97 | * |
| 98 | * 2 7/29/98 5:39p Jeff |
| 99 | * initial creation |
| 100 | * |
| 101 | * $NoKeywords: $ |
| 102 | */ |
| 103 | |
| 104 | #include <cstdlib> |
| 105 | #include <cstring> |
| 106 | #include <filesystem> |
| 107 | |
| 108 | #include "mono.h" |
| 109 | #include "renderer.h" |
| 110 | #include "render.h" |
| 111 | #include "ddio.h" |
| 112 | #include "descent.h" |
| 113 | #include "game.h" |
| 114 | #include "cfile.h" |
| 115 | #include "application.h" |
| 116 | #include "newui.h" |
| 117 | #include "grtext.h" |
| 118 | #include "gamefont.h" |
| 119 | #include "stringtable.h" |
| 120 | #include "ConfigItem.h" //for colors |
| 121 | #include "mem.h" |
| 122 | |
| 123 | newuiListBox *path_list = NULL; |
| 124 | char *path_edit = NULL; |
| 125 | char *working_filename = NULL; |
| 126 | char *working_listpath = NULL; |
| 127 | newuiSheet *fdlg_working_sheet = NULL; |
| 128 | |
| 129 | char NewuiFileDlg_lastpath[_MAX_PATH] = {'\0'}; |
| 130 | |
| 131 | // border size of the UI window |
| 132 | #define UI_BORDERSIZE 20 |
| 133 | |
| 134 | // returns the number of files in path p, that matches wildcard |
| 135 | // it fills in buffer (up to maxcount) with the files found |
| 136 | int GetFilesInPath(char **buffer, int maxcount, const char *p, const char *wildcard); |
| 137 | |
| 138 | // returns the number of directories in path p |
| 139 | // it fills in buffer (up to maxcount) with the directories found |
| 140 | int GetDirectoriesInPath(char **buffer, int maxcount, const char *p); |
| 141 | |
| 142 | // Updates the given listbox with the directories and files that match wildcards (each wildcard seperates by a ;) |
| 143 | void UpdateFileList(newuiListBox *lb, const char *path, char *wildcards); |
| 144 | |
| 145 | // Callback function for listbox |
| 146 | void FileSelectCallback(int index); |
| 147 | |
| 148 | void FDlg_EnableWaitMessage(bool enable) { |
| 149 | #define FDWM_HEIGHT 128 |
| 150 | #define FDWM_WIDTH 256 |
| 151 | |
| 152 | static NewUIGameWindow *msgbox = NULL; |
| 153 | static bool opened = false; |
| 154 | newuiSheet *sheet; |
| 155 | |
| 156 | if (opened == enable) { |
| 157 | Int3(); |
| 158 | } |
| 159 | |
| 160 | opened = enable; |
| 161 | |
| 162 | if (enable) { |
| 163 | msgbox = new NewUIGameWindow; |
| 164 | if (msgbox) { |
| 165 | |
| 166 | msgbox->Create(0, 0, FDWM_WIDTH, FDWM_HEIGHT, UIF_PROCESS_ALL | UIF_CENTER); |
| 167 | |
| 168 | sheet = msgbox->GetSheet(); |
| 169 | sheet->NewGroup(NULL, 45, 25); |
| 170 | sheet->AddText(TXT_GETTINGFILES); |
| 171 | |
| 172 | msgbox->Open(); |
| 173 | sheet->Realize(); |
| 174 | |
| 175 | // fake a DoUI |
| 176 | DoUIFrame(); |
| 177 | rend_Flip(); |
| 178 | } |
| 179 | } else { |
| 180 | |
| 181 | if (msgbox) { |
| 182 | msgbox->Close(); |
| 183 | msgbox->Destroy(); |
| 184 | delete msgbox; |
| 185 | } |
| 186 | msgbox = NULL; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | bool IsDirectoryItem(char *item) { |
| 191 | if (*item != ' ') |
| 192 | return false; |
| 193 | if (*(item + 1) != '[') |
| 194 | return false; |
| 195 | if (item[strlen(item) - 1] == ']') |
| 196 | return true; |
| 197 | return false; |
| 198 | } |
| 199 | // Callback function for listbox |
| 200 | void FileSelectCallback(int index) { |
| 201 | if ((!path_edit) || (!working_filename) || (!path_list) || (!working_listpath)) |
| 202 | return; |
| 203 | |
| 204 | char file[_MAX_PATH]; |
| 205 | path_list->GetCurrentItem(file, _MAX_PATH); |
| 206 | if (IsDirectoryItem(file)) |
| 207 | return; // we don't care about directories |
| 208 | |
| 209 | strncpy(working_filename, file, _MAX_FNAME - 1); |
| 210 | working_filename[_MAX_FNAME - 1] = '\0'; |
| 211 | |
| 212 | ddio_MakePath(file, working_listpath, working_filename, NULL); |
| 213 | |
| 214 | strncpy(path_edit, file, _MAX_PATH - 1); |
| 215 | path_edit[_MAX_PATH - 1] = '\0'; |
| 216 | mprintf(0, "New Path: %s\n", path_edit); |
| 217 | fdlg_working_sheet->UpdateChanges(); |
| 218 | } |
| 219 | |
| 220 | // Displays a file/directory dialog box |
| 221 | // path - Initial path, will contain the chosen file/path on exit with true, must be at least _MAX_PATH |
| 222 | // in size title - Title of the dialog wildc - semicolon (;) seperated list of wildcards to |
| 223 | // be shown flags - see header PFDF_ for flags |
| 224 | bool DoPathFileDialog(bool save_dialog, char *path, const char *title, const char *wildc, int flags) { |
| 225 | #define HS_CANCEL 0 |
| 226 | #define HS_OK 1 |
| 227 | #define HS_UPDIR 0 |
| 228 | #define ED_WILDCARD 0 |
| 229 | #define ED_FILENAME 1 |
| 230 | #define ID_LISTBOX 25 |
| 231 | #define ID_UPDIR 26 |
| 232 | #define ID_WILDCARD 27 |
| 233 | #define ID_FILENAME 28 |
| 234 | #define ID_CANCEL UID_CANCEL |
| 235 | #define ID_OK UID_OK |
| 236 | newuiTiledWindow window; |
| 237 | newuiSheet *sheet; |
| 238 | newuiListBox *listbox; |
| 239 | char *edits[2]; |
| 240 | |
| 241 | char wildcards[256]; |
| 242 | char working_path[_MAX_PATH]; |
| 243 | char working_file[_MAX_FNAME]; |
| 244 | |
| 245 | if (!path) |
| 246 | return false; |
| 247 | if (!title) |
| 248 | return false; |
| 249 | if (!wildc) |
| 250 | return false; |
| 251 | |
| 252 | strcpy(wildcards, wildc); |
| 253 | strcpy(working_file, ""); |
| 254 | |
| 255 | // figure out the correct path |
| 256 | char oldpath[_MAX_PATH]; |
| 257 | ddio_GetWorkingDir(oldpath, _MAX_PATH); |
| 258 | if (!ddio_SetWorkingDir(path)) { |
| 259 | // try to use last good path |
| 260 | if (!ddio_SetWorkingDir(NewuiFileDlg_lastpath)) { |
| 261 | strcpy(path, LocalD3Dir); |
| 262 | } else { |
| 263 | strcpy(path, NewuiFileDlg_lastpath); |
| 264 | } |
| 265 | } |
| 266 | ddio_SetWorkingDir(oldpath); |
| 267 | |
| 268 | // Create all the UI Items |
| 269 | window.Create(title, 0, 0, 384, 288); |
| 270 | fdlg_working_sheet = sheet = window.GetSheet(); |
| 271 | |
| 272 | sheet->NewGroup(NULL, 10, 0); |
| 273 | listbox = sheet->AddListBox(268, 100, ID_LISTBOX); |
| 274 | |
| 275 | sheet->NewGroup(NULL, 0, 145); |
| 276 | edits[ED_FILENAME] = sheet->AddEditBox(NULL, _MAX_PATH, 300, ID_FILENAME); |
| 277 | edits[ED_WILDCARD] = sheet->AddEditBox(NULL, 256, 150, ID_WILDCARD); |
| 278 | sheet->AddLongButton(TXT_UPTOPARENTDIR, ID_UPDIR); |
| 279 | |
| 280 | sheet->NewGroup(NULL, 200, 160); |
| 281 | sheet->AddButton((save_dialog) ? TXT_SAVE : TXT_OPEN, ID_OK); |
| 282 | sheet->AddButton(TXT_CANCEL, ID_CANCEL); |
| 283 | |
| 284 | working_filename = working_file; |
| 285 | working_listpath = working_path; |
| 286 | path_edit = edits[ED_FILENAME]; |
| 287 | path_list = listbox; |
| 288 | |
| 289 | listbox->SetSelectChangeCallback(FileSelectCallback); |
| 290 | |
| 291 | ddio_CleanPath(working_path, path); |
| 292 | UpdateFileList(listbox, working_path, wildcards); |
| 293 | |
| 294 | strncpy(edits[ED_FILENAME], working_path, _MAX_PATH - 1); |
| 295 | edits[ED_FILENAME][_MAX_PATH - 1] = '\0'; |
| 296 | |
| 297 | strncpy(edits[ED_WILDCARD], wildcards, 255); |
| 298 | edits[ED_WILDCARD][255] = '\0'; |
| 299 | |
| 300 | bool ret = false; |
| 301 | bool exit_menu = false; |
| 302 | |
| 303 | sheet->SetInitialFocusedGadget(ID_LISTBOX); |
| 304 | |
| 305 | window.Open(); |
| 306 | while (!exit_menu) { |
| 307 | int res = window.DoUI(); |
| 308 | |
| 309 | // handle all UI results. |
| 310 | switch (res) { |
| 311 | case ID_OK: { |
| 312 | strcpy(path, edits[ED_FILENAME]); |
| 313 | |
| 314 | if (path[0] == '\0') { |
| 315 | DoMessageBox(TXT_ERROR, TXT_ERRCHOOSEFILE, MSGBOX_OK); |
| 316 | } else { |
| 317 | if (std::filesystem::is_directory(path)) { |
| 318 | char newpath[_MAX_PATH]; |
| 319 | char file[_MAX_PATH]; |
| 320 | int selection = listbox->GetCurrentIndex(); |
| 321 | if ((selection >= 0) && (listbox->GetCurrentItem(file, _MAX_PATH))) { |
| 322 | if (*file) { |
| 323 | strcpy(working_file, ""); |
| 324 | |
| 325 | // ok the user double clicked on a directory |
| 326 | // convert file without the brackets |
| 327 | file[strlen(file) - 1] = '\0'; |
| 328 | if (strlen(working_path) != 0) |
| 329 | ddio_MakePath(newpath, working_path, file + 2, NULL); |
| 330 | else |
| 331 | strcpy(newpath, file + 2); |
| 332 | |
| 333 | UpdateFileList(listbox, newpath, wildcards); |
| 334 | ddio_CleanPath(working_path, newpath); |
| 335 | |
| 336 | strncpy(edits[ED_FILENAME], working_path, _MAX_PATH - 1); |
| 337 | edits[ED_FILENAME][_MAX_PATH - 1] = '\0'; |
| 338 | } |
| 339 | } |
| 340 | // DoMessageBox(TXT_ERROR,TXT_ERRCHOOSEFILE,MSGBOX_OK); |
| 341 | } else { |
| 342 | if (flags & PFDF_FILEMUSTEXIST) { |
| 343 | if (!cfexist(path)) { |
| 344 | DoMessageBox(TXT_ERROR, TXT_ERRFILENOTEXIST, MSGBOX_OK); |
| 345 | } else { |
| 346 | ddio_SplitPath(path, NewuiFileDlg_lastpath, NULL, NULL); |
| 347 | ret = exit_menu = true; |
| 348 | } |
| 349 | } else { |
| 350 | ddio_SplitPath(path, NewuiFileDlg_lastpath, NULL, NULL); |
| 351 | ret = exit_menu = true; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } break; |
| 356 | case ID_CANCEL: |
| 357 | ret = false; |
| 358 | exit_menu = true; |
| 359 | break; |
| 360 | case ID_LISTBOX: { |
| 361 | char newpath[_MAX_PATH]; |
| 362 | char file[_MAX_PATH]; |
| 363 | // check to see if the double clicked on a dir |
| 364 | int selection = listbox->GetCurrentIndex(); |
| 365 | |
| 366 | if ((selection >= 0) && (listbox->GetCurrentItem(file, _MAX_PATH))) { |
| 367 | if (*file) { |
| 368 | if (IsDirectoryItem(file)) { |
| 369 | strcpy(working_file, ""); |
| 370 | |
| 371 | // ok the user double clicked on a directory |
| 372 | // convert file without the brackets |
| 373 | file[strlen(file) - 1] = '\0'; |
| 374 | if (strlen(working_path) != 0) |
| 375 | ddio_MakePath(newpath, working_path, file + 2, NULL); |
| 376 | else |
| 377 | strcpy(newpath, file + 2); |
| 378 | |
| 379 | UpdateFileList(listbox, newpath, wildcards); |
| 380 | ddio_CleanPath(working_path, newpath); |
| 381 | |
| 382 | strncpy(edits[ED_FILENAME], working_path, _MAX_PATH - 1); |
| 383 | edits[ED_FILENAME][_MAX_PATH - 1] = '\0'; |
| 384 | } else { |
| 385 | // double click on a file |
| 386 | strcpy(working_file, file); |
| 387 | FileSelectCallback(selection); |
| 388 | strcpy(file, edits[ED_FILENAME]); |
| 389 | |
| 390 | // the user wants a file |
| 391 | if (flags & PFDF_FILEMUSTEXIST) { |
| 392 | if (cfexist(file)) { |
| 393 | exit_menu = true; |
| 394 | ret = true; |
| 395 | strcpy(path, file); |
| 396 | ddio_SplitPath(path, NewuiFileDlg_lastpath, NULL, NULL); |
| 397 | } else { |
| 398 | // invalid file |
| 399 | DoMessageBox(TXT_ERROR, TXT_ERRCHOOSEFILE, MSGBOX_OK); |
| 400 | } |
| 401 | } else { |
| 402 | strcpy(path, file); |
| 403 | exit_menu = true; |
| 404 | ret = true; |
| 405 | ddio_SplitPath(path, NewuiFileDlg_lastpath, NULL, NULL); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } break; |
| 411 | case ID_UPDIR: { |
| 412 | char newpath[_MAX_PATH]; |
| 413 | strcpy(working_file, ""); |
| 414 | ddio_GetParentPath(newpath, working_path); |
| 415 | strcpy(working_path, newpath); |
| 416 | |
| 417 | UpdateFileList(listbox, working_path, wildcards); |
| 418 | strncpy(edits[ED_FILENAME], working_path, _MAX_PATH - 1); |
| 419 | edits[ED_FILENAME][_MAX_PATH - 1] = '\0'; |
| 420 | } break; |
| 421 | case ID_WILDCARD: { |
| 422 | strcpy(wildcards, edits[ED_WILDCARD]); |
| 423 | UpdateFileList(listbox, working_path, wildcards); |
| 424 | } break; |
| 425 | case ID_FILENAME: { |
| 426 | char c[_MAX_PATH]; |
| 427 | strcpy(c, edits[ED_FILENAME]); |
| 428 | |
| 429 | if (strlen(c) > 0) { |
| 430 | if (!std::filesystem::is_directory(c)) { |
| 431 | // the user wants a file |
| 432 | if (flags & PFDF_FILEMUSTEXIST) { |
| 433 | if (cfexist(c)) { |
| 434 | strcpy(path, c); |
| 435 | exit_menu = true; |
| 436 | ret = true; |
| 437 | ddio_SplitPath(path, NewuiFileDlg_lastpath, NULL, NULL); |
| 438 | } else { |
| 439 | // invalid file |
| 440 | DoMessageBox(TXT_ERROR, TXT_ERRFILENOTEXIST, MSGBOX_OK); |
| 441 | } |
| 442 | } else { |
| 443 | strcpy(path, c); |
| 444 | ddio_SplitPath(path, NewuiFileDlg_lastpath, NULL, NULL); |
| 445 | exit_menu = true; |
| 446 | ret = true; |
| 447 | } |
| 448 | } else { |
| 449 | // just change directories |
| 450 | strcpy(working_path, c); |
| 451 | UpdateFileList(listbox, working_path, wildcards); |
| 452 | } |
| 453 | } |
| 454 | } break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if (ret) { |
| 459 | mprintf(0, "Selected Filename: %s\n", path); |
| 460 | } else { |
| 461 | mprintf(0, "Cancel!\n"); |
| 462 | } |
| 463 | |
| 464 | window.Close(); |
| 465 | window.Destroy(); |
| 466 | |
| 467 | working_filename = NULL; |
| 468 | working_listpath = NULL; |
| 469 | path_edit = NULL; |
| 470 | path_list = NULL; |
| 471 | |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | // returns the number of files in path p, that matches wildcard |
| 476 | // it fills in buffer (up to maxcount) with the files found |
| 477 | int GetFilesInPath(char **buffer, int maxcount, const char *p, const char *wildcard) { |
| 478 | ASSERT(buffer); |
| 479 | ASSERT(p); |
| 480 | ASSERT(wildcard); |
| 481 | if (maxcount <= 0) |
| 482 | return 0; |
| 483 | |
| 484 | char old_path[_MAX_PATH], path[_MAX_PATH]; |
| 485 | ddio_GetWorkingDir(old_path, _MAX_PATH); |
| 486 | if (!ddio_SetWorkingDir(p)) { |
| 487 | // directory doesn't exist |
| 488 | ddio_SetWorkingDir(old_path); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | ddio_GetWorkingDir(path, _MAX_PATH); |
| 493 | if (stricmp(path, p)) { |
| 494 | // the working dir is not the same as the passed in path, so try to create a correct path and set it |
| 495 | ddio_MakePath(path, p, "", NULL); |
| 496 | if (!ddio_SetWorkingDir(path)) { |
| 497 | ddio_SetWorkingDir(old_path); |
| 498 | return -1; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | int count = 0; |
| 503 | char tempbuffer[_MAX_PATH]; |
| 504 | |
| 505 | if (ddio_FindFileStart(wildcard, tempbuffer)) { |
| 506 | if (!std::filesystem::is_directory(tempbuffer)) { |
| 507 | buffer[count] = mem_strdup(tempbuffer); |
| 508 | count++; |
| 509 | } |
| 510 | |
| 511 | bool done = false; |
| 512 | |
| 513 | while (!done) { |
| 514 | if (ddio_FindNextFile(tempbuffer)) { |
| 515 | if (!std::filesystem::is_directory(tempbuffer)) { |
| 516 | if (count < maxcount) |
| 517 | buffer[count] = mem_strdup(tempbuffer); |
| 518 | count++; |
| 519 | } |
| 520 | } else |
| 521 | done = true; |
| 522 | } |
| 523 | } |
| 524 | ddio_FindFileClose(); |
| 525 | ddio_SetWorkingDir(old_path); |
| 526 | return count; |
| 527 | } |
| 528 | |
| 529 | // returns the number of directories in path p |
| 530 | // it fills in buffer (up to maxcount) with the directories found |
| 531 | int GetDirectoriesInPath(char **buffer, int maxcount, const char *p) { |
| 532 | ASSERT(buffer); |
| 533 | ASSERT(p); |
| 534 | if (maxcount <= 0) |
| 535 | return 0; |
| 536 | |
| 537 | int i; |
| 538 | for (i = 0; i < maxcount; i++) { |
| 539 | buffer[i] = NULL; |
| 540 | } |
| 541 | |
| 542 | char old_path[_MAX_PATH], path[_MAX_PATH]; |
| 543 | |
| 544 | ddio_GetWorkingDir(old_path, _MAX_PATH); |
| 545 | if (!ddio_SetWorkingDir(p)) { |
| 546 | // directory doesn't exist |
| 547 | ddio_SetWorkingDir(old_path); |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | ddio_GetWorkingDir(path, _MAX_PATH); |
| 552 | if (stricmp(path, p)) { |
| 553 | // the working dir is not the same as the passed in path, so try to create a correct path and set it |
| 554 | ddio_MakePath(path, p, "", NULL); |
| 555 | if (!ddio_SetWorkingDir(path)) { |
| 556 | ddio_SetWorkingDir(old_path); |
| 557 | return -1; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | int count = 0; |
| 562 | char tempbuffer[_MAX_PATH]; |
| 563 | |
| 564 | if (ddio_FindFileStart("*", tempbuffer)) { |
| 565 | if (std::filesystem::is_directory(tempbuffer)) { |
| 566 | buffer[count] = mem_strdup(tempbuffer); |
| 567 | count++; |
| 568 | } |
| 569 | |
| 570 | bool done = false; |
| 571 | |
| 572 | while (!done) { |
| 573 | if (ddio_FindNextFile(tempbuffer)) { |
| 574 | if (std::filesystem::is_directory(tempbuffer)) { |
| 575 | if (count < maxcount) |
| 576 | buffer[count] = mem_strdup(tempbuffer); |
| 577 | count++; |
| 578 | } |
| 579 | } else |
| 580 | done = true; |
| 581 | } |
| 582 | } |
| 583 | ddio_FindFileClose(); |
| 584 | ddio_SetWorkingDir(old_path); |
| 585 | return count; |
| 586 | } |
| 587 | |
| 588 | // Updates the given listbox with the directories and files that match wildcards (each wildcard seperates by a ;) |
| 589 | void UpdateFileList(newuiListBox *lb, const char *path, char *wildcards) { |
| 590 | FDlg_EnableWaitMessage(true); |
| 591 | lb->RemoveAll(); |
| 592 | |
| 593 | // if path is NULL or 0 length string then just list the directories |
| 594 | if ((!path) || (*path == '\0')) { |
| 595 | char *roots[30]; |
| 596 | int rootcount = ddio_GetFileSysRoots((char **)&roots, 30); |
| 597 | if (!rootcount) { |
| 598 | FDlg_EnableWaitMessage(false); |
| 599 | return; |
| 600 | } |
| 601 | char tempbuffer[5]; |
| 602 | for (int i = 0; i < rootcount; i++) { |
| 603 | if (roots[i]) { |
| 604 | snprintf(tempbuffer, sizeof(tempbuffer), " [%s]", roots[i]); |
| 605 | lb->AddItem(tempbuffer); |
| 606 | mem_free(roots[i]); |
| 607 | } |
| 608 | } |
| 609 | FDlg_EnableWaitMessage(false); |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | char **dirs = NULL; |
| 614 | char *tempdir[1]; |
| 615 | char fullpath[_MAX_PATH]; |
| 616 | ddio_GetRootFromPath(path, fullpath); |
| 617 | if (!stricmp(path, fullpath)) { |
| 618 | // we are are the root of the drive, make a dummy path |
| 619 | ddio_MakePath(fullpath, path, "", NULL); |
| 620 | } else { |
| 621 | // just use this |
| 622 | strcpy(fullpath, path); |
| 623 | } |
| 624 | |
| 625 | int dircount = GetDirectoriesInPath((char **)&tempdir, 1, fullpath); |
| 626 | if (dircount == -1) { |
| 627 | // invalid directory/path |
| 628 | DoMessageBox(TXT_ERROR, TXT_ERRPATHNOTVALID, MSGBOX_OK); |
| 629 | FDlg_EnableWaitMessage(false); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | if (tempdir[0]) |
| 634 | mem_free(tempdir[0]); |
| 635 | if (dircount > 0) { |
| 636 | dirs = (char **)mem_malloc(sizeof(char *) * dircount); |
| 637 | if (!dirs) { |
| 638 | // out of memory! |
| 639 | dircount = 0; |
| 640 | } else { |
| 641 | GetDirectoriesInPath(dirs, dircount, fullpath); |
| 642 | } |
| 643 | } else { |
| 644 | dircount = 0; |
| 645 | dirs = NULL; |
| 646 | } |
| 647 | |
| 648 | // figure out how many wildcards we have, and allocate accordingly |
| 649 | int wildcard_count = 0; |
| 650 | char *wc = NULL; |
| 651 | char **wc_strings = NULL; |
| 652 | int *wc_count = NULL; |
| 653 | char ***filelist = NULL; |
| 654 | int total_files = 0; |
| 655 | int wildcard_len = 0; |
| 656 | char *strptr = NULL; |
| 657 | |
| 658 | // save the wildcards |
| 659 | wc = (char *)mem_malloc(strlen(wildcards) + 2); |
| 660 | if (!wc) { |
| 661 | FDlg_EnableWaitMessage(false); |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | // make sure there is a tailing ; |
| 666 | strcpy(wc, wildcards); |
| 667 | wildcard_len = strlen(wildcards); |
| 668 | if (wc[wildcard_len - 1] != ';') { |
| 669 | wc[wildcard_len] = ';'; |
| 670 | wc[wildcard_len + 1] = '\0'; |
| 671 | } |
| 672 | |
| 673 | // break up wildcard string, counting the number of wildcards as we go |
| 674 | strptr = wc; |
| 675 | while (*strptr != '\0') { |
| 676 | if (*strptr == ';') { |
| 677 | *strptr = '\0'; |
| 678 | wildcard_count++; |
| 679 | } |
| 680 | strptr++; |
| 681 | } |
| 682 | |
| 683 | if (wildcard_count > 0) { |
| 684 | wc_strings = (char **)mem_malloc(sizeof(char *) * wildcard_count); |
| 685 | wc_count = (int *)mem_malloc(sizeof(int) * wildcard_count); |
| 686 | |
| 687 | if ((wc_strings) && (wc_count)) { |
| 688 | int e; |
| 689 | |
| 690 | // get each individual string of the wildcard strings |
| 691 | strptr = wc; |
| 692 | for (e = 0; e < wildcard_count; e++) { |
| 693 | wc_strings[e] = mem_strdup(strptr); |
| 694 | strptr += strlen(strptr) + 1; |
| 695 | } |
| 696 | |
| 697 | // malloc memory...an array of (array of char *) for each wildcard |
| 698 | filelist = (char ***)mem_malloc(sizeof(char **) * wildcard_count); |
| 699 | if (!filelist) { |
| 700 | // there was an error allocating the memory |
| 701 | if (wc) { |
| 702 | mem_free(wc); |
| 703 | wc = NULL; |
| 704 | } |
| 705 | for (e = 0; e < wildcard_count; e++) { |
| 706 | if (wc_strings[e]) |
| 707 | mem_free(wc_strings[e]); |
| 708 | wc_count[e] = 0; |
| 709 | filelist[e] = NULL; |
| 710 | } |
| 711 | |
| 712 | mem_free(wc_strings); |
| 713 | wc_strings = NULL; |
| 714 | goto wildcard_err; |
| 715 | } |
| 716 | |
| 717 | char *templist[1]; |
| 718 | int count; |
| 719 | |
| 720 | total_files = 0; |
| 721 | |
| 722 | for (e = 0; e < wildcard_count; e++) { |
| 723 | // we just need to get how many of each wildcard there is in the directory |
| 724 | templist[0] = NULL; |
| 725 | count = GetFilesInPath((char **)&templist, 1, path, wc_strings[e]); |
| 726 | if (count == -1) { |
| 727 | // invalid path |
| 728 | wc_count[e] = 0; |
| 729 | filelist[e] = NULL; |
| 730 | } else { |
| 731 | total_files += count; |
| 732 | if (templist[0]) { |
| 733 | mem_free(templist[0]); |
| 734 | templist[0] = NULL; |
| 735 | } |
| 736 | |
| 737 | if (count > 0) { |
| 738 | // now we need to get all the files for the wildcard |
| 739 | filelist[e] = (char **)mem_malloc(sizeof(char *) * count); |
| 740 | if (filelist[e]) { |
| 741 | GetFilesInPath(filelist[e], count, path, wc_strings[e]); |
| 742 | wc_count[e] = count; |
| 743 | } else { |
| 744 | wc_count[e] = 0; |
| 745 | } |
| 746 | } else { |
| 747 | filelist[e] = NULL; |
| 748 | wc_count[e] = 0; |
| 749 | } |
| 750 | } // end if(count==-1) |
| 751 | |
| 752 | // we don't need the wildcard anymore, we can free it up |
| 753 | if (wc_strings[e]) |
| 754 | mem_free(wc_strings[e]); |
| 755 | } // end for(e=0;e<wildcard_count;e++) |
| 756 | |
| 757 | // free up allocated memory that we don't need anymore |
| 758 | mem_free(wc_strings); |
| 759 | if (wc) |
| 760 | mem_free(wc); |
| 761 | |
| 762 | wc_strings = NULL; |
| 763 | wc = NULL; |
| 764 | } else { |
| 765 | // we couldn't allocate memory for wildcard strings |
| 766 | total_files = 0; |
| 767 | filelist = NULL; |
| 768 | if (wc_count) { |
| 769 | for (int e = 0; e < wildcard_count; e++) |
| 770 | wc_count[e] = 0; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | wildcard_err: |
| 776 | if ((dircount + total_files) == 0) { |
| 777 | FDlg_EnableWaitMessage(false); |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | // lbfilelist = new UITextItem[dircount+total_files]; |
| 782 | char tempbuffer[_MAX_PATH + 1]; |
| 783 | int i; |
| 784 | |
| 785 | for (i = 0; i < dircount; i++) { |
| 786 | if (dirs[i]) { |
| 787 | snprintf(tempbuffer, sizeof(tempbuffer), " [%s]", dirs[i]); |
| 788 | lb->AddItem(tempbuffer); |
| 789 | mem_free(dirs[i]); |
| 790 | } |
| 791 | } |
| 792 | i = dircount; |
| 793 | if (total_files > 0) { |
| 794 | for (int j = 0; j < wildcard_count; j++) { |
| 795 | int num_files; |
| 796 | char **wc_filelist; |
| 797 | num_files = wc_count[j]; |
| 798 | if (num_files > 0) { |
| 799 | wc_filelist = filelist[j]; |
| 800 | for (int k = 0; k < num_files; k++) { |
| 801 | if (wc_filelist[k]) { |
| 802 | lb->AddItem(wc_filelist[k]); |
| 803 | i++; |
| 804 | mem_free(wc_filelist[k]); |
| 805 | wc_filelist[k] = NULL; |
| 806 | } |
| 807 | } |
| 808 | if (wc_filelist) { |
| 809 | mem_free(wc_filelist); |
| 810 | wc_filelist = NULL; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | if (dirs) |
| 817 | mem_free(dirs); |
| 818 | if (filelist) |
| 819 | mem_free(filelist); |
| 820 | if (wc_count) |
| 821 | mem_free(wc_count); |
| 822 | FDlg_EnableWaitMessage(false); |
| 823 | } |