Opens a file for reading or writing If a path is specified, will try to open the file only in that path. If no path is specified, will look through search directories and library files. Parameters: filename - the name if the file, with or without a path mode - the standard C mode string Returns: the CFile handle, or NULL if file not opened
| 481 | // mode - the standard C mode string |
| 482 | // Returns: the CFile handle, or NULL if file not opened |
| 483 | CFILE *cfopen(const std::filesystem::path &filename, const char *mode) { |
| 484 | CFILE *cfile; |
| 485 | |
| 486 | // Check for valid mode |
| 487 | ASSERT((mode[0] == 'r') || (mode[0] == 'w')); |
| 488 | ASSERT((mode[1] == 'b') || (mode[1] == 't')); |
| 489 | // get the parts of the pathname |
| 490 | std::filesystem::path path = filename.parent_path(); |
| 491 | std::filesystem::path fname = filename.stem(); |
| 492 | std::filesystem::path ext = filename.extension(); |
| 493 | |
| 494 | // if there is a path specified, use it instead of the libraries, search dirs, etc. |
| 495 | // if the file is writable, just open it, instead of looking in libs, etc. |
| 496 | if (!path.empty() || (mode[0] == 'w')) { |
| 497 | // use path specified with file |
| 498 | cfile = open_file_in_directory(filename, mode, std::filesystem::path()); |
| 499 | goto got_file; // don't look in libs, etc. |
| 500 | } |
| 501 | |
| 502 | // First look in the directories for this file's extension |
| 503 | for (auto const &entry : extensions) { |
| 504 | if (!strnicmp(entry.first.u8string().c_str(), ext.u8string().c_str(), _MAX_EXT)) { |
| 505 | // found ext |
| 506 | cfile = open_file_in_directory(filename, mode, entry.second); |
| 507 | if (cfile) { |
| 508 | goto got_file; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Next look in the general directories |
| 514 | for (auto const &entry : paths) { |
| 515 | if (!entry.second) { |
| 516 | cfile = open_file_in_directory(filename, mode, entry.first); |
| 517 | if (cfile) |
| 518 | goto got_file; |
| 519 | } |
| 520 | } |
| 521 | // Lastly, try the hog files |
| 522 | cfile = open_file_in_lib(filename.u8string().c_str()); |
| 523 | got_file:; |
| 524 | if (cfile) { |
| 525 | if (mode[0] == 'w') |
| 526 | cfile->flags |= CFF_WRITING; |
| 527 | if (mode[1] == 't') |
| 528 | cfile->flags |= CFF_TEXT; |
| 529 | } |
| 530 | return cfile; |
| 531 | } |
| 532 | |
| 533 | // Returns the length of the specified file |
| 534 | // Parameters: cfp - the file pointer returned by cfopen() |