OSDN Git Service

eb363b90afbf031f34045adb9d79ce7c48cfc612
[csp-qt/common_source_project-fm7.git] / source / src / common.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2013.01.17-
6
7         [ common ]
8 */
9 #if defined(_USE_QT)
10 #include <string.h>
11 #include "common.h"
12 #include "config.h"
13 #else
14 # include <windows.h>
15 # include <shlwapi.h>
16 #pragma comment(lib, "shlwapi.lib")
17 #include "common.h"
18 #endif
19
20
21 #include "fileio.h"
22
23 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(Q_OS_WIN)
24 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource)
25 {
26         _tcscpy(strDestination, strSource);
27         return 0;
28 }
29
30 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context)
31 {
32         return _tcstok(strToken, strDelimit);
33 }
34
35 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...)
36 {
37         va_list ap;
38         va_start(ap, format);
39         int result = _vstprintf(buffer, format, ap);
40         va_end(ap);
41         return result;
42 }
43
44 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr)
45 {
46         return _vstprintf(buffer, format, argptr);
47 }
48 #endif
49
50 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext)
51 {
52 #if defined(_USE_AGAR)
53         int nam_len = strlen(file_path);
54         int ext_len = strlen(ext);
55         
56         return (nam_len >= ext_len && strncmp(&file_path[nam_len - ext_len], ext, ext_len) == 0);
57 #elif defined(_USE_QT)
58         QString s_fpath = file_path;
59         QString s_ext = ext;
60         bool f = false;
61         s_fpath = s_fpath.toUpper();
62         s_ext = s_ext.toUpper();
63         if(s_fpath.length() < s_ext.length()) return false;
64         s_fpath = s_fpath.right(s_ext.length());
65         if(s_fpath == s_ext) return true;
66         return false;
67 #else
68         int nam_len = _tcslen(file_path);
69         int ext_len = _tcslen(ext);
70         
71         return (nam_len >= ext_len && _tcsncicmp(&file_path[nam_len - ext_len], ext, ext_len) == 0);
72 #endif
73 }
74
75 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path)
76 {
77         static _TCHAR path[_MAX_PATH];
78         
79 #if defined(_USE_AGAR) || defined(_USE_QT)
80         _TCHAR *p1,  *p2;
81         static _TCHAR p3[_MAX_PATH];
82         strcpy(path, file_path);
83         p1 = (_TCHAR *)strrchr(path, '/');
84         p2 = (_TCHAR *)strrchr(path, '.');
85         
86         if(p2 == NULL) {
87                 return path;
88         } else if(p1 == NULL) {
89                 strncpy(p3, path, p2 - path);
90                 return p3;
91         } else if(p1 > p2) {
92                 return path;
93         } else {
94                 strncpy(p3, path, p2 - path);
95                 return p3;
96         }
97    
98    
99    
100 #else
101         _tcscpy_s(path, _MAX_PATH, file_path);
102         PathRemoveExtension(path);
103         return path;
104 #endif
105 }
106
107 uint32 getcrc32(uint8 data[], int size)
108 {
109         static bool initialized = false;
110         static uint32 table[256];
111         
112         if(!initialized) {
113                 for(int i = 0; i < 256; i++) {
114                         uint32 c = i;
115                         for(int j = 0; j < 8; j++) {
116                                 if(c & 1) {
117                                         c = (c >> 1) ^ 0xedb88320;
118                                 } else {
119                                         c >>= 1;
120                                 }
121                         }
122                         table[i] = c;
123                 }
124                 initialized = true;
125         }
126         
127         uint32 c = ~0;
128         for(int i = 0; i < size; i++) {
129                 c = table[(c ^ data[i]) & 0xff] ^ (c >> 8);
130         }
131         return ~c;
132 }
133
134 void cur_time_t::increment()
135 {
136         if(++second >= 60) {
137                 second = 0;
138                 if(++minute >= 60) {
139                         minute = 0;
140                         if(++hour >= 24) {
141                                 hour = 0;
142                                 // days in this month
143                                 int days = 31;
144                                 if(month == 2) {
145                                         days = LEAP_YEAR(year) ? 29 : 28;
146                                 } else if(month == 4 || month == 6 || month == 9 || month == 11) {
147                                         days = 30;
148                                 }
149                                 if(++day > days) {
150                                         day = 1;
151                                         if(++month > 12) {
152                                                 month = 1;
153                                                 year++;
154                                         }
155                                 }
156                                 if(++day_of_week >= 7) {
157                                         day_of_week = 0;
158                                 }
159                         }
160                 }
161         }
162 }
163
164 void cur_time_t::update_year()
165 {
166         // 1970-2069
167         if(year < 70) {
168                 year += 2000;
169         } else if(year < 100) {
170                 year += 1900;
171         }
172 }
173
174 void cur_time_t::update_day_of_week()
175 {
176         static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
177         int y = year - (month < 3);
178         day_of_week = (y + y / 4 - y / 100 + y / 400 + t[month - 1] + day) % 7;
179 }
180
181 #define STATE_VERSION   1
182
183 void cur_time_t::save_state(void *f)
184 {
185         FILEIO *state_fio = (FILEIO *)f;
186         
187         state_fio->FputUint32(STATE_VERSION);
188         
189         state_fio->FputInt32(year);
190         state_fio->FputInt32(month);
191         state_fio->FputInt32(day);
192         state_fio->FputInt32(day_of_week);
193         state_fio->FputInt32(hour);
194         state_fio->FputInt32(minute);
195         state_fio->FputInt32(second);
196         state_fio->FputBool(initialized);
197 }
198
199 bool cur_time_t::load_state(void *f)
200 {
201         FILEIO *state_fio = (FILEIO *)f;
202         
203         if(state_fio->FgetUint32() != STATE_VERSION) {
204                 return false;
205         }
206         year = state_fio->FgetInt32();
207         month = state_fio->FgetInt32();
208         day = state_fio->FgetInt32();
209         day_of_week = state_fio->FgetInt32();
210         hour = state_fio->FgetInt32();
211         minute = state_fio->FgetInt32();
212         second = state_fio->FgetInt32();
213         initialized = state_fio->FgetBool();
214         return true;
215 }
216