OSDN Git Service

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