OSDN Git Service

a5a40b5e70227a2fbe244402c0b5db3bff34e3e7
[mutilities/MUtilities.git] / src / Version.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #pragma once
23
24 #include <MUtils/Version.h>
25
26 #include <MUtils/Global.h>
27 #include <MUtils/Exception.h>
28
29 #ifdef _MSC_VER
30 #define _snscanf(X, Y, Z, ...) _snscanf_s((X), (Y), (Z), __VA_ARGS__)
31 #endif
32
33 ///////////////////////////////////////////////////////////////////////////////
34
35 static const char *g_months_lut[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
36
37 static int month2int(const char *str)
38 {
39         int ret = 0;
40
41         for(int j = 0; j < 12; j++)
42         {
43                 if(!_strcmpi(str, g_months_lut[j]))
44                 {
45                         ret = j+1;
46                         break;
47                 }
48         }
49
50         return ret;
51 }
52
53 const QDate MUtils::Version::build_date(const char *const date_str)
54 {
55         bool ok = true;
56         int date[3] = {0, 0, 0};
57         char month_s[4];
58
59         ok = ok && (_snscanf(&date_str[0x0], 3, "%s", &month_s) == 1);
60         ok = ok && ((date[1] = month2int(month_s)) > 0);
61         ok = ok && (_snscanf(&date_str[0x4], 2, "%d", &date[0]) == 1);
62         ok = ok && (_snscanf(&date_str[0x7], 4, "%d", &date[2]) == 1);
63
64         if(!ok)
65         {
66                 MUTILS_THROW("Internal error: Date format could not be recognized!");
67         }
68         
69         return QDate(date[0], date[1], date[2]);
70 }
71
72 static const QTime build_time(const char *const time_str)
73 {
74         bool ok = true;
75         int time[3] = {0, 0, 0};
76
77         ok = ok && (_snscanf(&time_str[0x0], 2, "%d", &time[0]) == 1);
78         ok = ok && (_snscanf(&time_str[0x3], 2, "%d", &time[1]) == 1);
79         ok = ok && (_snscanf(&time_str[0x6], 2, "%d", &time[2]) == 1);
80
81         if(!ok)
82         {
83                 MUTILS_THROW("Internal error: Time format could not be recognized!");
84         }
85
86         return QTime(time[0], time[1], time[2]);
87 }
88
89 ///////////////////////////////////////////////////////////////////////////////