OSDN Git Service

Fixed a possible stack overflow in decode_date_str() function + set debugger flags...
[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 #define MUTILS_INC_CONFIG 1
23
24 #include <MUtils/Version.h>
25
26 //Internal
27 #include <MUtils/Global.h>
28 #include <MUtils/Exception.h>
29 #include <MUtils/OSSupport.h>
30 #include "Config.h"
31
32 #ifdef _MSC_VER
33 #define _snscanf(X, Y, Z, ...) _snscanf_s((X), (Y), (Z), __VA_ARGS__)
34 #endif
35
36 ///////////////////////////////////////////////////////////////////////////////
37 // HELPER FUNCTIONS
38 ///////////////////////////////////////////////////////////////////////////////
39
40 static const char *g_months_lut[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
41
42 static int month_str2int(const char *str)
43 {
44         int ret = 0;
45
46         for(int j = 0; j < 12; j++)
47         {
48                 if(!_strcmpi(str, g_months_lut[j]))
49                 {
50                         ret = j+1;
51                         break;
52                 }
53         }
54
55         return ret;
56 }
57
58 static const QDate decode_date_str(const char *const date_str) //Mmm dd yyyy
59 {
60         bool ok = true;
61         int date[3] = {0, 0, 0};
62         char buffer[12];
63
64         strcpy_s(buffer, 12, date_str);
65         buffer[3] = buffer[6] = '\0';
66
67         ok = ok && ((date[1] = month_str2int(&buffer[0])) > 0);
68         ok = ok && (sscanf_s(&buffer[4], "%d", &date[2]) == 1);
69         ok = ok && (sscanf_s(&buffer[7], "%d", &date[0]) == 1);
70
71         if(!ok)
72         {
73                 MUTILS_THROW("Internal error: Date format could not be recognized!");
74         }
75         
76         //qWarning("MUtils::Version::build_date: y=%d, m=%d, d=%d", date[0], date[1], date[2]);
77         return QDate(date[0], date[1], date[2]);
78 }
79
80 static const QTime decode_time_str(const char *const time_str) //hh:mm:ss
81 {
82         bool ok = true;
83         int time[3] = {0, 0, 0};
84         char buffer[9];
85
86         strcpy_s(buffer, 9, time_str);
87         buffer[2] = buffer[5] = '\0';
88
89         ok = ok && (sscanf_s(&time_str[0], "%d", &time[0]) == 1);
90         ok = ok && (sscanf_s(&time_str[3], "%d", &time[1]) == 1);
91         ok = ok && (sscanf_s(&time_str[6], "%d", &time[2]) == 1);
92
93         if(!ok)
94         {
95                 MUTILS_THROW("Internal error: Time format could not be recognized!");
96         }
97
98         //qWarning("MUtils::Version::build_date: h=%d, m=%d, s=%d", time[0], time[1], time[2]);
99         return QTime(time[0], time[1], time[2]);
100 }
101
102 ///////////////////////////////////////////////////////////////////////////////
103 // LIB VERSION
104 ///////////////////////////////////////////////////////////////////////////////
105
106 const QDate MUtils::Version::lib_build_date(void)
107 {
108         static const char *const BUILD_DATE = __DATE__;
109         return decode_date_str(BUILD_DATE);
110 }
111
112 const QTime MUtils::Version::lib_build_time(void)
113 {
114         static const char *const BUILD_TIME = __TIME__;
115         return decode_time_str(BUILD_TIME);
116 }
117
118 const quint32 &MUtils::Version::lib_version_major(void)
119 {
120         static const quint32 VERSION_MAJOR = VER_MUTILS_MAJOR;
121         return VERSION_MAJOR;
122 }
123
124 const quint32 &MUtils::Version::lib_version_minor(void)
125 {
126         static const quint32 VERSION_MINOR = (10 * VER_MUTILS_MINOR_HI) + VER_MUTILS_MINOR_LO;
127         return VERSION_MINOR;
128 }
129
130
131 ///////////////////////////////////////////////////////////////////////////////
132 // APP VERSION
133 ///////////////////////////////////////////////////////////////////////////////
134
135 const QDate MUtils::Version::app_build_date(const char *const date_str)
136 {
137         return decode_date_str(date_str);
138 }
139
140 const QTime MUtils::Version::app_build_time(const char *const time_str)
141 {
142         return decode_time_str(time_str);
143 }
144
145 ///////////////////////////////////////////////////////////////////////////////