#include "reaxc_tool_box.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "reaxc_defs.h"
#if !defined(_MSC_VER)
#include <sys/time.h>
#endif
#include "error.h"
struct timeval tim;
double t_end;
double Get_Time( )
{
#if defined(_MSC_VER)
double t;
t = GetTickCount();
t /= 1000.0;
return t;
#else
gettimeofday(&tim, NULL );
return( tim.tv_sec + (tim.tv_usec / 1000000.0) );
#endif
}
int Tokenize( char* s, char*** tok )
{
char test[MAX_LINE];
const char *sep = (const char *)"\t \n\r\f!=";
char *word;
int count=0;
strncpy( test, s, MAX_LINE-1);
for( word = strtok(test, sep); word; word = strtok(NULL, sep) ) {
strncpy( (*tok)[count], word, MAX_LINE );
count++;
}
return count;
}
void *smalloc( LAMMPS_NS::Error *error_ptr, rc_bigint n, const char *name )
{
void *ptr;
char errmsg[256];
if (n <= 0) {
snprintf(errmsg, 256, "Trying to allocate %ld bytes for array %s. "
"returning NULL.", n, name);
error_ptr->one(FLERR,errmsg);
return NULL;
}
ptr = malloc( n );
if (ptr == NULL) {
snprintf(errmsg, 256, "Failed to allocate %ld bytes for array %s", n, name);
error_ptr->one(FLERR,errmsg);
}
return ptr;
}
void *scalloc( LAMMPS_NS::Error *error_ptr, rc_bigint n, rc_bigint size, const char *name )
{
void *ptr;
char errmsg[256];
if (n <= 0) {
snprintf(errmsg, 256, "Trying to allocate %ld elements for array %s. "
"returning NULL.\n", n, name );
error_ptr->one(FLERR,errmsg);
return NULL;
}
if (size <= 0) {
snprintf(errmsg, 256, "Elements size for array %s is %ld. "
"returning NULL", name, size );
error_ptr->one(FLERR,errmsg);
return NULL;
}
ptr = calloc( n, size );
if (ptr == NULL) {
char errmsg[256];
snprintf(errmsg, 256, "Failed to allocate %ld bytes for array %s", n*size, name);
error_ptr->one(FLERR,errmsg);
}
return ptr;
}
void sfree( LAMMPS_NS::Error* error_ptr, void *ptr, const char *name )
{
if (ptr == NULL) {
char errmsg[256];
snprintf(errmsg, 256, "Trying to free the already NULL pointer %s", name );
error_ptr->one(FLERR,errmsg);
return;
}
free( ptr );
ptr = NULL;
}