/* * 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 <stdlib.h> #include <string.h> #include <string> #include "musicio.h" #include "paths.h" #include "flagnames.h" #include "pflagnames.h" #include "misc.h" char **flag_names = 0; int FL_MAX=0; void free_flags() { if (!flag_names) return; for (int i=0;i<FL_MAX;i++) if (flag_names[i]) free(flag_names[i]); free(flag_names); } void free_stuff(char **&stuff, int count) { if (!stuff) return; for (int i=0;i<count;i++) if (stuff[i]) free(stuff[i]); free(stuff); stuff = 0; } void load_stuff(const char *dname, const char *fname, char **&names, int &namecount, int y=1, int sq=1); void load_flags() { load_stuff(STADATA, "flags", flag_names, FL_MAX); char buf[100]; for (int n=0;n<FL_MAX;n++) { if (flag_names[n] && flag_names[n][0]=='-') { sprintf(buf, "Spare%i", n); free(flag_names[n]); flag_names[n] = strdup(buf); } } } char **priv_names = 0; int PFL_MAX=0; void free_pflags() { if (!priv_names) return; for (int i=0;i<PFL_MAX;i++) if (priv_names[i]) free(priv_names[i]); free(priv_names); } void load_pflags() { load_stuff(STADATA, "pflags", priv_names, PFL_MAX); } void load_stuff(const char *dname, const char *fname, char **&names, int &namecount, int a, int qu) { FILE *f = xopen(dname, fname, "r"); if (!f) { return; } int n=0; char buf[1000]; while (1) { fgets(buf, 900, f); if (feof(f) || buf[0]=='\n') { break; } n++; } xclose(f); if (n) { FILE *f = xopen(dname, fname, "r"); if (!f) { return; } free_stuff(names, namecount); namecount = n; names = (char **)malloc(sizeof(char*)*namecount); for (n=0;n<namecount;n++) { fgets(buf, 900, f); if (feof(f) || buf[0]=='\n') { break; } if (strchr(buf, '\n')) *strchr(buf, '\n')=0; if (a && strchr(buf, ' ')) *strchr(buf, ' ')=0; char *b = buf; if (qu && strchr(buf, ']')) { b = strchr(buf, ']')+1; } names[n] = strdup(b); } xclose(f); } } class Names { int n; char **t; char *defa; public: Names(const char *d, const char *f, const char *def, int sq=1) { defa = strdup(def); t = 0; n = 0; load_stuff(d, f, t, n, 0, sq); } char *random() { if (n) { int i = random_number(n); if (t[i]) return t[i]; else return defa; } return defa; } ~Names() { free(defa); free_stuff(t, n); } }; Names *places=0, *males=0, *females=0, *surnames=0, *warships=0, *tips=0; void load_names() { places = new Names(DATA_NAMES, "placenames", "Southampton"); males = new Names(DATA_NAMES, "male", "John"); females = new Names(DATA_NAMES, "female", "Janet"); surnames = new Names(DATA_NAMES, "surnames", "Smith"); warships = new Names(DATA_NAMES, "starships", "Victory"); tips = new Names(DATA_INFO, "tips.i", "Never run with scissors!", 0); } string random_placename() { if (!places) load_names(); return places->random(); } string random_male() { if (!males) load_names(); return males->random(); } string random_female() { if (!females) load_names(); return females->random(); } string random_surname() { if (!surnames) load_names(); return surnames->random(); } string random_warshipname() { if (!warships) load_names(); return (string)"HMS " + warships->random(); } string dollar_parse(const char *); string random_tip() { if (!tips) load_names(); return dollar_parse(tips->random()).c_str(); }