emergency commit
[cl-cudd.git] / distr / dddmp / dddmpUtil.c
blobfeff43943019cccd653f5ec9b049bc9f51fa8699
1 /**CFile**********************************************************************
3 FileName [dddmpUtil.c]
5 PackageName [dddmp]
7 Synopsis [Util Functions for the dddmp package]
9 Description [Functions to manipulate arrays.]
11 Author [Gianpiero Cabodi and Stefano Quer]
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.
26 ******************************************************************************/
28 #include "dddmpInt.h"
30 /*---------------------------------------------------------------------------*/
31 /* Stucture declarations */
32 /*---------------------------------------------------------------------------*/
34 /*---------------------------------------------------------------------------*/
35 /* Type declarations */
36 /*---------------------------------------------------------------------------*/
38 /*---------------------------------------------------------------------------*/
39 /* Variable declarations */
40 /*---------------------------------------------------------------------------*/
42 /*---------------------------------------------------------------------------*/
43 /* Macro declarations */
44 /*---------------------------------------------------------------------------*/
46 /**AutomaticStart*************************************************************/
48 /*---------------------------------------------------------------------------*/
49 /* Static function prototypes */
50 /*---------------------------------------------------------------------------*/
53 /**AutomaticEnd***************************************************************/
55 /*---------------------------------------------------------------------------*/
56 /* Definition of exported functions */
57 /*---------------------------------------------------------------------------*/
59 /*---------------------------------------------------------------------------*/
60 /* Definition of internal functions */
61 /*---------------------------------------------------------------------------*/
63 /**Function********************************************************************
65 Synopsis [String compare for qsort]
67 Description [String compare for qsort]
69 SideEffects [None]
71 SeeAlso []
73 ******************************************************************************/
75 int
76 QsortStrcmp(
77 const void *ps1 /* IN: pointer to the first string */,
78 const void *ps2 /* IN: pointer to the second string */
81 return (strcmp (*((char**)ps1),*((char **)ps2)));
84 /**Function********************************************************************
86 Synopsis [Performs binary search of a name within a sorted array]
88 Description [Binary search of a name within a sorted array of strings.
89 Used when matching names of variables.
92 SideEffects [None]
94 SeeAlso []
96 ******************************************************************************/
98 int
99 FindVarname (
100 char *name /* IN: name to look for */,
101 char **array /* IN: search array */,
102 int n /* IN: size of the array */
105 int d, m, u, t;
107 d = 0; u = n-1;
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;
120 return (-1);
124 /**Function********************************************************************
126 Synopsis [Duplicates a string]
128 Description [Allocates memory and copies source string]
130 SideEffects [None]
132 SeeAlso []
134 ******************************************************************************/
136 char *
137 DddmpStrDup (
138 char *str /* IN: string to be duplicated */
141 char *str2;
143 str2 = DDDMP_ALLOC(char,strlen(str)+1);
144 if (str2 != NULL) {
145 strcpy (str2,str);
148 return (str2);
151 /**Function********************************************************************
153 Synopsis [Duplicates an array of strings]
155 Description [Allocates memory and copies source array]
157 SideEffects [None]
159 SeeAlso []
161 ******************************************************************************/
163 char **
164 DddmpStrArrayDup (
165 char **array /* IN: array of strings to be duplicated */,
166 int n /* IN: size of the array */
169 char **array2;
170 int i;
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;
180 * initialize all slots to NULL for fair FREEing in case of failure
183 for (i=0; i<n; i++) {
184 array2[i] = NULL;
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);
196 return (array2);
199 /**Function********************************************************************
201 Synopsis [Inputs an array of strings]
203 Description [Allocates memory and inputs source array]
205 SideEffects [None]
207 SeeAlso []
209 ******************************************************************************/
211 char **
212 DddmpStrArrayRead (
213 FILE *fp /* IN: input file */,
214 int n /* IN: size of the array */
217 char buf[DDDMP_MAXSTRLEN];
218 char **array;
219 int i;
221 assert(fp!=NULL);
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;
231 * initialize all slots to NULL for fair FREEing in case of failure
233 for (i=0; i<n; i++)
234 array[i] = NULL;
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);
243 if ((array[i]=DddmpStrDup(buf))==NULL) {
244 DddmpStrArrayFree (array, n);
245 return (NULL);
249 return (array);
252 /**Function********************************************************************
254 Synopsis [Outputs an array of strings]
256 Description [Outputs an array of strings to a specified file]
258 SideEffects [None]
260 SeeAlso []
262 ******************************************************************************/
265 DddmpStrArrayWrite (
266 FILE *fp /* IN: output file */,
267 char **array /* IN: array of strings */,
268 int n /* IN: size of the array */
271 int i;
273 assert(fp!=NULL);
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);
283 return (n);
287 /**Function********************************************************************
289 Synopsis [Frees an array of strings]
291 Description [Frees memory for strings and the array of pointers]
293 SideEffects [None]
295 SeeAlso []
297 ******************************************************************************/
299 void
300 DddmpStrArrayFree (
301 char **array /* IN: array of strings */,
302 int n /* IN: size of the array */
305 int i;
307 if (array == NULL) {
308 return;
311 for (i=0; i<n; i++) {
312 DDDMP_FREE (array[i]);
315 DDDMP_FREE (array);
317 return;
320 /**Function********************************************************************
322 Synopsis [Duplicates an array of ints]
324 Description [Allocates memory and copies source array]
326 SideEffects [None]
328 SeeAlso []
330 ******************************************************************************/
332 int *
333 DddmpIntArrayDup (
334 int *array /* IN: array of ints to be duplicated */,
335 int n /* IN: size of the array */
338 int *array2;
339 int i;
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);
348 for (i=0; i<n; i++) {
349 array2[i] = array[i];
352 return (array2);
356 /**Function********************************************************************
358 Synopsis [Inputs an array of ints]
360 Description [Allocates memory and inputs source array]
362 SideEffects [None]
364 SeeAlso []
366 ******************************************************************************/
368 int *
369 DddmpIntArrayRead (
370 FILE *fp /* IN: input file */,
371 int n /* IN: size of the array */
374 int *array;
375 int i;
377 assert(fp!=NULL);
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;
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);
396 return (array);
399 /**Function********************************************************************
401 Synopsis [Outputs an array of ints]
403 Description [Outputs an array of ints to a specified file]
405 SideEffects [None]
407 SeeAlso []
409 ******************************************************************************/
412 DddmpIntArrayWrite (
413 FILE *fp /* IN: output file */,
414 int *array /* IN: array of ints */,
415 int n /* IN: size of the array */
418 int i;
420 assert(fp!=NULL);
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;
430 return (n);
433 /*---------------------------------------------------------------------------*/
434 /* Definition of static functions */
435 /*---------------------------------------------------------------------------*/