amxmodx/amxmodx/CVault.cpp

179 lines
3.2 KiB
C++
Raw Normal View History

2004-03-05 21:03:14 +00:00
/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* 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
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
2004-01-31 20:56:22 +00:00
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
2004-04-22 07:52:26 +00:00
#include "amxmodx.h"
#include "CVault.h"
#include "CFile.h"
2004-01-31 20:56:22 +00:00
// *****************************************************
// class Vault
// *****************************************************
2005-09-10 20:09:14 +00:00
bool Vault::exists(const char* k)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (*k == 0) return false;
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
return *find(k) != 0;
2004-01-31 20:56:22 +00:00
}
2005-09-10 20:09:14 +00:00
void Vault::put(const char* k, const char* v)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (*k == 0) return;
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (*v == 0)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
remove(k);
2004-01-31 20:56:22 +00:00
return;
}
2005-09-10 20:09:14 +00:00
Obj** a = find(k);
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (*a)
2004-01-31 20:56:22 +00:00
{
2004-08-13 08:46:04 +00:00
(*a)->value.assign(v);
2005-09-10 20:09:14 +00:00
(*a)->number = atoi(v);
2004-01-31 20:56:22 +00:00
}
else
2005-09-10 20:09:14 +00:00
*a = new Obj(k, v);
2004-01-31 20:56:22 +00:00
}
2005-09-10 20:09:14 +00:00
Vault::Obj::Obj(const char* k, const char* v): key(k), value(v), next(0)
{
2004-01-31 20:56:22 +00:00
number = atoi(v);
}
2005-09-10 20:09:14 +00:00
Vault::Obj** Vault::find(const char* n)
2004-01-31 20:56:22 +00:00
{
Obj** a = &head;
2005-09-10 20:09:14 +00:00
while (*a)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (strcmp((*a)->key.c_str(), n) == 0)
2004-01-31 20:56:22 +00:00
return a;
a = &(*a)->next;
}
return a;
}
2005-09-10 20:09:14 +00:00
int Vault::get_number(const char* n)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (*n == 0) return 0;
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
Obj* b = *find(n);
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (b == 0) return 0;
2004-01-31 20:56:22 +00:00
return b->number;
}
2005-09-10 20:09:14 +00:00
const char* Vault::get(const char* n)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (*n == 0) return "";
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
Obj* b = *find(n);
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (b == 0) return "";
2004-01-31 20:56:22 +00:00
2004-08-13 08:46:04 +00:00
return b->value.c_str();
2004-01-31 20:56:22 +00:00
}
void Vault::clear()
{
2005-09-10 20:09:14 +00:00
while (head)
2004-01-31 20:56:22 +00:00
{
Obj* a = head->next;
delete head;
head = a;
}
}
2005-09-10 20:09:14 +00:00
void Vault::remove(const char* n)
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
Obj** b = find(n);
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (*b == 0) return;
2004-01-31 20:56:22 +00:00
Obj* a = (*b)->next;
delete *b;
*b = a;
}
2005-09-10 20:09:14 +00:00
void Vault::setSource(const char* n)
2004-01-31 20:56:22 +00:00
{
2004-08-13 08:46:04 +00:00
path.assign(n);
2004-01-31 20:56:22 +00:00
}
2005-09-10 20:09:14 +00:00
bool Vault::loadVault()
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (path.empty()) return false;
2004-01-31 20:56:22 +00:00
clear();
2005-09-10 20:09:14 +00:00
File a(path.c_str(), "r");
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (!a) return false;
2004-01-31 20:56:22 +00:00
const int sz = 512;
2005-09-10 20:09:14 +00:00
char value[sz + 1];
char key[sz + 1];
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
while (a >> key && a.skipWs() && a.getline(value, sz))
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (isalpha(*key))
put(key, value);
2004-01-31 20:56:22 +00:00
}
return true;
}
2005-09-10 20:09:14 +00:00
bool Vault::saveVault()
2004-01-31 20:56:22 +00:00
{
2005-09-10 20:09:14 +00:00
if (path.empty()) return false;
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
File a(path.c_str(), "w");
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
if (!a) return false;
2004-01-31 20:56:22 +00:00
a << "; Don't modify!" << '\n';
2005-09-10 20:09:14 +00:00
for (Obj* b = head; b; b = b->next)
2004-01-31 20:56:22 +00:00
a << b->key << '\t' << b->value << '\n';
return true;
}