Skip to content

Commit

Permalink
Remove uses of the deprecated Filesystem:file_extension, and extend t…
Browse files Browse the repository at this point in the history
…he preferred extension() to handle the missing functionality.
  • Loading branch information
lgritz committed Apr 28, 2012
1 parent 84a775d commit 8bdbb16
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/iconvert/iconvert.cpp
Expand Up @@ -365,8 +365,8 @@ convert_file (const std::string &in_filename, const std::string &out_filename)

std::string tempname = out_filename;
if (tempname == in_filename) {
tempname = out_filename + ".tmp."
+ Filesystem::file_extension (out_filename);
tempname = out_filename + ".tmp"
+ Filesystem::extension (out_filename);
}

// Find an ImageIO plugin that can open the input file, and open it.
Expand Down
16 changes: 9 additions & 7 deletions src/include/filesystem.h
Expand Up @@ -64,13 +64,15 @@ namespace Filesystem {
/// file extension, if any) of a filepath.
DLLPUBLIC std::string filename (const std::string &filepath);

/// Return the file extension (including the last '.') of a filename or
/// filepath.
DLLPUBLIC std::string extension (const std::string &filepath);

/// Return the file extension (just the part after the last '.') of a
/// filename or filepath. DEPRECATED.
DLLPUBLIC std::string file_extension (const std::string &filepath);
/// Return the file extension (including the last '.' if
/// include_dot=true) of a filename or filepath.
DLLPUBLIC std::string extension (const std::string &filepath,
bool include_dot=true);

/// DEPRECATED.
inline std::string file_extension (const std::string &filepath) {
return extension (filepath, false);
}

/// Replace the file extension of a filename or filepath. Does not
/// alter filepath, just returns a new string
Expand Down
6 changes: 3 additions & 3 deletions src/jpeg2000.imageio/jpeg2000output.cpp
Expand Up @@ -238,11 +238,11 @@ Jpeg2000Output::init_components(opj_image_cmptparm_t *components, int precision)
opj_cinfo_t*
Jpeg2000Output::create_compressor()
{
std::string ext = Filesystem::file_extension(m_filename);
std::string ext = Filesystem::extension(m_filename);
opj_cinfo_t *compressor = NULL;
if (ext == "j2k")
if (ext == ".j2k")
compressor = opj_create_compress(CODEC_J2K);
else if (ext == "jp2")
else if (ext == ".jp2")
compressor = opj_create_compress(CODEC_JP2);

return compressor;
Expand Down
4 changes: 2 additions & 2 deletions src/libOpenImageIO/imageioplugin.cpp
Expand Up @@ -308,7 +308,7 @@ ImageOutput::create (const std::string &filename,
}

// Extract the file extension from the filename (without the leading dot)
std::string format = Filesystem::file_extension (filename);
std::string format = Filesystem::extension (filename, false);
if (format.empty()) {
// If the file had no extension, maybe it was itself the format name
format = filename;
Expand Down Expand Up @@ -365,7 +365,7 @@ ImageInput::create (const std::string &filename,
}

// Extract the file extension from the filename (without the leading dot)
std::string format = Filesystem::file_extension (filename);
std::string format = Filesystem::extension (filename, false);
if (format.empty()) {
// If the file had no extension, maybe it was itself the format name
format = filename;
Expand Down
36 changes: 8 additions & 28 deletions src/libutil/filesystem.cpp
Expand Up @@ -61,37 +61,17 @@ Filesystem::filename (const std::string &filepath)


std::string
Filesystem::extension (const std::string &filepath)
Filesystem::extension (const std::string &filepath, bool include_dot)
{
std::string s;
#if BOOST_FILESYSTEM_VERSION == 3
return boost::filesystem::path(filepath).extension().string();
s = boost::filesystem::path(filepath).extension().string();
#else
return boost::filesystem::path(filepath).extension();
s = boost::filesystem::path(filepath).extension();
#endif
}



std::string
Filesystem::file_extension (const std::string &filepath)
{
// Search for the LAST dot in the filepath
const char *ext = strrchr (filepath.c_str(), '.');

// If there was no dot at all, or if it was the last character, there's
// no file extension, so just return "".
if (! ext || !ext[1])
return "";

++ext; // Advance to the char AFTER the dot

// But if there's a slash after the last dot, then the dot was part
// of the directory, not the leaf name.
if (strchr (ext, '/'))
return "";

// The extension starts AFTER the period!
return ext;
if (! include_dot && !s.empty() && s[0] == '.')
s.erase (0, 1); // erase the first character
return s;
}


Expand All @@ -100,7 +80,7 @@ std::string
Filesystem::replace_extension (const std::string &filepath,
const std::string &new_extension)
{
return boost::filesystem::path(filepath).replace_extension(new_extension).string();
return boost::filesystem::path(filepath).replace_extension(new_extension).string();
}


Expand Down

0 comments on commit 8bdbb16

Please sign in to comment.