📄 slog_irec_write.c
字号:
#include <stdio.h>#ifdef HAVE_SLOGCONF_H#include "slog_config.h"#endif#ifdef HAVE_SLOG_WINCONFIG_H#include "slog_winconfig.h"#endif#if defined( STDC_HEADERS ) || defined( HAVE_STDLIB_H )#include <stdlib.h>#endif#if defined( STDC_HEADERS ) || defined( HAVE_STDARG_H )#include <stdarg.h>#endif#include "slog_fileio.h"#include "slog_bbuf.h"#include "slog_tasklabel.h"#include "slog_vtrarg.h"#include "slog_header.h"#include "slog_pstat.h"#include "slog_profile.h"#include "slog_recdefs.h"#include "slog_impl.h" /*I "slog_impl.h" I*//*@C SLOG_OpenOutputStream - Returns Output SLOG_STREAM associcated with the given filename. Unmodified Input Variables :. filename - constant char pointer to the filename. Modified Output Variables :. returned value - pointer to the newly allocated SLOG_STREAM. It is NULL when the routine encounters error. Include File Needed : slog.h.N SLOG_RETURN_STATUS@*/SLOG_STREAM *SLOG_OpenOutputStream( const char *filename ){ SLOG_STREAM *slog; SLOG_InitGlobalData(); /* memory for slog is allocated by SLOG_OpenStream() the "+" is for the 2nd Pass of the file */ if ( ( slog = SLOG_OpenStream( filename, "w+b" ) ) == NULL ) return NULL; if ( SLOG_WriteHeader( slog ) != SLOG_SUCCESS ) return NULL; if ( SLOG_PSTAT_Open( slog ) != SLOG_SUCCESS ) return NULL; return slog;}/*@C SLOG_Irec_ToOutputStream - store the given interval record in the frame buffer and write it out to the disk when the frame buffer is full. Modified Input Variables :. slog - pointer to SLOG_STREAM where the interval record will be stored. Unmodified Input Variables :. irec - pointer to constant interval record with the fields set by the user before this routine is called. Modified Output Variables :. returned value - integer return status. Usage Notes on this subroutine : The routine provides all the core management of the implementation of SLOG_STREAM. Be sure to call SLOG_Irec_SetMinRec() at least once for the interval record to be appended to the SLOG_STREAM. Also this routine can be called in either increasing starttime and endtime order depending if SLOG_IsIncreasingStarttimeOrder() or SLOG_IsIncreasingEndtimeOrder() is called. But NOT both. And there is time order checking code if user turns on the corresponding compiler flag. Include File Needed : slog.h.N SLOG_RETURN_STATUS@*/int SLOG_Irec_ToOutputStream( SLOG_STREAM *slog, const SLOG_intvlrec_t *irec ){ SLOG_uint32 old_buf_Nbytes_in_file; SLOG_uint32 new_buf_Nbytes_in_file; SLOG_uint32 actual_Nbytes_irec_reserved; SLOG_uint32 Nbytes_in_bbufs_patched;#if 0 SLOG_uint32 Nbytes_written;#endif SLOG_int32 irec_bytesize_estimate; int IsLastFrame = SLOG_FALSE; int ierr;#if defined( CHECKTIMEORDER ) SLOG_time irec_endtime;#endif#if defined( COMPRESSION ) SLOG_recdef_t *recdef; SLOG_intvlinfo_t *intvlinfo;#endif if ( slog->HasIrec2IOStreamBeenUsed == SLOG_FALSE ) { /* Make sure this block is executed once for each SLOG_STREAM */ slog->HasIrec2IOStreamBeenUsed = SLOG_TRUE; ierr = SLOG_STM_CreateBbufs( slog ); if ( ierr != SLOG_SUCCESS ) return ierr; /* Update the content of SLOG_hdr_t to both memory and disk */ slog->hdr->fptr2framedata = slog_ftell( slog->fd ); ierr = SLOG_WriteUpdatedHeader( slog ); if ( ierr != SLOG_SUCCESS ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - " "SLOG_WriteUpdatedHeader() fails\n" ); fflush( errfile ); return ierr; } /* Write the content of SLOG Preview to the disk */ /* SLOG_STM_Init() can be called either __BEFORE__ or _AFTER_ SLOG_WriteInitFrameDir(). SLOG_STM_Init() initializes the Frame_Dir components and various Bbufs in the SLOG_STREAM. So SLOG_WriteInitFrameDir() and SLOG_STM_Init() are independent from one another. */ SLOG_STM_Init( slog ); ierr = SLOG_WriteInitFrameDir( slog ); if ( ierr != SLOG_SUCCESS ) return ierr;#if defined( CHECKPROFILE ) && defined( CHECKRECDEFS ) && defined( DEBUG ) ierr = SLOG_STM_IsPROFConsistentWithRDEF( slog->prof, slog->rec_defs ); if ( ierr != SLOG_TRUE ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - \n" "\t""The Display Profile is Inconsistent with " "the Record Definition Table\n" ); fflush( errfile ); return SLOG_FAIL; }#endif#if defined( CHECKTIMEORDER ) /* Do a more comprehensive testing here, so we can simplify the testing later by using one flag, slog->hdr->IsIncreasingEndtime */ if ( slog->hdr->IsIncreasingEndtime == SLOG_TRUE && slog->hdr->IsIncreasingStarttime == SLOG_FALSE ) slog->prev_endtime = irec->starttime + irec->duration; else if ( slog->hdr->IsIncreasingEndtime == SLOG_FALSE && slog->hdr->IsIncreasingStarttime == SLOG_TRUE ) slog->prev_starttime = irec->starttime; else { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - " "Inconsistent time order flags!\n" ); if ( slog->hdr->IsIncreasingEndtime == SLOG_TRUE ) fprintf( errfile, "\t""IsIncreasingEndtime = TRUE, " ); else fprintf( errfile, "\t""IsIncreasingEndtime = FALSE, " ); if ( slog->hdr->IsIncreasingStarttime == SLOG_TRUE ) fprintf( errfile, "\t""IsIncreasingStarttime = TRUE\n" ); else fprintf( errfile, "\t""IsIncreasingStarttime = FALSE\n" ); fflush( errfile ); return SLOG_FAIL; }#endif } /* Endof { if ( slog->HasIrec2IOStreamBeenUsed == SLOG_FALSE ) } */ /* Check if the input interval record is empty */ if ( SLOG_Irec_IsEmpty( irec ) ) { fprintf( stdout, __FILE__":SLOG_Irec_ToOutputStream() - \n" ); fprintf( stdout, " ""Input Interval Record is empty\n" ); fprintf( stdout, " ""irec = " ); SLOG_Irec_Print( irec, stdout ); fprintf( stdout, "\n" ); fflush( stdout ); return SLOG_FAIL; }#if defined( DEBUG ) if ( ! SLOG_Irec_IsConsistent( irec ) ) { fprintf( stdout, __FILE__":SLOG_Irec_ToOutputStream() - \n" ); fprintf( stdout, " ""Input Interval Record is InConsistent\n" ); fprintf( stdout, " ""irec = " ); SLOG_Irec_Print( irec, stdout ); fprintf( stdout, "\n" ); fflush( stdout ); return SLOG_FAIL; }#endif /* Check if writing the input interval record to the buffer is possible */ old_buf_Nbytes_in_file = SLOG_typesz[ FrameHdr ] + slog->hdr->frame_reserved_size + ( slog->cur_bbuf )->Nbytes_in_file + ( slog->tmp_bbuf )->Nbytes_in_file; if ( slog->reserve != NULL ) { /* Since the corresponding _Start_Event_ of the interval has been called with SLOG_Irec_ReserveSpace(), and the interval has NOT been added to any of the bbufs yet, so irec->Nbytes needs to be substracted from slog->reserve->Nbytes_eff to avoid double counting of irec in space usage. */ actual_Nbytes_irec_reserved = slog->reserve->Nbytes_eff - irec->bytesize; old_buf_Nbytes_in_file += actual_Nbytes_irec_reserved; } new_buf_Nbytes_in_file = old_buf_Nbytes_in_file + irec->bytesize;#if defined( DEBUG ) fprintf( stdout, __FILE__":SLOG_Irec_ToOutputStream() - \n" ); fprintf( stdout, "\t""FrameHdr + sum_reserved_space + " "_cur_ + _tmp_ = "fmt_ui32" bytes\n", old_buf_Nbytes_in_file ); fflush( stdout );#endif /* Check if new buffer size is too big for the buffer If so, dump the contents in the Bi-dir linked list buffers first before adding another interval record to the buffers */ if ( new_buf_Nbytes_in_file > fbuf_bufsz( slog->fbuf ) ) { /* Reset the total allocated size of all completed and incompleted irec's by substracting it the size of completed irec's which will be saved into the frame/disk. */ if ( slog->reserve != NULL ) { slog->reserve->Nbytes_tot -= slog->cur_bbuf->Nbytes_in_file; slog->reserve->Nrec_tot -= slog->cur_bbuf->count_irec; } /* Update the buffer's Frame Directory Record's Fptr2FrameHdr, StartTime & EndTime So, SLOG_STM_UpdateFrameDirEntry_Forward() HAS To Be Called BEFORE any operations on the various sectors of the frame, i.e. xxx_bbuf. Because the starttime and endtime of the frame determines where each interval record should go. */ ierr = SLOG_STM_UpdateFrameDirEntry_Forward( slog, IsLastFrame ); if ( ierr != SLOG_SUCCESS ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - \n" "\t""SLOG_STM_UpdateFrameDirEntry_Forward( " "slog ) fails!!\n" ); fflush( errfile ); return SLOG_FAIL; } ierr = SLOG_STM_UpdateFRAME_Forward( slog ); if ( ierr != SLOG_SUCCESS ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - \n" "\t""SLOG_STM_UpdateFRAME_Forward( slog ) " "fails!!\n" ); fflush( errfile ); return SLOG_FAIL; } ierr = SLOG_STM_WriteFRAME( slog ); if ( ierr == SLOG_FAIL ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - " "SLOG_STM_WriteFRAME( slog ) fails!!\n" ); fflush( errfile ); return SLOG_FAIL; }#if 0 Nbytes_written = ierr;#endif ierr = SLOG_STM_UpdateWriteFRAMEDIR( slog ); if ( ierr != SLOG_SUCCESS ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - \n" "\t""SLOG_STM_UpdateWriteFRAMEDIR( slog ) " "fails!!\n" ); fflush( errfile ); return SLOG_FAIL; } SLOG_STM_InitAgainBeforeAddRec( slog );#if 0 /* Check Number of Bytes written is consistent */ if ( Nbytes_written != old_buf_Nbytes_in_file ) { fprintf( errfile, __FILE__":SLOG_Irec_ToOutputStream() - " "WARNING !!\n" ); fprintf( errfile, " ""SLOG_STM_WriteFRAME()'s " "Nbytes_written {"fmt_ui32"} != SizeOf( " "FrameHdr + sum_reserved_size + " "_cur_ + _tmp_ ) {"fmt_ui32"}\n", Nbytes_written , old_buf_Nbytes_in_file ); fprintf( errfile, " ""Continue with the program.\n" ); fflush( errfile ); }#endif } /* Endof { if ( new_buf_Nbytes_in_file > fbuf( slog->fbuf ) ) } */#if defined( DEBUG ) if ( slog->reserve != NULL ) { fprintf( stdout, "\t""reserve->Nbytes_eff = "fmt_i32", " "reserve->Nbytes_tot = "fmt_i32"\n", slog->reserve->Nbytes_eff, slog->reserve->Nbytes_tot ); }#endif#if defined( CHECKTIMEORDER ) if ( slog->hdr->IsIncreasingEndtime == SLOG_TRUE ) { irec_endtime = irec->starttime + irec->duration; if ( irec_endtime < slog->prev_endtime ) { fprintf( errfile, __FILE__ ":SLOG_Irec_ToOutputStream() - Warning!!!\n" "\t""Current endtime("fmt_time") is earlier " "than previous endtime("fmt_time")\n", irec_endtime, slog->prev_endtime ); fflush( errfile ); } slog->prev_endtime = irec_endtime; } else { if ( irec->starttime < slog->prev_starttime ) { fprintf( errfile, __FILE__ ":SLOG_Irec_ToOutputStream() - Warning!!!\n" "\t""Current starttime("fmt_time") is earlier " "than previous starttime("fmt_time")\n", irec->starttime, slog->prev_starttime ); fflush( errfile ); } slog->prev_starttime = irec->starttime; }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -