OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / libfiletype / drd64_libfiletype_common.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBFILETYPE_COMMON
38 #include"drd64_libfiletype.h"
39
40 #define DRD64_SRC_LIBFILETYPE_COMMON_SRCID      0x01
41
42 #define LOCATION(n)     DRD64_ERR_LOCATION( \
43                         DRD64_ERROR_MODULE_LIBFILETYPE, DRD64_ERROR_ARCH_NODEPEND, \
44                         DRD64_SRC_LIBFILETYPE_COMMON_SRCID, (n))
45
46
47 /*----------------------------------------------------------------------
48 ----------------------------------------------------------------------*/
49 #define FUNCID_LibFileType_Common_GetFileNamePosition   0x01
50 char *
51         LibFileType_Common_GetFileNamePosition(
52                 char    *pstr_filepath )
53 {
54         int             i_maxlen;
55         int             i_len           = 0;
56         char    *pstr_now;
57
58         assert( NULL != pstr_filepath );
59
60         i_maxlen        = (int)strnlen( pstr_filepath, DRD64_MAX_PATH );
61         pstr_now        = pstr_filepath + i_maxlen;
62         for( i_len = i_maxlen; i_len > 0; i_len-- )             {
63                 if( '/' == *pstr_now-- )        { break; }
64         }
65         
66         if( 1 > i_len )         { pstr_now      = pstr_filepath + i_maxlen; }
67         else                            { pstr_now      += 2; }
68
69         return pstr_now;
70 }
71
72
73 /*----------------------------------------------------------------------
74 ----------------------------------------------------------------------*/
75 #define FUNCID_LibFileType_Common_GetFileExtPosition    0x02
76 char *
77         LibFileType_Common_GetFileExtPosition(
78                 char    *pstr_filepath )
79 {
80         int             i_maxlen;
81         int             i_len           = 0;
82         char    *pstr_now;
83
84         assert( NULL != pstr_filepath );
85
86         i_maxlen        = (int)strnlen( pstr_filepath, DRD64_MAX_PATH );
87         pstr_now        = pstr_filepath + i_maxlen;
88         for( i_len = i_maxlen; i_len > 0; i_len-- )             {
89                 if( '.' == *pstr_now-- )        { break; }
90         }
91         
92         if( 1 > i_len )         { pstr_now      = pstr_filepath + i_maxlen; }
93         else                            { pstr_now      += 2; }
94
95         return pstr_now;
96 }
97
98
99 /***********************************************************************
100 ***********************************************************************/
101 #define FUNCID_LibFileType_Common_GetFileType_fromFileName              0x04
102 LIBFILETYPE_FUNC
103 int
104         LibFileType_GetFileType_fromFileName(
105                 LibFileType_FileType    *p_filetype,
106                 char    *pstr_filepath )
107 {
108         char    *pstr_ext;
109         char    *pstr_name;
110         int             i_cnt;
111
112         /* Search from Ext. DB --- */
113         pstr_ext        = LibFileType_Common_GetFileExtPosition( pstr_filepath );
114         i_cnt   = 0;
115         do      {
116                 if( !strncmp( pstr_ext, fexttypedb[i_cnt].str_ext, DRD64_MAX_PATH ) )
117                                 {
118                         p_filetype->b_type              = fexttypedb[i_cnt].b_type;
119                         p_filetype->w_subtype   = fexttypedb[i_cnt].w_subtype;
120                         goto goto_LibFileType_Common_GetFileType_fromFileName_post;
121                 }
122         }while( (Byte)0xff != fexttypedb[++i_cnt].b_type );
123
124         /* Judge of Special File-Name */
125         pstr_name       = LibFileType_Common_GetFileNamePosition( pstr_filepath );
126         i_cnt   = 0;
127         do      {
128                 if( !strncmp( pstr_name, fnametypedb[i_cnt].str_filename, DRD64_MAX_PATH ) )
129                                 {
130                         p_filetype->b_type              = fnametypedb[i_cnt].b_type;
131                         p_filetype->w_subtype   = fnametypedb[i_cnt].w_subtype;
132                         goto goto_LibFileType_Common_GetFileType_fromFileName_post;
133                 }
134         }while( (Byte)0xff != fnametypedb[++i_cnt].b_type );
135                         
136         p_filetype->b_type              = DRD64_LIBFILETYPE_BINARY;
137         p_filetype->w_subtype   = DRD64_LIBFILETYPE_SUB_BINARY;
138         
139 goto_LibFileType_Common_GetFileType_fromFileName_post:
140         return 0x00;
141 }
142
143
144 /* EOF of drd64_.c ----------------------------------- */