Move EventMgr to GUI.
[gemrb.git] / gemrb / core / Calendar.cpp
blob435c95b57f09b66f0eb165ca54ba3c2c94f35074
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2009 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "Calendar.h"
23 #include "win32def.h"
25 #include "Interface.h"
26 #include "TableMgr.h"
27 #include "Variables.h"
29 Calendar::Calendar(void)
31 int i;
32 AutoTable tab("months");
33 if (!tab) {
34 monthnamecount=-1;
35 return;
37 monthnamecount = tab->GetRowCount();
38 monthnames = (int *) malloc(sizeof(int) * monthnamecount);
39 days = (int *) malloc(sizeof(int) * monthnamecount);
40 daysinyear=0;
41 for(i=0;i<monthnamecount;i++) {
42 days[i]=atoi(tab->QueryField(i,0));
43 daysinyear+=days[i];
44 monthnames[i]=atoi(tab->QueryField(i,1));
48 Calendar::~Calendar(void)
50 if (monthnames) free(monthnames);
51 if (days) free(days);
54 void Calendar::GetMonthName(int dayandmonth) const
56 int month=1;
58 for(int i=0;i<monthnamecount;i++) {
59 if (dayandmonth<days[i]) {
60 char *tmp;
62 core->GetTokenDictionary()->SetAtCopy("DAY", dayandmonth+1);
64 tmp = core->GetString( monthnames[i] );
65 core->GetTokenDictionary()->SetAt("MONTHNAME",tmp);
66 //must not free tmp, SetAt doesn't copy the pointer!
68 core->GetTokenDictionary()->SetAtCopy("MONTH", month);
69 return;
71 dayandmonth-=days[i];
72 //ignoring single days (they are not months)
73 if (days[i]!=1) month++;
77 int Calendar::GetCalendarDay(int date) const
79 int dayandmonth;
80 int month=1;
82 if (!daysinyear) return 0;
83 dayandmonth = date%daysinyear;
84 for(int i=0;i<monthnamecount;i++) {
85 if (dayandmonth<days[i]) {
86 break;
88 dayandmonth-=days[i];
89 //ignoring single days (they are not months)
90 if (days[i]!=1) month++;
92 return dayandmonth+1;