amxmodx/amxmodx/CVault.cpp

186 lines
2.5 KiB
C++
Raw Normal View History

2014-08-04 03:36:20 -05:00
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
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 "CFileSystem.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
{
(*a)->value = 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
{
if (strcmp((*a)->key.chars(), 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
return b->value.chars();
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;
2005-09-16 23:48:51 +00:00
head = a;
2004-01-31 20:56:22 +00:00
}
}
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
{
path = 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
{
if (!path.length())
{
return false;
}
2004-01-31 20:56:22 +00:00
clear();
FILE *fp = fopen(path.chars(), "r");
2004-01-31 20:56:22 +00:00
if (!fp)
{
return false;
}
2004-01-31 20:56:22 +00:00
char lineRead[512];
char key[sizeof(lineRead) + 1];
char value[sizeof(lineRead) + 1];
2004-01-31 20:56:22 +00:00
while (fgets(lineRead, sizeof(lineRead), fp))
2004-01-31 20:56:22 +00:00
{
UTIL_TrimLeft(lineRead);
if (!*lineRead || *lineRead == ';')
{
continue;
}
sscanf(lineRead, "%s%*[ \t]%[^\n]", key, value);
2005-09-10 20:09:14 +00:00
if (isalpha(*key))
{
2005-09-10 20:09:14 +00:00
put(key, value);
}
2004-01-31 20:56:22 +00:00
}
fclose(fp);
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
{
if (!path.length())
{
return false;
}
2004-01-31 20:56:22 +00:00
FILE *fp = fopen(path.chars(), "w");
2004-01-31 20:56:22 +00:00
if (!fp)
{
return false;
}
2004-01-31 20:56:22 +00:00
fputs("; Don't modify!\n", fp);
2004-01-31 20:56:22 +00:00
2005-09-10 20:09:14 +00:00
for (Obj* b = head; b; b = b->next)
{
fprintf(fp, "%s\t%s\n", b->key.chars(), b->value.chars());
}
fclose(fp);
2004-01-31 20:56:22 +00:00
return true;
}