Add upper case extensions, use binary search

git-svn-id: svn://svn.cc65.org/cc65/trunk@2183 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2003-06-02 11:01:23 +00:00
parent 1e4af7b04e
commit 4525b13eb2
2 changed files with 52 additions and 32 deletions

View file

@ -33,6 +33,7 @@
#include <stdlib.h>
#include <string.h> #include <string.h>
/* common */ /* common */
@ -47,26 +48,45 @@
/* Table that maps extensions to file types */ /* Table that maps extensions to file types. Sorted alphabetically. */
static const struct { typedef struct {
const char Ext[4]; const char Ext[4];
unsigned Type; FILETYPE Type;
} FileTypes [] = { } FileType;
{ "c", FILETYPE_C },
{ "s", FILETYPE_ASM }, static const FileType TypeTable[] = {
{ "asm", FILETYPE_ASM }, /* Upper case stuff for obsolete operating systems */
{ "a65", FILETYPE_ASM }, { "A", FILETYPE_LIB },
{ "o", FILETYPE_OBJ }, { "A65", FILETYPE_ASM },
{ "obj", FILETYPE_OBJ }, { "ASM", FILETYPE_ASM },
{ "C", FILETYPE_C },
{ "EMD", FILETYPE_O65 },
{ "GRC", FILETYPE_GR },
{ "JOY", FILETYPE_O65 },
{ "LIB", FILETYPE_LIB },
{ "O", FILETYPE_OBJ },
{ "O65", FILETYPE_O65 },
{ "OBJ", FILETYPE_OBJ },
{ "S", FILETYPE_ASM },
{ "TGI", FILETYPE_O65 },
{ "a", FILETYPE_LIB }, { "a", FILETYPE_LIB },
{ "lib", FILETYPE_LIB }, { "a65", FILETYPE_ASM },
{ "grc", FILETYPE_GR }, { "asm", FILETYPE_ASM },
{ "o65", FILETYPE_O65 }, { "c", FILETYPE_C },
{ "emd", FILETYPE_O65 }, { "emd", FILETYPE_O65 },
{ "grc", FILETYPE_GR },
{ "joy", FILETYPE_O65 }, { "joy", FILETYPE_O65 },
{ "lib", FILETYPE_LIB },
{ "o", FILETYPE_OBJ },
{ "o65", FILETYPE_O65 },
{ "obj", FILETYPE_OBJ },
{ "s", FILETYPE_ASM },
{ "tgi", FILETYPE_O65 }, { "tgi", FILETYPE_O65 },
}; };
#define FILETYPE_COUNT (sizeof (TypeTable) / sizeof (TypeTable[0]))
/*****************************************************************************/ /*****************************************************************************/
@ -75,34 +95,34 @@ static const struct {
int GetFileType (const char* Name) static int Compare (const void* Key, const void* Type)
/* Compare function for bsearch */
{
return strcmp (Key, ((const FileType*) Type)->Ext);
}
FILETYPE GetFileType (const char* Name)
/* Determine the type of the given file by looking at the name. If the file /* Determine the type of the given file by looking at the name. If the file
* type could not be determined, the function returns FILETYPE_UNKOWN. * type could not be determined, the function returns FILETYPE_UNKOWN.
*/ */
{ {
unsigned I; const FileType* FT;
/* Determine the file type by the extension */ /* Determine the file type by the extension */
const char* Ext = FindExt (Name); const char* Ext = FindExt (Name);
/* Do we have an extension? */ /* Do we have an extension? */
if (Ext == 0) { if (Ext == 0) {
return FILETYPE_UNKNOWN; return FILETYPE_UNKNOWN;
} }
/* Skip the dot */
++Ext;
/* Check for known extensions */ /* Search for a table entry */
for (I = 0; I < sizeof (FileTypes) / sizeof (FileTypes [0]); ++I) { FT = bsearch (Ext+1, TypeTable, FILETYPE_COUNT, sizeof (FileType), Compare);
if (strcmp (FileTypes [I].Ext, Ext) == 0) {
/* Found */
return FileTypes [I].Type;
}
}
/* Not found, return the default */ /* Return the result */
return FILETYPE_UNKNOWN; return FT? FT->Type : FILETYPE_UNKNOWN;
} }

View file

@ -45,7 +45,7 @@
/* File types */ /* File types */
enum { typedef enum {
FILETYPE_UNKNOWN = -1, /* Unknown file type */ FILETYPE_UNKNOWN = -1, /* Unknown file type */
FILETYPE_C, /* C source file */ FILETYPE_C, /* C source file */
FILETYPE_ASM, /* Assembler file */ FILETYPE_ASM, /* Assembler file */
@ -53,7 +53,7 @@ enum {
FILETYPE_LIB, /* Library file */ FILETYPE_LIB, /* Library file */
FILETYPE_GR, /* GEOS resource file */ FILETYPE_GR, /* GEOS resource file */
FILETYPE_O65 /* O65 object file */ FILETYPE_O65 /* O65 object file */
}; } FILETYPE;
@ -63,7 +63,7 @@ enum {
int GetFileType (const char* Name); FILETYPE GetFileType (const char* Name);
/* Determine the type of the given file by looking at the name. If the file /* Determine the type of the given file by looking at the name. If the file
* type could not be determined, the function returns FILETYPE_UNKOWN. * type could not be determined, the function returns FILETYPE_UNKOWN.
*/ */