/**CFile********************************************************************** FileName [dddmpUtil.c] PackageName [dddmp] Synopsis [Util Functions for the dddmp package] Description [Functions to manipulate arrays.] Author [Gianpiero Cabodi and Stefano Quer] Copyright [ Copyright (c) 2004 by Politecnico di Torino. All Rights Reserved. This software is for educational purposes only. Permission is given to academic institutions to use, copy, and modify this software and its documentation provided that this introductory message is not removed, that this software and its documentation is used for the institutions' internal research and educational purposes, and that no monies are exchanged. No guarantee is expressed or implied by the distribution of this code. Send bug-reports and/or questions to: {gianpiero.cabodi,stefano.quer}@polito.it. ] ******************************************************************************/ #include "dddmpInt.h" /*---------------------------------------------------------------------------*/ /* Stucture declarations */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Type declarations */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Variable declarations */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Macro declarations */ /*---------------------------------------------------------------------------*/ /**AutomaticStart*************************************************************/ /*---------------------------------------------------------------------------*/ /* Static function prototypes */ /*---------------------------------------------------------------------------*/ /**AutomaticEnd***************************************************************/ /*---------------------------------------------------------------------------*/ /* Definition of exported functions */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Definition of internal functions */ /*---------------------------------------------------------------------------*/ /**Function******************************************************************** Synopsis [String compare for qsort] Description [String compare for qsort] SideEffects [None] SeeAlso [] ******************************************************************************/ int QsortStrcmp( const void *ps1 /* IN: pointer to the first string */, const void *ps2 /* IN: pointer to the second string */ ) { return (strcmp (*((char**)ps1),*((char **)ps2))); } /**Function******************************************************************** Synopsis [Performs binary search of a name within a sorted array] Description [Binary search of a name within a sorted array of strings. Used when matching names of variables. ] SideEffects [None] SeeAlso [] ******************************************************************************/ int FindVarname ( char *name /* IN: name to look for */, char **array /* IN: search array */, int n /* IN: size of the array */ ) { int d, m, u, t; d = 0; u = n-1; while (u>=d) { m = (u+d)/2; t=strcmp(name,array[m]); if (t==0) return m; if (t<0) u=m-1; else d=m+1; } return (-1); } /**Function******************************************************************** Synopsis [Duplicates a string] Description [Allocates memory and copies source string] SideEffects [None] SeeAlso [] ******************************************************************************/ char * DddmpStrDup ( char *str /* IN: string to be duplicated */ ) { char *str2; str2 = DDDMP_ALLOC(char,strlen(str)+1); if (str2 != NULL) { strcpy (str2,str); } return (str2); } /**Function******************************************************************** Synopsis [Duplicates an array of strings] Description [Allocates memory and copies source array] SideEffects [None] SeeAlso [] ******************************************************************************/ char ** DddmpStrArrayDup ( char **array /* IN: array of strings to be duplicated */, int n /* IN: size of the array */ ) { char **array2; int i; array2 = DDDMP_ALLOC(char *, n); if (array2 == NULL) { (void) fprintf (stderr, "DddmpStrArrayDup: Error allocating memory\n"); fflush (stderr); return NULL; } /* * initialize all slots to NULL for fair FREEing in case of failure */ for (i=0; i