1 /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
2  * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
3 
4 #include "filetype.hh"
5 #include "utf8.hh"
6 #include <ctype.h>
7 
8 namespace Filetype {
9 
10 namespace {
11 
12 /// Checks if the given string ends with the given substring
endsWith(string const & str,string const & tail)13 bool endsWith( string const & str, string const & tail )
14 {
15   return str.size() >= tail.size() &&
16     str.compare( str.size() - tail.size(), tail.size(), tail ) == 0;
17 }
18 
19 }
20 
21 /// Removes any trailing or leading spaces and lowercases the string.
22 /// The lowercasing is done simplistically, but it is enough for file
23 /// extensions.
simplifyString(string const & str,bool lowercase)24 string simplifyString( string const & str, bool lowercase )
25 {
26   string result;
27 
28   size_t beginPos = 0;
29 
30   while( beginPos < str.size() && Utf8::isspace( str[ beginPos ] ) )
31     ++beginPos;
32 
33   size_t endPos = str.size();
34 
35   while( endPos && Utf8::isspace( str[ endPos - 1 ] ) )
36     --endPos;
37 
38   if( endPos <= beginPos )
39     return string();
40 
41   result.reserve( endPos - beginPos );
42 
43   while( beginPos < endPos )
44     result.push_back( lowercase ? tolower( str[ beginPos++ ] ) : str[ beginPos++ ] );
45 
46   return result;
47 }
48 
isNameOfSound(string const & name)49 bool isNameOfSound( string const & name )
50 {
51   string s = simplifyString( name );
52 
53   return
54     endsWith( s, ".wav" ) ||
55     endsWith( s, ".au" ) ||
56     endsWith( s, ".voc" ) ||
57     endsWith( s, ".ogg" ) ||
58     endsWith( s, ".mp3" ) ||
59     endsWith( s, ".m4a") ||
60     endsWith( s, ".aac" ) ||
61     endsWith( s, ".flac" ) ||
62     endsWith( s, ".mid" ) ||
63     endsWith( s, ".kar" ) ||
64     endsWith( s, ".mpc" ) ||
65     endsWith( s, ".wma" ) ||
66     endsWith( s, ".wv" ) ||
67     endsWith( s, ".ape" ) ||
68     endsWith( s, ".spx" ) ||
69     endsWith( s, ".opus" ) ||
70     endsWith( s, ".mpa" ) ||
71     endsWith( s, ".mp2" );
72 }
73 
isNameOfVideo(string const & name)74 bool isNameOfVideo( string const & name )
75 {
76   string s = simplifyString( name );
77 
78   return
79     endsWith( s, ".mpg" ) ||
80     endsWith( s, ".mpeg" )||
81     endsWith( s, ".mpe" ) ||
82     endsWith( s, ".ogv" ) ||
83     endsWith( s, ".ogm" ) ||
84     endsWith( s, ".avi" ) ||
85     endsWith( s, ".m4v" ) ||
86     endsWith( s, ".mp4" ) ||
87     endsWith( s, ".mkv" ) ||
88     endsWith( s, ".wmv" ) ||
89     endsWith( s, ".sfw" ) ||
90     endsWith( s, ".flv" ) ||
91     endsWith( s, ".divx" ) ||
92     endsWith( s, ".3gp" ) ||
93     endsWith( s, ".webm" ) ||
94     endsWith( s, ".mov" );
95 }
96 
isNameOfPicture(string const & name)97 bool isNameOfPicture( string const & name )
98 {
99   string s = simplifyString( name );
100 
101   return
102     endsWith( s, ".jpg" ) ||
103     endsWith( s, ".jpeg" ) ||
104     endsWith( s, ".jpe" ) ||
105     endsWith( s, ".png" ) ||
106     endsWith( s, ".gif" ) ||
107     endsWith( s, ".bmp" ) ||
108     endsWith( s, ".tif" ) ||
109     endsWith( s, ".tiff" ) ||
110     endsWith( s, ".tga" ) ||
111     endsWith( s, ".pcx" ) ||
112     endsWith( s, ".ico" ) ||
113     endsWith( s, ".webp" ) ||
114     endsWith( s, ".svg" );
115 }
116 
isNameOfTiff(string const & name)117 bool isNameOfTiff( string const & name )
118 {
119   string s = simplifyString( name );
120 
121   return
122     endsWith( s, ".tif" ) ||
123     endsWith( s, ".tiff" );
124 }
125 
isNameOfCSS(string const & name)126 bool isNameOfCSS( string const & name )
127 {
128   string s = simplifyString( name );
129 
130   return
131     endsWith( s, ".css" );
132 }
133 
isNameOfSvg(string const & name)134 bool isNameOfSvg( string const & name )
135 {
136   string s = simplifyString( name );
137 
138   return
139     endsWith( s, ".svg" );
140 }
141 
142 }
143