Files
2025-03-05 08:37:43 +01:00

278 lines
6.1 KiB
C

/*
* PVVMUD a 3D MUD
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "gamevalue.H"
CGameValueObject::CGameValueObject() {
type=GV_INVALID;
value._dword=0;
_ptr=NULL;
}
CGameValueObject::~CGameValueObject() {
type=GV_INVALID;
value._dword=0;
_ptr=NULL;
}
CGameValue::CGameValue() {};
CGameValue::~CGameValue() {
valhash::iterator i;
for(i=hash.begin();i!=hash.end();i++) delete (*i).second;
hash.clear();
}
void CGameValue::add(char *name, BYTE *value) {
CGameValueObject *gvo;
gvo = new CGameValueObject;
gvo->type = GV_BYTE;
gvo->value._byte = *value;
hash[name] = gvo;
}
void CGameValue::add(char *name, WORD *value) {
CGameValueObject *gvo;
gvo = new CGameValueObject;
gvo->type = GV_WORD;
gvo->value._word = *value;
hash[name] = gvo;
}
void CGameValue::add(char *name, DWORD *value) {
CGameValueObject *gvo;
gvo = new CGameValueObject;
gvo->type = GV_DWORD;
gvo->value._byte = *value;
hash[name] = gvo;
}
void CGameValue::add(char *name, float *value) {
CGameValueObject *gvo;
gvo = new CGameValueObject;
gvo->type = GV_FLOAT;
gvo->value._float = *value;
hash[name] = gvo;
}
void CGameValue::add(char *name, double *value) {
CGameValueObject *gvo;
gvo = new CGameValueObject;
gvo->type = GV_DOUBLE;
gvo->value._double = *value;
hash[name] = gvo;
}
void CGameValue::add(char *name, char *data, int *length) {
CGameValueObject *gvo;
gvo = new CGameValueObject;
gvo->type = GV_STRING;
gvo->value._dword = (DWORD) *length;
gvo->_ptr = data;
hash[name] = gvo;
}
bool CGameValue::get(char *name, BYTE *value) {
CGameValueObject *gvo;
gvo = hash[name];
*value = gvo->value._byte;
if (gvo->type != GV_BYTE) {
return(FALSE);
} else {
return(TRUE);
};
}
bool CGameValue::get(char *name, WORD *value) {
CGameValueObject *gvo;
gvo = hash[name];
*value = gvo->value._word;
if (gvo->type != GV_WORD) {
return(FALSE);
} else {
return(TRUE);
};
}
bool CGameValue::get(char *name, DWORD *value) {
CGameValueObject *gvo;
gvo = hash[name];
*value = gvo->value._dword;
if (gvo->type != GV_DWORD) {
return(FALSE);
} else {
return(TRUE);
};
}
bool CGameValue::get(char *name, float *value) {
CGameValueObject *gvo;
gvo = hash[name];
*value = gvo->value._float;
if (gvo->type != GV_FLOAT) {
return(FALSE);
} else {
return(TRUE);
};
}
bool CGameValue::get(char *name, double *value) {
CGameValueObject *gvo;
gvo = hash[name];
*value = gvo->value._double;
if (gvo->type != GV_DOUBLE) {
return(FALSE);
} else {
return(TRUE);
};
}
bool CGameValue::get(char *name, char *data, int *length) {
CGameValueObject *gvo;
gvo = hash[name];
*length = gvo->value._dword;
data = gvo->_ptr;
if (gvo->type != GV_STRING) {
return(FALSE);
} else {
return(TRUE);
};
}
bool CGameValue::load(iostream &readFile) {
char name[256];
int type;
union {
BYTE _byte;
WORD _word;
DWORD _dword;
float _float;
double _double;
} value;
char *_ptr;
valhash::iterator i;
for(i=hash.begin();i!=hash.end();i++) delete (*i).second;
hash.clear();
while (!readFile.eof()) {
readFile >> name;
if (!strcmp(name, "[\\Values]")) break;
readFile >> type;
switch (type) {
case GV_BYTE:
readFile >> value._byte;
add(name,value._byte);
break;
case GV_WORD:
readFile >> value._word;
add(name,value._word);
break;
case GV_DWORD:
readFile >> value._dword;
add(name,value._dword);
break;
case GV_FLOAT:
readFile >> value._float;
add(name,value._float);
break;
case GV_DOUBLE:
readFile >> value._double;
add(name,value._double);
break;
case GV_STRING:
readFile >> value._dword;
_ptr = new string();
for (int n=0;n<value._dword;n++){
char ch;
readFile >> ch;
_ptr += ch;
}
add(name,value._dword,_ptr);
_ptr=NULL;
break;
default:
cout << "Failed : Unknown type of variable";
return FALSE;
break;
};
};
return TRUE;
}
bool CGameValue::save(iostream &writefile) {
writeFile << "[Values]" << newln;
valhash::iterator i;
for(i=hash.begin();i!=hash.end();i++) {
writeFile << (*i).first() << (*i).second->type;
switch((*i).second->type) {
case GV_BYTE:
writeFile << (*i).second->value._byte;
break;
case GV_WORD:
writeFile << (*i).second->value._word;
break;
case GV_DWORD:
writeFile << (*i).second->value._dword;
break;
case GV_FLOAT:
writeFile << (*i).second->value._float;
break;
case GV_DOUBLE:
writeFile << (*i).second->value._double;
break;
case GV_STRING:
writeFile << (*i).second->value._dword << newln;
for (int n=0;n<(*i).second->value._dword;n++){
writeFile << *((*i).second->_ptr+n);
}
writeFile << newln;
break;
default:
break;
};
};
writeFile << "[\\Values]" << newln;
return TRUE;
}