Files
scummvm/engines/wintermute/base/scriptables/script_ext_date.cpp
T
Einar Johan Trøan Sømåen 5683f07633 WINTERMUTE: Rename CamelCased filenames to prefixed_under_score-filenames
This is mostly a lead-up to namespacing the Ad/Base folders, and then
possibly removing the prefixes from the files, it also has the added
benefit of getting rid of the odd case-typos that makes for issues
on platforms that don't ignore case.
2012-07-21 19:15:33 +02:00

298 lines
9.5 KiB
C++

/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This file is based on WME Lite.
* http://dead-code.org/redir.php?target=wmelite
* Copyright (c) 2011 Jan Nedoma
*/
#include "engines/wintermute/base/scriptables/script_stack.h"
#include "engines/wintermute/base/scriptables/script_value.h"
#include "engines/wintermute/base/scriptables/script_ext_date.h"
namespace WinterMute {
IMPLEMENT_PERSISTENT(CSXDate, false)
CBScriptable *makeSXDate(CBGame *inGame, CScStack *stack) {
return new CSXDate(inGame, stack);
}
//////////////////////////////////////////////////////////////////////////
CSXDate::CSXDate(CBGame *inGame, CScStack *stack): CBScriptable(inGame) {
stack->correctParams(6);
memset(&_tm, 0, sizeof(_tm));
CScValue *valYear = stack->pop();
_tm.tm_year = valYear->getInt() - 1900;
_tm.tm_mon = stack->pop()->getInt() - 1;
_tm.tm_mday = stack->pop()->getInt();
_tm.tm_hour = stack->pop()->getInt();
_tm.tm_min = stack->pop()->getInt();
_tm.tm_sec = stack->pop()->getInt();
if (valYear->isNULL()) {
g_system->getTimeAndDate(_tm);
}
}
//////////////////////////////////////////////////////////////////////////
CSXDate::~CSXDate() {
}
//////////////////////////////////////////////////////////////////////////
const char *CSXDate::scToString() {
// TODO: Make this more stringy, and less ISO 8601-like
_strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
return _strRep.c_str();
#if 0
return asctime(&_tm);
#endif
}
//////////////////////////////////////////////////////////////////////////
bool CSXDate::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) {
//////////////////////////////////////////////////////////////////////////
// GetYear
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "GetYear") == 0) {
stack->correctParams(0);
stack->pushInt(_tm.tm_year + 1900);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetMonth
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetMonth") == 0) {
stack->correctParams(0);
stack->pushInt(_tm.tm_mon + 1);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetDate
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetDate") == 0) {
stack->correctParams(0);
stack->pushInt(_tm.tm_mday);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetHours
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetHours") == 0) {
stack->correctParams(0);
stack->pushInt(_tm.tm_hour);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetMinutes
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetMinutes") == 0) {
stack->correctParams(0);
stack->pushInt(_tm.tm_min);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetSeconds
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetSeconds") == 0) {
stack->correctParams(0);
stack->pushInt(_tm.tm_sec);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// GetWeekday
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "GetWeekday") == 0) {
stack->correctParams(0);
warning("GetWeekday returns a wrong value on purpose");
stack->pushInt(_tm.tm_mday % 7);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetYear
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetYear") == 0) {
stack->correctParams(1);
_tm.tm_year = stack->pop()->getInt() - 1900;
stack->pushNULL();
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetMonth
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetMonth") == 0) {
stack->correctParams(1);
_tm.tm_mon = stack->pop()->getInt() - 1;
stack->pushNULL();
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetDate
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetDate") == 0) {
stack->correctParams(1);
_tm.tm_mday = stack->pop()->getInt();
stack->pushNULL();
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetHours
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetHours") == 0) {
stack->correctParams(1);
_tm.tm_hour = stack->pop()->getInt();
stack->pushNULL();
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetMinutes
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetMinutes") == 0) {
stack->correctParams(1);
_tm.tm_min = stack->pop()->getInt();
stack->pushNULL();
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetSeconds
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetSeconds") == 0) {
stack->correctParams(1);
_tm.tm_sec = stack->pop()->getInt();
stack->pushNULL();
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// SetCurrentTime
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "SetCurrentTime") == 0) {
stack->correctParams(0);
g_system->getTimeAndDate(_tm);
stack->pushNULL();
return STATUS_OK;
}
else
return STATUS_FAILED;
}
//////////////////////////////////////////////////////////////////////////
CScValue *CSXDate::scGetProperty(const char *name) {
_scValue->setNULL();
//////////////////////////////////////////////////////////////////////////
// Type
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "Type") == 0) {
_scValue->setString("date");
return _scValue;
}
else return _scValue;
}
//////////////////////////////////////////////////////////////////////////
bool CSXDate::scSetProperty(const char *name, CScValue *value) {
/*
//////////////////////////////////////////////////////////////////////////
// Name
//////////////////////////////////////////////////////////////////////////
if(strcmp(name, "Name")==0){
setName(value->getString());
return STATUS_OK;
}
else*/ return STATUS_FAILED;
}
//////////////////////////////////////////////////////////////////////////
bool CSXDate::persist(CBPersistMgr *persistMgr) {
CBScriptable::persist(persistMgr);
persistMgr->transfer(TMEMBER(_tm.tm_year));
persistMgr->transfer(TMEMBER(_tm.tm_mon));
persistMgr->transfer(TMEMBER(_tm.tm_mday));
persistMgr->transfer(TMEMBER(_tm.tm_hour));
persistMgr->transfer(TMEMBER(_tm.tm_min));
persistMgr->transfer(TMEMBER(_tm.tm_sec));
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
int CSXDate::scCompare(CBScriptable *Value) {
TimeDate time1 = _tm;
TimeDate time2 = ((CSXDate *)Value)->_tm;
if (time1.tm_year < time2.tm_year) {
return -1;
} else if (time1.tm_year == time2.tm_year) {
if (time1.tm_mon < time2.tm_mon) {
return -1;
} else if (time1.tm_mon == time2.tm_mon) {
if (time1.tm_mday < time2.tm_mday) {
return -1;
} else if (time1.tm_mday == time2.tm_mday) {
if (time1.tm_hour < time2.tm_hour) {
return -1;
} else if (time1.tm_hour == time2.tm_hour) {
if (time1.tm_min < time2.tm_min) {
return -1;
} else if (time1.tm_min == time2.tm_min) {
if (time1.tm_sec < time2.tm_sec) {
return -1;
} else if (time1.tm_sec == time2.tm_sec) {
return 0; // Equal
} else {
return 1; // Sec
}
} else {
return 1; // Minute
}
} else {
return 1; // Hour
}
} else {
return 1; // Day
}
} else {
return 1; // Month
}
} else {
return 1; // Year
}
}
} // end of namespace WinterMute