musicio.cc
musicmud-2.1.6/src/musicio.cc · 3.1 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 <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "musicmud.h"
#include "pflags.h"
#include "musicio.h"
#undef xopen
#undef xopen_tmp
#undef xreg
#define MUSIC_FDS 1024
namespace musicio {
FileInfo *fds[MUSIC_FDS];
bool io_started = false;
static void xstart() {
if (!io_started) {
for (int i = 0; i < MUSIC_FDS; i++)
fds[i] = 0;
io_started = true;
}
}
FILE *xopen(const char *d, const char *n, const char *mode, const char *w, int i) {
xstart();
char thing[4096];
sprintf(thing, "%s/%s", d, n);
struct stat buf;
int e = stat(thing, &buf);
if (e ==0 && S_ISDIR(buf.st_mode)) {
#if 0
log(PFL_SEEINFO, 0, "xopen", "xopen on directory \"%s\" (%x)", thing, buf.st_mode);
#endif
return 0;
}
if (!streq(d, ".")) {
if (strchr(n, '/')) {
#if 0
log(PFL_SEEINFO, 0, "xopen", "xopen(\"%s\" \"%s\") failing", d, n);
#endif
return 0;
}
}
FILE *f = fopen(thing, mode);
if (f) {
int fd = fileno(f);
FileInfo *o = new FileInfo();
o->set("filename", thing);
o->set("sourcefile", w);
o->set("lineno", i);
fds[fd] = o;
}
return f;
}
void xclose(FILE *f) {
if (f) {
int fd = fileno(f);
if (fds[fd])
delete fds[fd];
fds[fd] = 0;
}
fclose(f);
}
void xreg(int fd, const char *txt, const char *w, int i) {
xstart();
if (fds[fd]) {
delete fds[fd];
fds[fd] = 0;
}
FileInfo *o = new FileInfo();
o->set("filename", txt);
o->set("sourcefile", w);
o->set("lineno", i);
fds[fd] = o;
}
void xunreg(int fd) {
if (fds[fd])
delete fds[fd];
fds[fd] = 0;
}
void xselect(int fd) {
fds[fd]->toselect = 1;
}
int makefdset(fd_set *set) {
int max = 1;
FD_ZERO(set);
for (int i=0;i<MUSIC_FDS;i++) {
if (fds[i] && fds[i]->toselect) {
FD_SET(i, set);
max = i + 1;
}
}
return max;
}
XFILE *xopen_tmp(const char *a, const char *b, const char *c, const char *d, int e)
{
string f = b;
f += "-tmp";
return xopen(a, f.c_str(), c, d, e);
}
void xclose_confirm(XFILE *xf, const char *b, const char *c)
{
xclose(xf);
string from = b;
from += "/";
from += c;
string to = from;
from += "-tmp";
rename(from.c_str(), to.c_str());
}
void xunlink(const char *d, const char *f)
{
string n = d;
n += "/";
n += f;
unlink(n.c_str());
}
}