- file.cpp|h: port filetypes handling from martii

Signed-off-by: Thilo Graf <dbt@novatux.de>
This commit is contained in:
svenhoefer
2017-06-07 14:17:12 +02:00
committed by M. Liebmann
parent 535e4e2123
commit 37e2efb3ed
2 changed files with 68 additions and 55 deletions

View File

@@ -34,43 +34,65 @@
#include <config.h> #include <config.h>
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
#include <sys/stat.h>
#include <driver/file.h> #include <driver/file.h>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
/* ATTENTION: the array file_extension_list MUST BE SORTED ASCENDING (cf. sort, man bsearch) - otherwise bsearch will not work correctly! */ struct file_ext_s {
const char * const file_extension_list[] = const char *ext;
{ const CFile::FileType type;
"aac", "asf", "avi", "bin", "bmp", "cdr", "crw",
"dts", "flac", "flv", "gif", "imu", "ipk", "iso", "jpeg", "jpg",
"m2a", "m3u", "m3u8", "m4a", "mkv", "mp2", "mp3",
"mp4", "mpa", "mpeg", "mpg", "ogg", "opk", "pls", "png", "sh",
"ts", "txt", "url", "vob", "wav", "xml"
}; };
/* ATTENTION: the array file_extension_list MUST BE SORTED ASCENDING (cf. sort, man bsearch) - otherwise bsearch will not work correctly! */
const CFile::FileType file_type_list[] = // ATTENTION: the array MUST BE SORTED ASCENDING (cf. sort, man bsearch) - otherwise bsearch will not work correctly!
static const file_ext_s file_ext[] =
{ {
CFile::FILE_AAC , CFile::FILE_ASF , CFile::FILE_AVI , CFile::FILE_BIN_PACKAGE , CFile::FILE_PICTURE , CFile::FILE_CDR , CFile::FILE_PICTURE , { "aac", CFile::FILE_AAC },
CFile::FILE_WAV , CFile::FILE_FLAC , CFile::FILE_MPG , CFile::FILE_PICTURE , CFile::STREAM_PICTURE , CFile::FILE_PKG_PACKAGE , CFile::FILE_ISO , CFile::FILE_PICTURE , CFile::FILE_PICTURE , { "asf", CFile::FILE_ASF },
CFile::FILE_MP3 , CFile::FILE_PLAYLIST , CFile::FILE_PLAYLIST , CFile::FILE_AAC , CFile::FILE_MKV , CFile::FILE_MP3 , CFile::FILE_MP3 , { "avi", CFile::FILE_AVI },
CFile::FILE_MPG , CFile::FILE_MP3 , CFile::FILE_MPG , CFile::FILE_MPG , CFile::FILE_OGG , CFile::FILE_PKG_PACKAGE , CFile::FILE_PLAYLIST , CFile::FILE_PICTURE , CFile::FILE_TEXT , { "bin", CFile::FILE_BIN_PACKAGE },
CFile::FILE_TS , CFile::FILE_TEXT , CFile::STREAM_AUDIO , CFile::FILE_VOB , CFile::FILE_WAV , CFile::FILE_XML { "bmp", CFile::FILE_PICTURE },
{ "cdr", CFile::FILE_CDR },
{ "crw", CFile::FILE_PICTURE },
{ "dts", CFile::FILE_WAV },
{ "flac", CFile::FILE_FLAC },
{ "flv", CFile::FILE_MPG },
{ "gif", CFile::FILE_PICTURE },
{ "imu", CFile::STREAM_PICTURE },
{ "ipk", CFile::FILE_PKG_PACKAGE },
{ "iso", CFile::FILE_ISO },
{ "jpeg", CFile::FILE_PICTURE },
{ "jpg", CFile::FILE_PICTURE },
{ "m2a", CFile::FILE_MP3 },
{ "m3u", CFile::FILE_PLAYLIST },
{ "m3u8", CFile::FILE_PLAYLIST },
{ "m4a", CFile::FILE_AAC },
{ "mkv", CFile::FILE_MKV },
{ "mp2", CFile::FILE_MP3 },
{ "mp3", CFile::FILE_MP3 },
{ "mp4", CFile::FILE_MPG },
{ "mpa", CFile::FILE_MP3 },
{ "mpeg", CFile::FILE_MPG },
{ "mpg", CFile::FILE_MPG },
{ "ogg", CFile::FILE_OGG },
{ "opk", CFile::FILE_PKG_PACKAGE },
{ "pls", CFile::FILE_PLAYLIST },
{ "png", CFile::FILE_PICTURE },
{ "sh", CFile::FILE_TEXT },
{ "ts", CFile::FILE_TS },
{ "txt", CFile::FILE_TEXT },
{ "url", CFile::STREAM_AUDIO },
{ "vob", CFile::FILE_VOB },
{ "wav", CFile::FILE_WAV },
{ "xml", CFile::FILE_XML }
}; };
int mycasecmp(const void * a, const void * b) int mycasecmp(const void * a, const void * b)
{ {
return strcasecmp(*(const char * *)a, *(const char * *)b); return strcasecmp(((file_ext_s *)a)->ext, ((file_ext_s *)b)->ext);
} }
//------------------------------------------------------------------------ CFile::CFile() : Size( 0 ), Mode( 0 ), Marked( false ), Time( 0 )
//------------------------------------------------------------------------
CFile::CFile()
: Size( 0 ), Mode( 0 ), Marked( false ), Time( 0 )
{ {
Type = -1;
} }
CFile::FileType CFile::getType(void) const CFile::FileType CFile::getType(void) const
@@ -78,23 +100,16 @@ CFile::FileType CFile::getType(void) const
if(S_ISDIR(Mode)) if(S_ISDIR(Mode))
return FILE_DIR; return FILE_DIR;
if (Type < 0) {
Type = (int) FILE_UNKNOWN;
std::string::size_type ext_pos = Name.rfind('.'); std::string::size_type ext_pos = Name.rfind('.');
if (ext_pos != std::string::npos) { if (ext_pos != std::string::npos) {
const char * key = &(Name.c_str()[ext_pos + 1]); const char * key = &(Name.c_str()[ext_pos + 1]);
void * result = ::bsearch(&key, file_ext, sizeof(file_ext) / sizeof(file_ext_s), sizeof(file_ext_s), mycasecmp);
void * result = ::bsearch(&key, file_extension_list, sizeof(file_extension_list) / sizeof(const char *), sizeof(const char *), mycasecmp); if (result)
return ((file_ext_s *)result)->type;
if (result != NULL)
Type = (int) file_type_list[(const char * *)result - (const char * *)&file_extension_list];
} }
return FILE_UNKNOWN;
} }
return (CFile::FileType) Type;
}
//------------------------------------------------------------------------
std::string CFile::getFileName(void) const // return name.extension or folder name without trailing / std::string CFile::getFileName(void) const // return name.extension or folder name without trailing /
{ {
@@ -103,8 +118,6 @@ std::string CFile::getFileName(void) const // return name.extension or folder n
return (namepos == std::string::npos) ? Name : Name.substr(namepos + 1); return (namepos == std::string::npos) ? Name : Name.substr(namepos + 1);
} }
//------------------------------------------------------------------------
std::string CFile::getPath(void) const // return complete path including trailing / std::string CFile::getPath(void) const // return complete path including trailing /
{ {
int pos = 0; int pos = 0;

View File

@@ -55,28 +55,28 @@ class CFile
{ {
FILE_UNKNOWN = 0, FILE_UNKNOWN = 0,
FILE_AAC, FILE_AAC,
FILE_AVI,
FILE_ASF, FILE_ASF,
FILE_DIR, FILE_AVI,
FILE_ISO, FILE_BIN_PACKAGE,
FILE_TEXT,
FILE_CDR, FILE_CDR,
FILE_MP3, FILE_DIR,
FILE_MKV,
FILE_OGG,
FILE_WAV,
FILE_FLAC, FILE_FLAC,
FILE_FLV, FILE_FLV,
FILE_XML, FILE_ISO,
FILE_PLAYLIST, FILE_MKV,
STREAM_AUDIO, FILE_MP3,
FILE_PICTURE,
STREAM_PICTURE,
FILE_VOB,
FILE_MPG, FILE_MPG,
FILE_OGG,
FILE_PICTURE,
FILE_PKG_PACKAGE,
FILE_PLAYLIST,
FILE_TEXT,
FILE_TS, FILE_TS,
FILE_BIN_PACKAGE, FILE_VOB,
FILE_PKG_PACKAGE FILE_WAV,
FILE_XML,
STREAM_AUDIO,
STREAM_PICTURE
}; };
FileType getType(void) const; FileType getType(void) const;