--- deliantra/server/include/shstr.h 2006/09/04 11:07:59 1.9 +++ deliantra/server/include/shstr.h 2007/05/28 21:15:56 1.16 @@ -1,6 +1,30 @@ +/* + * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. + * + * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team + * + * Crossfire TRT 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 Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * The authors can be reached via e-mail to + */ + #ifndef SHSTR_H__ #define SHSTR_H__ +#include + #include "util.h" extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); @@ -31,6 +55,14 @@ return length (s); } + // returns wether this shared string begins with the given prefix, + // used mainly for searched when users give only the start of a name. + bool begins_with (const char *prefix) const + { + int plen = strlen (prefix); + return !strncasecmp (s, prefix, plen) && length () >= plen; + } + static const char *find (const char *s); static const char *intern (const char *s); @@ -84,6 +116,16 @@ return *this; } + + bool operator ==(const shstr &b) + { + return s == b.s; + } + + bool operator !=(const shstr &b) + { + return !(*this == b); + } }; inline int strlen (const shstr &sh) @@ -91,14 +133,51 @@ return sh.length (); } -inline bool operator ==(const shstr &a, const shstr &b) +inline int strcmp (const shstr &a, const shstr &b) +{ + // TODO: use this to find all the occurences of people using strcmp + // all uses should be bogus, as we should be never interested in + // comparing shstr's alphabetically +#if 0 + extern void do_not_use_strcmp_to_compare_shstr_values (); + do_not_use_strcmp_to_compare_shstr_values (); +#endif + return a != b; +} + +static std::ostream &operator <<(std::ostream &o, const shstr &sh) +{ + o.write (sh.s, sh.length ()); + return o; +} + +// only good for mass comparisons to shstr objects +struct shstr_cmp +{ + const char *s; + + explicit shstr_cmp (const char *s) + : s (shstr::find (s)) + { + } + + shstr_cmp (const shstr_cmp &sh) + : s (sh.s) + { + } + + shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; } + operator const char *() const { return s; } +}; + +inline bool operator ==(const shstr_cmp &a, const shstr &b) { return a.s == b.s; } -inline bool operator !=(const shstr &a, const shstr &b) +inline bool operator ==(const shstr &a, const shstr_cmp &b) { - return !(a == b); + return b == a; } extern const shstr undead_name; /* Used in hit_player() in main.c */