returns the number of files in path p, that matches wildcard it fills in buffer (up to maxcount) with the files found
| 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 |
no test coverage detected