summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorschnetter <schnetter@17b73243-c579-4c4c-a9d2-2d5706c11dac>2004-12-28 18:55:00 +0000
committerschnetter <schnetter@17b73243-c579-4c4c-a9d2-2d5706c11dac>2004-12-28 18:55:00 +0000
commitfe4ba087da02fb06b41e936626521f0fd77434e3 (patch)
treee0829b3f03596b79e7f80f827ed0da20468da299 /src
parentb26a4e235d92a4c2d91fb26db10f5034e7a88bb8 (diff)
Rename "round" to "myround". Otherwise, the function "round" has the
same name as the one from <math.h> (which is #included) but has a different return type, which is an error for gcc 4.0. git-svn-id: http://svn.cactuscode.org/flesh/trunk@3944 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src')
-rw-r--r--src/util/CactusTimers.c23
-rw-r--r--src/util/Malloc.c115
-rw-r--r--src/util/Misc.c7
-rw-r--r--src/util/ParseFile.c73
-rw-r--r--src/util/Table.c101
-rw-r--r--src/util/snprintf.c4
6 files changed, 193 insertions, 130 deletions
diff --git a/src/util/CactusTimers.c b/src/util/CactusTimers.c
index 7c630dac..6ad254d3 100644
--- a/src/util/CactusTimers.c
+++ b/src/util/CactusTimers.c
@@ -405,12 +405,13 @@ static int CCTKi_TimerCreate (const char *timername)
{
int retval;
t_Timer *timer;
+ void *tmp;
const cClockFuncs *funcs;
int this_timer;
int handle;
- if (Util_GetHandle (timers, timername, (void **) &timer) >= 0)
+ if (Util_GetHandle (timers, timername, &tmp) >= 0)
{
/* Handle already exists */
retval = -1;
@@ -476,11 +477,13 @@ static int CCTKi_TimerCreate (const char *timername)
@@*/
int CCTK_TimerDestroy (const char *timername)
{
+ void *tmp;
t_Timer *timer;
int this_timer;
- this_timer = Util_GetHandle (timers, timername, (void **) &timer);
+ this_timer = Util_GetHandle (timers, timername, &tmp);
+ timer = tmp;
if (this_timer >= 0)
{
CCTKi_TimerDestroy (this_timer, timer);
@@ -612,11 +615,13 @@ static void CCTKi_TimerDestroy (int this_timer, t_Timer *timer)
@@*/
int CCTK_TimerStart (const char *timername)
{
+ void *tmp;
t_Timer *timer;
int this_timer;
- this_timer = Util_GetHandle (timers, timername, (void **) &timer);
+ this_timer = Util_GetHandle (timers, timername, &tmp);
+ timer = tmp;
if (this_timer >= 0)
{
CCTKi_TimerStart (this_timer, timer);
@@ -744,11 +749,13 @@ static void CCTKi_TimerStart (int this_timer, t_Timer *timer)
@@*/
int CCTK_TimerStop (const char *timername)
{
+ void *tmp;
t_Timer *timer;
int this_timer;
- this_timer = Util_GetHandle (timers, timername, (void **)&timer);
+ this_timer = Util_GetHandle (timers, timername, &tmp);
+ timer = tmp;
if (this_timer >= 0)
{
CCTKi_TimerStop (this_timer, timer);
@@ -875,11 +882,13 @@ static void CCTKi_TimerStop (int this_timer, t_Timer *timer)
@@*/
int CCTK_TimerReset (const char *timername)
{
+ void *tmp;
t_Timer *timer;
int this_timer;
- this_timer = Util_GetHandle (timers, timername, (void **) &timer);
+ this_timer = Util_GetHandle (timers, timername, &tmp);
+ timer = tmp;
if (this_timer >= 0)
{
CCTKi_TimerReset (this_timer, timer);
@@ -1012,11 +1021,13 @@ static void CCTKi_TimerReset (int this_timer, t_Timer *timer)
@@*/
int CCTK_Timer (const char *timername, cTimerData *info)
{
+ void *tmp;
t_Timer *timer;
int this_timer;
- this_timer = Util_GetHandle (timers, timername, (void **) &timer);
+ this_timer = Util_GetHandle (timers, timername, &tmp);
+ timer = tmp;
if (this_timer >= 0)
{
CCTKi_Timer (this_timer, timer, info);
diff --git a/src/util/Malloc.c b/src/util/Malloc.c
index b0a505c3..17e48608 100644
--- a/src/util/Malloc.c
+++ b/src/util/Malloc.c
@@ -9,6 +9,7 @@
@version $Header$
@@*/
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -24,25 +25,10 @@
/* #define MEMDEBUG 1 */
/* Undefine malloc */
-#if defined(malloc)
#undef malloc
-#define malloc malloc
-#endif
-
-#if defined(realloc)
#undef realloc
-#define realloc realloc
-#endif
-
-#if defined(calloc)
#undef calloc
-#define calloc calloc
-#endif
-
-#if defined(free)
#undef free
-#define free free
-#endif
static const char *rcsid = "$Header$";
@@ -55,8 +41,7 @@ CCTK_FILEVERSION(util_Malloc_c);
/* Datastructure to track the allocation per file */
typedef struct
{
- unsigned long int size;
- unsigned long int tsize;
+ size_t size;
const char *file;
} t_memhash;
@@ -67,21 +52,29 @@ typedef struct
*/
typedef struct
{
- unsigned long int ok; /* magic number */
- unsigned long int size; /* requested size */
- unsigned long int tsize; /* requested size + database */
+ size_t size; /* requested size */
unsigned int line; /* allocated on line # */
const char *file; /* allocated by file # */
+ unsigned long int ok; /* magic number */
} t_mallocinfo;
/* used for integrity checking of the data */
#define OK_INTEGRITY 424242
+/* The t_mallocinfo structure needs to be padded to a multiple of
+ whatever the architecture requires */
+/* 16 seems a reasonable number; other reasonable numbers might be 32
+ or 64. This should really be autodetected, but probably isn't
+ worth the trouble */
+#define PADDING_BASE 16
+/* We have to pad the t_mallocinfo to this length */
+#define PADDED_MALLOCINFO_LENGTH ((sizeof(t_mallocinfo) + (PADDING_BASE) - 1) / (PADDING_BASE) * (PADDING_BASE))
+
/* Datastructure to track the allocation size at ticket
request time */
typedef struct
{
- unsigned long int size;
+ size_t size;
} t_memticket;
@@ -94,8 +87,8 @@ static cHandledData *memfileDB = NULL;
static int n_tickets = 0;
-static unsigned long int totmem=0;
-static unsigned long int pastmem=0;
+static size_t totmem=0;
+static size_t pastmem=0;
/********************************************************************
********************* internal prototypes ********************
@@ -130,7 +123,7 @@ void *CCTKi_Malloc(size_t size, int line, const char *file)
t_mallocinfo *info;
char *data;
- data = (char*)malloc(size+sizeof(t_mallocinfo));
+ data = (char*)malloc(size+PADDED_MALLOCINFO_LENGTH);
if(!data)
{
fprintf(stderr, "Allocation error! ");
@@ -138,7 +131,6 @@ void *CCTKi_Malloc(size_t size, int line, const char *file)
info = (t_mallocinfo*) data;
info->ok = OK_INTEGRITY;
info->size = size;
- info->tsize= size+sizeof(t_mallocinfo);
info->line = line;
info->file = file;
@@ -154,12 +146,12 @@ void *CCTKi_Malloc(size_t size, int line, const char *file)
*/
#ifdef MEMDEBUG
- printf("Allocating %lu - by %s in line %d TOTAL: %lu\n",
- info->size, info->file, info->line, CCTK_TotalMemory());
+ printf("Allocating %f - by %s in line %ud TOTAL: %f\n",
+ (double)info->size, info->file, info->line, (double)CCTK_TotalMemory());
#endif
/* return the pointer plus an OFFSET (t_mallocinfo) to hide the info*/
- return((void*)(data+sizeof(t_mallocinfo)));
+ return((void*)(data+PADDED_MALLOCINFO_LENGTH));
}
@@ -197,7 +189,7 @@ void *CCTKi_Realloc(void *pointer, size_t size, int line, const char *file)
}
/* get the info section */
- info = (t_mallocinfo *)((char*)pointer-sizeof(t_mallocinfo));
+ info = (t_mallocinfo *)((char*)pointer-PADDED_MALLOCINFO_LENGTH);
/* make a sanity check: memory could have be allocated with standard malloc */
if (info->ok!=OK_INTEGRITY)
@@ -219,31 +211,30 @@ void *CCTKi_Realloc(void *pointer, size_t size, int line, const char *file)
totmem = totmem - info->size + size;
/* reallocate starting at info pointer */
- data = (char*)realloc(info, size+sizeof(t_mallocinfo));
+ data = (char*)realloc(info, size+padded_mallaocinfo_length);
if (!data)
{
CCTK_VWarn(0,__LINE__,__FILE__,"Cactus",
"CCTKi_Realloc: Could not reallocate memory. "
- "Reallocation called from %s, line %d. \n",file,line);
+ "Reallocation called from %s, line %d.\n",file,line);
/*$CCTK_Abort(NULL);$*/
}
/* and update */
info = (t_mallocinfo*) data;
info->size = size;
- info->tsize= size+sizeof(t_mallocinfo);
/*$CCTKi_UpdateMemByFile(info->size, line, file);$*/
#ifdef MEMDEBUG
- printf("ReAllocating %lu - by %s in line %d TOTAL: %lu\n",
- info->size, info->file, info->line, CCTK_TotalMemory());
+ printf("ReAllocating %f - by %s in line %ud TOTAL: %f\n",
+ (double)info->size, info->file, info->line, (double)CCTK_TotalMemory());
#endif
/* return the pointer starting at the datasection, behind the info section */
- return((void*)(data+sizeof(t_mallocinfo)));
+ return((void*)(data+PADDED_MALLOCINFO_LENGTH));
}
}
@@ -295,7 +286,7 @@ void CCTKi_Free(void *pointer, int line, const char *file)
return;
}
- info = (t_mallocinfo *)((char*)pointer-sizeof(t_mallocinfo));
+ info = (t_mallocinfo *)((char*)pointer-PADDED_MALLOCINFO_LENGTH);
if (info->ok!=OK_INTEGRITY)
{
@@ -312,8 +303,8 @@ void CCTKi_Free(void *pointer, int line, const char *file)
totmem -= info->size;
#ifdef MEMDEBUG
- printf("Freeing %lu - allocated by %s in line %d TOTAL: %lu\n",
- info->size, info->file, info->line, CCTK_TotalMemory());
+ printf("Freeing %f - allocated by %s in line %ld TOTAL: %f\n",
+ (double)info->size, info->file, info->line, (double)CCTK_TotalMemory());
#endif
/* invalidate the magic number so that we catch things
@@ -342,6 +333,7 @@ void CCTKi_Free(void *pointer, int line, const char *file)
int CCTKi_UpdateMemByFile(int size, int line, const char *file)
{
+ void *tmp;
t_memhash *memfile;
int handle, retval=-1;
@@ -349,7 +341,7 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file)
/* avoid compiler warning about unused parameter */
line = line;
- if ((handle=Util_GetHandle(memfileDB, file, (void**)&memfile)) > -1)
+ if ((handle=Util_GetHandle(memfileDB, file, &tmp)) > -1)
{
memfile = (t_memhash*) Util_GetHandledData(memfileDB, handle);
@@ -357,7 +349,6 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file)
if (memfile)
{
memfile->size +=size;
- memfile->tsize+=size;
retval = 0;
}
else
@@ -372,7 +363,6 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file)
if (memfile)
{
memfile->size += size;
- memfile->tsize+= size+sizeof(t_memhash);
memfile->file = file;
retval = Util_NewHandle(&memfileDB, file, memfile);
}
@@ -410,12 +400,13 @@ int CCTKi_UpdateMemByFile(int size, int line, const char *file)
int CCTK_MemTicketRequest(void)
{
int this_ticket;
+ void *tmp;
t_memticket *tmem;
char tname[20];
sprintf(tname, "ticket_%d",n_tickets++);
- if (Util_GetHandle(ticketDB, tname, (void**)&tmem)>-1)
+ if (Util_GetHandle(ticketDB, tname, &tmp)>-1)
{
this_ticket=-3;
}
@@ -453,23 +444,23 @@ int CCTK_MemTicketRequest(void)
@@*/
-long int CCTK_MemTicketCash(int this_ticket)
+ptrdiff_t CCTK_MemTicketCash(int this_ticket)
{
- long int tdiff;
- unsigned long int tsize;
+ ptrdiff_t tdiff;
+ size_t size;
t_memticket *tmem;
tmem = (t_memticket*) Util_GetHandledData(ticketDB, this_ticket);
if (tmem)
{
- tsize = tmem->size;
- tdiff = CCTK_TotalMemory() - tsize;
+ size = tmem->size;
+ tdiff = CCTK_TotalMemory() - size;
}
else
{
CCTK_VWarn(1,__LINE__,__FILE__,"Cactus",
- "CCTK_MemTicketCash: Cannot find ticket %d \n",this_ticket);
+ "CCTK_MemTicketCash: Cannot find ticket %d\n",this_ticket);
tdiff = 666;
}
return(tdiff);
@@ -517,7 +508,7 @@ int CCTK_MemTicketDelete(int this_ticket)
@date Wed Mar 8 12:47:23 2000
@author Gerd Lanfermann
@desc
- prints a info string, statign current, past total memory
+ prints a info string, stating current, past total memory
and difference.
@enddesc
@calls
@@ -530,8 +521,8 @@ int CCTK_MemTicketDelete(int this_ticket)
void CCTK_MemStat(void)
{
- printf("CCTK_Memstat: total: %lu past: %lu diff %+ld \n",
- totmem, pastmem, totmem-pastmem);
+ printf("CCTK_Memstat: total: %f past: %f diff %f\n",
+ (double)totmem, (double)pastmem, (double)totmem-pastmem);
}
/*@@
@@ -549,7 +540,7 @@ void CCTK_MemStat(void)
@endhistory
@@*/
-unsigned long int CCTK_TotalMemory(void)
+size_t CCTK_TotalMemory(void)
{
return(totmem);
}
@@ -585,18 +576,18 @@ int main(int argc, char *argv[])
printf("### Start Allocating ...\n");
ticket[0]=CCTKi_MemTicketRequest();
myint = (int*) CCTKi_Malloc(n*sizeof(int),42,"My int");
- printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0]));
+ printf("check Ticket1: %f\n",(double)CCTK_MemTicketCash(ticket[0]));
ticket[1]=CCTKi_MemTicketRequest();
mydouble= (double*)CCTKi_Malloc(n*sizeof(double), 42,"My double");
- printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0]));
- printf("check Ticket2: %d\n",CCTK_MemTicketCash(ticket[1]));
+ printf("check Ticket1: %f\n",(double)CCTK_MemTicketCash(ticket[0]));
+ printf("check Ticket2: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[1]));
ticket[2]=CCTKi_MemTicketRequest();
mychar = (char*) CCTKi_Malloc(n*sizeof(char), 42, "My char");
- printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0]));
- printf("check Ticket2: %d\n",CCTK_MemTicketCash(ticket[1]));
- printf("check Ticket3: %d\n",CCTK_MemTicketCash(ticket[2]));
+ printf("check Ticket1: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[0]));
+ printf("check Ticket2: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[1]));
+ printf("check Ticket3: %f\n",(double)CCTKCCTK_MemTicketCash(ticket[2]));
for (i=0;i<n;i++)
{
@@ -611,11 +602,11 @@ int main(int argc, char *argv[])
CCTKi_Free(mydouble, 43, "My double");
CCTKi_Free(mychar, 43, "My char");
- printf("check Ticket1: %d\n",CCTK_MemTicketCash(ticket[0]));
- printf("check Ticket2: %d\n",CCTK_MemTicketCash(ticket[1]));
- printf("check Ticket3: %d\n",CCTK_MemTicketCash(ticket[2]));
+ printf("check Ticket1: %f\n",(double)CCTK_MemTicketCash(ticket[0]));
+ printf("check Ticket2: %f\n",(double)CCTK_MemTicketCash(ticket[1]));
+ printf("check Ticket3: %f\n",(double)CCTK_MemTicketCash(ticket[2]));
- printf("Total Memory allocated: %d ", CCTK_TotalMemory);
+ printf("Total Memory allocated: %f ", (double)CCTK_TotalMemory());
CCTK_MemTicketDelete(ticket[0]);
CCTK_MemTicketDelete(ticket[1]);
diff --git a/src/util/Misc.c b/src/util/Misc.c
index e08285b9..2b351fc1 100644
--- a/src/util/Misc.c
+++ b/src/util/Misc.c
@@ -457,7 +457,7 @@ int Util_IntInRange(int inval, const char *range)
if(inval >= start + !start_closed &&
inval <= end - !end_closed &&
- ! ((inval-start) % step))
+ ! (((unsigned int)inval - (unsigned int)start) % step))
{
retval = 1;
}
@@ -1221,10 +1221,9 @@ void CCTK_FCALL CCTK_FNAME(CCTK_PrintString)
@@*/
int CCTK_FortranString (const char *c_string,
char *fortran_string,
- size_t fortran_length)
+ int fortran_length)
{
- int nchars;
- size_t c_strlen;
+ int nchars, c_strlen;
nchars = c_strlen = strlen (c_string);
diff --git a/src/util/ParseFile.c b/src/util/ParseFile.c
index 9c483ced..ca886201 100644
--- a/src/util/ParseFile.c
+++ b/src/util/ParseFile.c
@@ -51,14 +51,6 @@ int ParseFile(FILE *ifp,
********************* Local Data *****************************
********************************************************************/
-#ifndef WIN32
-#define BOLDON "\033[1m"
-#define BOLDOFF "\033[0m"
-#else
-#define BOLDON ""
-#define BOLDOFF ""
-#endif
-
/* parse buffer size */
#define BUF_SZ (8 * 1024)
@@ -268,8 +260,8 @@ int ParseFile(FILE *ifp,
if (c != '\n') value[p++] = c;
if (c == '\n')
{
- printf ("%sWarning:%s Quoted string contains newline for token %s\n",
- BOLDON, BOLDOFF, tokens);
+ printf ("Warning: Quoted string contains newline for token %s\n",
+ tokens);
printf ("This could indicated a parameter file error or missing quote\n");
#ifdef DEBUG
printf ("LINE %d\n",lineno);
@@ -288,35 +280,52 @@ int ParseFile(FILE *ifp,
else if (c == '$')
{
/* We got a define */
- /* FIXME: Assume it is a parameter file for now */
- char path[500];
- char *parfile;
+ char buf[1000];
+ char * p = buf;
- CCTK_ParameterFilename(500,path);
- parfile = strrchr (path, '/');
- if (parfile == NULL)
- {
- parfile = path;
- }
- else
- {
- parfile++;
- }
- /* skip the parameter file extension */
- if (strcmp (parfile + strlen (parfile) - 4, ".par") == 0)
- {
- parfile[strlen (parfile) - 4] = 0;
- }
-
- /* ignore everything else on the line */
- while (!(c==' ' || c=='\t' || c == '\n' || c == '\r' || c == EOF))
+ /* read name */
+ do
{
c = fgetc(ifp);
#ifdef DEBUG
printf("%c",c);
#endif
+ *p++ = c;
+ }
+ while (!(c==' ' || c=='\t' || c == '\n' || c == '\r' || c == EOF));
+ *--p = 0;
+
+ if (strcmp (buf, "parfile") == 0)
+ {
+ char path[1000];
+ char *parfile;
+ char *dot;
+
+ CCTK_ParameterFilename(sizeof path,path);
+ /* remove the leading directory part */
+ parfile = strrchr (path, '/');
+ if (parfile == NULL)
+ {
+ parfile = path;
+ }
+ else
+ {
+ parfile++;
+ }
+ /* skip the parameter file extension */
+ dot = strrchr (parfile, '.');
+ if (dot)
+ {
+ *dot = 0;
+ }
+
+ set_function(tokens,parfile,lineno);
+ }
+ else
+ {
+ fprintf(stderr, "Error at line %d. Unknown define '%s'.\n", lineno, buf);
+ num_errors++;
}
- set_function(tokens,parfile,lineno);
}
else
{
diff --git a/src/util/Table.c b/src/util/Table.c
index 0aa20d75..7a2c4b29 100644
--- a/src/util/Table.c
+++ b/src/util/Table.c
@@ -1114,19 +1114,18 @@ void CCTK_FCALL CCTK_FNAME(Util_TableCreateFromString)
In more detail, the strings recognized are defined by the
following BNF:<BR>
<BLOCKQUOTE>
- string -> assign*<BR>
- assign -> whitespace*<BR>
- assign -> whitespace* key whitespace* =
- whitespace* value delimiter<BR>
+ string -> whitespace* assign*<BR>
+ assign -> key whitespace* '=' whitespace* value delimiter<BR>
key -> any string not containing '/' or '=' or
whitespace<BR>
value -> array | int_value | real_value | string_value<BR>
- array -> { int_value* } | { real_value* }<BR>
+ array -> { whitespace* (int_value whitespace* [',' whitespace*])* }
+ | { whitespace* (real_value whitespace* [',' whitespace*])* }<BR>
int_value -> anything recognized as a valid integer
- by strdol(3) in base 10<BR>
+ by strtol(3) in base 10<BR>
real_value -> anything not recognized as a valid integer
by strtol(3) but recognized as valid by
- strdod(3)<BR>
+ strtod(3)<BR>
string_value -> a C-style string enclosed in "double quotes"
(C-style character escape codes are allowed
ie. '\a', '\b', '\f', '\n', '\r', '\t',
@@ -1135,10 +1134,14 @@ void CCTK_FCALL CCTK_FNAME(Util_TableCreateFromString)
(C-style character escape codes are *not*
allowed, ie. every character within the
string is interpreted literally)<BR>
- delimiter -> end-of-string | whitespace<BR>
+ delimiter -> end-of-string | whitespace+
+ | whitespace* ',' whitespace*<BR>
whitespace --> ' ' | '\t' | '\n' | '\r' | '\f' | '\v'<BR>
</BLOCKQUOTE>
- where * denotes 0 or more repetitions and | denotes logical or.
+ where * denotes 0 or more repetitions,
+ + denotes 1 or more repetitions,
+ [] denotes 0 or 1 repetitions (i.e. an optional part),
+ and | denotes logical or.
<P>
Notice also that the keys allowed by this function are
somewhat more restricted than those allowed by the other
@@ -1180,7 +1183,7 @@ void CCTK_FCALL CCTK_FNAME(Util_TableCreateFromString)
@@*/
int Util_TableSetFromString(int handle, const char string[])
{
-#define WHITESPACE " \t\n\r\f\v"
+#define WHITESPACE " \t\n\r\f\v"
struct scalar_value scalar;
#ifdef UTIL_TABLE_DEBUG
@@ -1200,6 +1203,9 @@ int Util_TableSetFromString(int handle, const char string[])
int Set_count = 0, status = 0;
char *p = buffer;
+ /* skip leading whitespace */
+ p += strspn(p, WHITESPACE);
+
while (*p != '\0' && status >= 0)
{
/*
@@ -1207,9 +1213,6 @@ int Util_TableSetFromString(int handle, const char string[])
* assignment starting at p, creating a table entry for it
*/
- /* skip leading whitespaces */
- p += strspn(p, WHITESPACE);
-
#ifdef UTIL_TABLE_DEBUG2
printf(" skipped over delimiters to p-buffer=%d\n", (int) (p-buffer));
#endif
@@ -1225,6 +1228,7 @@ int Util_TableSetFromString(int handle, const char string[])
p = q + strspn (q, WHITESPACE); /* p -> "= value..." */
if (*p != '=')
{
+ assert (status >= 0);
status = UTIL_ERROR_BAD_INPUT; /* no '=" in "key=value" string */
break;
}
@@ -1234,6 +1238,7 @@ int Util_TableSetFromString(int handle, const char string[])
p += strspn (p, WHITESPACE); /* p -> "value..." */
if (*p == '\0')
{
+ assert (status >= 0);
status = UTIL_ERROR_BAD_INPUT; /* no value supplied */
break;
}
@@ -1243,9 +1248,10 @@ int Util_TableSetFromString(int handle, const char string[])
/* split "value..." into "value" and "..." */
- /* check the type of value which is either
+ /*
+ * check the type of value which is either
* - a string enclosed in single or double quotes
- * - an array of scalars enclosed in round brackets
+ * - an array of scalars enclosed in curly braces
* - a scalar (integer or real)
*/
if (*value == '\'' || *value == '"' || *value == '{')
@@ -1268,6 +1274,7 @@ int Util_TableSetFromString(int handle, const char string[])
if (*p != *q)
{
+ assert (status >= 0);
status = UTIL_ERROR_BAD_INPUT; /* no closing delimiter found */
break; /* in string or array value */
}
@@ -1296,10 +1303,11 @@ int Util_TableSetFromString(int handle, const char string[])
}
}
q++;
- }
+ } /* while */
if (p != q)
{
+ assert (status >= 0);
status = UTIL_ERROR_BAD_INPUT; /* invalid escape code found */
break;
}
@@ -1312,7 +1320,7 @@ int Util_TableSetFromString(int handle, const char string[])
/*
* this block handles numbers
*/
- p += strcspn (value, WHITESPACE);
+ p += strcspn (value, WHITESPACE ",");
q = value;
}
@@ -1330,6 +1338,7 @@ int Util_TableSetFromString(int handle, const char string[])
*/
if (*q == '\'' || *q == '"')
{
+ assert (status >= 0);
status = Util_TableSetString(handle, value, key);
}
else
@@ -1337,14 +1346,17 @@ int Util_TableSetFromString(int handle, const char string[])
convert_string_to_number (value, &scalar);
if (scalar.datatype == CCTK_VARIABLE_INT)
{
+ assert (status >= 0);
status = Util_TableSetInt(handle, scalar.value.int_scalar, key);
}
else if (scalar.datatype == CCTK_VARIABLE_REAL)
{
+ assert (status >= 0);
status = Util_TableSetReal(handle, scalar.value.real_scalar, key);
}
else
{
+ assert (status >= 0);
status = UTIL_ERROR_BAD_INPUT; /* can't parse scalar value */
}
}
@@ -1370,6 +1382,8 @@ int Util_TableSetFromString(int handle, const char string[])
int datatypesize = 0;
char *array = NULL;
+ /* skip leading whitespace */
+ value += strspn (value, WHITESPACE);
while (*value)
{
@@ -1380,11 +1394,8 @@ int Util_TableSetFromString(int handle, const char string[])
* and pushing it to the resulting generic array buffer
*/
- /* skip leading whitespaces */
- value += strspn (value, WHITESPACE);
-
/* split "value..." into "value" and "..." */
- q = value + strcspn (value, WHITESPACE);
+ q = value + strcspn (value, WHITESPACE ",");
if (*q != '\0') /* if we're already at the end of the list */
/* we don't want to advance further */
{
@@ -1395,6 +1406,7 @@ int Util_TableSetFromString(int handle, const char string[])
if (scalar.datatype == -1)
{
datatype = scalar.datatype;
+ assert (status >= 0);
status = UTIL_ERROR_BAD_INPUT; /* can't parse array value */
break;
}
@@ -1407,6 +1419,7 @@ int Util_TableSetFromString(int handle, const char string[])
{
/* all array values must have the same datatype */
datatype = -1;
+ assert (status >= 0);
status = UTIL_ERROR_TABLE_NO_MIXED_TYPE_ARRAY;
break;
}
@@ -1427,6 +1440,7 @@ int Util_TableSetFromString(int handle, const char string[])
}
if (array == NULL)
{
+ assert (status >= 0);
status = UTIL_ERROR_NO_MEMORY;
break;
}
@@ -1441,10 +1455,20 @@ int Util_TableSetFromString(int handle, const char string[])
nvals++;
value = q;
- }
+
+ /* skip whitespace* [',' whitespace*] */
+ value += strspn (value, WHITESPACE);
+ if (*p == ',')
+ {
+ ++p;
+ value += strspn (value, WHITESPACE);
+ }
+
+ } /* while (*value) */
if (datatype == CCTK_VARIABLE_INT || datatype == CCTK_VARIABLE_REAL)
{
+ assert (status >= 0);
status = Util_TableSetGenericArray(handle, datatype, nvals, array, key);
}
@@ -1452,12 +1476,21 @@ int Util_TableSetFromString(int handle, const char string[])
{
free (array);
}
- }
+ } /* if this block handles array values */
++Set_count;
}
}
- }
+
+ /* skip whitespace* [',' whitespace*] */
+ p += strspn (p, WHITESPACE);
+ if (*p == ',')
+ {
+ ++p;
+ p += strspn (p, WHITESPACE);
+ }
+
+ } /* loop over all assignments */
#ifdef UTIL_TABLE_DEBUG2
printf(" returning with code %d\n", status >= 0 ? Set_count : status);
@@ -6367,6 +6400,7 @@ static
assert( Util_TableSetFromString(handle, "foo/" ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo" ) == UTIL_ERROR_BAD_INPUT );
+ assert( Util_TableCreateFromString("foo," ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo/" ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo=" ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo/=12" ) == UTIL_ERROR_TABLE_BAD_KEY );
@@ -6382,8 +6416,16 @@ static
assert( Util_TableCreateFromString("foo={bar}" ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString(" foo = { \"\r\t\n\v\" } " ) ==
UTIL_ERROR_BAD_INPUT );
+ assert( Util_TableCreateFromString(" foo = { \"\r\t\n\v\", } " ) ==
+ UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo={0 1.0}" ) ==
UTIL_ERROR_TABLE_NO_MIXED_TYPE_ARRAY );
+ assert( Util_TableCreateFromString("foo={0, 1.0}" ) ==
+ UTIL_ERROR_TABLE_NO_MIXED_TYPE_ARRAY );
+ assert( Util_TableCreateFromString("foo={0, 1.0,}" ) ==
+ UTIL_ERROR_TABLE_NO_MIXED_TYPE_ARRAY );
+ assert( Util_TableCreateFromString("foo={0 1.0 ,}" ) ==
+ UTIL_ERROR_TABLE_NO_MIXED_TYPE_ARRAY );
/*
* Test some "good" strings with single values
@@ -6438,12 +6480,23 @@ static
assert( int_array[0] == -1 && int_array[1] == 2 &&
int_array[2] == -3 && int_array[3] == 4 );
+ assert( Util_TableSetFromString(handle, " foo = {\t-1, +2, -3, +4,\t} " ) == 1 );
+ assert( Util_TableGetIntArray (handle, 5, int_array, "foo") == 4 );
+ assert( int_array[0] == -1 && int_array[1] == 2 &&
+ int_array[2] == -3 && int_array[3] == 4 );
+
assert( Util_TableSetFromString(handle,
" foo = {\t-1.1\t+2.2\t-3.3\t+4.4\t} ") == 1);
assert( Util_TableGetRealArray (handle, 100, real_array, "foo") == 4 );
assert( real_array[0] == -1.1 && real_array[1] == 2.2 &&
real_array[2] == -3.3 && real_array[3] == 4.4 );
+ assert( Util_TableSetFromString(handle,
+ " foo = {\t-1.1,\t+2.2\t,-3.3\t+4.4\t,\t} ") == 1);
+ assert( Util_TableGetRealArray (handle, 100, real_array, "foo") == 4 );
+ assert( real_array[0] == -1.1 && real_array[1] == 2.2 &&
+ real_array[2] == -3.3 && real_array[3] == 4.4 );
+
assert( Util_TableDeleteKey (handle, "foo") == 0 );
assert( Util_TableQueryNKeys (handle) == 0 );
}
diff --git a/src/util/snprintf.c b/src/util/snprintf.c
index 561dced0..61390c9c 100644
--- a/src/util/snprintf.c
+++ b/src/util/snprintf.c
@@ -586,7 +586,7 @@ static LDOUBLE pow10 (int exp)
return result;
}
-static long round (LDOUBLE value)
+static long myround (LDOUBLE value)
{
long intpart;
@@ -680,7 +680,7 @@ static int fmtfp (char *buffer, size_t *currlen, size_t maxlen,
/* We "cheat" by converting the fractional part to integer by
* multiplying by a factor of 10
*/
- fracpart = round ((pow10 (max)) * (ufvalue - intpart));
+ fracpart = myround ((pow10 (max)) * (ufvalue - intpart));
if (fracpart >= pow10 (max))
{