From 4525b13eb2c24cc5194b291e481fcaae10e85ee8 Mon Sep 17 00:00:00 2001 From: cuz Date: Mon, 2 Jun 2003 11:01:23 +0000 Subject: [PATCH] Add upper case extensions, use binary search git-svn-id: svn://svn.cc65.org/cc65/trunk@2183 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/common/filetype.c | 82 +++++++++++++++++++++++++++---------------- src/common/filetype.h | 6 ++-- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/common/filetype.c b/src/common/filetype.c index 488890bd9..230c9fefd 100644 --- a/src/common/filetype.c +++ b/src/common/filetype.c @@ -33,6 +33,7 @@ +#include #include /* common */ @@ -47,26 +48,45 @@ -/* Table that maps extensions to file types */ -static const struct { +/* Table that maps extensions to file types. Sorted alphabetically. */ +typedef struct { const char Ext[4]; - unsigned Type; -} FileTypes [] = { - { "c", FILETYPE_C }, - { "s", FILETYPE_ASM }, - { "asm", FILETYPE_ASM }, - { "a65", FILETYPE_ASM }, - { "o", FILETYPE_OBJ }, - { "obj", FILETYPE_OBJ }, + FILETYPE Type; +} FileType; + +static const FileType TypeTable[] = { + /* Upper case stuff for obsolete operating systems */ + { "A", FILETYPE_LIB }, + { "A65", FILETYPE_ASM }, + { "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 }, - { "lib", FILETYPE_LIB }, - { "grc", FILETYPE_GR }, - { "o65", FILETYPE_O65 }, + { "a65", FILETYPE_ASM }, + { "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 }, }; +#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 * type could not be determined, the function returns FILETYPE_UNKOWN. */ { - unsigned I; + const FileType* FT; /* Determine the file type by the extension */ const char* Ext = FindExt (Name); /* Do we have an extension? */ if (Ext == 0) { - return FILETYPE_UNKNOWN; - } - - /* Skip the dot */ - ++Ext; - - /* Check for known extensions */ - for (I = 0; I < sizeof (FileTypes) / sizeof (FileTypes [0]); ++I) { - if (strcmp (FileTypes [I].Ext, Ext) == 0) { - /* Found */ - return FileTypes [I].Type; - } - } - - /* Not found, return the default */ - return FILETYPE_UNKNOWN; + return FILETYPE_UNKNOWN; + } + + /* Search for a table entry */ + FT = bsearch (Ext+1, TypeTable, FILETYPE_COUNT, sizeof (FileType), Compare); + + /* Return the result */ + return FT? FT->Type : FILETYPE_UNKNOWN; } diff --git a/src/common/filetype.h b/src/common/filetype.h index b21f982d1..9923742cf 100644 --- a/src/common/filetype.h +++ b/src/common/filetype.h @@ -45,7 +45,7 @@ /* File types */ -enum { +typedef enum { FILETYPE_UNKNOWN = -1, /* Unknown file type */ FILETYPE_C, /* C source file */ FILETYPE_ASM, /* Assembler file */ @@ -53,7 +53,7 @@ enum { FILETYPE_LIB, /* Library file */ FILETYPE_GR, /* GEOS resource 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 * type could not be determined, the function returns FILETYPE_UNKOWN. */ -- 2.39.5