make it compile with modern C++
[prop.git] / lib-src / strings / quark.cc
blob47999c3e4b41945a8a31add5ed82aa9d952501b4
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #include <string.h>
26 #include <ctype.h>
27 #include <iostream>
28 #include <strstream>
29 #include <AD/strings/quark.h> // Atomic strings
30 #include <AD/strings/charesc.h> // Escape characters
31 #include <AD/hash/lhash.h> // Hash table
32 #include <AD/memory/strpool.h> // String pools
34 class QuarkMap
36 QuarkMap(const QuarkMap&); // no copy constructor
37 void operator = (const QuarkMap&); // no assignment
38 public:
39 // A table of all strings encountered
40 LHashTable<char *,char> universe;
41 // A string pool to store the strings
42 StringPool pool;
44 inline QuarkMap() : universe(397), pool(8196) {}
47 static QuarkMap * map = 0;
49 int Quark::entries() { return map ? map->universe.size() : 0; }
51 void Quark::intern(const char * string)
53 if (map == 0) map = new QuarkMap;
54 Ix key = map->universe.lookup((char * const)string);
57 // If the string is already in the universe, use the old copy.
58 // Otherwise, allocate a new string and put it into the map.
60 if (key) {
61 name = map->universe.key(key);
62 } else {
63 char * newName = map->pool[string]; // allocate the string from the pool
64 map->universe.insert(newName,' '); // add the new key into the map
65 name = newName;
69 //////////////////////////////////////////////////////////////////////////////
70 // Constructors
71 //////////////////////////////////////////////////////////////////////////////
72 Quark::Quark(const char string[])
73 { intern(string); }
74 Quark::Quark(const unsigned char string[])
75 { intern((const char *)string); }
76 Quark::Quark(short n)
77 { char buf[32];
78 std::ostrstream S(buf,sizeof(buf));
79 std::ostream& S2 = S;
80 S2 << n << std::ends;
81 intern(buf);
83 Quark::Quark(unsigned short n)
84 { char buf[32];
85 std::ostrstream S(buf,sizeof(buf));
86 std::ostream& S2 = S;
87 S2 << n << std::ends;
88 intern(buf);
90 Quark::Quark(int n)
91 { char buf[32];
92 std::ostrstream S(buf,sizeof(buf));
93 std::ostream& S2 = S;
94 S2 << n << std::ends;
95 intern(buf);
97 Quark::Quark(const char string[], int n)
98 { std::ostrstream S;
99 std::ostream& S2 = S;
100 S2 << string << n << std::ends;
101 intern(S.str());
102 S.rdbuf()->freeze(0);
104 Quark::Quark(unsigned int n)
105 { char buf[32];
106 std::ostrstream S(buf,sizeof(buf));
107 std::ostream& S2 = S;
108 S2 << n << std::ends;
109 intern(buf);
111 Quark::Quark(long n)
112 { char buf[32];
113 std::ostrstream S(buf,sizeof(buf));
114 std::ostream& S2 = S;
115 S2 << n << std::ends;
116 intern(buf);
118 Quark::Quark(unsigned long n)
119 { char buf[32];
120 std::ostrstream S(buf,sizeof(buf));
121 std::ostream& S2 = S;
122 S2 << n << std::ends;
123 intern(buf);
125 Quark::Quark(double n)
126 { char buf[64];
127 std::ostrstream S(buf,sizeof(buf));
128 std::ostream& S2 = S;
129 S2 << n << std::ends;
130 intern(buf);
132 Quark::Quark(char c)
133 { char buf[32];
134 buf[0] = '\'';
135 char * p = print_char(buf+1,c); *p++ = '\''; *p = '\0';
136 intern(buf);
138 Quark::Quark (const char s1[], const char s2[])
139 { std::ostrstream S;
140 std::ostream& S2 = S;
141 S2 << s1 << s2 << std::ends;
142 intern(S.str());
143 S.rdbuf()->freeze(0);
145 Quark::Quark (const char s1[], const char s2[], const char s3[])
146 { std::ostrstream S;
147 std::ostream& S2 = S;
148 S2 << s1 << s2 << s3 << std::ends;
149 intern(S.str());
150 S.rdbuf()->freeze(0);
152 Quark::Quark (const char s1[], const char s2[], const char s3[], const char s4[])
153 { std::ostrstream S;
154 std::ostream& S2 = S;
155 S2 << s1 << s2 << s3 << s4 << std::ends;
156 intern(S.str());
157 S.rdbuf()->freeze(0);
160 std::ostream& operator << (std::ostream& f, const Quark& q)
161 { return f << q.name; }
163 std::istream& operator >> (std::istream& f, Quark& q)
164 { char buf[4096];
165 f >> buf;
166 q = Quark(buf);
167 return f;
170 unsigned int hash_nocase(const Quark& q)
171 { unsigned int h = 0;
172 for (const char * p = q.name; *p; p++)
173 { if (isupper(*p)) h += h + (*p - 'A') + 'a';
174 else h += h + *p;
176 return h;
179 int compare_nocase(const Quark& a, const Quark& b)
180 { return stricmp(a.string(),b.string()); }