27#include <lal/StringInput.h>
28#include <lal/LALStdio.h>
29#include <lal/LogPrintf.h>
30#include <lal/LogPrintf.h>
31#include <lal/HeapToplist.h>
43#define snprintf _snprintf
60#define min(a,b) ((a)<(b)?(a):(b))
64#define SYNC_FAIL_LIMIT 5
155 UINT4 len, chars = 0;
175 if ( maxbytes == 0 ) {
180 while ( fgets(
line,
sizeof(
line ) - 1,
fp ) ) {
182 if ( !strncmp(
line,
"%DONE\n", strlen(
"%DONE\n" ) ) ) {
187 len = strlen(
line );
193 }
else if (
line[len - 1] !=
'\n' ) {
195 "Line %d is too long or has no NEWLINE. First %zu chars are:\n'%s'\n",
200 items = sscanf(
line,
217 !isfinite( FstatLine.
Freq ) ||
218 !isfinite( FstatLine.
f1dot ) ||
219 !isfinite( FstatLine.
Alpha ) ||
220 !isfinite( FstatLine.
Delta ) ||
221 !isfinite( FstatLine.
Fstat ) ||
223 FstatLine.
Freq < 0.0 ||
224 FstatLine.
Alpha < 0.0 - epsilon ||
232 "Line %d has invalid values.\n"
233 "First %zu chars are:\n"
235 "All fields should be finite\n"
236 "1st field should be positive.\n"
237 "2nd field should lie between 0 and %1.15f.\n"
238 "3rd field should lie between %1.15f and %1.15f.\n",
245 for (
i = 0;
i < len;
i++ ) {
246 *checksum +=
line[
i];
261 if ( chars == maxbytes ) {
262 LogPrintf(
LOG_DEBUG,
"Read exactly %d == maxbytes from Fstat-file, that's enough.\n",
267 if ( chars > maxbytes ) {
284 return ( snprintf( buf, buflen,
291 "%.13g %.7g %.7g %.5g %.6g\n",
310 if ( length >
sizeof( linebuf ) - 1 ) {
315 for (
i = 0;
i < length;
i++ ) {
316 *checksum += linebuf[
i];
362 errno, strerror( errno ) );
393#define TEMP_EXT ".tmp"
395 tempname = (
char * )malloc(
s );
403 fpnew = fopen( tempname,
"wb" );
406 tempname, errno, strerror( errno ) );
415 if ( ( write_done ) && ( length >= 0 ) ) {
417 ret =
fprintf( fpnew,
"%%DONE\n" );
429 tempname, errno, strerror( errno ) );
437 if ( rename( tempname,
filename ) ) {
439 filename, errno, strerror( errno ) );
475 if ( ( cptf == NULL ) ||
492 if ( !( ( *cptf )->filename ) ) {
501 if ( !( ( *cptf )->buffer ) ) {
504 LALFree( ( *cptf )->filename );
511 strcpy( ( *cptf )->filename,
filename );
513 ( *cptf )->bytes = 0;
514 ( *cptf )->bufsize = bufsize;
515 ( *cptf )->maxsize = maxsize;
516 ( *cptf )->checksum = 0;
517 ( *cptf )->fp = NULL;
518 ( *cptf )->list = list;
531 if ( ( *cptf )->filename ) {
532 LALFree( ( *cptf )->filename );
534 if ( ( *cptf )->buffer ) {
553 if ( !( cptf->
fp ) ) {
559 if ( !( cptf->
fp ) ) {
580 if ( !( cptf->
fp ) ) {
584 return ( fflush( cptf->
fp ) );
601 *bytes = cptf->
bytes;
646 errno, strerror( errno ) );
652 cptf->
bytes += bytes;
654 if ( (
int )cptf->
bytes != ftell( cptf->
fp ) )
655 LogPrintf(
LOG_DEBUG,
"ERROR: bytecount mismatch: returned: %d, bytes: %u, file: %ld\n",
656 bytes, cptf->
bytes, ftell( cptf->
fp ) );
688 }
else if ( bytes == -2 ) {
691 }
else if ( bytes == -1 ) {
696 }
else if ( checksum_read != checksum_should ) {
698 checksum_should, checksum_read );
704 if ( ftell( cptf->
fp ) > bytes ) {
706 bytes, ftell( cptf->
fp ) );
707 fseek( cptf->
fp, bytes, SEEK_SET );
712 if ( (
int )( cptf->
bytes ) != ftell( cptf->
fp ) ) {
739 fseek( cptf->
fp, 0
L, SEEK_END );
756#define LOGIOERROR(mess,filename) \
757 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: line:%d, doserr:%d, ferr:%d, errno:%d: %s\n",\
758 mess,filename,__LINE__,_doserrno,((fp)?(ferror(fp)):0),errno,strerror(errno))
760#define LOGIOERROR(mess,filename) \
761 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: line:%d, errno:%d: %s\n",\
762 mess,filename,__LINE__,errno,strerror(errno))
768#define TMP_EXT ".tmp"
773 static UINT4 sync_fail_counter = 0;
777 tmpfilename =
LALCalloc( len,
sizeof(
char ) );
778 if ( !tmpfilename ) {
783 strcat( tmpfilename,
TMP_EXT );
787 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
788 checksum += *( ( (
char * ) & ( tl->
elems ) ) + len );
790 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
791 checksum += *( ( (
char * )tl->
data ) + len );
793 for ( len = 0; len <
sizeof( counter ); len++ ) {
794 checksum += *( ( (
char * )&counter ) + len );
798 fp = fopen( tmpfilename,
"wb" );
805 len = fwrite( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
807 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
809 if ( fclose(
fp ) ) {
810 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
817 if ( len != tl->
elems ) {
818 LOGIOERROR(
"Couldn't write data to", tmpfilename );
820 if ( fclose(
fp ) ) {
821 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
827 len = fwrite( &counter,
sizeof( counter ), 1,
fp );
829 LOGIOERROR(
"Couldn't write counter to", tmpfilename );
831 if ( fclose(
fp ) ) {
832 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
838 len = fwrite( &checksum,
sizeof( checksum ), 1,
fp );
840 LOGIOERROR(
"Couldn't write checksum to", tmpfilename );
842 if ( fclose(
fp ) ) {
843 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
850 if ( fsync( fileno(
fp ) ) ) {
857 sync_fail_counter = 0;
862 if ( fclose(
fp ) ) {
868 if ( rename( tmpfilename,
filename ) ) {
869 LOGIOERROR(
"Couldn't rename\n", tmpfilename );
896 len = fread( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
900 if ( fclose(
fp ) ) {
908 "Number of elements read larger than length of toplist: %zu > %zu\n",
910 if ( fclose(
fp ) ) {
918 if ( len != tl->
elems ) {
921 if ( fclose(
fp ) ) {
929 len = fread( counter,
sizeof( *counter ), 1,
fp );
933 if ( fclose(
fp ) ) {
941 len = fread( &checksum,
sizeof( checksum ), 1,
fp );
945 if ( fclose(
fp ) ) {
953 if ( fclose(
fp ) ) {
960 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
961 checksum -= *( ( (
char * ) & ( tl->
elems ) ) + len );
963 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
964 checksum -= *( ( (
char * )tl->
data ) + len );
966 for ( len = 0; len <
sizeof( *counter ); len++ ) {
967 checksum -= *( ( (
char * )counter ) + len );
976 for ( len = 0; len < tl->
elems; len++ ) {
int read_fstat_toplist_from_fp(toplist_t *l, FILE *fp, UINT4 *checksum, UINT4 maxbytes)
reads a (created!) toplist from an open filepointer sets the checksum if non-NULL reads maximum maxby...
void sort_fstat_toplist(toplist_t *l)
sorts the toplist with an internal sorting function, used before finally writing it
void free_fstat_toplist(toplist_t **l)
frees the space occupied by the toplist
int fstat_cpt_file_destroy(FstatCheckpointFile **cptf)
destroys a FstatCheckpointFile
int final_write_fstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
meant for the final writing of the toplist
static int fstat_smaller(const void *a, const void *b)
int write_fstat_toplist_item_to_fp(FstatOutputEntry fline, FILE *fp, UINT4 *checksum)
File IO.
int fstat_cpt_file_flush(FstatCheckpointFile *cptf)
flushes the checkpoint file (only useful if buffered)
int fstat_cpt_file_create(FstatCheckpointFile **cptf, CHAR *filename, UINT4 bufsize, UINT4 maxsize, toplist_t *list)
creates a FstatCheckpointFile
int fstat_cpt_file_compact(FstatCheckpointFile *cptf)
compact a toplist file if the length has reached maxbytes
int fstat_cpt_file_open(FstatCheckpointFile *cptf)
opens a file for checkpointing the desired toplist
int fstat_cpt_file_close(FstatCheckpointFile *cptf)
closes the file, reduces the precision, sorts the toplist, finally rewrites the file (sorted and comp...
int write_hs_oputput(const char *filename, toplist_t *tl)
write the final output file:
int fstat_cpt_file_info(FstatCheckpointFile *cptf, CHAR **filename, UINT4 *bytes, UINT4 *checksum)
returns information for checkpointing
int fstat_cpt_file_read(FstatCheckpointFile *cptf, UINT4 checksum_should, UINT4 maxbytes)
reads a written checkpointed toplist back into memory
int read_hs_checkpoint(const char *filename, toplist_t *tl, UINT4 *counter)
tries to read a checkpoint
int create_fstat_toplist(toplist_t **tl, UINT8 length)
creates a toplist with length elements, returns -1 on error (usually out of memory),...
static int print_fstatline_to_str(FstatOutputEntry fline, char *buf, int buflen)
int atomic_write_fstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
writes the given toplitst to a temporary file, then renames the temporary file to filename.
static void reduce_fstatline_precision(void *line)
int write_fstat_toplist_to_fp(toplist_t *tl, FILE *fp, UINT4 *checksum)
Writes the toplist to an (already open) filepointer Returns the number of written charactes sets the ...
static int fstat_toplist_qsort_function(const void *a, const void *b)
int insert_into_fstat_toplist(toplist_t *tl, FstatOutputEntry elem)
Inserts an element in to the toplist either if there is space left or the element is larger than the ...
int write_hs_checkpoint(const char *filename, toplist_t *tl, UINT4 counter, BOOLEAN do_sync)
new, simpler checkpointing for HierarchicalSearch
static void reduce_fstat_toplist_precision(toplist_t *l)
#define LOGIOERROR(mess, filename)
log an I/O error, i.e.
static int _atomic_write_fstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum, int write_done)
int fstat_cpt_file_add(FstatCheckpointFile *cptf, FstatOutputEntry line)
adds an item to the toplist and keeps the file consistent, i.e.
int create_toplist(toplist_t **list, size_t length, size_t size, int(*smaller)(const void *, const void *))
void free_toplist(toplist_t **list)
void qsort_toplist(toplist_t *list, int(*compare)(const void *, const void *))
void qsort_toplist_r(toplist_t *list, int(*compare)(const void *, const void *))
int insert_into_toplist(toplist_t *list, void *element)
void go_through_toplist(toplist_t *list, void(*handle)(void *))
void clear_toplist(toplist_t *list)
#define XLAL_LAST_ELEM(x)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
a toplist as a checkpointed file
CHAR * buffer
write buffer if needed
FILE * fp
FILE* currently associated.
CHAR * filename
name of the toplist file
UINT4 bytes
counts the bytes in the file
toplist_t * list
toplist this file reflects
UINT4 bufsize
buffer size if needed
UINT4 checksum
keeps the checksum
Type to hold the fields that will be output in unclustered output file
REAL8 Alpha
Skyposition: longitude in equatorial coords, radians.
REAL8 Delta
skyposition: latitude
REAL8 Freq
Frequency at maximum (?) of the cluster.
REAL8 f1dot
spindown value f1dot = df/dt