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 | #include "mglong.h"
|
---|
26 |
|
---|
27 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
|
---|
28 | #pragma warning(disable:4786)
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | // need this to avoid bizarre compiler problems under VC++ 6.0
|
---|
32 | #if !defined (GSDL_NAMESPACE_BROKEN) && !defined (GSDL_USE_IOS_H)
|
---|
33 | # include <iostream>
|
---|
34 | using namespace std;
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
38 | # include <ospace\std\vector>
|
---|
39 | # include <ospace\std\set>
|
---|
40 | #elif defined(GSDL_USE_STL_H)
|
---|
41 | # include <vector.h>
|
---|
42 | # include <set.h>
|
---|
43 | #else
|
---|
44 | # include <vector>
|
---|
45 | # include <set>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
49 | # include <ospace\std\iostream>
|
---|
50 | #elif defined(GSDL_USE_IOS_H)
|
---|
51 | # include <iostream.h>
|
---|
52 | #else
|
---|
53 | # include <iostream>
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #include <stdio.h>
|
---|
57 | #include <cstring>
|
---|
58 |
|
---|
59 |
|
---|
60 | typedef vector<unsigned char> UCArray;
|
---|
61 |
|
---|
62 | // functions to manipulate UCArrays
|
---|
63 | void SetCStr (UCArray &text, const char *cStr);
|
---|
64 | // same as SetCStr but first tries to allocate nSizeHint space (only if needed)
|
---|
65 | void SetCStr (UCArray &text, const char *cStr, size_t nSizeHint);
|
---|
66 | char * GetCStr(const UCArray& text);
|
---|
67 | inline void UCArrayClear (UCArray &a) {
|
---|
68 | a.erase (a.begin(), a.end());
|
---|
69 | }
|
---|
70 | bool UCArrayCStrEquals(const UCArray &text, const unsigned char *cStr);
|
---|
71 | inline bool UCArrayCStrEquals(const UCArray &text, const char *cStr) { return UCArrayCStrEquals(text, (const unsigned char *)cStr); }
|
---|
72 |
|
---|
73 | // stream operator to print UCArray
|
---|
74 | ostream &operator<<(ostream &s, const UCArray &a);
|
---|
75 |
|
---|
76 | // used for reading and writing unsigned characters
|
---|
77 | inline bool ReadUC (FILE *f, unsigned char &c) {
|
---|
78 | return (fread (&c, sizeof (unsigned char), 1, f) == 1);
|
---|
79 | }
|
---|
80 | inline bool WriteUC (FILE *f, unsigned char c) {
|
---|
81 | return (fwrite (&c, sizeof (unsigned char), 1, f) == 1);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // used for reading and writing variable length unsigned longs
|
---|
85 | bool ReadVarLenUL (FILE *f, mg_u_long &n);
|
---|
86 | bool WriteVarLenUL (FILE *f, mg_u_long n);
|
---|
87 |
|
---|
88 | // used for reading and writing unsigned longs
|
---|
89 | bool ReadUL (FILE *f, mg_u_long &n);
|
---|
90 | bool WriteUL (FILE *f, mg_u_long n);
|
---|
91 |
|
---|
92 | // used for reading and writing floats
|
---|
93 | bool ReadF (FILE *f, float &n);
|
---|
94 | bool WriteF (FILE *f, float n);
|
---|
95 |
|
---|
96 | // used for reading and writing doubles
|
---|
97 | bool ReadD (FILE *f, double &n);
|
---|
98 | bool WriteD (FILE *f, double n);
|
---|
99 |
|
---|
100 | // used for reading and writing arrays to files
|
---|
101 | bool ReadUCArray (FILE *f, UCArray &a);
|
---|
102 | bool WriteUCArray (FILE *f, const UCArray &a);
|
---|
103 |
|
---|
104 |
|
---|
105 | // compares the two strings in dictionary order
|
---|
106 | int DictCompare (const UCArray &a1, const UCArray &a2);
|
---|
107 |
|
---|
108 | // does the first string start with the second?
|
---|
109 | bool StartsWith(const UCArray &a1, const UCArray &a2);
|
---|
110 | // does the first string start with the second, ignoring case?
|
---|
111 | bool StartsWithCasefold(const UCArray &a1, const UCArray &a2);
|
---|
112 |
|
---|
113 | struct LTUCArray {
|
---|
114 | bool operator()(const UCArray &a1, const UCArray &a2) const
|
---|
115 | { return (a1 < a2); }
|
---|
116 | };
|
---|
117 |
|
---|
118 | struct DictLTUCArray {
|
---|
119 | bool operator() (const UCArray &a1, const UCArray &a2) const
|
---|
120 | { return (DictCompare (a1, a2) < 0); }
|
---|
121 | };
|
---|
122 |
|
---|
123 |
|
---|
124 | typedef set<UCArray, LTUCArray> UCArraySet;
|
---|
125 | typedef vector<UCArray> UCArrayVector;
|
---|
126 |
|
---|
127 | mg_u_long PrefixLen (const UCArray &a1, const UCArray &a2);
|
---|
128 |
|
---|
129 | // prev == NULL if no previous string
|
---|
130 | bool WritePreSufStr (FILE *f, const UCArray *prev, const UCArray &a);
|
---|
131 | bool ReadPreSufStr (FILE *f, UCArray &a); // a also used for prev
|
---|
132 |
|
---|
133 |
|
---|
134 | #endif
|
---|