From a244eb773908fa1f86a61ab52f2cad6224017e88 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Thu, 10 Jan 2008 15:01:49 +0000 Subject: - implemented strm object serializer (untested as the code required for test is not yet present - hen/egg problem...) --- obj.h | 2 ++ queue.c | 5 ----- stream.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- stream.h | 13 ++++++------- 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/obj.h b/obj.h index e73e61f3..31a08004 100644 --- a/obj.h +++ b/obj.h @@ -59,6 +59,8 @@ free(pThis); \ } +#define objSerializeSCALAR_VAR(strm, propName, propType, var) \ + CHKiRet(objSerializeProp(strm, (uchar*) #propName, PROPTYPE_##propType, (void*) &var)); #define objSerializeSCALAR(strm, propName, propType) \ CHKiRet(objSerializeProp(strm, (uchar*) #propName, PROPTYPE_##propType, (void*) &pThis->propName)); #define objSerializePTR(strm, propName, propType) \ diff --git a/queue.c b/queue.c index f64ec2c8..eb0e5a8a 100644 --- a/queue.c +++ b/queue.c @@ -243,11 +243,6 @@ static rsRetVal qAddDisk(queue_t *pThis, void* pUsr) CHKiRet((objSerialize(pUsr))(pUsr, pThis->tVars.disk.pWrite)); CHKiRet(strmFlush(pThis->tVars.disk.pWrite)); -#if 0 - //rsCStrObj *pCStr; - CHKiRet((objSerialize(pUsr))(pUsr, &pCStr)); - CHKiRet(strmWrite(pThis->tVars.disk.pWrite, rsCStrGetBufBeg(pCStr), rsCStrLen(pCStr))); -#endif finalize_it: return iRet; diff --git a/stream.c b/stream.c index 6f5e058a..5413f1b8 100644 --- a/stream.c +++ b/stream.c @@ -71,11 +71,11 @@ static rsRetVal strmOpenFile(strm_t *pThis) if(pThis->fd != -1) ABORT_FINALIZE(RS_RET_OK); - if(pThis->pszFilePrefix == NULL) + if(pThis->pszFName == NULL) ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING); CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir, - pThis->pszFilePrefix, pThis->lenFilePrefix, pThis->iCurrFNum, pThis->iFileNumDigits)); + pThis->pszFName, pThis->lenFilePrefix, pThis->iCurrFNum, pThis->iFileNumDigits)); /* compute which flags we need to provide to open */ if(pThis->tOperationsMode == STREAMMODE_READ) @@ -517,10 +517,10 @@ strmSetFilePrefix(strm_t *pThis, uchar *pszPrefix, size_t iLenPrefix) if(iLenPrefix < 1) ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING); - if((pThis->pszFilePrefix = malloc(sizeof(uchar) * iLenPrefix + 1)) == NULL) + if((pThis->pszFName = malloc(sizeof(uchar) * iLenPrefix + 1)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); - memcpy(pThis->pszFilePrefix, pszPrefix, iLenPrefix + 1); /* always think about the \0! */ + memcpy(pThis->pszFName, pszPrefix, iLenPrefix + 1); /* always think about the \0! */ pThis->lenFilePrefix = iLenPrefix; finalize_it: @@ -602,16 +602,56 @@ dbgprintf("strmRecordEnd out %d\n", iRet); /* end stream record support functions */ +/* This method serializes a stream object. That means the whole + * object is modified into text form. That text form is suitable for + * later reconstruction of the object. + * The most common use case for this method is the creation of an + * on-disk representation of the message object. + * We do not serialize the dynamic properties. + * rgerhards, 2008-01-10 + */ +rsRetVal strmSerialize(strm_t *pThis, strm_t *pStrm) +{ + DEFiRet; + int i; + long l; + + assert(pThis != NULL); + assert(pStrm != NULL); + + CHKiRet(objBeginSerialize(pStrm, (obj_t*) pThis)); + + i = pThis->sType; + objSerializeSCALAR_VAR(pStrm, sType, INT, i); + objSerializeSCALAR(pStrm, iCurrFNum, INT); + objSerializePTR(pStrm, pszFName, PSZ); + i = pThis->tOperationsMode; + objSerializeSCALAR_VAR(pStrm, tOperationsMode, INT, i); + i = pThis->tOpenMode; + objSerializeSCALAR_VAR(pStrm, tOpenMode, INT, i); + i = (long) pThis->iMaxFileSize; + objSerializeSCALAR_VAR(pStrm, iMaxFileSize, LONG, l); + objSerializeSCALAR(pStrm, iMaxFiles, INT); + objSerializeSCALAR(pStrm, iFileNumDigits, INT); + + CHKiRet(objEndSerialize(pStrm)); + +finalize_it: + return iRet; +} + /* Initialize the stream class. Must be called as the very first method * before anything else is called inside this class. * rgerhards, 2008-01-09 */ BEGINObjClassInit(strm, 1) - //OBJSetMethodHandler(objMethod_SERIALIZE, strmSerialize); + OBJSetMethodHandler(objMethod_SERIALIZE, strmSerialize); //OBJSetMethodHandler(objMethod_SETPROPERTY, strmSetProperty); OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, strmConstructFinalize); ENDObjClassInit(strm) + + /* * vi:set ai: */ diff --git a/stream.h b/stream.h index 6e901449..ac02e66c 100644 --- a/stream.h +++ b/stream.h @@ -64,20 +64,17 @@ typedef struct strm_s { strmType_t sType; /* descriptive properties */ int iCurrFNum;/* current file number (NOT descriptor, but the number in the file name!) */ - uchar *pszDir; /* Directory */ - int lenDir; - uchar *pszFilePrefix; /* prefix for generated filenames */ + uchar *pszFName; /* prefix for generated filenames */ int lenFilePrefix; strmMode_t tOperationsMode; mode_t tOpenMode; - size_t sIOBufSize;/* size of IO buffer */ - int iFlagsOpenOS; - int iModeOpenOS; size_t iMaxFileSize;/* maximum size a file may grow to */ - int bDeleteOnClose; /* set to 1 to auto-delete on close -- be careful with that setting! */ int iMaxFiles; /* maximum number of files if a circular mode is in use */ int iFileNumDigits;/* min number of digits to use in file number (only in circular mode) */ /* dynamic properties, valid only during file open, not to be persistet */ + size_t sIOBufSize;/* size of IO buffer */ + uchar *pszDir; /* Directory */ + int lenDir; int fd; /* the file descriptor, -1 if closed */ size_t iCurrOffs;/* current offset */ uchar *pszCurrFName; /* name of current file (if open) */ @@ -86,6 +83,7 @@ typedef struct strm_s { size_t iBufPtr; /* pointer into current buffer */ int iUngetC; /* char set via UngetChar() call or -1 if none set */ int bInRecord; /* if 1, indicates that we are currently writing a not-yet complete record */ + int bDeleteOnClose; /* set to 1 to auto-delete on close -- be careful with that setting! */ } strm_t; /* prototypes */ @@ -104,6 +102,7 @@ rsRetVal strmSetDir(strm_t *pThis, uchar *pszDir, size_t iLenDir); rsRetVal strmFlush(strm_t *pThis); rsRetVal strmRecordBegin(strm_t *pThis); rsRetVal strmRecordEnd(strm_t *pThis); +rsRetVal strmSerialize(strm_t *pThis, strm_t *pStrm); PROTOTYPEObjClassInit(strm); PROTOTYPEpropSetMeth(strm, bDeleteOnClose, int); PROTOTYPEpropSetMeth(strm, iMaxFileSize, int); -- cgit v1.2.3