| 1 |
/* Copyright (C) 1992-1998 The Geometry Center |
| 2 |
* Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips |
| 3 |
* |
| 4 |
* This file is part of Geomview. |
| 5 |
* |
| 6 |
* Geomview is free software; you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU Lesser General Public License as published |
| 8 |
* by the Free Software Foundation; either version 2, or (at your option) |
| 9 |
* any later version. |
| 10 |
* |
| 11 |
* Geomview is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with Geomview; see the file COPYING. If not, write |
| 18 |
* to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, |
| 19 |
* USA, or visit http://www.gnu.org. |
| 20 |
*/ |
| 21 |
#ifndef _QV_STRING_ |
| 22 |
#define _QV_STRING_ |
| 23 |
|
| 24 |
#include <QvBasic.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
class QvString { |
| 28 |
public: |
| 29 |
QvString() { string = staticStorage; |
| 30 |
string[0] = '\0'; } |
| 31 |
QvString(const char *str) { string = staticStorage; |
| 32 |
*this = str; } |
| 33 |
QvString(const QvString &str) { string = staticStorage; |
| 34 |
*this = str.string; } |
| 35 |
~QvString(); |
| 36 |
u_long hash() { return QvString::hash(string); } |
| 37 |
int getLength() const { return strlen(string); } |
| 38 |
void makeEmpty(QvBool freeOld = TRUE); |
| 39 |
const char * getString() const { return string; } |
| 40 |
QvString & operator =(const char *str); |
| 41 |
QvString & operator =(const QvString &str) |
| 42 |
{ return (*this = str.string); } |
| 43 |
QvString & operator +=(const char *str); |
| 44 |
int operator !() const { return (string[0] == '\0'); } |
| 45 |
friend int operator ==(const QvString &str, const char *s); |
| 46 |
|
| 47 |
friend inline int operator ==(const char *s, const QvString &str) |
| 48 |
{ return (str == s); } |
| 49 |
|
| 50 |
friend inline int operator ==(const QvString &str1, const QvString &str2) |
| 51 |
{ return (str1 == str2.string); } |
| 52 |
friend int operator !=(const QvString &str, const char *s); |
| 53 |
|
| 54 |
friend inline int operator !=(const char *s, const QvString &str) |
| 55 |
{ return (str != s); } |
| 56 |
friend inline int operator !=(const QvString &str1, |
| 57 |
const QvString &str2) |
| 58 |
{ return (str1 != str2.string); } |
| 59 |
static u_long hash(const char *s); |
| 60 |
private: |
| 61 |
char *string; |
| 62 |
int storageSize; |
| 63 |
#define QV_STRING_STATIC_STORAGE_SIZE 32 |
| 64 |
char staticStorage[QV_STRING_STATIC_STORAGE_SIZE]; |
| 65 |
void expand(int bySize); |
| 66 |
}; |
| 67 |
|
| 68 |
class QvNameEntry { |
| 69 |
public: |
| 70 |
QvBool isEmpty() const { return (string[0] == '\0'); } |
| 71 |
QvBool isEqual(const char *s) const |
| 72 |
{ return (string[0] == s[0] && ! strcmp(string, s)); } |
| 73 |
private: |
| 74 |
static int nameTableSize; |
| 75 |
static QvNameEntry **nameTable; |
| 76 |
static struct QvNameChunk *chunk; |
| 77 |
const char *string; |
| 78 |
u_long hashValue; |
| 79 |
QvNameEntry *next; |
| 80 |
static void initClass(); |
| 81 |
QvNameEntry(const char *s, u_long h, QvNameEntry *n) |
| 82 |
{ string = s; hashValue = h; next = n; } |
| 83 |
static const QvNameEntry * insert(const char *s); |
| 84 |
|
| 85 |
friend class QvName; |
| 86 |
}; |
| 87 |
|
| 88 |
class QvName { |
| 89 |
public: |
| 90 |
QvName(); |
| 91 |
QvName(const char *s) { entry = QvNameEntry::insert(s); } |
| 92 |
QvName(const QvString &s) { entry = QvNameEntry::insert(s.getString()); } |
| 93 |
|
| 94 |
QvName(const QvName &n) { entry = n.entry; } |
| 95 |
~QvName() {} |
| 96 |
const char *getString() const { return entry->string; } |
| 97 |
int getLength() const { return strlen(entry->string); } |
| 98 |
static QvBool isIdentStartChar(char c); |
| 99 |
static QvBool isIdentChar(char c); |
| 100 |
static QvBool isNodeNameStartChar(char c); |
| 101 |
static QvBool isNodeNameChar(char c); |
| 102 |
int operator !() const { return entry->isEmpty(); } |
| 103 |
friend inline int operator ==(const QvName &n, const char *s) |
| 104 |
{ return n.entry->isEqual(s); } |
| 105 |
friend inline int operator ==(const char *s, const QvName &n) |
| 106 |
{ return n.entry->isEqual(s); } |
| 107 |
|
| 108 |
friend inline int operator ==(const QvName &n1, const QvName &n2) |
| 109 |
{ return n1.entry == n2.entry; } |
| 110 |
friend inline int operator !=(const QvName &n, const char *s) |
| 111 |
{ return ! n.entry->isEqual(s); } |
| 112 |
friend inline int operator !=(const char *s, const QvName &n) |
| 113 |
{ return ! n.entry->isEqual(s); } |
| 114 |
|
| 115 |
friend inline int operator !=(const QvName &n1, const QvName &n2) |
| 116 |
{ return n1.entry != n2.entry; } |
| 117 |
private: |
| 118 |
const QvNameEntry *entry; |
| 119 |
}; |
| 120 |
|
| 121 |
#endif /* _QV_STRING_ */ |