1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * UCArray.h -- vector based string class
|
---|
4 | * Copyright (C) 1999 Rodger McNab
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | **************************************************************************/
|
---|
21 |
|
---|
22 | #ifndef UCARRAY_H
|
---|
23 | #define UCARRAY_H
|
---|
24 |
|
---|
25 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
|
---|
26 | #pragma warning(disable:4786)
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | // need this to avoid bizarre compiler problems under VC++ 6.0
|
---|
30 | #if !defined (GSDL_NAMESPACE_BROKEN) && !defined (GSDL_USE_IOS_H)
|
---|
31 | # include <iostream>
|
---|
32 | using namespace std;
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
36 | # include <ospace\std\vector>
|
---|
37 | # include <ospace\std\set>
|
---|
38 | #elif defined(GSDL_USE_STL_H)
|
---|
39 | # include <vector.h>
|
---|
40 | # include <set.h>
|
---|
41 | #else
|
---|
42 | # include <vector>
|
---|
43 | # include <set>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
47 | # include <ospace\std\iostream>
|
---|
48 | #elif defined(GSDL_USE_IOS_H)
|
---|
49 | # include <iostream.h>
|
---|
50 | #else
|
---|
51 | # include <iostream>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #include <stdio.h>
|
---|
55 |
|
---|
56 |
|
---|
57 | typedef vector<unsigned char> UCArray;
|
---|
58 |
|
---|
59 | // functions to manipulate UCArrays
|
---|
60 | void SetCStr (UCArray &text, const char *cStr);
|
---|
61 | // same as SetCStr but first tries to allocate nSizeHint space (only if needed)
|
---|
62 | void SetCStr (UCArray &text, const char *cStr, size_t nSizeHint);
|
---|
63 | char * GetCStr(const UCArray& text);
|
---|
64 | inline void UCArrayClear (UCArray &a) {
|
---|
65 | a.erase (a.begin(), a.end());
|
---|
66 | }
|
---|
67 | bool UCArrayCStrEquals(const UCArray &text, const unsigned char *cStr);
|
---|
68 | inline bool UCArrayCStrEquals(const UCArray &text, const char *cStr) { return UCArrayCStrEquals(text, (const unsigned char *)cStr); }
|
---|
69 |
|
---|
70 | // stream operator to print UCArray
|
---|
71 | ostream &operator<<(ostream &s, const UCArray &a);
|
---|
72 |
|
---|
73 | // used for reading and writing unsigned characters
|
---|
74 | inline bool ReadUC (FILE *f, unsigned char &c) {
|
---|
75 | return (fread (&c, sizeof (unsigned char), 1, f) == 1);
|
---|
76 | }
|
---|
77 | inline bool WriteUC (FILE *f, unsigned char c) {
|
---|
78 | return (fwrite (&c, sizeof (unsigned char), 1, f) == 1);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // used for reading and writing variable length unsigned longs
|
---|
82 | bool ReadVarLenUL (FILE *f, unsigned long &n);
|
---|
83 | bool WriteVarLenUL (FILE *f, unsigned long n);
|
---|
84 |
|
---|
85 | // used for reading and writing unsigned longs
|
---|
86 | bool ReadUL (FILE *f, unsigned long &n);
|
---|
87 | bool WriteUL (FILE *f, unsigned long n);
|
---|
88 |
|
---|
89 | // used for reading and writing floats
|
---|
90 | bool ReadF (FILE *f, float &n);
|
---|
91 | bool WriteF (FILE *f, float n);
|
---|
92 |
|
---|
93 | // used for reading and writing doubles
|
---|
94 | bool ReadD (FILE *f, double &n);
|
---|
95 | bool WriteD (FILE *f, double n);
|
---|
96 |
|
---|
97 | // used for reading and writing arrays to files
|
---|
98 | bool ReadUCArray (FILE *f, UCArray &a);
|
---|
99 | bool WriteUCArray (FILE *f, const UCArray &a);
|
---|
100 |
|
---|
101 |
|
---|
102 | // compares the two strings in dictionary order
|
---|
103 | int DictCompare (const UCArray &a1, const UCArray &a2);
|
---|
104 |
|
---|
105 | // does the first string start with the second?
|
---|
106 | bool StartsWith(const UCArray &a1, const UCArray &a2);
|
---|
107 | // does the first string start with the second, ignoring case?
|
---|
108 | bool StartsWithCasefold(const UCArray &a1, const UCArray &a2);
|
---|
109 |
|
---|
110 | struct LTUCArray {
|
---|
111 | bool operator()(const UCArray &a1, const UCArray &a2) const
|
---|
112 | { return (a1 < a2); }
|
---|
113 | };
|
---|
114 |
|
---|
115 | struct DictLTUCArray {
|
---|
116 | bool operator() (const UCArray &a1, const UCArray &a2) const
|
---|
117 | { return (DictCompare (a1, a2) < 0); }
|
---|
118 | };
|
---|
119 |
|
---|
120 |
|
---|
121 | typedef set<UCArray, LTUCArray> UCArraySet;
|
---|
122 | typedef vector<UCArray> UCArrayVector;
|
---|
123 |
|
---|
124 | unsigned long PrefixLen (const UCArray &a1, const UCArray &a2);
|
---|
125 |
|
---|
126 | // prev == NULL if no previous string
|
---|
127 | bool WritePreSufStr (FILE *f, const UCArray *prev, const UCArray &a);
|
---|
128 | bool ReadPreSufStr (FILE *f, UCArray &a); // a also used for prev
|
---|
129 |
|
---|
130 |
|
---|
131 | #endif
|
---|