emergency commit
[cl-cudd.git] / distr / dddmp / dddmpBinary.c
bloba7cd67482957f7fe82179f45d651df434bd5f8b5
1 /**CFile**********************************************************************
3 FileName [dddmpBinary.c]
5 PackageName [dddmp]
7 Synopsis [Input and output BDD codes and integers from/to file]
9 Description [Input and output BDD codes and integers from/to file
10 in binary mode.
11 DD node codes are written as one byte.
12 Integers of any length are written as sequences of "linked" bytes.
13 For each byte 7 bits are used for data and one (MSBit) as link with
14 a further byte (MSB = 1 means one more byte).
15 Low level read/write of bytes filter <CR>, <LF> and <ctrl-Z>
16 with escape sequences.
19 Author [Gianpiero Cabodi and Stefano Quer]
21 Copyright [
22 Copyright (c) 2004 by Politecnico di Torino.
23 All Rights Reserved. This software is for educational purposes only.
24 Permission is given to academic institutions to use, copy, and modify
25 this software and its documentation provided that this introductory
26 message is not removed, that this software and its documentation is
27 used for the institutions' internal research and educational purposes,
28 and that no monies are exchanged. No guarantee is expressed or implied
29 by the distribution of this code.
30 Send bug-reports and/or questions to:
31 {gianpiero.cabodi,stefano.quer}@polito.it.
34 ******************************************************************************/
36 #include "dddmpInt.h"
38 /*---------------------------------------------------------------------------*/
39 /* Stucture declarations */
40 /*---------------------------------------------------------------------------*/
42 /*---------------------------------------------------------------------------*/
43 /* Type declarations */
44 /*---------------------------------------------------------------------------*/
46 /*---------------------------------------------------------------------------*/
47 /* Variable declarations */
48 /*---------------------------------------------------------------------------*/
50 /*---------------------------------------------------------------------------*/
51 /* Macro declarations */
52 /*---------------------------------------------------------------------------*/
54 /**AutomaticStart*************************************************************/
56 /*---------------------------------------------------------------------------*/
57 /* Static function prototypes */
58 /*---------------------------------------------------------------------------*/
60 static int WriteByteBinary(FILE *fp, unsigned char c);
61 static int ReadByteBinary(FILE *fp, unsigned char *cp);
63 /**AutomaticEnd***************************************************************/
66 /*---------------------------------------------------------------------------*/
67 /* Definition of exported functions */
68 /*---------------------------------------------------------------------------*/
71 /*---------------------------------------------------------------------------*/
72 /* Definition of internal functions */
73 /*---------------------------------------------------------------------------*/
75 /**Function********************************************************************
77 Synopsis [Writes 1 byte node code]
79 Description [outputs a 1 byte node code using the following format:
80 <pre>
81 Unused : 1 bit;
82 V : 2 bits; (variable code)
83 T : 2 bits; (Then code)
84 Ecompl : 1 bit; (Else complemented)
85 E : 2 bits; (Else code)
86 </pre>
87 Ecompl is set with complemented edges.
90 SideEffects [None]
92 SeeAlso [DddmpReadCode()]
94 ******************************************************************************/
96 int
97 DddmpWriteCode (
98 FILE *fp /* IN: file where to write the code */,
99 struct binary_dd_code code /* IN: the code to be written */
102 unsigned char c;
103 int retValue;
105 c = (code.Unused<<7)|(code.V<<5)|(code.T<<3)|
106 (code.Ecompl<<2)|(code.E);
108 retValue = WriteByteBinary (fp, c);
110 return (retValue);
113 /**Function********************************************************************
115 Synopsis [Reads a 1 byte node code]
117 Description [Reads a 1 byte node code. See DddmpWriteCode()
118 for code description.]
120 SideEffects [None]
122 SeeAlso [DddmpWriteCode()]
124 ******************************************************************************/
126 int
127 DddmpReadCode (
128 FILE *fp /* IN: file where to read the code */,
129 struct binary_dd_code *pcode /* OUT: the read code */
132 unsigned char c;
134 if (ReadByteBinary (fp, &c) == EOF) {
135 return (0);
138 pcode->Unused = c>>7;
139 pcode->V = (c>>5) & 3;
140 pcode->T = (c>>3) & 3;
141 pcode->Ecompl = (c>>2) & 1;
142 pcode->E = c & 3;
144 return (1);
147 /**Function********************************************************************
149 Synopsis [Writes a "packed integer"]
151 Description [Writes an integer as a sequence of bytes (MSByte first).
152 For each byte 7 bits are used for data and one (LSBit) as link
153 with a further byte (LSB = 1 means one more byte).
156 SideEffects [None]
158 SeeAlso [DddmpReadInt()]
160 ******************************************************************************/
162 int
163 DddmpWriteInt (
164 FILE *fp /* IN: file where to write the integer */,
165 int id /* IN: integer to be written */
168 char cvet[4];
169 int i;
171 for (i=0; i<4; i++) {
172 cvet[i] = (char)((id & 0x0000007f) << 1);
173 id >>= 7;
176 for (i=3; (i>0) && (cvet[i] == 0); i--);
178 for (; i>0; i--) {
179 cvet[i] |= (char)1;
180 if (WriteByteBinary (fp, cvet[i]) == EOF)
181 return (0);
184 if (WriteByteBinary (fp, cvet[0]) == EOF) {
185 return (0);
188 return (1);
192 /**Function********************************************************************
194 Synopsis [Reads a "packed integer"]
196 Description [Reads an integer coded on a sequence of bytes. See
197 DddmpWriteInt() for format.]
199 SideEffects [None]
201 SeeAlso [DddmpWriteInt()]
203 ******************************************************************************/
206 DddmpReadInt (
207 FILE *fp /* IN: file where to read the integer */,
208 int *pid /* OUT: the read integer */
211 unsigned char c;
212 int i;
213 unsigned int id;
215 id = 0;
216 for (i=0; i<4; i++) {
217 if (ReadByteBinary (fp, &c) == EOF)
218 return (0);
219 id = (id<<7) | (c>>1);
220 if ((c & 1) == 0)
221 break;
224 /* Check for correct format: last char should
225 be found before i = 4 */
226 assert(i<4);
228 *pid = id;
230 return (i+1);
233 /*---------------------------------------------------------------------------*/
234 /* Definition of static functions */
235 /*---------------------------------------------------------------------------*/
237 /**Function********************************************************************
239 Synopsis [Writes a byte to file filtering <CR>, <LF> and <ctrl-Z>]
241 Description [outputs a byte to file fp. Uses 0x00 as escape character
242 to filter <CR>, <LF> and <ctrl-Z>.
243 This is done for compatibility between unix and dos/windows systems.
246 SideEffects [None]
248 SeeAlso [ReadByteBinary()]
250 ******************************************************************************/
252 static int
253 WriteByteBinary (
254 FILE *fp /* IN: file where to write the byte */,
255 unsigned char c /* IN: the byte to be written */
258 unsigned char BinaryEscape;
260 switch (c) {
262 case 0x00: /* Escape */
263 BinaryEscape = 0x00;
264 if (fwrite (&BinaryEscape, sizeof(char), 1, fp) != sizeof(char))
265 return (0);
266 c = 0x00;
267 break;
268 case 0x0a: /* <LF> */
269 BinaryEscape = 0x00;
270 if (fwrite (&BinaryEscape, sizeof(char), 1, fp) != sizeof(char))
271 return (0);
272 c = 0x01;
273 break;
274 case 0x0d: /* <CR> */
275 BinaryEscape = 0x00;
276 if (fwrite (&BinaryEscape, sizeof(char), 1, fp) != sizeof(char))
277 return (0);
278 c = 0x02;
279 break;
280 case 0x1a: /* <ctrl-Z> */
281 BinaryEscape = 0x00;
282 if (fwrite (&BinaryEscape, sizeof(char), 1, fp) != sizeof(char))
283 return (0);
284 c = 0x03;
285 break;
287 if (fwrite (&c, sizeof(char), 1, fp) != sizeof(char))
288 return (0);
290 return (1);
293 /**Function********************************************************************
295 Synopsis [Reads a byte from file with escaped <CR>, <LF> and <ctrl-Z>]
297 Description [inputs a byte to file fp. 0x00 has been used as escape character
298 to filter <CR>, <LF> and <ctrl-Z>. This is done for
299 compatibility between unix and dos/windows systems.
302 SideEffects [None]
304 SeeAlso [WriteByteBinary()]
306 ******************************************************************************/
308 static int
309 ReadByteBinary (
310 FILE *fp /* IN: file where to read the byte */,
311 unsigned char *cp /* OUT: the read byte */
315 if (fread (cp, sizeof(char), 1, fp) != sizeof(char)) {
316 return (0);
319 if (*cp == 0x00) { /* Escape */
320 if (fread (cp, sizeof(char), 1, fp) != sizeof(char)) {
321 return (0);
324 switch (*cp) {
326 case 0x00: /* Escape */
327 break;
328 case 0x01: /* <LF> */
329 *cp = 0x0a;
330 break;
331 case 0x02: /* <CR> */
332 *cp = 0x0d;
333 break;
334 case 0x03: /* <ctrl-Z> */
335 *cp = 0x1a;
336 break;
340 return (1);