1 /**CFile**********************************************************************
3 FileName [dddmpBinary.c]
7 Synopsis [Input and output BDD codes and integers from/to file]
9 Description [Input and output BDD codes and integers from/to file
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]
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 ******************************************************************************/
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:
82 V : 2 bits; (variable code)
83 T : 2 bits; (Then code)
84 Ecompl : 1 bit; (Else complemented)
85 E : 2 bits; (Else code)
87 Ecompl is set with complemented edges.
92 SeeAlso [DddmpReadCode()]
94 ******************************************************************************/
98 FILE *fp
/* IN: file where to write the code */,
99 struct binary_dd_code code
/* IN: the code to be written */
105 c
= (code
.Unused
<<7)|(code
.V
<<5)|(code
.T
<<3)|
106 (code
.Ecompl
<<2)|(code
.E
);
108 retValue
= WriteByteBinary (fp
, c
);
113 /**Function********************************************************************
115 Synopsis [Reads a 1 byte node code]
117 Description [Reads a 1 byte node code. See DddmpWriteCode()
118 for code description.]
122 SeeAlso [DddmpWriteCode()]
124 ******************************************************************************/
128 FILE *fp
/* IN: file where to read the code */,
129 struct binary_dd_code
*pcode
/* OUT: the read code */
134 if (ReadByteBinary (fp
, &c
) == EOF
) {
138 pcode
->Unused
= c
>>7;
139 pcode
->V
= (c
>>5) & 3;
140 pcode
->T
= (c
>>3) & 3;
141 pcode
->Ecompl
= (c
>>2) & 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).
158 SeeAlso [DddmpReadInt()]
160 ******************************************************************************/
164 FILE *fp
/* IN: file where to write the integer */,
165 int id
/* IN: integer to be written */
171 for (i
=0; i
<4; i
++) {
172 cvet
[i
] = (char)((id
& 0x0000007f) << 1);
176 for (i
=3; (i
>0) && (cvet
[i
] == 0); i
--);
180 if (WriteByteBinary (fp
, cvet
[i
]) == EOF
)
184 if (WriteByteBinary (fp
, cvet
[0]) == EOF
) {
192 /**Function********************************************************************
194 Synopsis [Reads a "packed integer"]
196 Description [Reads an integer coded on a sequence of bytes. See
197 DddmpWriteInt() for format.]
201 SeeAlso [DddmpWriteInt()]
203 ******************************************************************************/
207 FILE *fp
/* IN: file where to read the integer */,
208 int *pid
/* OUT: the read integer */
216 for (i
=0; i
<4; i
++) {
217 if (ReadByteBinary (fp
, &c
) == EOF
)
219 id
= (id
<<7) | (c
>>1);
224 /* Check for correct format: last char should
225 be found before i = 4 */
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.
248 SeeAlso [ReadByteBinary()]
250 ******************************************************************************/
254 FILE *fp
/* IN: file where to write the byte */,
255 unsigned char c
/* IN: the byte to be written */
258 unsigned char BinaryEscape
;
262 case 0x00: /* Escape */
264 if (fwrite (&BinaryEscape
, sizeof(char), 1, fp
) != sizeof(char))
268 case 0x0a: /* <LF> */
270 if (fwrite (&BinaryEscape
, sizeof(char), 1, fp
) != sizeof(char))
274 case 0x0d: /* <CR> */
276 if (fwrite (&BinaryEscape
, sizeof(char), 1, fp
) != sizeof(char))
280 case 0x1a: /* <ctrl-Z> */
282 if (fwrite (&BinaryEscape
, sizeof(char), 1, fp
) != sizeof(char))
287 if (fwrite (&c
, sizeof(char), 1, fp
) != sizeof(char))
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.
304 SeeAlso [WriteByteBinary()]
306 ******************************************************************************/
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)) {
319 if (*cp
== 0x00) { /* Escape */
320 if (fread (cp
, sizeof(char), 1, fp
) != sizeof(char)) {
326 case 0x00: /* Escape */
328 case 0x01: /* <LF> */
331 case 0x02: /* <CR> */
334 case 0x03: /* <ctrl-Z> */