Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libpz/pzsync.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1998-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
22
#include "pzlib.h"
23
24
/*
25
* sync outstanding data in a pz stream
26
*/
27
28
int
29
pzsync(register Pz_t* pz)
30
{
31
register unsigned char* buf;
32
register size_t i;
33
register size_t j;
34
size_t n;
35
size_t m;
36
Pzpart_t* pp;
37
Sfio_t* tmp;
38
Sfio_t* op;
39
Pzelt_t* elt;
40
Pzelt_t* old;
41
int r;
42
43
op = pz->ws.io ? pz->ws.io : pz->io;
44
if (pz->ws.bp)
45
{
46
/*
47
* flush the pzwrite() window
48
*/
49
50
pz->ws.bp = 0;
51
pp = pz->part;
52
if (pz->flags & PZ_SORT)
53
{
54
pz->flags &= ~PZ_SORT;
55
elt = (Pzelt_t*)dtfirst(pz->sort.order);
56
n = pp->row;
57
while (elt && pzwrite(pz, op, elt->buf, n) == n)
58
{
59
old = elt;
60
elt = (Pzelt_t*)dtnext(pz->sort.order, elt);
61
dtdelete(pz->sort.order, old);
62
dtinsert(pz->sort.free, old);
63
}
64
r = pzsync(pz);
65
pz->flags |= PZ_SORT;
66
pz->ws.bp = pz->buf;
67
return r;
68
}
69
tmp = pz->tmp;
70
n = pz->ws.row;
71
if (pz->flags & PZ_SECTION)
72
pz->count.sections++;
73
else
74
pz->count.windows++;
75
pz->count.records += pz->ws.rep;
76
sfputu(tmp, pz->ws.rep);
77
sfputu(tmp, 0);
78
79
/*
80
* transpose the hi frequency from row major to col major
81
* and write it by group to op
82
*/
83
84
if (pp->nmap)
85
{
86
pp->mix[0] = buf = pz->wrk;
87
m = 0;
88
for (j = 1; j < pp->nmap; j++)
89
{
90
m += n;
91
pp->mix[j] = (pp->lab[j] == pp->lab[j - 1]) ? (pp->mix[j - 1] + 1) : (buf + m);
92
}
93
buf = pz->buf;
94
for (i = 0; i < n; i++)
95
for (j = 0; j < pp->nmap; j++)
96
{
97
*pp->mix[j] = *buf++;
98
pp->mix[j] += pp->inc[j];
99
}
100
m = n * pp->nmap;
101
sfputu(op, m);
102
buf = pz->wrk;
103
for (i = 0; i < pp->ngrp; i++)
104
{
105
m = n * pp->grp[i];
106
if (sfwrite(op, buf, m) != m || sfsync(op))
107
{
108
if (pz->disc->errorf)
109
(*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "hi frequency write error");
110
return -1;
111
}
112
buf += m;
113
}
114
}
115
else
116
{
117
/*
118
* this is a phony size that is verified on inflate
119
* 0 here would terminate the inflate loop in the
120
* first window
121
*/
122
123
sfputu(op, 1);
124
}
125
126
/*
127
* now write the lo frequency encoding
128
*/
129
130
m = pz->ws.vp - pz->val;
131
sfputu(op, m);
132
if (sfwrite(op, pz->val, m) != m || sfsync(op))
133
{
134
if (pz->disc->errorf)
135
(*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "lo frequency value write error");
136
return -1;
137
}
138
m = sfstrtell(tmp);
139
if (sfwrite(op, sfstrseek(tmp, 0, SEEK_SET), m) != m || sfsync(op))
140
{
141
if (pz->disc->errorf)
142
(*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "lo frequency code write error");
143
return -1;
144
}
145
}
146
else if ((pz->flags & PZ_WRITE) && sfsync(op))
147
{
148
if (pz->disc->errorf)
149
(*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "write error");
150
return -1;
151
}
152
return 0;
153
}
154
155