File: filetype.cc

package info (click to toggle)
goldendict-webengine 23.02.05-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,148 kB
  • sloc: cpp: 58,537; javascript: 9,942; ansic: 9,242; xml: 41; makefile: 15; sh: 9
file content (143 lines) | stat: -rw-r--r-- 3,215 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
 * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */

#include "filetype.hh"
#include "utf8.hh"
#include <ctype.h>

namespace Filetype {

namespace {

/// 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;
}

}

/// Removes any trailing or leading spaces and lowercases the string.
/// The lowercasing is done simplistically, but it is enough for file
/// extensions.
string simplifyString( string const & str, bool lowercase )
{
  string result;

  size_t beginPos = 0;

  while( beginPos < str.size() && Utf8::isspace( str[ beginPos ] ) )
    ++beginPos;

  size_t endPos = str.size();

  while( endPos && Utf8::isspace( str[ endPos - 1 ] ) )
    --endPos;

  if( endPos <= beginPos )
    return string();

  result.reserve( endPos - beginPos );

  while( beginPos < endPos )
    result.push_back( lowercase ? tolower( str[ beginPos++ ] ) : str[ beginPos++ ] );

  return result;
}

bool isNameOfSound( string const & name )
{
  string s = simplifyString( name );

  return
    endsWith( s, ".wav" ) ||
    endsWith( s, ".au" ) ||
    endsWith( s, ".voc" ) ||
    endsWith( s, ".ogg" ) ||
    endsWith( s, ".oga" ) ||
    endsWith( s, ".mp3" ) ||
    endsWith( s, ".m4a") ||
    endsWith( s, ".aac" ) ||
    endsWith( s, ".flac" ) ||
    endsWith( s, ".mid" ) ||
    endsWith( s, ".kar" ) ||
    endsWith( s, ".mpc" ) ||
    endsWith( s, ".wma" ) ||
    endsWith( s, ".wv" ) ||
    endsWith( s, ".ape" ) ||
    endsWith( s, ".spx" ) ||
    endsWith( s, ".opus" ) ||
    endsWith( s, ".mpa" ) ||
    endsWith( s, ".mp2" );
}

bool isNameOfVideo( string const & name )
{
  string s = simplifyString( name );

  return
    endsWith( s, ".mpg" ) ||
    endsWith( s, ".mpeg" )||
    endsWith( s, ".mpe" ) ||
    endsWith( s, ".ogv" ) ||
    endsWith( s, ".ogm" ) ||
    endsWith( s, ".avi" ) ||
    endsWith( s, ".m4v" ) ||
    endsWith( s, ".mp4" ) ||
    endsWith( s, ".mkv" ) ||
    endsWith( s, ".wmv" ) ||
    endsWith( s, ".sfw" ) ||
    endsWith( s, ".flv" ) ||
    endsWith( s, ".divx" ) ||
    endsWith( s, ".3gp" ) ||
    endsWith( s, ".webm" ) ||
    endsWith( s, ".mov" );
}

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" ) ||
    endsWith( s, ".webp" ) ||
    endsWith( s, ".svg" );
}

bool isNameOfTiff( string const & name )
{
  string s = simplifyString( name );

  return
    endsWith( s, ".tif" ) ||
    endsWith( s, ".tiff" );
}

bool isNameOfCSS( string const & name )
{
  string s = simplifyString( name );

  return
    endsWith( s, ".css" );
}

bool isNameOfSvg( string const & name )
{
  string s = simplifyString( name );

  return
    endsWith( s, ".svg" );
}

}