1 /*************************************************************************\
2 *
3 * Package: MyLib
4 * File: util.c
5 * Environment: ANSI C
6 *
7 * Copyright (c) 2002 Pierre L'Ecuyer, DIRO, Université de Montréal.
8 * e-mail: lecuyer@iro.umontreal.ca
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted without a fee for private, research,
13 * academic, or other non-commercial purposes.
14 * Any use of this software in a commercial environment requires a
15 * written licence from the copyright owner.
16 *
17 * Any changes made to this package must be clearly identified as such.
18 *
19 * In scientific publications which used this software, a reference to it
20 * would be appreciated.
21 *
22 * Redistributions of source code must retain this copyright notice
23 * and the following disclaimer.
24 *
25 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 *
29 \*************************************************************************/
30
31 #include "util.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37
38
39 #define MAXCAR 256 /* Max length of a line of data */
40
41
42
43 /************************************************************************/
44
util_Fopen(const char * path,const char * mode)45 FILE *util_Fopen (const char *path, const char *mode)
46 {
47 FILE *f;
48 errno = 0;
49 f = fopen (path, mode);
50 if (f == NULL) {
51 fprintf (stdout, "\nOpening of %s failed: %s\n\n",
52 path, strerror (errno));
53 exit (EXIT_FAILURE);
54 return NULL; /* to eliminate a warning from the compiler */
55 } else
56 return f;
57 }
58
util_Fclose(FILE * f)59 int util_Fclose (FILE * f)
60 {
61 int s;
62 if (f == NULL)
63 return 0;
64 errno = 0;
65 s = fclose (f);
66 if (s != 0)
67 fprintf (stdout, "\nClosing of file failed: %s\n\n", strerror (errno));
68 return s;
69 }
70
71
72 /************************************************************************/
73
util_Malloc(size_t size)74 void *util_Malloc (size_t size)
75 {
76 void *p;
77 errno = 0;
78 p = malloc (size);
79 if (p == NULL) {
80 fprintf (stdout, "\nmalloc failed: %s\n\n", strerror (errno));
81 exit (EXIT_FAILURE);
82 return NULL; /* to eliminate a warning from the compiler */
83 } else
84 return p;
85 }
86
util_Calloc(size_t count,size_t esize)87 void *util_Calloc (size_t count, size_t esize)
88 {
89 void *p;
90 errno = 0;
91 p = calloc (count, esize);
92 if (p == NULL) {
93 fprintf (stdout, "\ncalloc failed: %s\n\n", strerror (errno));
94 exit (EXIT_FAILURE);
95 return NULL; /* to eliminate a warning from the compiler */
96 } else
97 return p;
98 }
99
util_Realloc(void * ptr,size_t size)100 void *util_Realloc (void *ptr, size_t size)
101 {
102 void *p;
103 errno = 0;
104 p = realloc (ptr, size);
105 if ((p == NULL) && (size != 0)) {
106 fprintf (stdout, "\nrealloc failed: %s\n\n", strerror (errno));
107 exit (EXIT_FAILURE);
108 return ptr; /* to eliminate a warning from the compiler */
109 } else
110 return p;
111
112 }
113
util_Free(void * p)114 void *util_Free (void *p)
115 {
116 if (p == NULL)
117 return NULL;
118 free (p);
119 return NULL;
120 }
121
122
123 /************************************************************************/
124
util_WriteBool(lebool b,int d)125 void util_WriteBool (lebool b, int d)
126 {
127 if (b)
128 printf ("%*s", d, "TRUE");
129 else
130 printf ("%*s", d, "FALSE");
131 }
132
133
util_ReadBool(char S[],lebool * x)134 void util_ReadBool (char S[], lebool *x)
135 {
136 int j;
137 char B[6];
138 j = sscanf (S, " %6s", B);
139 util_Assert (j > 0, "util_ReadBool: on reading lebool");
140 if (!strncmp (B, "TRUE", (size_t) 5))
141 *x = TRUE;
142 else if (!strncmp (B, "FALSE", (size_t) 6))
143 *x = FALSE;
144 else {
145 util_Error ("util_ReadBool: lebool values must be TRUE or FALSE");
146 }
147 }
148
149
150 /************************************************************************/
151
util_GetLine(FILE * infile,char * Line,char c)152 int util_GetLine (FILE *infile, char *Line, char c)
153 {
154 size_t j;
155
156 while (NULL != fgets (Line, MAXCAR, infile)) { /* Not EOF and no error */
157 /* Find first non-white character in Line */
158 j = strspn (Line, " \t\r\f\v");
159 /* Discard blank lines and lines whose first non-white character is c */
160 if (Line[j] == '\n' || Line[j] == c)
161 continue;
162 else {
163 char *p;
164 /* If the character c appears, delete the rest of the line*/
165 if ((p = strchr (Line, c)))
166 *p = '\0';
167
168 else {
169 /* Remove the \n char at the end of line */
170 j = strlen (Line);
171 if (Line[j - 1] == '\n')
172 Line[j - 1] = '\0';
173 }
174 return 0;
175 }
176 }
177
178 util_Fclose (infile);
179 return -1;
180 /* util_Error ("GetLine: an error has occurred on reading"); */
181 }
182