source: icGREP/icgrep-devel/cudd-2.5.1/dddmp/dddmpBinary.c @ 4597

Last change on this file since 4597 was 4597, checked in by nmedfort, 4 years ago

Upload of the CUDD library.

File size: 9.4 KB
Line 
1/**CFile**********************************************************************
2
3  FileName    [dddmpBinary.c]
4
5  PackageName [dddmp]
6
7  Synopsis    [Input and output BDD codes and integers from/to file]
8
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.
17    ]
18
19  Author      [Gianpiero Cabodi and Stefano Quer]
20
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.
32    ]
33
34******************************************************************************/
35
36#include "dddmpInt.h"
37
38/*---------------------------------------------------------------------------*/
39/* Stucture declarations                                                     */
40/*---------------------------------------------------------------------------*/
41
42/*---------------------------------------------------------------------------*/
43/* Type declarations                                                         */
44/*---------------------------------------------------------------------------*/
45
46/*---------------------------------------------------------------------------*/
47/* Variable declarations                                                     */
48/*---------------------------------------------------------------------------*/
49
50/*---------------------------------------------------------------------------*/
51/* Macro declarations                                                        */
52/*---------------------------------------------------------------------------*/
53
54/**AutomaticStart*************************************************************/
55
56/*---------------------------------------------------------------------------*/
57/* Static function prototypes                                                */
58/*---------------------------------------------------------------------------*/
59
60static int WriteByteBinary(FILE *fp, unsigned char c);
61static int ReadByteBinary(FILE *fp, unsigned char *cp);
62
63/**AutomaticEnd***************************************************************/
64
65
66/*---------------------------------------------------------------------------*/
67/* Definition of exported functions                                          */
68/*---------------------------------------------------------------------------*/
69
70
71/*---------------------------------------------------------------------------*/
72/* Definition of internal functions                                          */
73/*---------------------------------------------------------------------------*/
74
75/**Function********************************************************************
76
77  Synopsis    [Writes 1 byte node code]
78
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.
88    ]
89
90  SideEffects [None]
91
92  SeeAlso     [DddmpReadCode()]
93
94******************************************************************************/
95
96int
97DddmpWriteCode (
98  FILE *fp                   /* IN: file where to write the code */,
99  struct binary_dd_code code /* IN: the code to be written */
100  )
101{
102  unsigned char c;
103  int retValue;
104
105  c = (code.Unused<<7)|(code.V<<5)|(code.T<<3)|
106                (code.Ecompl<<2)|(code.E);
107
108  retValue = WriteByteBinary (fp, c);
109
110  return (retValue);
111}
112
113/**Function********************************************************************
114
115  Synopsis    [Reads a 1 byte node code]
116
117  Description [Reads a 1 byte node code. See DddmpWriteCode()
118    for code description.]
119
120  SideEffects [None]
121
122  SeeAlso     [DddmpWriteCode()]
123
124******************************************************************************/
125
126int 
127DddmpReadCode (
128  FILE *fp                     /*  IN: file where to read the code */,
129  struct binary_dd_code *pcode /* OUT: the read code */
130  )
131{
132  unsigned char c;
133
134  if (ReadByteBinary (fp, &c) == EOF) {
135    return (0);
136  }
137
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;
143
144  return (1);
145}
146
147/**Function********************************************************************
148
149  Synopsis    [Writes a "packed integer"]
150
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).
154    ]
155
156  SideEffects [None]
157
158  SeeAlso     [DddmpReadInt()]
159
160******************************************************************************/
161
162int 
163DddmpWriteInt (
164  FILE *fp   /* IN: file where to write the integer */,
165  int id     /* IN: integer to be written */
166  )
167{
168  char cvet[4];
169  int i;
170
171  for (i=0; i<4; i++) {
172    cvet[i] = (char)((id & 0x0000007f) << 1);
173    id >>= 7;
174  }
175
176  for (i=3; (i>0) && (cvet[i] == 0); i--);
177
178  for (; i>0; i--) {
179    cvet[i] |= (char)1;
180    if (WriteByteBinary (fp, cvet[i]) == EOF)
181      return (0);
182  }
183
184  if (WriteByteBinary (fp, cvet[0]) == EOF) {
185    return (0);
186  }
187
188  return (1);
189}
190
191
192/**Function********************************************************************
193
194  Synopsis    [Reads a "packed integer"]
195
196  Description [Reads an integer coded on a sequence of bytes. See
197    DddmpWriteInt() for format.]
198
199  SideEffects [None]
200
201  SeeAlso     [DddmpWriteInt()]
202
203******************************************************************************/
204
205int
206DddmpReadInt (
207  FILE *fp   /*  IN: file where to read the integer */,
208  int *pid   /* OUT: the read integer */
209  )
210{
211  unsigned char c;
212  int i;
213  unsigned int id;
214
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;
222  }
223
224 /* Check for correct format: last char should
225    be found before i = 4 */
226  assert(i<4);
227
228  *pid = id;
229
230  return (i+1);
231}
232
233/*---------------------------------------------------------------------------*/
234/* Definition of static functions                                            */
235/*---------------------------------------------------------------------------*/
236
237/**Function********************************************************************
238
239  Synopsis    [Writes a byte to file filtering <CR>, <LF> and <ctrl-Z>]
240
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.
244    ]
245
246  SideEffects [None]
247
248  SeeAlso     [ReadByteBinary()]
249
250******************************************************************************/
251
252static int
253WriteByteBinary (
254  FILE *fp         /* IN: file where to write the byte */,
255  unsigned char c  /* IN: the byte to be written */
256  )
257{
258  unsigned char BinaryEscape;
259 
260  switch (c) {
261
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;
286  }
287  if (fwrite (&c, sizeof(char), 1, fp) != sizeof(char))
288    return (0);
289
290  return (1);
291}
292
293/**Function********************************************************************
294
295  Synopsis    [Reads a byte from file with escaped <CR>, <LF> and <ctrl-Z>]
296
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.
300    ]
301
302  SideEffects [None]
303
304  SeeAlso     [WriteByteBinary()]
305
306******************************************************************************/
307
308static int
309ReadByteBinary (
310  FILE *fp           /*  IN: file where to read the byte */,
311  unsigned char *cp  /* OUT: the read byte */
312  )
313{
314 
315  if (fread (cp, sizeof(char), 1, fp) != sizeof(char)) {
316    return (0);
317  }
318
319  if (*cp == 0x00) { /* Escape */
320    if (fread (cp, sizeof(char), 1, fp) != sizeof(char)) {
321      return (0);
322    }
323
324    switch (*cp) {
325
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;
337    }
338  }
339
340  return (1);
341}
342
343
Note: See TracBrowser for help on using the repository browser.