source: icGREP/icgrep-devel/cudd-2.5.1/dddmp/dddmpUtil.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: 10.5 KB
Line 
1/**CFile**********************************************************************
2
3  FileName     [dddmpUtil.c]
4
5  PackageName  [dddmp]
6
7  Synopsis     [Util Functions for the dddmp package]
8
9  Description  [Functions to manipulate arrays.]
10
11  Author       [Gianpiero Cabodi and Stefano Quer]
12
13  Copyright    [
14    Copyright (c) 2004 by Politecnico di Torino.
15    All Rights Reserved. This software is for educational purposes only.
16    Permission is given to academic institutions to use, copy, and modify
17    this software and its documentation provided that this introductory
18    message is not removed, that this software and its documentation is
19    used for the institutions' internal research and educational purposes,
20    and that no monies are exchanged. No guarantee is expressed or implied
21    by the distribution of this code.
22    Send bug-reports and/or questions to:
23    {gianpiero.cabodi,stefano.quer}@polito.it.
24    ]
25
26******************************************************************************/
27
28#include "dddmpInt.h"
29
30/*---------------------------------------------------------------------------*/
31/* Stucture declarations                                                     */
32/*---------------------------------------------------------------------------*/
33
34/*---------------------------------------------------------------------------*/
35/* Type declarations                                                         */
36/*---------------------------------------------------------------------------*/
37
38/*---------------------------------------------------------------------------*/
39/* Variable declarations                                                     */
40/*---------------------------------------------------------------------------*/
41
42/*---------------------------------------------------------------------------*/
43/* Macro declarations                                                        */
44/*---------------------------------------------------------------------------*/
45
46/**AutomaticStart*************************************************************/
47
48/*---------------------------------------------------------------------------*/
49/* Static function prototypes                                                */
50/*---------------------------------------------------------------------------*/
51
52
53/**AutomaticEnd***************************************************************/
54
55/*---------------------------------------------------------------------------*/
56/* Definition of exported functions                                          */
57/*---------------------------------------------------------------------------*/
58
59/*---------------------------------------------------------------------------*/
60/* Definition of internal functions                                          */
61/*---------------------------------------------------------------------------*/
62
63/**Function********************************************************************
64
65  Synopsis    [String compare for qsort]
66
67  Description [String compare for qsort]
68
69  SideEffects [None]
70
71  SeeAlso     []
72
73******************************************************************************/
74
75int
76QsortStrcmp(
77  const void *ps1   /* IN: pointer to the first string */,
78  const void *ps2   /* IN: pointer to the second string */
79  )
80{
81  return (strcmp (*((char**)ps1),*((char **)ps2)));
82}
83
84/**Function********************************************************************
85
86  Synopsis    [Performs binary search of a name within a sorted array]
87
88  Description [Binary search of a name within a sorted array of strings.
89    Used when matching names of variables.
90    ]
91
92  SideEffects [None]
93
94  SeeAlso     []
95
96******************************************************************************/
97
98int
99FindVarname (
100  char *name   /* IN: name to look for */,
101  char **array /* IN: search array */,
102  int n        /* IN: size of the array */
103  )
104{
105  int d, m, u, t;
106
107  d = 0; u = n-1;
108
109  while (u>=d) {
110    m = (u+d)/2;
111    t=strcmp(name,array[m]);
112    if (t==0)
113      return m;
114    if (t<0)
115      u=m-1;
116    else
117      d=m+1;
118  }
119
120  return (-1);
121}
122
123
124/**Function********************************************************************
125
126  Synopsis    [Duplicates a string]
127
128  Description [Allocates memory and copies source string]
129
130  SideEffects [None]
131
132  SeeAlso     []
133
134******************************************************************************/
135
136char *
137DddmpStrDup (
138  char *str   /* IN: string to be duplicated */
139  )
140{
141  char *str2;
142
143  str2 = DDDMP_ALLOC(char,strlen(str)+1);
144  if (str2 != NULL) {
145    strcpy (str2,str);
146  }
147
148  return (str2);
149}
150
151/**Function********************************************************************
152
153  Synopsis    [Duplicates an array of strings]
154
155  Description [Allocates memory and copies source array]
156
157  SideEffects [None]
158
159  SeeAlso     []
160
161******************************************************************************/
162
163char **
164DddmpStrArrayDup (
165  char **array    /* IN: array of strings to be duplicated */,
166  int n           /* IN: size of the array */
167  )
168{
169  char **array2;
170  int i;
171
172  array2 = DDDMP_ALLOC(char *, n);
173  if (array2 == NULL) {
174    (void) fprintf (stderr, "DddmpStrArrayDup: Error allocating memory\n");
175    fflush (stderr);
176    return NULL;
177  }
178
179  /*
180   * initialize all slots to NULL for fair FREEing in case of failure
181   */
182
183  for (i=0; i<n; i++) {
184    array2[i] = NULL;
185  }
186
187  for (i=0; i<n; i++) { 
188    if (array[i] != NULL) {
189      if ((array2[i]=DddmpStrDup(array[i]))==NULL) {
190        DddmpStrArrayFree (array2, n);
191        return (NULL);
192      }
193    }
194  }
195
196  return (array2);
197}
198
199/**Function********************************************************************
200
201  Synopsis    [Inputs an array of strings]
202
203  Description [Allocates memory and inputs source array]
204
205  SideEffects [None]
206
207  SeeAlso     []
208
209******************************************************************************/
210
211char **
212DddmpStrArrayRead (
213  FILE *fp         /* IN: input file */,
214  int n            /* IN: size of the array */
215  )
216{
217  char buf[DDDMP_MAXSTRLEN];
218  char **array;
219  int i;
220
221  assert(fp!=NULL);
222
223  array = DDDMP_ALLOC(char *, n);
224  if (array == NULL) {
225    (void) fprintf (stderr, "DddmpStrArrayRead: Error allocating memory\n");
226    fflush (stderr);
227    return NULL;
228  }
229
230  /*
231   * initialize all slots to NULL for fair FREEing in case of failure
232   */
233  for (i=0; i<n; i++) 
234    array[i] = NULL;
235
236  for (i=0; i < n; i++) { 
237    if (fscanf (fp, "%s", buf)==EOF) {
238      fprintf (stderr, "DddmpStrArrayRead: Error reading file - EOF found\n");
239      fflush (stderr);
240      DddmpStrArrayFree (array, n);
241      return (NULL);
242    }
243    if ((array[i]=DddmpStrDup(buf))==NULL) {
244      DddmpStrArrayFree (array, n);
245      return (NULL);
246    }
247  }
248
249  return (array);
250}
251
252/**Function********************************************************************
253
254  Synopsis    [Outputs an array of strings]
255
256  Description [Outputs an array of strings to a specified file]
257
258  SideEffects [None]
259
260  SeeAlso     []
261
262******************************************************************************/
263
264int
265DddmpStrArrayWrite (
266  FILE *fp          /* IN: output file */,
267  char **array      /* IN: array of strings */,
268  int n             /* IN: size of the array */
269  )
270{
271  int i;
272
273  assert(fp!=NULL);
274
275  for (i=0; i<n; i++) { 
276    if (fprintf (fp, " %s", array[i]) == EOF) {
277      fprintf (stderr, "DddmpStrArrayWrite: Error writing to file\n");
278      fflush (stderr);
279      return (EOF);
280    }
281  }
282
283  return (n);
284}
285
286
287/**Function********************************************************************
288
289  Synopsis    [Frees an array of strings]
290
291  Description [Frees memory for strings and the array of pointers]
292
293  SideEffects [None]
294
295  SeeAlso     []
296
297******************************************************************************/
298
299void
300DddmpStrArrayFree (
301  char **array      /* IN: array of strings */,
302  int n             /* IN: size of the array */
303  )
304{
305  int i;
306
307  if (array == NULL) {
308    return;
309  }
310
311  for (i=0; i<n; i++) {
312    DDDMP_FREE (array[i]);
313  }
314
315  DDDMP_FREE (array);
316
317  return;
318}
319
320/**Function********************************************************************
321
322  Synopsis    [Duplicates an array of ints]
323
324  Description [Allocates memory and copies source array]
325
326  SideEffects [None]
327
328  SeeAlso     []
329
330******************************************************************************/
331
332int *
333DddmpIntArrayDup (
334  int *array      /* IN: array of ints to be duplicated */,
335  int n           /* IN: size of the array */
336  )
337{
338  int *array2;
339  int i;
340
341  array2 = DDDMP_ALLOC(int, n);
342  if (array2 == NULL) {
343    (void) fprintf (stderr, "DddmpIntArrayDup: Error allocating memory\n");
344    fflush (stderr);
345    return (NULL);
346  }
347
348  for (i=0; i<n; i++) { 
349    array2[i] = array[i];
350  }
351
352  return (array2);
353}
354
355
356/**Function********************************************************************
357
358  Synopsis    [Inputs an array of ints]
359
360  Description [Allocates memory and inputs source array]
361
362  SideEffects [None]
363
364  SeeAlso     []
365
366******************************************************************************/
367
368int *
369DddmpIntArrayRead (
370  FILE *fp          /* IN: input file */,
371  int n             /* IN: size of the array */
372  )
373{
374  int *array;
375  int i;
376
377  assert(fp!=NULL);
378
379  array = DDDMP_ALLOC(int, n);
380  if (array == NULL) {
381    (void) fprintf (stderr, "DddmpIntArrayRead: Error allocating memory\n");
382    fflush (stderr);
383    return NULL;
384  }
385
386  for (i=0; i < n; i++) { 
387    if (fscanf (fp, "%d", &array[i])==EOF) {
388      (void) fprintf (stderr,
389        "DddmpIntArrayRead: Error reading file - EOF found\n");
390      fflush (stderr);
391      DDDMP_FREE (array);
392      return (NULL);
393    }
394  }
395
396  return (array);
397}
398
399/**Function********************************************************************
400
401  Synopsis    [Outputs an array of ints]
402
403  Description [Outputs an array of ints to a specified file]
404
405  SideEffects [None]
406
407  SeeAlso     []
408
409******************************************************************************/
410
411int
412DddmpIntArrayWrite (
413  FILE *fp          /* IN: output file */,
414  int  *array       /* IN: array of ints */,
415  int n              /* IN: size of the array */
416  )
417{
418  int i;
419
420  assert(fp!=NULL);
421
422  for (i=0; i<n; i++) { 
423    if (fprintf (fp, " %d", array[i]) == EOF) {
424      (void) fprintf (stderr, "DddmpIntArrayWrite: Error writing to file\n");
425      fflush (stderr);
426      return EOF;
427    }
428  }
429
430  return (n);
431}
432
433/*---------------------------------------------------------------------------*/
434/* Definition of static functions                                            */
435/*---------------------------------------------------------------------------*/
436
Note: See TracBrowser for help on using the repository browser.