1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>swcomprs.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> </center>
<hr><h1>swcomprs.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> * swcomprs.cpp - code for class 'SWCompress'- a driver class that provides</font>
00003 <font class="comment"> * compression utilities.</font>
00004 <font class="comment"> */</font>
00005
00006 <font class="preprocessor">#include <string.h></font>
00007 <font class="preprocessor">#include <stdlib.h></font>
00008 <font class="preprocessor">#include <swcomprs.h></font>
00009
00010
00011 <font class="comment">/******************************************************************************</font>
00012 <font class="comment"> * SWCompress Constructor - Initializes data for instance of SWCompress</font>
00013 <font class="comment"> *</font>
00014 <font class="comment"> */</font>
00015
00016 SWCompress::SWCompress()
00017 {
00018 buf = zbuf = 0;
00019 Init();
00020 }
00021
00022
00023 <font class="comment">/******************************************************************************</font>
00024 <font class="comment"> * SWCompress Destructor - Cleans up instance of SWCompress</font>
00025 <font class="comment"> */</font>
00026
00027 SWCompress::~SWCompress()
00028 {
00029 <font class="keywordflow">if</font> (zbuf)
00030 free(zbuf);
00031
00032 <font class="keywordflow">if</font> (buf)
00033 free(buf);
00034 }
00035
00036
00037 <font class="keywordtype">void</font> SWCompress::Init()
00038 {
00039 <font class="keywordflow">if</font> (buf)
00040 free(buf);
00041
00042 <font class="keywordflow">if</font> (zbuf)
00043 free(zbuf);
00044
00045 buf = 0;
00046 zbuf = 0;
00047 direct = 0;
00048 zlen = 0;
00049 slen = 0;
00050 zpos = 0;
00051 pos = 0;
00052 }
00053
00054
00055 <font class="keywordtype">char</font> *SWCompress::Buf(<font class="keyword">const</font> <font class="keywordtype">char</font> *ibuf, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> *len) {
00056 <font class="comment">// setting an uncompressed buffer</font>
00057 <font class="keywordflow">if</font> (ibuf) {
00058 Init();
00059 slen = (len) ? *len : strlen(ibuf);
00060 buf = (<font class="keywordtype">char</font> *) calloc(slen + 1, 1);
00061 memcpy(buf, ibuf, slen);
00062 }
00063
00064 <font class="comment">// getting an uncompressed buffer</font>
00065 <font class="keywordflow">if</font> (!buf) {
00066 buf = (<font class="keywordtype">char</font> *)calloc(1,1); <font class="comment">// be sure we at least allocate an empty buf for return;</font>
00067 direct = 1;
00068 Decode();
00069 <font class="comment">// slen = strlen(buf);</font>
00070 <font class="keywordflow">if</font> (len)
00071 *len = slen;
00072 }
00073 <font class="keywordflow">return</font> buf;
00074 }
00075
00076
00077 <font class="keywordtype">char</font> *SWCompress::zBuf(<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> *len, <font class="keywordtype">char</font> *ibuf)
00078 {
00079 <font class="comment">// setting a compressed buffer</font>
00080 <font class="keywordflow">if</font> (ibuf) {
00081 Init();
00082 zbuf = (<font class="keywordtype">char</font> *) malloc(*len);
00083 memcpy(zbuf, ibuf, *len);
00084 zlen = *len;
00085 }
00086
00087 <font class="comment">// getting a compressed buffer</font>
00088 <font class="keywordflow">if</font> (!zbuf) {
00089 direct = 0;
00090 Encode();
00091 }
00092
00093 *len = zlen;
00094 <font class="keywordflow">return</font> zbuf;
00095 }
00096
00097
00098 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> SWCompress::GetChars(<font class="keywordtype">char</font> *ibuf, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> len)
00099 {
00100 <font class="keywordflow">if</font> (direct) {
00101 len = (((zlen - zpos) > (unsigned)len) ? len : zlen - zpos);
00102 <font class="keywordflow">if</font> (len > 0) {
00103 memmove(ibuf, &zbuf[zpos], len);
00104 zpos += len;
00105 }
00106 }
00107 <font class="keywordflow">else</font> {
00108 <font class="comment">// slen = strlen(buf);</font>
00109 len = (((slen - pos) > (unsigned)len) ? len : slen - pos);
00110 <font class="keywordflow">if</font> (len > 0) {
00111 memmove(ibuf, &buf[pos], len);
00112 pos += len;
00113 }
00114 }
00115 <font class="keywordflow">return</font> len;
00116 }
00117
00118
00119 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> SWCompress::SendChars(<font class="keywordtype">char</font> *ibuf, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> len)
00120 {
00121 <font class="keywordflow">if</font> (direct) {
00122 <font class="keywordflow">if</font> (buf) {
00123 <font class="comment">// slen = strlen(buf);</font>
00124 <font class="keywordflow">if</font> ((pos + len) > (unsigned)slen) {
00125 buf = (<font class="keywordtype">char</font> *) realloc(buf, pos + len + 1024);
00126 memset(&buf[pos], 0, len + 1024);
00127 }
00128 }
00129 <font class="keywordflow">else</font> buf = (<font class="keywordtype">char</font> *)calloc(1, len + 1024);
00130 memmove(&buf[pos], ibuf, len);
00131 pos += len;
00132 }
00133 <font class="keywordflow">else</font> {
00134 <font class="keywordflow">if</font> (zbuf) {
00135 <font class="keywordflow">if</font> ((zpos + len) > zlen) {
00136 zbuf = (<font class="keywordtype">char</font> *) realloc(zbuf, zpos + len + 1024);
00137 zlen = zpos + len + 1024;
00138 }
00139 }
00140 <font class="keywordflow">else</font> {
00141 zbuf = (<font class="keywordtype">char</font> *)calloc(1, len + 1024);
00142 zlen = len + 1024;
00143 }
00144 memmove(&zbuf[zpos], ibuf, len);
00145 zpos += len;
00146 }
00147 <font class="keywordflow">return</font> len;
00148 }
00149
00150
00151 <font class="comment">/******************************************************************************</font>
00152 <font class="comment"> * SWCompress::Encode - This function "encodes" the input stream into the</font>
00153 <font class="comment"> * output stream.</font>
00154 <font class="comment"> * The GetChars() and SendChars() functions are</font>
00155 <font class="comment"> * used to separate this method from the actual</font>
00156 <font class="comment"> * i/o.</font>
00157 <font class="comment"> */</font>
00158
00159 <font class="keywordtype">void</font> SWCompress::Encode(<font class="keywordtype">void</font>)
00160 {
00161 cycleStream();
00162 }
00163
00164
00165 <font class="comment">/******************************************************************************</font>
00166 <font class="comment"> * SWCompress::Decode - This function "decodes" the input stream into the</font>
00167 <font class="comment"> * output stream.</font>
00168 <font class="comment"> * The GetChars() and SendChars() functions are</font>
00169 <font class="comment"> * used to separate this method from the actual</font>
00170 <font class="comment"> * i/o.</font>
00171 <font class="comment"> */</font>
00172
00173 <font class="keywordtype">void</font> SWCompress::Decode(<font class="keywordtype">void</font>)
00174 {
00175 cycleStream();
00176 }
00177
00178
00179 <font class="keywordtype">void</font> SWCompress::cycleStream() {
00180 <font class="keywordtype">char</font> buf[1024];
00181 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> len, totlen = 0;
00182
00183 <font class="keywordflow">do</font> {
00184 len = GetChars(buf, 1024);
00185 <font class="keywordflow">if</font> (len)
00186 totlen += SendChars(buf, len);
00187 } <font class="keywordflow">while</font> (len == 1024);
00188
00189 zlen = slen = totlen;
00190 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>
|