#include "copyright.h"
#include "config.h"
#include <sys/file.h>
#include "mudconf.h"
#include "externs.h"
#include "db.h"
#include "vattr.h"
#include "attrs.h"
#include "alloc.h"
#include "powers.h"
Include dependency graph for db_xml.c:
Go to the source code of this file.
Defines | |
#define | MUX_REPRESENTATION |
Functions | |
const char * | getstring_noalloc (FILE *, int) |
void | putstring (FILE *, const char *) |
void | db_grow (dbref) |
static int | xml_putescaped (FILE *f, const char *string) |
static int | xml_putobjstring (FILE *f, const char *name, const char *value) |
static int | xml_putobjref (FILE *f, const char *name, long value) |
static int | xml_putattr (FILE *f, const char *name, const char *value, long owner, long flags) |
static int | xml_db_write_object (FILE *f, dbref i, int db_format, int flags) |
static int | xml_db_write_mux (FILE *f, dbref i, int db_format, int flags) |
dbref | xml_db_write (FILE *f, int format, int version) |
Variables | |
object * | db |
static int | g_version |
static int | g_format |
static int | g_flags |
void db_grow | ( | dbref | ) |
Definition at line 1609 of file db.c.
References confdata::cache_names, flatfiledb::db, statedata::db_size, statedata::db_top, GOD, GOING, confdata::init_size, initialize_objects(), LOG_ALWAYS, LOG_SIMPLE, statedata::markbits, statedata::min_size, mudconf, mudstate, NOTHING, purenames, s_Contents, s_Exits, s_Flags, s_Link, s_Location, s_Next, s_Owner, s_Parent, s_Powers, s_Powers2, s_Stack, s_Zone, SIZE_HACK, tprintf(), TYPE_GARBAGE, XFREE, and XMALLOC.
Referenced by create_obj(), db_make_minimal(), db_read(), and mmdb_db_read().
01610 { 01611 int newsize, marksize, delta, i; 01612 MARKBUF *newmarkbuf; 01613 OBJ *newdb; 01614 NAME *newpurenames; 01615 01616 char *cp; 01617 01618 delta = mudconf.init_size; 01619 01620 /* 01621 * Determine what to do based on requested size, current top and * * 01622 * 01623 * * * * * * * * size. Make sure we grow in reasonable-sized 01624 * chunks to * * prevent * * * * frequent reallocations of the db 01625 * array. 01626 */ 01627 01628 /* 01629 * If requested size is smaller than the current db size, ignore it 01630 */ 01631 01632 if(newtop <= mudstate.db_top) { 01633 return; 01634 } 01635 /* 01636 * If requested size is greater than the current db size but smaller 01637 * * * * * * * than the amount of space we have allocated, raise the 01638 * db * * * size * * and * initialize the new area. 01639 */ 01640 01641 if(newtop <= mudstate.db_size) { 01642 for(i = mudstate.db_top; i < newtop; i++) { 01643 if(mudconf.cache_names) 01644 purenames[i] = NULL; 01645 } 01646 initialize_objects(mudstate.db_top, newtop); 01647 mudstate.db_top = newtop; 01648 return; 01649 } 01650 /* 01651 * Grow by a minimum of delta objects 01652 */ 01653 01654 if(newtop <= mudstate.db_size + delta) { 01655 newsize = mudstate.db_size + delta; 01656 } else { 01657 newsize = newtop; 01658 } 01659 01660 /* 01661 * Enforce minimumdatabase size 01662 */ 01663 01664 if(newsize < mudstate.min_size) 01665 newsize = mudstate.min_size + delta;; 01666 01667 /* 01668 * Grow the name tables 01669 */ 01670 01671 if(mudconf.cache_names) { 01672 newpurenames = 01673 (NAME *) XMALLOC((newsize + SIZE_HACK) * sizeof(NAME), 01674 "db_grow.purenames"); 01675 01676 if(!newpurenames) { 01677 LOG_SIMPLE(LOG_ALWAYS, "ALC", "DB", 01678 tprintf 01679 ("Could not allocate space for %d item name cache.", 01680 newsize)); 01681 abort(); 01682 } 01683 bzero((char *) newpurenames, (newsize + SIZE_HACK) * sizeof(NAME)); 01684 01685 if(purenames) { 01686 01687 /* 01688 * An old name cache exists. Copy it. 01689 */ 01690 01691 purenames -= SIZE_HACK; 01692 bcopy((char *) purenames, (char *) newpurenames, 01693 (newtop + SIZE_HACK) * sizeof(NAME)); 01694 cp = (char *) purenames; 01695 XFREE(cp, "db_grow.purename"); 01696 } else { 01697 01698 /* 01699 * Creating a brand new struct database. Fill in the 01700 * 'reserved' area in case it gets referenced. 01701 */ 01702 01703 purenames = newpurenames; 01704 for(i = 0; i < SIZE_HACK; i++) { 01705 purenames[i] = NULL; 01706 } 01707 } 01708 purenames = newpurenames + SIZE_HACK; 01709 newpurenames = NULL; 01710 } 01711 /* 01712 * Grow the db array 01713 */ 01714 01715 newdb = (OBJ *) 01716 XMALLOC((newsize + SIZE_HACK) * sizeof(OBJ), "db_grow.db"); 01717 if(!newdb) { 01718 01719 LOG_SIMPLE(LOG_ALWAYS, "ALC", "DB", 01720 tprintf 01721 ("Could not allocate space for %d item struct database.", 01722 newsize)); 01723 abort(); 01724 } 01725 if(db) { 01726 01727 /* 01728 * An old struct database exists. Copy it to the new buffer 01729 */ 01730 01731 db -= SIZE_HACK; 01732 bcopy((char *) db, (char *) newdb, 01733 (mudstate.db_top + SIZE_HACK) * sizeof(OBJ)); 01734 cp = (char *) db; 01735 XFREE(cp, "db_grow.db"); 01736 } else { 01737 01738 /* 01739 * Creating a brand new struct database. Fill in the * * * * 01740 * 01741 * * * * 'reserved' area in case it gets referenced. 01742 */ 01743 01744 db = newdb; 01745 for(i = 0; i < SIZE_HACK; i++) { 01746 s_Owner(i, GOD); 01747 s_Flags(i, (TYPE_GARBAGE | GOING)); 01748 s_Powers(i, 0); 01749 s_Powers2(i, 0); 01750 s_Location(i, NOTHING); 01751 s_Contents(i, NOTHING); 01752 s_Exits(i, NOTHING); 01753 s_Link(i, NOTHING); 01754 s_Next(i, NOTHING); 01755 s_Zone(i, NOTHING); 01756 s_Parent(i, NOTHING); 01757 s_Stack(i, NULL); 01758 db[i].ahead = NULL; 01759 db[i].at_count = 0; 01760 } 01761 } 01762 db = newdb + SIZE_HACK; 01763 newdb = NULL; 01764 01765 for(i = mudstate.db_top; i < newtop; i++) { 01766 if(mudconf.cache_names) { 01767 purenames[i] = NULL; 01768 } 01769 } 01770 initialize_objects(mudstate.db_top, newtop); 01771 mudstate.db_top = newtop; 01772 mudstate.db_size = newsize; 01773 01774 /* 01775 * Grow the db mark buffer 01776 */ 01777 01778 marksize = (newsize + 7) >> 3; 01779 newmarkbuf = (MARKBUF *) XMALLOC(marksize, "db_grow"); 01780 bzero((char *) newmarkbuf, marksize); 01781 if(mudstate.markbits) { 01782 marksize = (newtop + 7) >> 3; 01783 bcopy((char *) mudstate.markbits, (char *) newmarkbuf, marksize); 01784 cp = (char *) mudstate.markbits; 01785 XFREE(cp, "db_grow"); 01786 } 01787 mudstate.markbits = newmarkbuf; 01788 }
const char* getstring_noalloc | ( | FILE * | , | |
int | ||||
) |
Definition at line 1885 of file db.c.
01886 { 01887 static char buf[LBUF_SIZE]; 01888 char *p; 01889 int c, lastc; 01890 01891 p = buf; 01892 c = fgetc(f); 01893 if(!new_strings || (c != '"')) { 01894 ungetc(c, f); 01895 c = '\0'; 01896 for(;;) { 01897 lastc = c; 01898 c = fgetc(f); 01899 01900 /* 01901 * If EOF or null, return 01902 */ 01903 01904 if(!c || (c == EOF)) { 01905 *p = '\0'; 01906 return buf; 01907 } 01908 /* 01909 * If a newline, return if prior char is not a cr. * 01910 * * * Otherwise * keep on truckin' 01911 */ 01912 01913 if((c == '\n') && (lastc != '\r')) { 01914 *p = '\0'; 01915 return buf; 01916 } 01917 safe_chr(c, buf, &p); 01918 } 01919 } else { 01920 for(;;) { 01921 c = fgetc(f); 01922 if(c == '"') { 01923 if((c = fgetc(f)) != '\n') 01924 ungetc(c, f); 01925 *p = '\0'; 01926 return buf; 01927 } else if(c == '\\') { 01928 c = fgetc(f); 01929 } 01930 if((c == '\0') || (c == EOF)) { 01931 *p = '\0'; 01932 return buf; 01933 } 01934 safe_chr(c, buf, &p); 01935 } 01936 } 01937 }
void putstring | ( | FILE * | , | |
const char * | ||||
) |
dbref xml_db_write | ( | FILE * | f, | |
int | format, | |||
int | version | |||
) |
Definition at line 234 of file db_xml.c.
References statedata::db_top, DO_WHOLE_DB, object::flags, Going, mudstate, statedata::now, xml_db_write_mux(), and xml_db_write_object().
00235 { 00236 dbref i; 00237 int flags; 00238 VATTR *vp; 00239 00240 fprintf(f, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); 00241 fprintf(f, "<TinyMUXDataBase dumptime=\"%lu\">\n", mudstate.now); 00242 DO_WHOLE_DB(i) { 00243 if(!(Going(i))) { 00244 #ifdef MUX_REPRESENTATION 00245 xml_db_write_mux(f, i, format, flags); 00246 #else 00247 xml_db_write_object(f, i, format, flags); 00248 #endif 00249 } 00250 } 00251 fprintf(f, "</TinyMUXDataBase>\n"); 00252 fflush(f); 00253 return (mudstate.db_top); 00254 }
static int xml_db_write_mux | ( | FILE * | f, | |
dbref | i, | |||
int | db_format, | |||
int | flags | |||
) | [static] |
Definition at line 172 of file db_xml.c.
References A_LIST, A_LOCK, A_MONEY, A_NAME, atr_get(), atr_head(), atr_next(), atr_num(), Contents, Exits, Flags, Flags2, Flags3, free_lbuf, Link, Location, attr::name, Name(), attr::number, Owner, Parent, Pennies(), Powers, Powers2, V_ATRKEY, V_ATRNAME, xml_putattr(), xml_putobjref(), xml_putobjstring(), and Zone.
Referenced by xml_db_write().
00173 { 00174 ATTR *a; 00175 char *got, *as; 00176 dbref aowner; 00177 long sub; 00178 int ca, aflags, save, j; 00179 BOOLEXP *tempbool; 00180 00181 fprintf(f, "\t<Object dbref=\"%ld\">\n", (long) i); 00182 xml_putobjstring(f, "Name", Name(i)); 00183 xml_putobjref(f, "Location", Location(i)); 00184 xml_putobjref(f, "Zone", Zone(i)); 00185 xml_putobjref(f, "Contents", Contents(i)); 00186 xml_putobjref(f, "Exits", Exits(i)); 00187 got = atr_get(i, A_LOCK, &aowner, &aflags); 00188 xml_putobjstring(f, "Lock", got); 00189 free_lbuf(got); 00190 00191 xml_putobjref(f, "Link", Link(i)); 00192 xml_putobjref(f, "Owner", Owner(i)); 00193 xml_putobjref(f, "Parent", Parent(i)); 00194 xml_putobjref(f, "Pennies", Pennies(i)); 00195 xml_putobjref(f, "Flags", Flags(i)); 00196 xml_putobjref(f, "Flags2", Flags2(i)); 00197 xml_putobjref(f, "Flags3", Flags3(i)); 00198 xml_putobjref(f, "Powers", Powers(i)); 00199 xml_putobjref(f, "Powers2", Powers2(i)); 00200 for(ca = atr_head(i, &as); ca; ca = atr_next(&as)) { 00201 save = 0; 00202 a = atr_num(ca); 00203 if(a) 00204 j = a->number; 00205 else 00206 j = -1; 00207 00208 if(j > 0) { 00209 switch (j) { 00210 case A_NAME: 00211 if(flags & V_ATRNAME) 00212 save = 1; 00213 break; 00214 case A_LOCK: 00215 if(flags & V_ATRKEY) 00216 save = 1; 00217 break; 00218 case A_LIST: 00219 case A_MONEY: 00220 break; 00221 default: 00222 save = 1; 00223 } 00224 } 00225 if(save) { 00226 got = atr_get(i, j, &aowner, &aflags); 00227 xml_putattr(f, a->name, got, aowner, aflags); 00228 } 00229 } 00230 fprintf(f, "\t</Object>\n"); 00231 return 0; 00232 }
static int xml_db_write_object | ( | FILE * | f, | |
dbref | i, | |||
int | db_format, | |||
int | flags | |||
) | [static] |
Definition at line 89 of file db_xml.c.
References A_LIST, A_LOCK, A_MONEY, A_NAME, atr_get(), atr_head(), atr_next(), atr_num(), Contents, Exits, Flags, Flags2, Flags3, free_lbuf, Link, Location, attr::name, Name(), Next, attr::number, Owner, Parent, Pennies(), Powers, Powers2, V_ATRKEY, V_ATRNAME, xml_putattr(), xml_putobjref(), xml_putobjstring(), and Zone.
Referenced by xml_db_write().
00090 { 00091 ATTR *a; 00092 char *got, *as; 00093 dbref aowner; 00094 long sub; 00095 int ca, aflags, save, j; 00096 BOOLEXP *tempbool; 00097 00098 fprintf(f, "\t<Object dbref=\"%ld\">\n", (long) i); 00099 xml_putobjstring(f, "Name", Name(i)); 00100 xml_putobjref(f, "Location", Location(i)); 00101 xml_putobjref(f, "Zone", Zone(i)); 00102 if(Contents(i) == -1) { 00103 fprintf(f, "\t\t<Contents/>\n"); 00104 } else { 00105 fprintf(f, "\t\t<Contents>\n"); 00106 sub = Contents(i); 00107 while (sub != -1) { 00108 fprintf(f, "\t\t\t<DBREF dbref=\"%ld\"/>\n", sub); 00109 sub = Next(sub); 00110 } 00111 fprintf(f, "\t\t</Contents>\n"); 00112 } 00113 if(Exits(i) == -1) { 00114 fprintf(f, "\t\t<Exits/>\n"); 00115 } else { 00116 fprintf(f, "\t\t<Exits>\n"); 00117 sub = Exits(i); 00118 while (sub != -1) { 00119 fprintf(f, "\t\t\t<DBREF dbref=\"%ld\"/>\n", sub); 00120 sub = Next(sub); 00121 } 00122 fprintf(f, "\t\t</Exits>\n"); 00123 } 00124 got = atr_get(i, A_LOCK, &aowner, &aflags); 00125 xml_putobjstring(f, "Lock", got); 00126 free_lbuf(got); 00127 00128 xml_putobjref(f, "Link", Link(i)); 00129 xml_putobjref(f, "Owner", Owner(i)); 00130 xml_putobjref(f, "Parent", Parent(i)); 00131 xml_putobjref(f, "Pennies", Pennies(i)); 00132 xml_putobjref(f, "Flags", Flags(i)); 00133 xml_putobjref(f, "Flags2", Flags2(i)); 00134 xml_putobjref(f, "Flags3", Flags3(i)); 00135 xml_putobjref(f, "Powers", Powers(i)); 00136 xml_putobjref(f, "Powers2", Powers2(i)); 00137 00138 for(ca = atr_head(i, &as); ca; ca = atr_next(&as)) { 00139 save = 0; 00140 a = atr_num(ca); 00141 if(a) 00142 j = a->number; 00143 else 00144 j = -1; 00145 00146 if(j > 0) { 00147 switch (j) { 00148 case A_NAME: 00149 if(flags & V_ATRNAME) 00150 save = 1; 00151 break; 00152 case A_LOCK: 00153 if(flags & V_ATRKEY) 00154 save = 1; 00155 break; 00156 case A_LIST: 00157 case A_MONEY: 00158 break; 00159 default: 00160 save = 1; 00161 } 00162 } 00163 if(save) { 00164 got = atr_get(i, j, &aowner, &aflags); 00165 xml_putattr(f, a->name, got, aowner, aflags); 00166 } 00167 } 00168 fprintf(f, "\t</Object>\n"); 00169 return 0; 00170 }
static int xml_putattr | ( | FILE * | f, | |
const char * | name, | |||
const char * | value, | |||
long | owner, | |||
long | flags | |||
) | [static] |
Definition at line 78 of file db_xml.c.
References xml_putescaped().
Referenced by xml_db_write_mux(), and xml_db_write_object().
00080 { 00081 fprintf(f, "\t\t<Attribute name=\""); 00082 xml_putescaped(f, name); 00083 fprintf(f, "\" owner=\"%ld\" flags=\"%ld\">", owner, flags); 00084 xml_putescaped(f, value); 00085 fprintf(f, "</Attribute>\n"); 00086 return 1; 00087 }
static int xml_putescaped | ( | FILE * | f, | |
const char * | string | |||
) | [static] |
Definition at line 32 of file db_xml.c.
References LBUF_SIZE.
Referenced by xml_putattr(), and xml_putobjstring().
00033 { 00034 char emit_buffer[LBUF_SIZE * 7]; 00035 char *r, *s; 00036 s = emit_buffer; 00037 memset(emit_buffer, 0, sizeof(emit_buffer)); 00038 for(r = (char *)string; *r; r++) { 00039 switch (*r) { 00040 case '"': 00041 s = stpcpy(s, """); 00042 break; 00043 case '\'': 00044 s = stpcpy(s, "'"); 00045 break; 00046 case '&': 00047 s = stpcpy(s, "&"); 00048 case '<': 00049 s = stpcpy(s, "<"); 00050 break; 00051 case '>': 00052 s = stpcpy(s, ">"); 00053 break; 00054 case '\\': 00055 s = stpcpy(s, "\\\\"); 00056 break; 00057 default: 00058 *s++ = *r; 00059 } 00060 } 00061 *s = '\0'; 00062 return fprintf(f, "%s", emit_buffer); 00063 }
static int xml_putobjref | ( | FILE * | f, | |
const char * | name, | |||
long | value | |||
) | [static] |
Definition at line 73 of file db_xml.c.
Referenced by xml_db_write_mux(), and xml_db_write_object().
static int xml_putobjstring | ( | FILE * | f, | |
const char * | name, | |||
const char * | value | |||
) | [static] |
Definition at line 65 of file db_xml.c.
References xml_putescaped().
Referenced by xml_db_write_mux(), and xml_db_write_object().
00066 { 00067 fprintf(f, "\t\t<%s>", name); 00068 xml_putescaped(f, value); 00069 fprintf(f, "</%s>\n", name); 00070 return 1; 00071 }