zoneload.cc
musicmud-2.1.6/src/zoneload.cc · 2.9 KB
From the archive musicmud2.1.6.tar.gz
Download the archive « Back to the archive contents
/*
* MusicMUD Daemon, version 1.0
* Copyright (C) 1998-2003 Abigail Brady
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include "musicmud.h"
#include "zoneload.h"
#include "flags.h"
#include "pflags.h"
#include "paths.h"
string getline(FILE *f) {
string s;
while (1) {
int ch = getc(f);
if (ch == EOF)
return s;
if (ch == '\n')
return s;
s += ch;
}
}
string getproperty(FILE *who) {
string buffer;
int quotes = 0;
while (quotes!=2) {
int value = fgetc(who);
if (value == EOF) return buffer;
if (value=='"')
quotes++;
if (value=='\n' && quotes!=1) {
break;
}
if (value=='\\') {
int value = fgetc(who);
if (value == EOF) return buffer;
buffer += value;
continue;
}
buffer += value;
}
return buffer;
}
void load_newzone(const char *zonename) {
FILE *f = xopen(DATA_WORLD, zonename, "r");
if (!f) {
log(PFL_SEEINFO, 0, "zones", "can't load zone : %s", zonename);
return;
}
string lastid;
string buffer;
while (!feof(f)) {
buffer = getline(f);
if (feof(f)) break;
if (buffer.substr(0, 10)=="mudobject ")
{
string id = buffer.substr(buffer.find('"')+1);
id = id.substr(0, id.find('"'));
if (planet->get(id.c_str())) {
log(PFL_SEEINFO, 0, "zoneload", "corrupt zone file : object %s repeated", id.c_str());
while (!feof(f) && buffer != "}")
buffer = getproperty(f);
} else {
lastid = id;
MudObject *o = new MudObject(id.c_str());
planet->add(*o);
while (buffer != "}") {
buffer = getproperty(f);
if (feof(f))
break;
o->load(buffer.c_str());
}
if (const char *s = o->get("!owner")) {
if (!o->get("start"))
o->set("start", s);
o->unset("!owner");
}
if (!o->get("zone"))
o->set("zone", zonename);
}
} else {
log(PFL_SEEINFO, 0, "zoneload", "error in zone file : %s (%s)", buffer.c_str(), lastid.c_str());
}
}
xclose(f);
}
void load_zones() {
FILE *f = xopen(DATA_WORLD, "index", "r");
if (!f)
return;
do {
string zone = getline(f);
if (!feof(f)) {
load_newzone(zone.c_str());
}
} while (!feof(f));
xclose(f);
}