You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
10 KiB

3 months ago
  1. /**CFile**********************************************************************
  2. FileName [dddmpUtil.c]
  3. PackageName [dddmp]
  4. Synopsis [Util Functions for the dddmp package]
  5. Description [Functions to manipulate arrays.]
  6. Author [Gianpiero Cabodi and Stefano Quer]
  7. Copyright [
  8. Copyright (c) 2004 by Politecnico di Torino.
  9. All Rights Reserved. This software is for educational purposes only.
  10. Permission is given to academic institutions to use, copy, and modify
  11. this software and its documentation provided that this introductory
  12. message is not removed, that this software and its documentation is
  13. used for the institutions' internal research and educational purposes,
  14. and that no monies are exchanged. No guarantee is expressed or implied
  15. by the distribution of this code.
  16. Send bug-reports and/or questions to:
  17. {gianpiero.cabodi,stefano.quer}@polito.it.
  18. ]
  19. ******************************************************************************/
  20. #include "dddmpInt.h"
  21. /*---------------------------------------------------------------------------*/
  22. /* Stucture declarations */
  23. /*---------------------------------------------------------------------------*/
  24. /*---------------------------------------------------------------------------*/
  25. /* Type declarations */
  26. /*---------------------------------------------------------------------------*/
  27. /*---------------------------------------------------------------------------*/
  28. /* Variable declarations */
  29. /*---------------------------------------------------------------------------*/
  30. /*---------------------------------------------------------------------------*/
  31. /* Macro declarations */
  32. /*---------------------------------------------------------------------------*/
  33. /**AutomaticStart*************************************************************/
  34. /*---------------------------------------------------------------------------*/
  35. /* Static function prototypes */
  36. /*---------------------------------------------------------------------------*/
  37. /**AutomaticEnd***************************************************************/
  38. /*---------------------------------------------------------------------------*/
  39. /* Definition of exported functions */
  40. /*---------------------------------------------------------------------------*/
  41. /*---------------------------------------------------------------------------*/
  42. /* Definition of internal functions */
  43. /*---------------------------------------------------------------------------*/
  44. /**Function********************************************************************
  45. Synopsis [String compare for qsort]
  46. Description [String compare for qsort]
  47. SideEffects [None]
  48. SeeAlso []
  49. ******************************************************************************/
  50. int
  51. QsortStrcmp(
  52. const void *ps1 /* IN: pointer to the first string */,
  53. const void *ps2 /* IN: pointer to the second string */
  54. )
  55. {
  56. return (strcmp (*((char**)ps1),*((char **)ps2)));
  57. }
  58. /**Function********************************************************************
  59. Synopsis [Performs binary search of a name within a sorted array]
  60. Description [Binary search of a name within a sorted array of strings.
  61. Used when matching names of variables.
  62. ]
  63. SideEffects [None]
  64. SeeAlso []
  65. ******************************************************************************/
  66. int
  67. FindVarname (
  68. char *name /* IN: name to look for */,
  69. char **array /* IN: search array */,
  70. int n /* IN: size of the array */
  71. )
  72. {
  73. int d, m, u, t;
  74. d = 0; u = n-1;
  75. while (u>=d) {
  76. m = (u+d)/2;
  77. t=strcmp(name,array[m]);
  78. if (t==0)
  79. return m;
  80. if (t<0)
  81. u=m-1;
  82. else
  83. d=m+1;
  84. }
  85. return (-1);
  86. }
  87. /**Function********************************************************************
  88. Synopsis [Duplicates a string]
  89. Description [Allocates memory and copies source string]
  90. SideEffects [None]
  91. SeeAlso []
  92. ******************************************************************************/
  93. char *
  94. DddmpStrDup (
  95. char *str /* IN: string to be duplicated */
  96. )
  97. {
  98. char *str2;
  99. str2 = DDDMP_ALLOC(char,strlen(str)+1);
  100. if (str2 != NULL) {
  101. strcpy (str2,str);
  102. }
  103. return (str2);
  104. }
  105. /**Function********************************************************************
  106. Synopsis [Duplicates an array of strings]
  107. Description [Allocates memory and copies source array]
  108. SideEffects [None]
  109. SeeAlso []
  110. ******************************************************************************/
  111. char **
  112. DddmpStrArrayDup (
  113. char **array /* IN: array of strings to be duplicated */,
  114. int n /* IN: size of the array */
  115. )
  116. {
  117. char **array2;
  118. int i;
  119. array2 = DDDMP_ALLOC(char *, n);
  120. if (array2 == NULL) {
  121. (void) fprintf (stderr, "DddmpStrArrayDup: Error allocating memory\n");
  122. fflush (stderr);
  123. return NULL;
  124. }
  125. /*
  126. * initialize all slots to NULL for fair FREEing in case of failure
  127. */
  128. for (i=0; i<n; i++) {
  129. array2[i] = NULL;
  130. }
  131. for (i=0; i<n; i++) {
  132. if (array[i] != NULL) {
  133. if ((array2[i]=DddmpStrDup(array[i]))==NULL) {
  134. DddmpStrArrayFree (array2, n);
  135. return (NULL);
  136. }
  137. }
  138. }
  139. return (array2);
  140. }
  141. /**Function********************************************************************
  142. Synopsis [Inputs an array of strings]
  143. Description [Allocates memory and inputs source array]
  144. SideEffects [None]
  145. SeeAlso []
  146. ******************************************************************************/
  147. char **
  148. DddmpStrArrayRead (
  149. FILE *fp /* IN: input file */,
  150. int n /* IN: size of the array */
  151. )
  152. {
  153. char buf[DDDMP_MAXSTRLEN];
  154. char **array;
  155. int i;
  156. assert(fp!=NULL);
  157. array = DDDMP_ALLOC(char *, n);
  158. if (array == NULL) {
  159. (void) fprintf (stderr, "DddmpStrArrayRead: Error allocating memory\n");
  160. fflush (stderr);
  161. return NULL;
  162. }
  163. /*
  164. * initialize all slots to NULL for fair FREEing in case of failure
  165. */
  166. for (i=0; i<n; i++)
  167. array[i] = NULL;
  168. for (i=0; i < n; i++) {
  169. if (fscanf (fp, "%s", buf)==EOF) {
  170. fprintf (stderr, "DddmpStrArrayRead: Error reading file - EOF found\n");
  171. fflush (stderr);
  172. DddmpStrArrayFree (array, n);
  173. return (NULL);
  174. }
  175. if ((array[i]=DddmpStrDup(buf))==NULL) {
  176. DddmpStrArrayFree (array, n);
  177. return (NULL);
  178. }
  179. }
  180. return (array);
  181. }
  182. /**Function********************************************************************
  183. Synopsis [Outputs an array of strings]
  184. Description [Outputs an array of strings to a specified file]
  185. SideEffects [None]
  186. SeeAlso []
  187. ******************************************************************************/
  188. int
  189. DddmpStrArrayWrite (
  190. FILE *fp /* IN: output file */,
  191. char **array /* IN: array of strings */,
  192. int n /* IN: size of the array */
  193. )
  194. {
  195. int i;
  196. assert(fp!=NULL);
  197. for (i=0; i<n; i++) {
  198. if (fprintf (fp, " %s", array[i]) == EOF) {
  199. fprintf (stderr, "DddmpStrArrayWrite: Error writing to file\n");
  200. fflush (stderr);
  201. return (EOF);
  202. }
  203. }
  204. return (n);
  205. }
  206. /**Function********************************************************************
  207. Synopsis [Frees an array of strings]
  208. Description [Frees memory for strings and the array of pointers]
  209. SideEffects [None]
  210. SeeAlso []
  211. ******************************************************************************/
  212. void
  213. DddmpStrArrayFree (
  214. char **array /* IN: array of strings */,
  215. int n /* IN: size of the array */
  216. )
  217. {
  218. int i;
  219. if (array == NULL) {
  220. return;
  221. }
  222. for (i=0; i<n; i++) {
  223. DDDMP_FREE (array[i]);
  224. }
  225. DDDMP_FREE (array);
  226. return;
  227. }
  228. /**Function********************************************************************
  229. Synopsis [Duplicates an array of ints]
  230. Description [Allocates memory and copies source array]
  231. SideEffects [None]
  232. SeeAlso []
  233. ******************************************************************************/
  234. int *
  235. DddmpIntArrayDup (
  236. int *array /* IN: array of ints to be duplicated */,
  237. int n /* IN: size of the array */
  238. )
  239. {
  240. int *array2;
  241. int i;
  242. array2 = DDDMP_ALLOC(int, n);
  243. if (array2 == NULL) {
  244. (void) fprintf (stderr, "DddmpIntArrayDup: Error allocating memory\n");
  245. fflush (stderr);
  246. return (NULL);
  247. }
  248. for (i=0; i<n; i++) {
  249. array2[i] = array[i];
  250. }
  251. return (array2);
  252. }
  253. /**Function********************************************************************
  254. Synopsis [Inputs an array of ints]
  255. Description [Allocates memory and inputs source array]
  256. SideEffects [None]
  257. SeeAlso []
  258. ******************************************************************************/
  259. int *
  260. DddmpIntArrayRead (
  261. FILE *fp /* IN: input file */,
  262. int n /* IN: size of the array */
  263. )
  264. {
  265. int *array;
  266. int i;
  267. assert(fp!=NULL);
  268. array = DDDMP_ALLOC(int, n);
  269. if (array == NULL) {
  270. (void) fprintf (stderr, "DddmpIntArrayRead: Error allocating memory\n");
  271. fflush (stderr);
  272. return NULL;
  273. }
  274. for (i=0; i < n; i++) {
  275. if (fscanf (fp, "%d", &array[i])==EOF) {
  276. (void) fprintf (stderr,
  277. "DddmpIntArrayRead: Error reading file - EOF found\n");
  278. fflush (stderr);
  279. DDDMP_FREE (array);
  280. return (NULL);
  281. }
  282. }
  283. return (array);
  284. }
  285. /**Function********************************************************************
  286. Synopsis [Outputs an array of ints]
  287. Description [Outputs an array of ints to a specified file]
  288. SideEffects [None]
  289. SeeAlso []
  290. ******************************************************************************/
  291. int
  292. DddmpIntArrayWrite (
  293. FILE *fp /* IN: output file */,
  294. int *array /* IN: array of ints */,
  295. int n /* IN: size of the array */
  296. )
  297. {
  298. int i;
  299. assert(fp!=NULL);
  300. for (i=0; i<n; i++) {
  301. if (fprintf (fp, " %d", array[i]) == EOF) {
  302. (void) fprintf (stderr, "DddmpIntArrayWrite: Error writing to file\n");
  303. fflush (stderr);
  304. return EOF;
  305. }
  306. }
  307. return (n);
  308. }
  309. /*---------------------------------------------------------------------------*/
  310. /* Definition of static functions */
  311. /*---------------------------------------------------------------------------*/