2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "filetype.hh"
|
2016-04-15 14:44:53 +00:00
|
|
|
#include "utf8.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
namespace Filetype {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-09-26 16:40:39 +00:00
|
|
|
/// Checks if the given string ends with the given substring
|
|
|
|
bool endsWith( string const & str, string const & tail )
|
|
|
|
{
|
|
|
|
return str.size() >= tail.size() &&
|
|
|
|
str.compare( str.size() - tail.size(), tail.size(), tail ) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Removes any trailing or leading spaces and lowercases the string.
|
|
|
|
/// The lowercasing is done simplistically, but it is enough for file
|
|
|
|
/// extensions.
|
2019-09-26 16:40:39 +00:00
|
|
|
string simplifyString( string const & str, bool lowercase )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
string result;
|
|
|
|
|
|
|
|
size_t beginPos = 0;
|
|
|
|
|
2016-04-15 14:44:53 +00:00
|
|
|
while( beginPos < str.size() && Utf8::isspace( str[ beginPos ] ) )
|
2009-04-04 16:32:40 +00:00
|
|
|
++beginPos;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
size_t endPos = str.size();
|
|
|
|
|
2016-04-15 14:44:53 +00:00
|
|
|
while( endPos && Utf8::isspace( str[ endPos - 1 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
--endPos;
|
|
|
|
|
2018-03-19 14:42:30 +00:00
|
|
|
if( endPos <= beginPos )
|
|
|
|
return string();
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
result.reserve( endPos - beginPos );
|
|
|
|
|
|
|
|
while( beginPos < endPos )
|
2019-09-26 16:40:39 +00:00
|
|
|
result.push_back( lowercase ? tolower( str[ beginPos++ ] ) : str[ beginPos++ ] );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNameOfSound( string const & name )
|
|
|
|
{
|
|
|
|
string s = simplifyString( name );
|
|
|
|
|
|
|
|
return
|
|
|
|
endsWith( s, ".wav" ) ||
|
|
|
|
endsWith( s, ".au" ) ||
|
|
|
|
endsWith( s, ".voc" ) ||
|
|
|
|
endsWith( s, ".ogg" ) ||
|
2010-01-02 18:16:22 +00:00
|
|
|
endsWith( s, ".mp3" ) ||
|
2013-05-05 10:22:12 +00:00
|
|
|
endsWith( s, ".m4a") ||
|
2010-01-02 18:16:22 +00:00
|
|
|
endsWith( s, ".aac" ) ||
|
|
|
|
endsWith( s, ".flac" ) ||
|
|
|
|
endsWith( s, ".mid" ) ||
|
|
|
|
endsWith( s, ".kar" ) ||
|
|
|
|
endsWith( s, ".mpc" ) ||
|
|
|
|
endsWith( s, ".wma" ) ||
|
|
|
|
endsWith( s, ".wv" ) ||
|
2013-05-01 05:39:27 +00:00
|
|
|
endsWith( s, ".ape" ) ||
|
2019-09-26 19:22:23 +00:00
|
|
|
endsWith( s, ".spx" ) ||
|
|
|
|
endsWith( s, ".opus" ) ||
|
2019-09-27 17:09:20 +00:00
|
|
|
endsWith( s, ".mpa" ) ||
|
|
|
|
endsWith( s, ".mp2" );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 16:36:25 +00:00
|
|
|
bool isNameOfVideo( string const & name )
|
|
|
|
{
|
|
|
|
string s = simplifyString( name );
|
|
|
|
|
|
|
|
return
|
|
|
|
endsWith( s, ".mpg" ) ||
|
|
|
|
endsWith( s, ".mpeg" )||
|
|
|
|
endsWith( s, ".mpe" ) ||
|
|
|
|
endsWith( s, ".ogv" ) ||
|
2013-06-23 10:06:53 +00:00
|
|
|
endsWith( s, ".ogm" ) ||
|
2013-06-22 16:36:25 +00:00
|
|
|
endsWith( s, ".avi" ) ||
|
|
|
|
endsWith( s, ".m4v" ) ||
|
2013-06-23 09:57:51 +00:00
|
|
|
endsWith( s, ".mp4" ) ||
|
2013-06-22 16:36:25 +00:00
|
|
|
endsWith( s, ".mkv" ) ||
|
|
|
|
endsWith( s, ".wmv" ) ||
|
|
|
|
endsWith( s, ".sfw" ) ||
|
|
|
|
endsWith( s, ".flv" ) ||
|
|
|
|
endsWith( s, ".divx" ) ||
|
|
|
|
endsWith( s, ".3gp" ) ||
|
2018-04-01 13:18:11 +00:00
|
|
|
endsWith( s, ".webm" ) ||
|
2019-09-27 17:09:20 +00:00
|
|
|
endsWith( s, ".mov" );
|
2013-06-22 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
bool isNameOfPicture( string const & name )
|
|
|
|
{
|
|
|
|
string s = simplifyString( name );
|
|
|
|
|
|
|
|
return
|
|
|
|
endsWith( s, ".jpg" ) ||
|
|
|
|
endsWith( s, ".jpeg" ) ||
|
|
|
|
endsWith( s, ".jpe" ) ||
|
|
|
|
endsWith( s, ".png" ) ||
|
|
|
|
endsWith( s, ".gif" ) ||
|
|
|
|
endsWith( s, ".bmp" ) ||
|
|
|
|
endsWith( s, ".tif" ) ||
|
|
|
|
endsWith( s, ".tiff" ) ||
|
|
|
|
endsWith( s, ".tga" ) ||
|
|
|
|
endsWith( s, ".pcx" ) ||
|
|
|
|
endsWith( s, ".ico" ) ||
|
2019-09-26 19:22:23 +00:00
|
|
|
endsWith( s, ".webp" ) ||
|
2009-01-28 20:55:45 +00:00
|
|
|
endsWith( s, ".svg" );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNameOfTiff( string const & name )
|
|
|
|
{
|
|
|
|
string s = simplifyString( name );
|
|
|
|
|
|
|
|
return
|
|
|
|
endsWith( s, ".tif" ) ||
|
|
|
|
endsWith( s, ".tiff" );
|
|
|
|
}
|
|
|
|
|
2013-05-23 14:01:50 +00:00
|
|
|
bool isNameOfCSS( string const & name )
|
|
|
|
{
|
|
|
|
string s = simplifyString( name );
|
|
|
|
|
|
|
|
return
|
|
|
|
endsWith( s, ".css" );
|
|
|
|
}
|
|
|
|
|
2013-05-25 12:07:49 +00:00
|
|
|
bool isNameOfSvg( string const & name )
|
|
|
|
{
|
|
|
|
string s = simplifyString( name );
|
|
|
|
|
|
|
|
return
|
|
|
|
endsWith( s, ".svg" );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|