1 /* EasyTAG - tag editor for audio files
2  * Copyright (C) 2014  David King <amigadave@amigadave.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #include "config.h"
20 #include "file_description.h"
21 
22 #include <string.h>
23 
24 const ET_File_Description ETFileDescription[] =
25 {
26 #ifdef ENABLE_MP3
27     { MP3_FILE, ".mp3", ID3_TAG},
28     { MP2_FILE, ".mp2", ID3_TAG},
29 #endif
30 #ifdef ENABLE_OPUS
31     { OPUS_FILE, ".opus", OPUS_TAG},
32 #endif
33 #ifdef ENABLE_OGG
34     { OGG_FILE, ".ogg", OGG_TAG},
35     { OGG_FILE, ".oga", OGG_TAG},
36 #endif
37 #ifdef ENABLE_SPEEX
38     { SPEEX_FILE, ".spx", OGG_TAG}, /* Implemented by Pierre Dumuid. */
39 #endif
40 #ifdef ENABLE_FLAC
41     { FLAC_FILE, ".flac", FLAC_TAG},
42     { FLAC_FILE, ".fla",  FLAC_TAG},
43 #endif
44     { MPC_FILE, ".mpc", APE_TAG}, /* Implemented by Artur Polaczynski. */
45     { MPC_FILE, ".mp+", APE_TAG}, /* Implemented by Artur Polaczynski. */
46     { MPC_FILE, ".mpp", APE_TAG}, /* Implemented by Artur Polaczynski. */
47     { MAC_FILE, ".ape", APE_TAG}, /* Implemented by Artur Polaczynski. */
48     { MAC_FILE, ".mac", APE_TAG}, /* Implemented by Artur Polaczynski. */
49     { OFR_FILE, ".ofr", APE_TAG},
50     { OFR_FILE, ".ofs", APE_TAG},
51 #ifdef ENABLE_MP4
52     { MP4_FILE, ".mp4", MP4_TAG}, /* Implemented by Michael Ihde. */
53     { MP4_FILE, ".m4a", MP4_TAG}, /* Implemented by Michael Ihde. */
54     { MP4_FILE, ".m4p", MP4_TAG}, /* Implemented by Michael Ihde. */
55     { MP4_FILE, ".m4v", MP4_TAG},
56     { MP4_FILE, ".aac", MP4_TAG},
57 #endif
58 #ifdef ENABLE_WAVPACK
59     { WAVPACK_FILE, ".wv", WAVPACK_TAG}, /* Implemented by Maarten Maathuis. */
60 #endif
61     { UNKNOWN_FILE, "", UNKNOWN_TAG } /* This item must be placed at the end! */
62 };
63 
64 const gsize ET_FILE_DESCRIPTION_SIZE = G_N_ELEMENTS (ETFileDescription) - 1;
65 
66 /*
67  * Returns the extension of the file
68  */
69 const gchar *
ET_Get_File_Extension(const gchar * filename)70 ET_Get_File_Extension (const gchar *filename)
71 {
72     if (filename)
73     {
74         return strrchr (filename, '.');
75     }
76     else
77     {
78         return NULL;
79     }
80 }
81 
82 
83 /*
84  * Determine description of file using his extension.
85  * If extension is NULL or not found into the tab, it returns the last entry for UNKNOWN_FILE.
86  */
87 static const ET_File_Description *
ET_Get_File_Description_From_Extension(const gchar * extension)88 ET_Get_File_Description_From_Extension (const gchar *extension)
89 {
90     guint i;
91 
92     if (!extension) // Unknown file
93         return &ETFileDescription[ET_FILE_DESCRIPTION_SIZE];
94 
95     for (i=0; i<ET_FILE_DESCRIPTION_SIZE; i++)  // Use of '<' instead of '<=' to avoid to test for Unknown file
96         if ( strcasecmp(extension,ETFileDescription[i].Extension)==0 )
97             return &ETFileDescription[i];
98 
99     // If not found in the list
100     return &ETFileDescription[ET_FILE_DESCRIPTION_SIZE];
101 }
102 
103 
104 /*
105  * Determines description of file.
106  * Determines first the extension. If extension is NULL or not found into the tab,
107  * it returns the last entry for UNKNOWN_FILE.
108  */
109 const ET_File_Description *
ET_Get_File_Description(const gchar * filename)110 ET_Get_File_Description (const gchar *filename)
111 {
112     return ET_Get_File_Description_From_Extension(ET_Get_File_Extension(filename));
113 }
114 
115 
116 /*
117  * Returns TRUE if the file is supported, else returns FALSE
118  */
119 gboolean
et_file_is_supported(const gchar * filename)120 et_file_is_supported (const gchar *filename)
121 {
122     if (ET_Get_File_Description(filename)->FileType != UNKNOWN_FILE)
123         return TRUE;
124     else
125         return FALSE;
126 }
127