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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * MISCELLANEOUS * */
/* * * */
/* * $Module: MISC * */
/* * * */
/* * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 * */
/* * MPI fuer Informatik * */
/* * * */
/* * This program is free software; you can redistribute * */
/* * it and/or modify it under the terms of the FreeBSD * */
/* * Licence. * */
/* * * */
/* * This program is distributed in the hope that it will * */
/* * be useful, but WITHOUT ANY WARRANTY; without even * */
/* * the implied warranty of MERCHANTABILITY or FITNESS * */
/* * FOR A PARTICULAR PURPOSE. See the LICENCE file * */
/* * for more details. * */
/* * * */
/* * * */
/* $Revision: 1.6 $ * */
/* $State: Exp $ * */
/* $Date: 2016-03-23 13:35:45 $ * */
/* $Author: weidenb $ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: spass@mpi-inf.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/* $RCSfile: misc.c,v $ */
/**************************************************************/
/* Includes */
/**************************************************************/
#include "misc.h"
#include "strings.h" /* Cannot be moved to misc.h as long as there is no separate type.h, e.g. for pointers */
/**************************************************************/
/* Functions */
/**************************************************************/
/**************************************************************/
/* previously Inlined Functions */
/**************************************************************/
void misc_Error(void)
{
fflush(misc_USERERROROUT);
fflush(stdout);
fflush(stderr);
#ifdef CHECK
abort();
#else
exit(EXIT_FAILURE);
#endif
}
BOOL misc_SmallerThan(int i, int j)
{
return (BOOL)(i < j);
}
/**************************************************************/
/* Inline Functions ends here */
/**************************************************************/
void misc_StartErrorReporting(const char* file, int line)
{
fflush(stdout);
fprintf(misc_ERROROUT,"\n\tError in file %s at line %d\n",file, line);
}
void misc_FinishErrorReport(void)
{
fputs("\n Please report this error via email to spass@mpi-sb.mpg.de including\n the SPASS version, input problem, options, operating system.\n",misc_ERROROUT);
misc_DumpCore();
}
void misc_ErrorReport(const char * Format, ...)
{
va_list args;
va_start(args,Format);
vfprintf(misc_ERROROUT,Format,args);
va_end(args);
}
void misc_UserWarning(const char * Format, ...)
{
va_list args;
va_start(args,Format);
vfprintf(misc_ERROROUT,Format,args);
va_end(args);
}
void misc_UserErrorReport(const char * Format, ...)
{
va_list args;
va_start(args,Format);
vfprintf(misc_USERERROROUT,Format,args);
va_end(args);
}
void misc_PrintChar(NAT Number, char Character)
/**************************************************************
INPUT: A positive number and a character.
RETURNS: Nothing.
EFFECT: Prints as many times the same character as the number
demands.
***************************************************************/
{
NAT Counter;
for (Counter = 1; Counter <= Number; Counter++)
putchar(Character);
}
void misc_DumpCore(void)
/**************************************************************
INPUT: Nothing.
RETURNS: Nothing.
EFFECT: Flushes the streams then dumps a core.
***************************************************************/
{
fputs("\n\n", misc_ERROROUT);
fflush(misc_ERROROUT);
fflush(stdout);
fflush(stderr);
abort();
}
void misc_DumpCoreOut(const char* String)
/**************************************************************
INPUT: A string.
RETURNS: Nothing.
EFFECT: Prints <String> and then dumps a core.
***************************************************************/
{
fprintf(stderr, "\n %s \n", String);
misc_DumpCore();
}
int misc_ReturnValue(void)
{
return 0;
}
int misc_Max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
FILE* misc_OpenFile(const char* Name, const char* Mode)
/**************************************************************
INPUT: The name of a file and a string containing the mode
for opening the file (see fopen(3)).
Examples for Mode are "r" for reading and "w" for writing.
RETURNS: The FILE pointer, if the file was successfully opened.
EFFECT: If it wasn't possible to open the file with the
requested mode, an error message is printed and the
program exits.
***************************************************************/
{
FILE* File;
File = fopen(Name,Mode);
if (File == (FILE*)NULL) {
misc_StartUserErrorReport();
misc_UserErrorReport("\n\tError in opening file %s for %s !\n\n", Name,
(Mode[0] == 'r' ? "reading" :
(Mode[0] == 'w' ? "writing" : "i/o operations")));
misc_FinishUserErrorReport();
}
return File;
}
FILE* misc_OpenFileExt(const char* Name, const char* Mode,
char* SearchPaths,
char ** const DiscoveredName)
/**************************************************************
INPUT: The name of a file, a string containing the mode
for opening the file (see fopen(3)),
a string containg a colon/semicolon separated list of directories
to look for the file, and a pointer to char pointer
to hold the fullpath name of the file
that was opened in the end (can be null).
Examples for Mode are "r" for reading and "w" for writing.
RETURNS: The FILE pointer, if the file was successfully opened.
if <DiscoveredName> is not null, the location it points
to will contain a newly allocated string
containg the full path name of the opened file.
EFFECT: The file is first looked for in the current directory.
If not found, the individual directories from SearchPaths
are tried consecutively to look for the file,
until a first file is found that can be opened.
If it wasn't possible to open any of the files with the
requested mode, an error message is printed and the
program exits.
***************************************************************/
{
FILE* File;
File = fopen(Name,Mode);
if (File == (FILE*)NULL) {
const char * const Delims = ":;";
LIST DirNames, Scan;
DirNames = string_Split(SearchPaths,Delims);
for(Scan = DirNames; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
char* DirName;
char* WholeName;
DirName = list_Car(Scan);
WholeName = string_Conc(DirName,"/");
WholeName = string_Nconc(WholeName,string_StringCopy(Name));
File = fopen(WholeName,Mode);
if (File != (FILE*)NULL) {
if (DiscoveredName)
*DiscoveredName = WholeName;
else
string_StringFree(WholeName);
list_DeleteWithElement(DirNames,(void (*)(POINTER))string_StringFree);
return File;
}
string_StringFree(WholeName);
}
list_DeleteWithElement(DirNames,(void (*)(POINTER))string_StringFree);
} else {
if (DiscoveredName)
*DiscoveredName = string_StringCopy(Name);
}
if (File == (FILE*)NULL) {
misc_StartUserErrorReport();
misc_UserErrorReport("\n\tError in opening file %s for %s !\n\n", Name,
(Mode[0] == 'r' ? "reading" :
(Mode[0] == 'w' ? "writing" : "i/o operations")));
misc_FinishUserErrorReport();
}
return File;
}
FILE* misc_OpenFileEnv(const char* Name, const char* Mode,
const char* Environ,
char ** const DiscoveredName)
/**************************************************************
INPUT: The name of a file, a string containing the mode
for opening the file (see fopen(3)),
a string containg a name of environment variable
and a pointer to char pointer
to hold the fullpath name of the file
that was opened in the end (can be null).
Examples for Mode are "r" for reading and "w" for writing.
RETURNS: The FILE pointer, if the file was successfully opened.
if <DiscoveredName> is not null, the location it points
to will contain a newly allocated string
containg the full path name of the opened file.
EFFECT: Fetches a value from environment variable Environ
to be used as SearchPaths in a call to misc_OpenFileExt
(see also there).
***************************************************************/
{
char dummy[] = "\0";
char* paths;
paths = getenv(Environ);
return misc_OpenFileExt(Name,Mode,paths ? paths : dummy,DiscoveredName);
}
void misc_CloseFile(FILE* File, const char* Name)
/**************************************************************
INPUT: A FILE and its name.
RETURNS: Nothing.
EFFECT: Closes the file. If an error occurs, a message is
printed and the program exits.
***************************************************************/
{
int Result;
Result = fclose(File);
if (Result != 0) {
misc_StartUserErrorReport();
misc_UserErrorReport("\n\tError in closing file %s !\n\n", Name);
misc_FinishUserErrorReport();
}
}
|