source: trunk/indexers/mg/lib/bitio_m_stdio.h@ 3745

Last change on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago

Addition of MG package for search and retrieval

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/**************************************************************************
2 *
3 * bitio_m_stdio.h -- Macros for bitio to a file
4 * Copyright (C) 1994 Neil Sharman
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 * $Id: bitio_m_stdio.h 3745 2003-02-20 21:20:24Z mdewsnip $
21 *
22 **************************************************************************
23 *
24 * This file contains macros for doing bitwise input and output on a FILE*.
25 * With these routines you cannot mix reads and writes on the FILE,
26 * or multiple writes, at the same time and guarantee them to work, also you
27 * cannot seek to a point and do a write. The decode routine can detect when
28 * you run off the end of the file and will produce an approate error message.
29 *
30 *
31 **************************************************************************/
32
33#ifndef H_BITIO_M_STDIO
34#define H_BITIO_M_STDIO
35
36typedef struct stdio_bitio_state
37 {
38 FILE *File;
39 unsigned char Buff;
40 unsigned char Btg;
41 }
42stdio_bitio_state;
43
44
45
46#ifndef DECODE_ERROR
47#define DECODE_ERROR (fprintf(stderr,"Unexpected EOF in \"%s\" on line %d\n",\
48 __FILE__, __LINE__), exit(1))
49#endif
50
51
52
53#define ENCODE_START(f) \
54 { \
55 FILE *__outfile = f; \
56 register unsigned char __buff = 0; \
57 register unsigned char __btg = sizeof(__buff)*8;
58
59#define ENCODE_CONTINUE(b) \
60 { \
61 FILE *__outfile = (b).File; \
62 register unsigned char __buff = (b).Buff; \
63 register unsigned char __btg = (b).Btg;
64
65#define ENCODE_BIT(b) \
66 do { \
67 __btg--; \
68 if (b) __buff |= (1 << __btg); \
69 if (!__btg) \
70 { \
71 putc(__buff, __outfile); \
72 __buff = 0; \
73 __btg = sizeof(__buff)*8; \
74 } \
75 } while(0)
76
77#define ENCODE_PAUSE(b) \
78 (b).File = __outfile; \
79 (b).Buff = __buff; \
80 (b).Btg = __btg; \
81 }
82
83#define ENCODE_DONE \
84 if (__btg != sizeof(__buff)*8) \
85 putc(__buff,__outfile); \
86 }
87
88
89#define DECODE_START(f) \
90 { \
91 FILE *__infile = f; \
92 register unsigned char __buff = 0; \
93 register unsigned char __btg = 0;
94
95#define DECODE_CONTINUE(b) \
96 { \
97 FILE *__infile = (b).File; \
98 register unsigned char __buff = (b).Buff; \
99 register unsigned char __btg = (b).Btg;
100
101#define DECODE_ADD_FF(b) \
102 do { \
103 if (!__btg) \
104 { \
105 register int val = getc(__infile); \
106 __buff = val == EOF ? 0xff : val; \
107 __btg = sizeof(__buff)*8; \
108 } \
109 (b) += (b) + ((__buff >> --__btg) & 1); \
110 } while(0)
111
112#define DECODE_ADD_00(b) \
113 do { \
114 if (!__btg) \
115 { \
116 register int val = getc(__infile); \
117 __buff = val == EOF ? 0xff : val; \
118 __btg = sizeof(__buff)*8; \
119 } \
120 (b) += (b) + ((__buff >> --__btg) & 1); \
121 } while(0)
122
123#define DECODE_BIT (__btg ? (__buff >> --__btg) & 1 : \
124 (__buff = getc(__infile), (feof(__infile) ? \
125 (DECODE_ERROR, 0) : \
126 (__btg = sizeof(__buff)*8, (__buff >> --__btg) & 1))))
127
128#define DECODE_DONE ; \
129 }
130
131#define DECODE_PAUSE(b) \
132 (b).File = __infile; \
133 (b).Buff = __buff; \
134 (b).Btg = __btg; \
135 }
136
137
138#define DECODE_SEEK(pos) \
139 do { \
140 register long __new_pos = (pos); \
141 fseek(__infile, __new_pos >> 3, 0); \
142 __buff = getc(__infile); \
143 __btg = 8 - (__new_pos&7); \
144 } \
145 while(0)
146
147#endif
Note: See TracBrowser for help on using the repository browser.