diff options
author | Nicholas Feinberg <pleasingfung@gmail.com> | 2014-05-25 22:07:04 -0700 |
---|---|---|
committer | Nicholas Feinberg <pleasingfung@gmail.com> | 2014-05-25 22:07:58 -0700 |
commit | 3798112249fd3d3cba55ec8c0c0f48c73bbea6f3 (patch) | |
tree | c0c4546b5cf05270939293894d7deb2476658c86 /crawl-ref/source/files.cc | |
parent | 33abf97284f7ffd35d99379bc68d4260be3217dd (diff) | |
download | crawl-ref-3798112249fd3d3cba55ec8c0c0f48c73bbea6f3.tar.gz crawl-ref-3798112249fd3d3cba55ec8c0c0f48c73bbea6f3.zip |
Cleanup lk_open()
Remove some redundant checks, add support for 'r+b',
generally clean things up.
Diffstat (limited to 'crawl-ref/source/files.cc')
-rw-r--r-- | crawl-ref/source/files.cc | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index 5b8be96976..e60839eb81 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -2301,17 +2301,23 @@ bool unlock_file_handle(FILE *handle) return unlock_file(fileno(handle)); } +/** + * Attempts to open & lock a file. + * + * @param mode The file access mode. ('r', 'ab+', etc) + * @param file The path to the file to be opened. + * @return A handle for the specified file, if successful; else NULL. + */ FILE *lk_open(const char *mode, const string &file) { + ASSERT(mode); + FILE *handle = fopen_u(file.c_str(), mode); if (!handle) return NULL; - bool locktype = false; - if (mode && mode[0] != 'r') - locktype = true; - - if (handle && !lock_file_handle(handle, locktype)) + const bool write_lock = mode[0] != 'r' || strchr(mode, '+'); + if (!lock_file_handle(handle, write_lock)) { mprf(MSGCH_ERROR, "ERROR: Could not lock file %s", file.c_str()); fclose(handle); |