From a4f24a24d3ed131d75b407a96a44b7d7c5d68b65 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Sun, 31 Jul 2016 15:06:06 -0400 Subject: This changes device_image_interface::filetype() to return 'const std::string &' instead of 'const char *'. In the interests of removing burdens from client code, I also changed the following: - filetype() will always return the file extension in lower case - device_image_interface::filetype() will return the correct extension for files loaded as a part of softlists - The code for extracting a file extension is now encapsulated in core_filename_extract_extension() Client code has been updated --- src/lib/util/corefile.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src/lib/util/corefile.cpp') diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 5fcd9e00239..22b67046150 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -1266,11 +1266,11 @@ core_file::core_file() FILENAME UTILITIES ***************************************************************************/ -/*------------------------------------------------- - core_filename_extract_base - extract the base - name from a filename; note that this makes - assumptions about path separators --------------------------------------------------*/ +// ------------------------------------------------- +// core_filename_extract_base - extract the base +// name from a filename; note that this makes +// assumptions about path separators +// ------------------------------------------------- std::string core_filename_extract_base(const std::string &name, bool strip_extension) { @@ -1290,10 +1290,26 @@ std::string core_filename_extract_base(const std::string &name, bool strip_exten } -/*------------------------------------------------- - core_filename_ends_with - does the given - filename end with the specified extension? --------------------------------------------------*/ +// ------------------------------------------------- +// core_filename_extract_extension +// ------------------------------------------------- + +std::string core_filename_extract_extension(const std::string &filename, bool strip_period, bool normalize_to_lowercase) +{ + auto loc = filename.find_last_of('.'); + std::string result = loc != std::string::npos + ? filename.substr(loc + (strip_period ? 1 : 0)) + : ""; + if (normalize_to_lowercase) + std::transform(result.begin(), result.end(), result.begin(), ::tolower); + return result; +} + + +// ------------------------------------------------- +// core_filename_ends_with - does the given +// filename end with the specified extension? +// ------------------------------------------------- bool core_filename_ends_with(const std::string &filename, const std::string &extension) { -- cgit v1.2.3-70-g09d2