Commit ffe5ac70 authored by Karl Wette's avatar Karl Wette
Browse files

LALAtomicDatatypes: add macros XLAL_NUM_ELEM() and XLAL_LAST_ELEM()

- XLAL_NUM_ELEM(x) gives the number of elements in a fixed-size array 'x'
- XLAL_LAST_ELEM(x) accesses the last element of a fixed-size array 'x'
- Use where appropriate
Original: 1123acb434535b85c20cad14350b785516749d7d
parent 3e186bc2
......@@ -65,6 +65,10 @@ typedef uint64_t UINT8; /**< Eight-byte unsigned integer; on some platforms thi
/** C99 MACRO to declare and zero-initialize a variable, use as "type XLAL_INIT_DECL(var);" */
#define XLAL_INIT_DECL(var) var; XLAL_INIT_MEM(var)
/** MACRO which gives the number of elements in a fixed-size array */
#define XLAL_NUM_ELEM(x) ( sizeof((x)) / sizeof((x)[0]) )
/** MACRO to access the last element in a fixed-size array */
#define XLAL_LAST_ELEM(x) (x)[ XLAL_NUM_ELEM(x) - 1 ]
/* Macros for integer constants */
/**
......
......@@ -183,7 +183,7 @@ int XLALPrintProgressBar(double fraction)
"+++++++++++++++++++++++++++++++++++++++++++++++++)";
static const char spc[] =
"-------------------------------------------------)";
int l = sizeof(mrk) / sizeof(*mrk) - 1;
int l = XLAL_NUM_ELEM(mrk) - 1;
int offset =
floor((fraction < 0.0 ? 0.0 : fraction >
1.0 ? 1.0 : fraction) * l + 0.5);
......
......@@ -228,7 +228,7 @@ int XLALFileIsCompressed( const char *path )
size_t c;
if ( ! ( fp = LALFopen( path, "rb" ) ) )
XLAL_ERROR( XLAL_EIO );
c = fread( magic, sizeof(*magic), sizeof(magic)/sizeof(*magic), fp );
c = fread( magic, sizeof(*magic), XLAL_NUM_ELEM(magic), fp );
if (c == 0)
XLAL_ERROR( XLAL_EIO );
fclose( fp );
......
......@@ -332,7 +332,7 @@ LALCache *XLALCacheGlob(const char *dirstr, const char *fnptrn)
char dirname[PATH_MAX];
char *nextdir;
strncpy(dirname, dirstr, sizeof(dirname) - 1);
dirname[sizeof(dirname) - 1] = 0;
XLAL_LAST_ELEM(dirname) = 0;
do {
if ((nextdir = strchr(dirname, ':')))
*nextdir++ = 0;
......
......@@ -493,14 +493,14 @@ XLALUserVarHelpString ( const CHAR *progname )
CHAR fmtStr[256]; // for building a dynamic format-string
snprintf ( fmtStr, sizeof(fmtStr), " %%s --%%-%ds %%-%ds %%s [%%s]\n", nameFieldLen, typeFieldLen );
fmtStr[sizeof(fmtStr)-1]=0;
XLAL_LAST_ELEM(fmtStr)=0;
CHAR defaultstr[256]; // for display of default-value
CHAR strbuf[512];
// ---------- provide header line: info about config-file reading
snprintf (strbuf, sizeof(strbuf), "Usage: %s [@ConfigFile] [options], where options are:\n\n", progname);
strbuf[sizeof(strbuf)-1] = 0;
XLAL_LAST_ELEM(strbuf) = 0;
XLAL_CHECK_NULL ( (helpstr_regular = XLALStringDuplicate ( strbuf )) != NULL, XLAL_EFUNC );
// ---------- MAIN LOOP: step through all user variables and add entry to appropriate help string
......@@ -518,7 +518,7 @@ XLALUserVarHelpString ( const CHAR *progname )
CHAR *valstr;
XLAL_CHECK_NULL ( (valstr = UserVarTypeMap [ ptr->type ].printer( ptr->varp )) != NULL, XLAL_EFUNC );
strncpy ( defaultstr, valstr, sizeof(defaultstr) ); // cut short for default-entry
defaultstr[sizeof(defaultstr)-1] = 0;
XLAL_LAST_ELEM(defaultstr) = 0;
XLALFree (valstr);
}
......@@ -536,7 +536,7 @@ XLALUserVarHelpString ( const CHAR *progname )
ptr->help ? ptr->help : "-NONE-",
defaultstr
);
strbuf[sizeof(strbuf)-1] = 0;
XLAL_LAST_ELEM(strbuf) = 0;
// now append new line to the appropriate helpstring
if ( ptr->category == UVAR_CATEGORY_DEVELOPER ) {
......@@ -746,7 +746,7 @@ XLALUserVarGetLog ( UserVarLogFormat format /**< output format: return as confi
XLAL_ERROR_NULL ( XLAL_EINVAL, "Unknown format for recording user-input: '%i'\n", format );
break;
} // switch (format)
append[sizeof(append)-1] = 0;
XLAL_LAST_ELEM(append) = 0;
XLAL_CHECK_NULL ( (record = XLALStringAppend (record, append)) != NULL, XLAL_EFUNC );
......
......@@ -151,7 +151,7 @@ XLALParseStringValueAsINT4PlusFrac ( INT4 *valINT4, ///< [out] return INT4 inte
char buf[256];
strncpy ( buf, valString, sizeof(buf)-1 );
buf[ sizeof(buf)-1 ] = 0;
XLAL_LAST_ELEM(buf) = 0;
REAL8 sign = 1;
if ( valString[0] == '-' ) { // that's why no initial whitespace is allowed in input string
......@@ -260,7 +260,7 @@ XLALParseStringValueAsEPOCH ( LIGOTimeGPS *gps, ///< [out] return LIGOTimeGPS
char buf[256];
strncpy ( buf, valString, sizeof(buf)-1 );
buf[ sizeof(buf)-1 ] = 0;
XLAL_LAST_ELEM(buf) = 0;
// ---------- first check if there's a postfix indicating the time 'units' (GPS or MJD):
BOOLEAN is_gps;
......
......@@ -350,7 +350,7 @@ LALUpdateCalibration(
(params->epoch.gpsSeconds < CAL_S2END ) )
{
/* if the zero is during S2 print a warning... */
snprintf( warnMsg, sizeof(warnMsg)/sizeof(*warnMsg),
snprintf( warnMsg, XLAL_NUM_ELEM(warnMsg),
"Zero calibration factors found during S2 at GPS %10.9f",
first_cal + (REAL8) i * params->sensingFactor->deltaT );
LALWarning( status, warnMsg );
......@@ -358,7 +358,7 @@ LALUpdateCalibration(
else
{
/* ...or abort if we are outside S2 */
snprintf( warnMsg, sizeof(warnMsg)/sizeof(*warnMsg),
snprintf( warnMsg, XLAL_NUM_ELEM(warnMsg),
"Zero calibration factor found at GPS %10.9f",
first_cal + (REAL8) i * params->sensingFactor->deltaT );
LALWarning( status, warnMsg );
......@@ -386,7 +386,7 @@ LALUpdateCalibration(
(fabs( creal(a) ) < tiny && fabs( cimag(a) ) < tiny) ||
(fabs( creal(ab) ) < tiny && fabs( cimag(ab) ) < tiny) )
{
snprintf( warnMsg, sizeof(warnMsg)/sizeof(*warnMsg),
snprintf( warnMsg, XLAL_NUM_ELEM(warnMsg),
"Got %d calibration samples\nalpha and/or beta are zero:\n"
"Re a = %e\tIm a = %e\nRe ab = %e\tIm ab = %e",
length, creal(a), cimag(a), creal(ab), cimag(ab) );
......@@ -401,7 +401,7 @@ LALUpdateCalibration(
/* return the used values of alpha and alphabeta */
params->alpha = a;
params->alphabeta = ab;
snprintf( warnMsg, sizeof(warnMsg)/sizeof(*warnMsg),
snprintf( warnMsg, XLAL_NUM_ELEM(warnMsg),
"Got %d calibration samples\n"
"Re a = %e\tIm a = %e\nRe ab = %e\tIm ab = %e",
length, creal(a), cimag(a), creal(ab), cimag(ab) );
......
......@@ -153,7 +153,7 @@ REAL4 XLALUniformDeviate( RandomParams *params )
XLAL_ERROR_REAL4( XLAL_EFAULT );
/* randomly choose which element of the vector of random numbers to use */
ndiv = 1 + (m - 1)/(sizeof(params->v)/sizeof(*params->v));
ndiv = 1 + (m - 1)/(XLAL_NUM_ELEM(params->v));
n = params->y/ndiv;
params->y = params->v[n];
......
......@@ -177,7 +177,7 @@ int main( int argc, char *argv[] )
avgSpecParams.method = useMean;
for ( np = 0; np < sizeof(npts)/sizeof(*npts) ; ++np )
for ( np = 0; np < XLAL_NUM_ELEM(npts) ; ++np )
{
/* length of time series for 7 segments, overlapped by 1/2 segment */
UINT4 tsLength = npts[np] * 7 - 6 * npts[np] / 2;
......@@ -193,11 +193,11 @@ int main( int argc, char *argv[] )
LALCreateForwardRealFFTPlan( &status, &avgSpecParams.plan, npts[np], 0 );
TestStatus( &status, CODES( 0 ), 1 );
for ( sr = 0; sr < sizeof(srate)/sizeof(*srate) ; ++sr )
for ( sr = 0; sr < XLAL_NUM_ELEM(srate) ; ++sr )
{
/* set the sample rate of the time series */
y.deltaT = 1.0 / (REAL8) srate[sr];
for ( vr = 0; vr < sizeof(var)/sizeof(*var) ; ++vr )
for ( vr = 0; vr < XLAL_NUM_ELEM(var) ; ++vr )
{
REAL4 eps = 1e-6; /* very conservative fp precision */
REAL4 Sfk = 2.0 * var[vr] * var[vr] * y.deltaT;
......
......@@ -714,7 +714,7 @@ int main ( int argc, char *argv[] )
/* sieve out the requested data type */
snprintf( ifoRegExPattern,
sizeof(ifoRegExPattern) / sizeof(*ifoRegExPattern), ".*%c.*",
XLAL_NUM_ELEM(ifoRegExPattern), ".*%c.*",
fqChanName[0] );
frInCache = XLALCacheDuplicate(frGlobCache);
XLALCacheSieve(frInCache, 0, 0, ifoRegExPattern, frInType, NULL);
......
......@@ -741,7 +741,7 @@ static double sequence_preset_next(gsl_rng *rng)
1e-20
};
return presets[gsl_rng_uniform_int(rng, sizeof(presets)/sizeof(*presets))];
return presets[gsl_rng_uniform_int(rng, XLAL_NUM_ELEM(presets))];
}
#endif
......
......@@ -371,7 +371,7 @@ static int write_gctFstat_toplist_item_to_fp(GCTtopOutputEntry fline, FILE*fp, U
for(i=0;i<length;i++)
*checksum += linebuf[i];
linebuf[sizeof(linebuf)-1] = '\0';
XLAL_LAST_ELEM(linebuf) = '\0';
return(fprintf(fp,"%s",linebuf));
}
......
......@@ -325,14 +325,14 @@ int main(int argc, char **argv)
/** extract epoch and duration from gwf file name **/
strncpy(strepoch, strchr(gwfnamelist[k]->d_name, '9'), 9 ); /*All epochs in S6 begin with 9... potential problem in future */
strepoch[sizeof(strepoch)-1] = '\0'; /*Null terminate the string*/
XLAL_LAST_ELEM(strepoch) = '\0'; /*Null terminate the string*/
epoch.gpsSeconds = atoi(strepoch); /* convert to integer from string */
epoch.gpsNanoSeconds = 0; /* no nanosecond precision */
/* fprintf(stderr, "epoch = %i\n", epoch.gpsSeconds);*/
char strdur[4];
strncpy(strdur, (strrchr(gwfnamelist[k]->d_name, '-')+1), 3); /* duration is last number in frame file */
strdur[sizeof(strdur)-1] = '\0';
XLAL_LAST_ELEM(strdur) = '\0';
/* assigns duration from .gwf frame */
ndata = atoi(strdur);
......
......@@ -251,7 +251,7 @@ void sort_crossCorrBinary_toplist(toplist_t*l) {
/* return -2; */
/* /\* make sure the line buffer is terminated correctly *\/ */
/* line[sizeof(line)-1]='\0'; */
/* XLAL_LAST_ELEM(line)='\0'; */
/* /\* init the checksum if given *\/ */
/* if(checksum) */
......@@ -385,7 +385,7 @@ void sort_crossCorrBinary_toplist(toplist_t*l) {
/* return -2; */
/* /\* make sure the line buffer is terminated correctly *\/ */
/* line[sizeof(line)-1]='\0'; */
/* XLAL_LAST_ELEM(line)='\0'; */
/* /\* init the checksum if given *\/ */
/* if(checksum) */
......@@ -547,7 +547,7 @@ int write_crossCorr_toplist_item_to_fp(CrossCorrOutputEntry fline, FILE*fp, UINT
for(i=0;i<length;i++)
*checksum += linebuf[i];
linebuf[sizeof(linebuf)-1] = '\0';
XLAL_LAST_ELEM(linebuf) = '\0';
return(fprintf(fp,"%s",linebuf));
}
......@@ -567,7 +567,7 @@ int write_crossCorrBinary_toplist_item_to_fp(CrossCorrBinaryOutputEntry fline, F
for(i=0;i<length;i++)
*checksum += linebuf[i];
linebuf[sizeof(linebuf)-1] = '\0';
XLAL_LAST_ELEM(linebuf) = '\0';
return(fprintf(fp,"%s",linebuf));
}
......
......@@ -162,7 +162,7 @@ int read_fstat_toplist_from_fp(toplist_t*l, FILE*fp, UINT4*checksum, UINT4 maxby
return -2;
/* make sure the line buffer is terminated correctly */
line[sizeof(line)-1]='\0';
XLAL_LAST_ELEM(line)='\0';
/* init the checksum if given */
if(checksum)
......@@ -311,7 +311,7 @@ int write_fstat_toplist_item_to_fp(FstatOutputEntry fline, FILE*fp, UINT4*checks
for(i=0;i<length;i++)
*checksum += linebuf[i];
linebuf[sizeof(linebuf)-1] = '\0';
XLAL_LAST_ELEM(linebuf) = '\0';
return(fprintf(fp,"%s",linebuf));
}
......
......@@ -190,7 +190,7 @@ int read_houghFstat_toplist_from_fp(toplist_t*l, FILE*fp, UINT4*checksum, UINT4
return -2;
/* make sure the line buffer is terminated correctly */
line[sizeof(line)-1]='\0';
XLAL_LAST_ELEM(line)='\0';
/* init the checksum if given */
if(checksum)
......@@ -367,7 +367,7 @@ int write_houghFstat_toplist_item_to_fp(HoughFstatOutputEntry fline, FILE*fp, UI
for(i=0;i<length;i++)
*checksum += linebuf[i];
linebuf[sizeof(linebuf)-1] = '\0';
XLAL_LAST_ELEM(linebuf) = '\0';
return(fprintf(fp,"%s",linebuf));
}
......
......@@ -403,12 +403,12 @@ int OutputEvents(const struct CommandLineArgsTag *CLA, ProcessTable *proctable,
char ifo[3];
strncpy( ifo, CLA->ChannelName, 2 );
ifo[sizeof(ifo) - 1] = '\0';
XLAL_LAST_ELEM(ifo) = '\0';
if (!CLA->outputFileName){
CHAR outfilename[256];
snprintf(outfilename, sizeof(outfilename)-1, "%s-STRINGSEARCH-%d-%d.xml", ifo, CLA->GPSStart.gpsSeconds, CLA->GPSEnd.gpsSeconds - CLA->GPSEnd.gpsSeconds);
outfilename[sizeof(outfilename)-1] = '\0';
XLAL_LAST_ELEM(outfilename) = '\0';
xml = XLALOpenLIGOLwXMLFile(outfilename);
} else
xml = XLALOpenLIGOLwXMLFile(CLA->outputFileName);
......@@ -640,7 +640,7 @@ int CreateStringFilters(struct CommandLineArgsTag CLA, REAL8TimeSeries *ht, unsi
if (CLA.printfilterflag){
CHAR filterfilename[256];
snprintf(filterfilename, sizeof(filterfilename)-1, "Filter-%d.txt", m);
filterfilename[sizeof(filterfilename)-1] = '\0';
XLAL_LAST_ELEM(filterfilename) = '\0';
LALDPrintFrequencySeries( strtemplate[m].StringFilter, filterfilename );
}
......@@ -656,7 +656,7 @@ int CreateStringFilters(struct CommandLineArgsTag CLA, REAL8TimeSeries *ht, unsi
XLALREAL8FreqTimeFFT( vector, vtilde, rplan );
snprintf(filterfilename, sizeof(filterfilename)-1, "FIRFilter-%d.txt", m);
filterfilename[sizeof(filterfilename)-1] = '\0';
XLAL_LAST_ELEM(filterfilename) = '\0';
LALDPrintTimeSeries( vector, filterfilename );
}
}
......
......@@ -138,14 +138,14 @@ void LALFrNext(LALStatus * status, LALFrStream * stream)
XLALClearErrno();
if (stream->state & LAL_FR_STREAM_ERR) {
if (stream->state & LAL_FR_STREAM_URL) { /* must have failed to open a file */
snprintf(frErrMsg, sizeof(frErrMsg) / sizeof(*frErrMsg),
snprintf(frErrMsg, XLAL_NUM_ELEM(frErrMsg),
"Could not open URL %s\n",
stream->cache->list[stream->fnum].url);
LALError(status, frErrMsg);
ABORT(status, FRAMESTREAMH_EOPEN, FRAMESTREAMH_MSGEOPEN);
}
if (stream->state & LAL_FR_STREAM_TOC) { /* must have failed to read a file */
snprintf(frErrMsg, sizeof(frErrMsg) / sizeof(*frErrMsg),
snprintf(frErrMsg, XLAL_NUM_ELEM(frErrMsg),
"Could not read TOC from %s\n",
stream->cache->list[stream->fnum].url);
LALError(status, frErrMsg);
......@@ -179,14 +179,14 @@ void LALFrSeek(LALStatus * status, const LIGOTimeGPS * epoch,
XLALClearErrno();
if (stream->state & LAL_FR_STREAM_ERR) { /* a file error */
if (stream->state & LAL_FR_STREAM_URL) { /* must have failed to open a file */
snprintf(frErrMsg, sizeof(frErrMsg) / sizeof(*frErrMsg),
snprintf(frErrMsg, XLAL_NUM_ELEM(frErrMsg),
"Could not open URL %s\n",
stream->cache->list[stream->fnum].url);
LALError(status, frErrMsg);
ABORT(status, FRAMESTREAMH_EOPEN, FRAMESTREAMH_MSGEOPEN);
}
if (stream->state & LAL_FR_STREAM_TOC) { /* must have failed to read a file */
snprintf(frErrMsg, sizeof(frErrMsg) / sizeof(*frErrMsg),
snprintf(frErrMsg, XLAL_NUM_ELEM(frErrMsg),
"Could not read TOC from %s\n",
stream->cache->list[stream->fnum].url);
LALError(status, frErrMsg);
......
......@@ -501,7 +501,7 @@ void xml_put_f0(double f0, FILE *fp)
void xml_put_det(const char *name, FILE *fp)
{
const char *dets[] = {"G1", "H1", "H2", "K1", "L1", "T1", "V1"};
size_t ndet = sizeof(dets)/sizeof(*dets);
size_t ndet = XLAL_NUM_ELEM(dets);
size_t d;
fputs("\t\t<Param Type=\"lstring\" Name=\"instrument:param\">", fp);
for (d = 0; d < ndet; ++d)
......
......@@ -158,12 +158,12 @@ int main( int argc, char *argv[] )
response.sampleUnits = strainPerCount;
/* loop over the three interferometers */
for ( j = 0; j < sizeof(ifoCode) / sizeof(*ifoCode); ++j )
for ( j = 0; j < XLAL_NUM_ELEM(ifoCode); ++j )
{
snprintf( response.name, LALNameLength * sizeof(CHAR),
CHANNEL, ifoCode[j] );
for ( i = 0; i < sizeof(calTime) / sizeof(*calTime); ++i )
for ( i = 0; i < XLAL_NUM_ELEM(calTime); ++i )
{
/* set the time of the calibration and the frame cahche file to use */
snprintf( calCacheName, LALNameLength * sizeof(CHAR), CAL_CATALOG,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment