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