/*
* EasyWave - A realtime tsunami simulation program with GPU support.
* Copyright (C) 2014 Andrey Babeyko, Johannes Spazier
* GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
*
* Parts of this program (especially the GPU extension) were developed
* within the context of the following publicly funded project:
* - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
* (http://www.tridec-online.eu)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
// last modified 11.07.2012
#include
#include
#include
#include
#include
#include
#include
#include
#define ERRORFILE "error.msg"
int iscomment( int );
cMsg Msg;
cErrMsg Err;
cLog Log;
// ========================== Class Message =======================
cMsg::cMsg()
{
enabled = 1;
setchannel(MSG_OUTSCRN);
setfilename( "default.msg" );
}
cMsg::~cMsg()
{
}
void cMsg::enable()
{
enabled = 1;
}
void cMsg::disable()
{
enabled = 0;
}
void cMsg::setfilename( const char* newfilename )
{
sprintf( fname, "%s", newfilename );
}
void cMsg::setchannel( int newchannel )
{
channel = newchannel;
}
int cMsg::print( const char* fmt, ... )
{
if(!enabled) return 0;
if( channel & MSG_OUTSCRN ) {
va_list arglst;
va_start(arglst, fmt);
vprintf( fmt, arglst );
va_end(arglst);
}
if( channel & MSG_OUTFILE ) {
va_list arglst;
va_start(arglst, fmt);
FILE *fp = fopen( fname, "at" );
vfprintf( fp, fmt, arglst );
fclose( fp );
va_end(arglst);
}
return 0;
}
// =================== END Class Message =================
// ========================== Class Error Message =======================
cErrMsg::cErrMsg()
{
enabled = 1;
setfilename( "error.msg" );
setchannel(MSG_OUTSCRN|MSG_OUTFILE);
}
cErrMsg::~cErrMsg()
{
}
int cErrMsg::post( const char* fmt, ... )
{
if(!enabled) return 0;
if( channel & MSG_OUTSCRN ) {
va_list arglst;
va_start(arglst, fmt);
vprintf( fmt, arglst );
va_end(arglst);
printf( "\n" );
}
if( channel & MSG_OUTFILE ) {
va_list arglst;
va_start(arglst, fmt);
FILE *fp = fopen( fname, "at" );
vfprintf( fp, fmt, arglst );
fprintf( fp, "\n" );
fclose( fp );
va_end(arglst);
}
return -1;
}
char* cErrMsg::msgAllocateMem( void )
{
char msg[256];
sprintf( msg, "Error allocating memory" );
return strdup(msg);
}
char* cErrMsg::msgOpenFile( const char *fname )
{
char msg[256];
sprintf( msg, "Cannot open file %s", fname );
return strdup(msg);
}
char* cErrMsg::msgReadFile( const char *fname, int line, const char *expected )
{
char msg[256];
sprintf( msg, "Error reading file %s, line number %-d. Expected: %s", fname, line, expected );
return strdup(msg);
}
// =================== END Class Message =================
// ======================= Class Log =======================
cLog::cLog()
{
enabled = 0;
timestamp_enabled = 1;
setfilename( "default.log" );
setchannel(MSG_OUTFILE);
}
cLog::~cLog()
{
}
void cLog::start( const char* filename )
{
int ifenabled=0;
setfilename( filename );
enabled = 1;
if( timestamp_enabled ) {
ifenabled = 1;
timestamp_disable();
}
print(" ============= Starting new log at %s", utlCurrentTime() );
if( ifenabled ) timestamp_enable();
}
void cLog::timestamp_enable()
{
timestamp_enabled = 1;
}
void cLog::timestamp_disable()
{
timestamp_enabled = 0;
}
int cLog::print( const char* fmt, ... )
{
if(!enabled) return 0;
va_list arglst;
va_start(arglst, fmt);
FILE *fp = fopen( fname, "at" );
if( timestamp_enabled )fprintf( fp, "%s --> ", utlCurrentTime() );
vfprintf( fp, fmt, arglst );
fprintf( fp, "\n" );
fclose( fp );
va_end(arglst);
return 0;
}
// =================== END Class Log =================
char *utlCurrentTime()
{
time_t timer;
char *cp;
timer = time(NULL);
cp = asctime(localtime(&timer));
cp[strlen(cp)-1] = '\0';
return cp;
}
int utlPostError( const char *message )
{
FILE *fp = fopen( ERRORFILE, "at" );
fprintf( fp, "%s", utlCurrentTime() );
fprintf( fp, " -> %s\n", message );
fclose( fp );
return -1;
}
char *utlErrMsgMemory()
{
return strdup("Cannot allocate memory");
}
char *utlErrMsgOpenFile( const char *fname )
{
char msg[256];
sprintf( msg, "Cannot open file %s", fname );
return strdup(msg);
}
char *utlErrMsgEndOfFile( const char *fname )
{
char msg[256];
sprintf( msg, "Unexpected end of file: %s", fname );
return strdup(msg);
}
char *utlErrMsgReadFile( const char *fname, int line, const char *expected )
{
char msg[256];
sprintf( msg, "Error reading file %s, line number %-d. Expected: %s", fname, line, expected );
return strdup(msg);
}
/*** Command-line processing ***/
int utlCheckCommandLineOption( int argc, char **argv, const char *option, int letters_to_compare )
{
int k;
for( k=1; k (string+strlen(string)) ) { word[0] = '\0'; return NULL; }
for( cp = pos; isspace(*cp); cp++ ) ;
if( *cp == '\0' ) { word[0] = '\0'; return NULL; }
sscanf( cp, "%s", word );
cp += strlen(word);
return cp;
}
int utlCountWordsInString( char *line )
// Count words in a string
{
int nwords=0;
char *cp=line;
while( 1 )
{
while( isspace( *cp ) ) cp++;
if( *cp == '\0' || *cp == ';' ) return nwords;
nwords++;
while( !isspace( *cp ) && *cp != '\0' ) cp++;
}
}
/***************************************************************************/
/* Write sequance of words into a string */
/***************************************************************************/
char *utlWords2String( int nwords, char **word )
{
char *buf;
int k,lenstr;
for( lenstr=0,k=0; k < nwords; k++ )
lenstr += strlen( word[k] );
lenstr += (nwords + 1); // add separators plus final null character
buf = new char[lenstr];
memset( buf, 0, lenstr );
for( k=0; k < nwords; k++ ) {
if( k>0 ) strcat( buf, " " );
strcat( buf, word[k] );
}
return buf;
}
int utlSubString( char *str, int p1, int p2, char *substr )
// get substring from p1 to p2 into a buffer substr
{
char *cp;
int k;
if( p1<0 || p2<0 || p1>p2 || (unsigned)p2 > (strlen(str)-1) ) return -1;
for( k=0,cp = &str[p1]; cp <= &str[p2]; cp++ )
substr[k++] = *cp;
substr[k] = '\0';
return 0;
}
void utlPrintToString( char *string, int position, char *insert )
// Prints string to another starting from given position
{
char *cp;
int i;
for( i = 0; i < position; i++ )
if( string[i] == '\0' ) string[i] = ' ';
for( cp = insert, i = 0; *cp != '\0'; i++, cp++ )
string[position+i] = *cp;
}
/***************************************************************************/
/*** ***/
/*** F I L E H A N D L I N G ***/
/*** ***/
/***************************************************************************/
int iscomment( int ch )
{
if( ch == ';' || ch == '!' )
return 1;
else
return 0;
}
int utlFindNextInputField( FILE *fp )
// Search for next input field in a file skipping blanks, tabs, empty lines, comments (from ';' to EOL)
// Returns number of lines scanned until input field found
{
int ch;
int lines = 0;
L1: while( isspace( (ch=fgetc(fp)) ) )
if( ch == '\n' ) lines++;
if( iscomment(ch) )
{
while( ( ch = fgetc( fp ) ) != '\n' && ch != EOF ) ;
if( ch == '\n' )
{
lines++;
goto L1;
}
else if( ch == EOF )
return EOF;
}
else if( ch == EOF )
return EOF;
ungetc( ch, fp );
return( lines );
}
int utlReadNextRecord( FILE *fp, char *record, int *line )
{
int found = 0;
char *cp, firstchar;
while( !found && fgets( record, MaxFileRecordLength, fp ) )
{
for( cp = record; *cp == ' ' || *cp == '\t'; cp++ )
;
if( *cp != '\n' && *cp != '\r' && *cp != ';' )
found = 1;
(*line)++;
}
if( !found )
return EOF;
else
{
firstchar = *cp;
while( *cp != '\n' && *cp != '\r' && *cp != '\0' )
cp++;
*cp = '\0';
return( firstchar );
}
}
int utlReadNextDataRecord( FILE *fp, char *record, int *line )
{
int found = 0;
char *cp, firstchar;
while( !found && fgets( record, MaxFileRecordLength, fp ) )
{
for( cp = record; *cp == ' ' || *cp == '\t'; cp++ )
;
if( isdigit(*cp) || (*cp=='-' && isdigit(*(cp+1))) )
found = 1;
(*line)++;
}
if( !found )
return EOF;
else
{
firstchar = *cp;
while( *cp != '\n' && *cp != '\r' && *cp != '\0' )
cp++;
*cp = '\0';
return( firstchar );
}
}
char *utlFileFindRecord( char *fname, char *pattern, char *record )
{
char *cp;
int line;
FILE *fp;
if( (fp = fopen(fname,"rt")) == NULL ) { utlPostError( utlErrMsgOpenFile(fname) ); return NULL; }
line = 0;
cp = NULL;
while( utlReadNextRecord( fp, record, &line ) != EOF ) {
if( (cp = strstr(record, pattern) ) == NULL ) continue;
break;
}
fclose(fp);
return cp;
}
int utlFileParceToString( FILE *fp, char *pattern )
{
int ch,pos=0,ierr=1;
while( (ch=fgetc(fp)) != EOF ) {
if( ch != pattern[pos] ) {
pos = 0;
continue;
}
pos++;
if( pattern[pos] == '\0' ) {
ierr = 0;
break;
}
}
return ierr;
}
int utlGetNumberOfRecords( const char *fname )
{
FILE *fp;
int nrec,line=0;
char record[1024];
fp = fopen( fname, "rt" );
if( fp == NULL ) return 0;
nrec = 0;
while( utlReadNextRecord( fp, record, &line ) != EOF )
nrec++;
fclose( fp );
return nrec;
}
int utlGetNumberOfDataRecords( const char *fname )
{
FILE *fp;
int nrec,line=0;
char record[1024];
fp = fopen( fname, "rt" );
if( fp == NULL ) return 0;
nrec = 0;
while( utlReadNextDataRecord( fp, record, &line ) != EOF )
nrec++;
fclose( fp );
return nrec;
}
int utlFileReadOption( char *fname, char *option_name, char *contents )
{
FILE *fp;
int found,line;
char record[1024];
fp = fopen( fname, "rt" );
if( !fp ) return 0;
for( line=0, found=0; utlReadNextRecord( fp, record, &line ) != EOF; ) {
if( utlStringReadOption( record, option_name, contents ) == 0 ) {
found = 1;
break;
}
}
fclose( fp );
if( found )
return 0;
else
return -1;
}
char *utlFileChangeExtension( const char *fname, const char *newext )
// Change file extension. Return new file name
{
char buf[256], *cp;
int len,extlen;
len = strlen(fname);
extlen = strlen(newext);
memset( buf, 0, len+extlen+1 );
strcpy( buf, fname );
for( cp=&buf[len-1]; *cp!='.' && cp>=&buf[0]; cp-- ) ;
if( *cp=='.' )
sprintf( cp+1, "%s", newext );
else
sprintf( &buf[len], ".%s", newext );
return strdup( buf );
}
char *utlFileAddExtension( const char *fname, const char *addext )
// Add new extension. Return new file name
{
char buf[256], *cp;
int len,extlen;
len = strlen(fname);
extlen = strlen(addext);
memset( buf, 0, len+extlen+1 );
sprintf( buf, "%s.%s", fname, addext );
return strdup( buf );
}
char *utlFileRemoveExtension( const char *fname )
// Remove file extension. Return file name without extension
{
char buf[256], *cp;
strcpy( buf, fname );
for( cp=&buf[strlen(fname)-1]; *cp!='.' && cp>=&buf[0]; cp-- ) ;
if( *cp=='.' ) *cp = '\0';
return strdup( buf );
}
int utlFileRemoveExtension( char *newname, const char *fname )
// Remove file extension
{
char *cp;
strcpy( newname, fname );
for( cp=&newname[strlen(newname)-1]; *cp!='.' && cp>=&newname[0]; cp-- ) ;
if( *cp=='.' ) *cp = '\0';
return 0;
}
int utlReadXYdata( char *fname, double **x, double **y )
// Reads XY ASCII data file. Returns number of data points read
{
FILE *fp;
char record[256];
int n,line=0;
double xv,yv;
fp = fopen( fname, "rt" );
if( fp == NULL )
return utlPostError( utlErrMsgOpenFile(fname) );
// Calculate number of data lines
for( n=0; utlReadNextDataRecord( fp, record, &line ) != EOF; ) {
if( sscanf( record, "%lf %lf", &xv, &yv ) != 2 )
return utlPostError( utlErrMsgReadFile( fname, line, "X Y" ) );
n++;
}
// Allocate memory
*x = new double [n];
*y = new double [n];
// Read data
rewind( fp );
line=0;
for( int k=0; k |
// x: initial seed for random number generator (integer) |
// |
//