OpenMS
Loading...
Searching...
No Matches
FileNameUtils Class Reference

Filename-only format recognition and extension manipulation. More...

#include <OpenMS/FORMAT/FileNameUtils.h>

Static Public Member Functions

static FileTypes::Type getTypeByFileName (const std::string &filename)
 Determine a file type from its name, including compression suffixes.
 
static bool hasValidExtension (const std::string &filename, FileTypes::Type type)
 Accept the expected extension or an unknown extension.
 
static FileTypes::Type compressionType (const std::string &filename)
 Which compression suffix does filename end in?
 
static bool hasCompressionSuffix (const std::string &filename)
 Does filename end in a compression suffix (.gz/.bz2/.zip)?
 
static std::string stripExtension (const std::string &filename)
 Remove the extension using the established FileHandler rules.
 
static std::string swapExtension (const std::string &filename, FileTypes::Type new_type)
 Strip the existing extension and append the requested extension.
 

Static Private Member Functions

static FileTypes::Type matchExtension_ (const std::string &filename, size_t &ext_start)
 Match the longest known extension at the end of filename.
 

Detailed Description

Filename-only format recognition and extension manipulation.

These operations preserve FileHandler's filename rules and never inspect file contents or require the path to exist. Extensions are resolved through the FileTypes registry, so registered aliases (e.g. '.fa' for FASTA) are recognized alongside the preferred extension. Both slash styles are recognized on every platform. FileHandler retains its existing forwarding entry points.

Member Function Documentation

◆ compressionType()

static FileTypes::Type compressionType ( const std::string &  filename)
static

Which compression suffix does filename end in?

getTypeByFileName() sees through such a suffix to the inner type, so callers that need to know whether a reader must decompress have to ask separately (see FileTypes::supportsCompressedReading, which needs this value because not every reader handles every container).

Parameters
[in]filenameFilename to inspect.
Returns
FileTypes::GZ, BZ2 or ZIP, or FileTypes::UNKNOWN if there is no compression suffix.

◆ getTypeByFileName()

static FileTypes::Type getTypeByFileName ( const std::string &  filename)
static

Determine a file type from its name, including compression suffixes.

Parameters
[in]filenameFilename to inspect.
Returns
Recognized type, or FileTypes::UNKNOWN.

◆ hasCompressionSuffix()

static bool hasCompressionSuffix ( const std::string &  filename)
static

Does filename end in a compression suffix (.gz/.bz2/.zip)?

Parameters
[in]filenameFilename to inspect.

◆ hasValidExtension()

static bool hasValidExtension ( const std::string &  filename,
FileTypes::Type  type 
)
static

Accept the expected extension or an unknown extension.

Parameters
[in]filenameFilename to inspect.
[in]typeExpected type.

◆ matchExtension_()

static FileTypes::Type matchExtension_ ( const std::string &  filename,
size_t &  ext_start 
)
staticprivate

Match the longest known extension at the end of filename.

Every dot-delimited suffix of the basename is looked up in the FileTypes registry (preferred extensions and aliases alike) and the longest match wins, so '.pep.xml' is preferred over '.xml'. A trailing compression suffix is seen through and counted as part of the extension.

Parameters
[in]filenameFilename to inspect.
[out]ext_startOffset of the '.' that starts the extension, or npos if the basename has no extension at all. Set even when the type is UNKNOWN, in which case it points at the last dot.
Returns
Recognized type, or FileTypes::UNKNOWN.

◆ stripExtension()

static std::string stripExtension ( const std::string &  filename)
static

Remove the extension using the established FileHandler rules.

Parameters
[in]filenameFilename whose extension should be removed.
Returns
Filename with its recognized extension removed, or the last basename suffix removed for an unknown type.

◆ swapExtension()

static std::string swapExtension ( const std::string &  filename,
FileTypes::Type  new_type 
)
static

Strip the existing extension and append the requested extension.

Parameters
[in]filenameOriginal filename.
[in]new_typeType of the new extension.
Returns
Filename with the replacement extension.