1 /**CFile**********************************************************************
7 Synopsis [Util Functions for the dddmp package]
9 Description [Functions to manipulate arrays.]
11 Author [Gianpiero Cabodi and Stefano Quer]
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 ******************************************************************************/
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]
73 ******************************************************************************/
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.
96 ******************************************************************************/
100 char *name
/* IN: name to look for */,
101 char **array
/* IN: search array */,
102 int n
/* IN: size of the array */
111 t
=strcmp(name
,array
[m
]);
124 /**Function********************************************************************
126 Synopsis [Duplicates a string]
128 Description [Allocates memory and copies source string]
134 ******************************************************************************/
138 char *str
/* IN: string to be duplicated */
143 str2
= DDDMP_ALLOC(char,strlen(str
)+1);
151 /**Function********************************************************************
153 Synopsis [Duplicates an array of strings]
155 Description [Allocates memory and copies source array]
161 ******************************************************************************/
165 char **array
/* IN: array of strings to be duplicated */,
166 int n
/* IN: size of the array */
172 array2
= DDDMP_ALLOC(char *, n
);
173 if (array2
== NULL
) {
174 (void) fprintf (stderr
, "DddmpStrArrayDup: Error allocating memory\n");
180 * initialize all slots to NULL for fair FREEing in case of failure
183 for (i
=0; i
<n
; i
++) {
187 for (i
=0; i
<n
; i
++) {
188 if (array
[i
] != NULL
) {
189 if ((array2
[i
]=DddmpStrDup(array
[i
]))==NULL
) {
190 DddmpStrArrayFree (array2
, n
);
199 /**Function********************************************************************
201 Synopsis [Inputs an array of strings]
203 Description [Allocates memory and inputs source array]
209 ******************************************************************************/
213 FILE *fp
/* IN: input file */,
214 int n
/* IN: size of the array */
217 char buf
[DDDMP_MAXSTRLEN
];
223 array
= DDDMP_ALLOC(char *, n
);
225 (void) fprintf (stderr
, "DddmpStrArrayRead: Error allocating memory\n");
231 * initialize all slots to NULL for fair FREEing in case of failure
236 for (i
=0; i
< n
; i
++) {
237 if (fscanf (fp
, "%s", buf
)==EOF
) {
238 fprintf (stderr
, "DddmpStrArrayRead: Error reading file - EOF found\n");
240 DddmpStrArrayFree (array
, n
);
243 if ((array
[i
]=DddmpStrDup(buf
))==NULL
) {
244 DddmpStrArrayFree (array
, n
);
252 /**Function********************************************************************
254 Synopsis [Outputs an array of strings]
256 Description [Outputs an array of strings to a specified file]
262 ******************************************************************************/
266 FILE *fp
/* IN: output file */,
267 char **array
/* IN: array of strings */,
268 int n
/* IN: size of the array */
275 for (i
=0; i
<n
; i
++) {
276 if (fprintf (fp
, " %s", array
[i
]) == EOF
) {
277 fprintf (stderr
, "DddmpStrArrayWrite: Error writing to file\n");
287 /**Function********************************************************************
289 Synopsis [Frees an array of strings]
291 Description [Frees memory for strings and the array of pointers]
297 ******************************************************************************/
301 char **array
/* IN: array of strings */,
302 int n
/* IN: size of the array */
311 for (i
=0; i
<n
; i
++) {
312 DDDMP_FREE (array
[i
]);
320 /**Function********************************************************************
322 Synopsis [Duplicates an array of ints]
324 Description [Allocates memory and copies source array]
330 ******************************************************************************/
334 int *array
/* IN: array of ints to be duplicated */,
335 int n
/* IN: size of the array */
341 array2
= DDDMP_ALLOC(int, n
);
342 if (array2
== NULL
) {
343 (void) fprintf (stderr
, "DddmpIntArrayDup: Error allocating memory\n");
348 for (i
=0; i
<n
; i
++) {
349 array2
[i
] = array
[i
];
356 /**Function********************************************************************
358 Synopsis [Inputs an array of ints]
360 Description [Allocates memory and inputs source array]
366 ******************************************************************************/
370 FILE *fp
/* IN: input file */,
371 int n
/* IN: size of the array */
379 array
= DDDMP_ALLOC(int, n
);
381 (void) fprintf (stderr
, "DddmpIntArrayRead: Error allocating memory\n");
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");
399 /**Function********************************************************************
401 Synopsis [Outputs an array of ints]
403 Description [Outputs an array of ints to a specified file]
409 ******************************************************************************/
413 FILE *fp
/* IN: output file */,
414 int *array
/* IN: array of ints */,
415 int n
/* IN: size of the array */
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");
433 /*---------------------------------------------------------------------------*/
434 /* Definition of static functions */
435 /*---------------------------------------------------------------------------*/