00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <stdlib.h>
00015 #include <fcntl.h>
00016 #include <string.h>
00017 #include <memory.h>
00018 #include <time.h>
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021
00022 #ifdef Lynx
00023 #include <unistd.h>
00024 #include <pwd.h>
00025 #include <timeb.h>
00026 #endif
00027
00028 #ifdef Linux
00029 #include <unistd.h>
00030 #include <pwd.h>
00031 #include <sys/timeb.h>
00032 #endif
00033
00034 #ifdef Solaris
00035 #include <unistd.h>
00036 #include <pwd.h>
00037 #include <sys/timeb.h>
00038 #endif
00039
00040 #ifdef Darwin
00041 #include <unistd.h>
00042 #include <pwd.h>
00043 #include <sys/timeb.h>
00044 #define fgetpos64 fgetpos
00045 #define fopen64 fopen
00046 #define fseeko64 fseek
00047 #define fpos64_t fpos_t
00048
00049
00050 #define CLOCK_REALTIME 1
00051 int clock_gettime(int clockid, struct timespec *tp)
00052 {
00053 return 0;
00054 }
00055
00056 #endif
00057
00058
00059
00060 #ifdef WIN32
00061 #include <WTypes.h>
00062 #include <wchar.h>
00063
00064 #define fgetpos64 fgetpos
00065 #define fopen64 fopen
00066 #define fseeko64 fseek
00067 #define fpos64_t fpos_t
00068
00069 struct timespec {
00070 long tv_sec;
00071 long tv_nsec;
00072 };
00073
00074 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
00075 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
00076 #else
00077 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
00078 #endif
00079
00080 #define CLOCK_REALTIME 1
00081
00082 int clock_gettime(int clockid, struct timespec *tp)
00083 {
00084 FILETIME ft;
00085 unsigned __int64 tmpres = 0;
00086
00087 tp->tv_sec = 0;
00088 tp->tv_nsec = 0;
00089
00090 GetSystemTimeAsFileTime(&ft);
00091
00092 tmpres |= ft.dwHighDateTime;
00093 tmpres <<= 32;
00094 tmpres |= ft.dwLowDateTime;
00095
00096
00097 tmpres /= 10;
00098 tmpres -= DELTA_EPOCH_IN_MICROSECS;
00099 tp->tv_sec = (long)(tmpres / 1000000UL);
00100 tp->tv_nsec = (long)(tmpres % 1000000UL) * 1000;
00101
00102 return 0;
00103 }
00104 #endif
00105
00106 #include "fLmd.h"
00107
00108 int32_t fLmdWriteBuffer(sLmdControl *, char *, uint32_t);
00109 uint32_t fLmdCleanup(sLmdControl *);
00110 void fLmdOffsetResize(sLmdControl *, uint32_t);
00111 uint32_t fLmdOffsetSet(sLmdControl *, uint32_t );
00112 uint32_t fLmdOffsetRead(sLmdControl *);
00113 uint32_t fLmdOffsetWrite(sLmdControl *);
00114 lmdoff_t fLmdOffsetGet(sLmdControl *, uint32_t);
00115 void fLmdOffsetElements(sLmdControl *, uint32_t, uint32_t *, uint32_t *);
00116 #define OFFSET__ENTRIES 250000
00117
00118
00119 uint32_t fLmdPutOpen(sLmdControl *pLmdControl,
00120 char *Filename,
00121 sMbsFileHeader *pBuffHead,
00122 uint32_t iBytes,
00123 uint32_t iOver,
00124 uint32_t iUseOffset,
00125 uint32_t iLargeFile)
00126 {
00127 int32_t iReturn;
00128 struct timespec clock;
00129
00130 memset(pLmdControl,0,sizeof(sLmdControl));
00131
00132
00133 if(pBuffHead == LMD__STANDARD_HEADER){
00134 pLmdControl->pMbsFileHeader= (sMbsFileHeader *)malloc(sizeof(sMbsFileHeader));
00135 memset(pLmdControl->pMbsFileHeader,0,sizeof(sMbsFileHeader));
00136 pLmdControl->iInternHeader=1;
00137 } else {
00138 pLmdControl->pMbsFileHeader= pBuffHead;
00139 pLmdControl->iInternHeader=0;
00140 }
00141
00142 clock_gettime(CLOCK_REALTIME,&clock);
00143 pLmdControl->pMbsFileHeader->iTimeSpecSec=clock.tv_sec;
00144 pLmdControl->pMbsFileHeader->iTimeSpecNanoSec=clock.tv_nsec;
00145
00146 pLmdControl->pMbsFileHeader->iType=LMD__TYPE_FILE_HEADER_101_1;
00147 pLmdControl->pMbsFileHeader->iEndian=1;
00148 strcpy(pLmdControl->cFile,Filename);
00149
00150
00151 if(iBytes > 0){
00152 pLmdControl->pBuffer = (int16_t*) malloc(iBytes);
00153 pLmdControl->iInternBuffer = 1;
00154 }
00155 pLmdControl->iBufferWords=iBytes/2;
00156 pLmdControl->iLeftWords=iBytes/2;
00157
00158 if(iOver == LMD__NO_OVERWRITE){
00159 if((pLmdControl->fFile=(FILE *)fopen64(Filename,"r") )!=NULL){
00160 printf("fLmdPutOpen: File exists: %s\n",Filename);
00161 fLmdCleanup(pLmdControl);
00162 fclose(pLmdControl->fFile);
00163 return(PUTLMD__FILE_EXIST);
00164 }
00165 }
00166
00167 if((pLmdControl->fFile=(FILE *)fopen64(Filename,"w+") )== NULL){
00168 printf("fLmdPutOpen: Error open file %s\n",Filename);
00169 fLmdCleanup(pLmdControl);
00170 return(PUTLMD__OPEN_ERR);
00171 }
00172
00173 if(iLargeFile == LMD__LARGE_FILE)pLmdControl->iOffsetSize=8;
00174 else pLmdControl->iOffsetSize=4;
00175 pLmdControl->pMbsFileHeader->iOffsetSize=pLmdControl->iOffsetSize;
00176
00177
00178 iReturn=fLmdWriteBuffer(pLmdControl,(char *)pLmdControl->pMbsFileHeader,
00179 (pLmdControl->pMbsFileHeader->iUsedWords)*2+sizeof(sMbsFileHeader));
00180 pLmdControl->iBytes+=iReturn;
00181
00182 if(iUseOffset == LMD__INDEX)fLmdOffsetResize(pLmdControl,iReturn/4);
00183 printf("fLmdPutOpen: %s. Bytes:%d over:%d table:%d large:%d.\n",
00184 Filename,iBytes,iOver,iUseOffset,iLargeFile);
00185 return(LMD__SUCCESS);
00186 }
00187
00188
00189 uint32_t fLmdPutElement(
00190 sLmdControl *pLmdControl,
00191 sMbsHeader *pHeader){
00192 uint32_t *ps, *pd, i, elements;
00193 int64_t fileleft,used;
00194 int32_t iReturn;
00195
00196
00197 if(pLmdControl->iOffsetEntries && (pLmdControl->iOffsetSize == 4)){
00198 elements=pLmdControl->iElements+2;
00199 used=pLmdControl->iBytes/4;
00200 fileleft=0xffffffff - used - (4+elements);
00201 if((int64_t)(pHeader->iWords/2+2) > fileleft){
00202 printf("fLmdPutElement: File size exceed\n");
00203 return(PUTLMD__EXCEED);
00204 }
00205 }
00206
00207 if((pHeader->iWords+4) > pLmdControl->pMbsFileHeader->iMaxWords)
00208 pLmdControl->pMbsFileHeader->iMaxWords=pHeader->iWords+4;
00209
00210 if(pLmdControl->iBufferWords == 0){
00211 pLmdControl->pMbsHeader=pHeader;
00212 iReturn=fLmdWriteBuffer(pLmdControl,(char *)pHeader,(pHeader->iWords+4)*2);
00213 pLmdControl->iBytes+=iReturn;
00214 if(iReturn != (pHeader->iWords+4)*2){
00215 printf("fLmdPutElement: Write error \n");
00216 return(LMD__FAILURE);
00217 }
00218 pLmdControl->pMbsFileHeader->iElements++;
00219 pLmdControl->iElements++;
00220 if(pLmdControl->iOffsetEntries)fLmdOffsetSet(pLmdControl,iReturn/4);
00221 return(LMD__SUCCESS);
00222
00223 }
00224 if((pHeader->iWords+4) > pLmdControl->iLeftWords){
00225 iReturn=fLmdWriteBuffer(pLmdControl, (char *)pLmdControl->pBuffer,
00226 (pLmdControl->iBufferWords-pLmdControl->iLeftWords)*2);
00227 pLmdControl->iBytes+=iReturn;
00228 if(iReturn != (pLmdControl->iBufferWords-pLmdControl->iLeftWords)*2)return(LMD__FAILURE);
00229 pLmdControl->iLeftWords=pLmdControl->iBufferWords;
00230 }
00231 if((pHeader->iWords+4) > pLmdControl->iLeftWords){
00232 printf("fLmdPutElement: Element too big: %d words\n",pHeader->iWords+4);
00233 return(PUTLMD__TOOBIG);
00234 }
00235
00236 ps=(uint32_t *)pHeader;
00237 pd=(uint32_t *)pLmdControl->pBuffer+(pLmdControl->iBufferWords-pLmdControl->iLeftWords)/2;
00238 iReturn=(pHeader->iWords+4)/2;
00239 for(i=0;i<iReturn;i++) *pd++ = *ps++;
00240 pLmdControl->pMbsFileHeader->iElements++;
00241 pLmdControl->iElements++;
00242 pLmdControl->iLeftWords -= (pHeader->iWords+4);
00243 if(pLmdControl->iOffsetEntries)fLmdOffsetSet(pLmdControl,iReturn);
00244 return(LMD__SUCCESS);
00245 }
00246
00247
00248 uint32_t fLmdPutBuffer(
00249 sLmdControl *pLmdControl,
00250 sMbsHeader *pHeader,
00251 uint32_t Items){
00252
00253 sMbsHeader *pH;
00254 uint32_t Bytes=0,TotalBytes=0,i, elements;
00255 int64_t fileleft,used;
00256 int32_t iReturn;
00257
00258
00259 if(pLmdControl->iOffsetEntries && (pLmdControl->iOffsetSize == 4)){
00260 pH=pHeader;
00261 elements=pLmdControl->iElements+Items+2;
00262 used=pLmdControl->iBytes/4;
00263 fileleft=0xffffffff - used - (4+elements);
00264 for(i=0;i<Items;i++) {
00265 Bytes = (4+pH->iWords)*2;
00266 TotalBytes += Bytes;
00267 pH=(sMbsHeader *)((int16_t*)pH + Bytes/2);
00268 }
00269 if((int64_t)TotalBytes/4 > fileleft){
00270 printf("fLmdPutElement: File size exceed\n");
00271 return(PUTLMD__EXCEED);
00272 }
00273 Bytes=0;
00274 TotalBytes=0;
00275 }
00276 pH=pHeader;
00277 for(i=0;i<Items;i++)
00278 {
00279 pLmdControl->iElements++;
00280 Bytes = (4+pH->iWords)*2;
00281 TotalBytes += Bytes;
00282 if(pLmdControl->iOffsetEntries)fLmdOffsetSet(pLmdControl,Bytes/4);
00283 if((pH->iWords+4) > pLmdControl->pMbsFileHeader->iMaxWords)
00284 pLmdControl->pMbsFileHeader->iMaxWords=pH->iWords+4;
00285 pH=(sMbsHeader *)((int16_t *)pH+Bytes/2);
00286 }
00287 iReturn=fLmdWriteBuffer(pLmdControl,(char *)pHeader,TotalBytes);
00288 pLmdControl->iBytes+=iReturn;
00289 if(iReturn != TotalBytes)return(LMD__FAILURE);
00290 else {
00291 pLmdControl->pMbsFileHeader->iElements += Items;
00292 return(LMD__SUCCESS);
00293 }
00294 }
00295
00296
00297 uint32_t fLmdPutClose(
00298 sLmdControl *pLmdControl){
00299 int32_t iReturn,i;
00300 lmdoff_t current,c;
00301
00302 if(pLmdControl->iBufferWords > pLmdControl->iLeftWords){
00303 iReturn=fLmdWriteBuffer(pLmdControl, (char *)pLmdControl->pBuffer,
00304 (pLmdControl->iBufferWords-pLmdControl->iLeftWords)*2);
00305 pLmdControl->iBytes+=iReturn;
00306 if(iReturn != (pLmdControl->iBufferWords-pLmdControl->iLeftWords)*2){
00307 printf("fLmdPutClose: Error writing last buffer. Closing file.\n");
00308
00309 rewind(pLmdControl->fFile);
00310 fLmdWriteBuffer(pLmdControl, (char *)pLmdControl->pMbsFileHeader,
00311 sizeof(sMbsFileHeader));
00312 fLmdCleanup(pLmdControl);
00313 return(LMD__FAILURE);
00314 }
00315 }
00316 if(pLmdControl->iOffsetEntries)
00317 if(fLmdOffsetWrite(pLmdControl) != LMD__SUCCESS)
00318 pLmdControl->pMbsFileHeader->iTableOffset=0;
00319
00320
00321 rewind(pLmdControl->fFile);
00322 fLmdWriteBuffer(pLmdControl, (char *)pLmdControl->pMbsFileHeader,
00323 sizeof(sMbsFileHeader));
00324 return(fLmdGetClose(pLmdControl));
00325 }
00326
00327 #ifndef FILEONLY
00328
00329 uint32_t fLmdConnectMbs(sLmdControl *pLmdControl,
00330 char *Nodename,
00331 uint32_t iPort,
00332 uint32_t *iBufferBytes)
00333 {
00334
00335 int32_t stat;
00336 sMbsTransportInfo sMbs;
00337
00338 if(iPort==0) pLmdControl->iPort=PORT__TRANS;
00339 else pLmdControl->iPort=iPort;
00340 memset(pLmdControl,0,sizeof(sLmdControl));
00341 pLmdControl->pTCP=(struct s_tcpcomm *)malloc(sizeof(struct s_tcpcomm));
00342 pLmdControl->iTCPowner=1;
00343 if(pLmdControl->iPort==PORT__TRANS)
00344 printf("fLmdConnectMbs: Connect to transport server %s port %d\n",Nodename,pLmdControl->iPort);
00345 if(pLmdControl->iPort==PORT__STREAM)
00346 printf("fLmdConnectMbs: Connect to stream server %s port %d\n",Nodename,pLmdControl->iPort);
00347 stat=f_stc_connectserver(Nodename,pLmdControl->iPort,&pLmdControl->iTCP,pLmdControl->pTCP);
00348 if (stat != STC__SUCCESS) {
00349 printf ("fLmdConnectMbs: Error connect to %s \n",Nodename);
00350 fLmdCleanup(pLmdControl);
00351 return(LMD__FAILURE);
00352 }
00353 stat=f_stc_read((int32_t *)&sMbs, sizeof(sMbsTransportInfo),pLmdControl->iTCP,3);
00354 if (stat != STC__SUCCESS) {
00355 printf ("fLmdConnectMbs: Error read info from %s \n",Nodename);
00356 fLmdCleanup(pLmdControl);
00357 return(LMD__FAILURE);
00358 }
00359 if(sMbs.iEndian != 1)pLmdControl->iSwap=1;
00360 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)&sMbs,sizeof(sMbsTransportInfo)/4);
00361 if(sMbs.iBuffers > 1){
00362 printf("fLmdConnectMbs: Event spanning not supported!\n");
00363 fLmdCleanup(pLmdControl);
00364 return(LMD__FAILURE);
00365 }
00366 if(sMbs.iStreams > 0){
00367 printf("fLmdConnectMbs: MBS not in DABC mode!\n");
00368 fLmdCleanup(pLmdControl);
00369 return(LMD__FAILURE);
00370 }
00371 strcpy(pLmdControl->cFile,Nodename);
00372 if(iBufferBytes == LMD__GET_EVENTS){
00373 pLmdControl->pBuffer = (int16_t *) malloc(sMbs.iMaxBytes);
00374 pLmdControl->iBufferWords=sMbs.iMaxBytes/2;
00375 pLmdControl->iInternBuffer=1;
00376 } else {
00377 *iBufferBytes = sMbs.iMaxBytes;
00378 }
00379 return(LMD__SUCCESS);
00380 }
00381
00382 uint32_t fLmdInitMbs(sLmdControl *pLmdControl,
00383 char *Nodename,
00384 uint32_t iMaxBytes,
00385 uint32_t iBuffers,
00386 uint32_t iStreams,
00387 uint32_t iPort,
00388 uint32_t iTimeout)
00389 {
00390 int32_t stat;
00391
00392 if(iBuffers > 1){printf("fLmdInitMbs: Event spanning not supported!\n");return(LMD__FAILURE);}
00393 if(iStreams > 0){printf("fLmdInitMbs: MBS not in DABC mode!\n");return(LMD__FAILURE);}
00394 pLmdControl->iPort=iPort;
00395 strcpy(pLmdControl->cFile,Nodename);
00396 if(pLmdControl->pBuffer == NULL) pLmdControl->pBuffer = (int16_t*) malloc(iMaxBytes);
00397 pLmdControl->iBufferWords=iMaxBytes/2;
00398 pLmdControl->iInternBuffer=1;
00399 pLmdControl->iTCP=pLmdControl->pTCP->socket;
00400 pLmdControl->iTcpTimeout=iTimeout;
00401 pLmdControl->iTCPowner=0;
00402 return(LMD__SUCCESS);
00403 }
00404
00405 uint32_t fLmdCloseMbs(sLmdControl *pLmdControl){
00406
00407 int32_t stat;
00408 char cClose[12];
00409
00410 if(pLmdControl->iPort == PORT__STREAM) {
00411 memset(cClose,0,sizeof(cClose));
00412 strcpy(cClose, "CLOSE");
00413 stat=f_stc_write(cClose,12,pLmdControl->iTCP);
00414 }
00415 stat=f_stc_close(pLmdControl->pTCP);
00416 pLmdControl->pMbsFileHeader = NULL;
00417 if(pLmdControl->iTCPowner==0)pLmdControl->pTCP=NULL;
00418 fLmdCleanup(pLmdControl);
00419 return(stat);
00420 }
00421
00422 uint32_t fLmdGetMbsEvent(sLmdControl *pLmdControl, sMbsHeader** event)
00423 {
00424 uint32_t stat;
00425 sMbsHeader *pM;
00426 *event=NULL;
00427 if(pLmdControl->iLeftWords == 0){
00428 stat=fLmdGetMbsBuffer(pLmdControl,NULL,0,NULL,NULL);
00429 if(stat != LMD__SUCCESS){
00430 return(stat);
00431 }
00432
00433 pLmdControl->pMbsHeader=(sMbsHeader *)(pLmdControl->pBuffer+sizeof(sMbsBufferHeader)/2);
00434 }
00435 pM=pLmdControl->pMbsHeader;
00436 pLmdControl->iLeftWords -= (pLmdControl->pMbsHeader->iWords+4);
00437 pLmdControl->pMbsHeader =
00438 (sMbsHeader *)((int16_t *)pLmdControl->pMbsHeader +
00439 pLmdControl->pMbsHeader->iWords+4);
00440 pLmdControl->iElements++;
00441 *event=pM;
00442 return(LMD__SUCCESS);
00443 }
00444
00445 uint32_t fLmdGetMbsBuffer(
00446 sLmdControl *pLmdControl,
00447 sMbsBufferHeader *pBuffer,
00448 uint32_t iBytes,
00449 uint32_t *iElements,
00450 uint32_t *iBytesUsed){
00451
00452 sMbsHeader *pm;
00453 sMbsBufferHeader *pBuf;
00454 uint32_t *ps, *pd, i,ii, elem=0, size=0, usedBytes=0,leftBytes=0;
00455 int32_t iReturn;
00456 char cRequest[12];;
00457
00458 leftBytes=iBytes;
00459 pBuf=pBuffer;
00460 if(pBuf == NULL){
00461 pBuf=(sMbsBufferHeader *)pLmdControl->pBuffer;
00462 leftBytes=pLmdControl->iBufferWords*2;
00463 }
00464 if(pBuf == NULL){
00465 printf("fLmdGetMbsBuffer: Need buffer to read\n");
00466 return(LMD__FAILURE);
00467 }
00468 if(leftBytes < sizeof(sMbsBufferHeader)){
00469 printf("fLmdGetMbsBuffer: %s buffer size %d too small for %d bytes\n",
00470 pLmdControl->cFile,leftBytes,sizeof(sMbsBufferHeader));
00471 return(LMD__FAILURE);
00472 }
00473
00474 if(pLmdControl->iPort == PORT__STREAM) {
00475 memset(cRequest,0,sizeof(cRequest));
00476 strcpy(cRequest, "GETEVT");
00477 iReturn=f_stc_write(cRequest,12,pLmdControl->iTCP);
00478 }
00479 iReturn=f_stc_read((int32_t *)pBuf,sizeof(sMbsBufferHeader),pLmdControl->iTCP,pLmdControl->iTcpTimeout);
00480 if(iReturn == STC__TIMEOUT) return(LMD__TIMEOUT);
00481 if(iReturn != STC__SUCCESS) return(LMD__FAILURE);
00482 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)pBuf,sizeof(sMbsBufferHeader)/4);
00483 if(leftBytes < (sizeof(sMbsBufferHeader)+2*pBuf->iUsedWords)){
00484 printf("fLmdGetMbsBuffer: %s buffer size %d too small for %d bytes\n",
00485 pLmdControl->cFile,leftBytes,sizeof(sMbsBufferHeader)+2*pBuf->iMaxWords);
00486 return(LMD__FAILURE);
00487 }
00488 usedBytes=pBuf->iUsedWords*2;
00489 if((pBuf->iType&0xffff) == 100)
00490 iReturn=f_stc_read((int32_t *)(pBuf+1),usedBytes,pLmdControl->iTCP,-1);
00491 if(iReturn == STC__TIMEOUT) return(LMD__TIMEOUT);
00492 if(iReturn != STC__SUCCESS) return(LMD__FAILURE);
00493 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)(pBuf+1),usedBytes/4);
00494 if(iBytesUsed != NULL)*iBytesUsed =usedBytes+sizeof(sMbsBufferHeader);
00495 if(iElements != NULL)*iElements =pBuf->iElements;
00496 pLmdControl->iBytes += usedBytes;
00497 pLmdControl->iLeftWords = usedBytes/2;
00498 pLmdControl->pMbsFileHeader = (sMbsFileHeader *)pBuf;
00499 return(LMD__SUCCESS);
00500 }
00501 #endif
00502
00503
00504
00505 uint32_t fLmdGetOpen(sLmdControl *pLmdControl,
00506 char *Filename,
00507 sMbsFileHeader *pBuffHead,
00508 uint32_t iBytes,
00509 uint32_t iUseOffset)
00510 {
00511 int32_t iReturn;
00512 uint32_t l=0,i,bufferBytes=0,h[12];
00513 lmdoff_t to;
00514
00515 memset(pLmdControl,0,sizeof(sLmdControl));
00516 if(pBuffHead == LMD__INTERNAL_HEADER){
00517 pLmdControl->pMbsFileHeader = (sMbsFileHeader *)malloc(sizeof(sMbsFileHeader));
00518 pLmdControl->iInternHeader=1;
00519 } else {
00520 pLmdControl->pMbsFileHeader = pBuffHead;
00521 pLmdControl->iInternHeader=0;
00522 }
00523 memset(pLmdControl->pMbsFileHeader,0,sizeof(sMbsFileHeader));
00524
00525
00526 strcpy(pLmdControl->cFile,Filename);
00527 if((pLmdControl->fFile=(FILE *)fopen64(Filename,"r"))== NULL)
00528 {
00529 printf("fLmdGetOpen: File not found: %d\n",Filename);
00530 fLmdCleanup(pLmdControl);
00531 return(GETLMD__NOFILE);
00532 }
00533
00534 iReturn=fLmdReadBuffer(pLmdControl,
00535 (char *)pLmdControl->pMbsFileHeader,
00536 sizeof(sMbsFileHeader));
00537 if(iReturn!=sizeof(sMbsFileHeader)) {
00538 printf("fLmdGetOpen: LMD format error: no LMD file: %s\n",Filename);
00539 fLmdGetClose(pLmdControl);
00540 return(GETLMD__NOLMDFILE);
00541 }
00542
00543 if(pLmdControl->pMbsFileHeader->iEndian != 1) pLmdControl->iSwap=1;
00544 if(pLmdControl->iSwap){
00545 printf("do swap !!!\n");
00546 fLmdSwap4((uint32_t *)pLmdControl->pMbsFileHeader,sizeof(sMbsFileHeader)/4);
00547 fLmdSwap8((uint64_t *)&pLmdControl->pMbsFileHeader->iTableOffset,1);
00548 }
00549 if(pLmdControl->pMbsFileHeader->iType != LMD__TYPE_FILE_HEADER_101_1){
00550 printf("fLmdGetOpen: LMD format error: no LMD file: %s, type is %0x\n",
00551 Filename,pLmdControl->pMbsFileHeader->iType);
00552 fLmdGetClose(pLmdControl);
00553 return(GETLMD__NOLMDFILE);
00554 }
00555
00556 if((iUseOffset == LMD__INDEX)&&(pLmdControl->pMbsFileHeader->iTableOffset > 0)){
00557
00558 pLmdControl->iOffsetSize=pLmdControl->pMbsFileHeader->iOffsetSize;
00559 iReturn=fLmdOffsetRead(pLmdControl);
00560 if(iReturn != LMD__SUCCESS){
00561 printf("fLmdGetOpen: Index format error: %s\n",Filename);
00562 fLmdGetClose(pLmdControl);
00563 return(iReturn);
00564 }
00565 }
00566
00567 pLmdControl->iBytes+=iReturn;
00568
00569 if(pLmdControl->pMbsFileHeader->iUsedWords > 0)
00570 {
00571
00572
00573 pLmdControl->cHeader = malloc(pLmdControl->pMbsFileHeader->iUsedWords*2);
00574 iReturn = fLmdReadBuffer(pLmdControl,pLmdControl->cHeader,
00575 pLmdControl->pMbsFileHeader->iUsedWords*2 );
00576 if(iReturn!=pLmdControl->pMbsFileHeader->iUsedWords*2) {
00577 printf("fLmdGetOpen: LMD format error: no LMD file: %s\n",Filename);
00578 fLmdGetClose(pLmdControl);
00579 return(GETLMD__NOLMDFILE);
00580 }
00581 }
00582
00583 bufferBytes = iBytes;
00584 if(bufferBytes < pLmdControl->pMbsFileHeader->iMaxWords*2)
00585 bufferBytes = pLmdControl->pMbsFileHeader->iMaxWords*2;
00586 fLmdPrintFileHeader(1,pLmdControl->pMbsFileHeader);
00587 pLmdControl->pBuffer = (int16_t *)malloc(bufferBytes);
00588 pLmdControl->iInternBuffer = 1;
00589 pLmdControl->iBufferWords=bufferBytes/2;
00590
00591 printf("fLmdGetOpen: %s words %u\n", Filename, pLmdControl->iBufferWords);
00592
00593 pLmdControl->iLeftWords = 0;
00594 pLmdControl->pMbsHeader = NULL;
00595 return(LMD__SUCCESS);
00596 }
00597
00598 uint32_t fLmdGetBuffer(
00599 sLmdControl *pLmdControl,
00600 sMbsHeader *pMbsHeader,
00601 uint32_t iBytes,
00602 uint32_t *iElements,
00603 uint32_t *iBytesUsed){
00604
00605 sMbsHeader *pm;
00606 uint32_t *ps, *pd, i,ii, elem=0, size=0, leftBytes=0, used, elem_sz;
00607 int32_t iReturn;
00608
00609 if(iBytes < pLmdControl->pMbsFileHeader->iMaxWords){
00610 printf("fLmdGetBuffer: %s buffer size %d too small for %d bytes\n",
00611 pLmdControl->cFile,iBytes,pLmdControl->pMbsFileHeader->iMaxWords);
00612 return(LMD__FAILURE);
00613 }
00614 if(pMbsHeader == NULL){
00615 printf("fLmdGetBuffer: Need buffer to read\n");
00616 return(LMD__FAILURE);
00617 }
00618 *iBytesUsed=0;
00619 *iElements=0;
00620 if(pLmdControl->iElements == pLmdControl->pMbsFileHeader->iElements) return(GETLMD__EOFILE);
00621
00622
00623 if(pLmdControl->iOffsetEntries){
00624 fLmdOffsetElements(pLmdControl,iBytes, &elem, &used);
00625
00626 iReturn=fLmdReadBuffer(pLmdControl,(char *)pMbsHeader,used);
00627 if(iReturn <= 0){
00628 printf("fLmdGetBuffer: EOF: %s\n",pLmdControl->cFile);
00629 return(GETLMD__EOFILE);
00630 }
00631 if(iReturn!=used) {
00632 printf("fLmdGetBuffer: LMD read error: unexpected EOF: %s %u %u\n",
00633 pLmdControl->cFile, iReturn, used);
00634 return(GETLMD__NOLMDFILE);
00635 }
00636 *iBytesUsed=used;
00637 *iElements=elem;
00638 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)pMbsHeader,iReturn/4);
00639 pLmdControl->iBytes+=iReturn;
00640 return(LMD__SUCCESS);
00641 }
00642
00643
00644 leftBytes = pLmdControl->iLeftWords*2;
00645 if(leftBytes>0) {
00646 if (leftBytes > iBytes) {
00647 printf("fLmdGetBuffer: stored piece of data (%u) larger than provided buffer (%u)\n",
00648 leftBytes, iBytes);
00649 return(LMD__FAILURE);
00650 }
00651
00652 if (pLmdControl->pMbsHeader==0) {
00653 printf("fLmdGetBuffer: Internal error pMbsHeader==0\n");
00654 return(LMD__FAILURE);
00655 }
00656
00657 memcpy(pMbsHeader, pLmdControl->pMbsHeader, leftBytes);
00658 }
00659 iReturn = fLmdReadBuffer(pLmdControl,(char *)pMbsHeader+leftBytes, iBytes-leftBytes);
00660 if(iReturn <= 0) {
00661 printf("fLmdGetBuffer: EOF: %s\n",pLmdControl->cFile);
00662 if (leftBytes>0)
00663 printf("fLmdGetBuffer: EOF while we have some rest data (%u)\n", leftBytes);
00664 else
00665 return(GETLMD__EOFILE);
00666 }
00667
00668 if(iReturn > (iBytes-leftBytes)) {
00669 printf("fLmdGetBuffer: LMD read error %s - too many bytes read %u wants %u",
00670 pLmdControl->cFile, iReturn, iBytes-leftBytes);
00671 return(GETLMD__NOLMDFILE);
00672 }
00673
00674 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)pMbsHeader+leftBytes/4,iReturn/4);
00675 pLmdControl->iBytes += iReturn;
00676 leftBytes += iReturn;
00677
00678 pm=pMbsHeader;
00679 while(leftBytes >=8){
00680 if(pm->iType == LMD__TYPE_FILE_INDEX_101_2) break;
00681 elem_sz = (pm->iWords+4)*2;
00682 if(elem_sz > leftBytes) break;
00683
00684 *iBytesUsed += elem_sz;
00685 *iElements += 1;
00686 pLmdControl->iElements++;
00687 pm = (sMbsHeader *)((char*)pm + elem_sz);
00688 leftBytes -= elem_sz;
00689 }
00690
00691
00692 if(leftBytes>0){
00693 if(leftBytes > pLmdControl->iBufferWords*2){
00694 printf("fLmdGetBuffer: ERROR: internal buffer overflow. Needed:%d available:%d\n",
00695 leftBytes,pLmdControl->iBufferWords*2);
00696 return(LMD__FAILURE);
00697 } else {
00698 memcpy(pLmdControl->pBuffer,pm,leftBytes);
00699 }
00700 }
00701 pLmdControl->iLeftWords = leftBytes/2;
00702 if (pLmdControl->iLeftWords>0)
00703 pLmdControl->pMbsHeader = (sMbsHeader*)pLmdControl->pBuffer;
00704 else
00705 pLmdControl->pMbsHeader = 0;
00706
00707 return(LMD__SUCCESS);
00708 }
00709
00710 uint32_t fLmdGetElement(sLmdControl *pLmdControl, uint32_t iEvent, sMbsHeader **event){
00711 sMbsHeader *pM;
00712 uint32_t *ps, *pd, i, evsz;
00713 int32_t iReturn;
00714 *event=NULL;
00715
00716 if(iEvent == LMD__NO_INDEX) {
00717 if(pLmdControl->pBuffer==NULL) return(GETLMD__NOBUFFER);
00718 if(pLmdControl->pMbsFileHeader->iElements==0) return(GETLMD__NOMORE);
00719
00720
00721 if ((pLmdControl->iLeftWords < 4) ||
00722 (pLmdControl->pMbsHeader == 0) ||
00723 (pLmdControl->pMbsHeader->iWords+4 > pLmdControl->iLeftWords)) {
00724
00725 if (pLmdControl->iLeftWords > 0) {
00726 memmove(pLmdControl->pBuffer, pLmdControl->pMbsHeader, pLmdControl->iLeftWords*2);
00727
00728 }
00729
00730
00731
00732 iReturn = fLmdReadBuffer(pLmdControl,
00733 (char *)(pLmdControl->pBuffer+pLmdControl->iLeftWords),
00734 (pLmdControl->iBufferWords-pLmdControl->iLeftWords)*2);
00735
00736 if(iReturn <= 0) { printf("fLmdGetElement: EOF\n"); return(GETLMD__EOFILE); }
00737
00738 if(pLmdControl->iSwap) fLmdSwap4((uint32_t *)(pLmdControl->pBuffer+pLmdControl->iLeftWords),iReturn/4);
00739
00740 pLmdControl->iBytes += iReturn;
00741 pLmdControl->pMbsHeader=(sMbsHeader *)pLmdControl->pBuffer;
00742 pLmdControl->iLeftWords += iReturn/2;
00743 }
00744
00745
00746
00747 evsz = (pLmdControl->pMbsHeader->iWords + 4) * 2;
00748
00749 if (evsz > pLmdControl->iLeftWords*2) {
00750 printf ("fLmdGetElement: Error, full element %u does not fit in buffer %u",
00751 evsz, pLmdControl->iLeftWords*2);
00752 return (GETLMD__TOOBIG);
00753 }
00754
00755 pLmdControl->pMbsFileHeader->iElements--;
00756 pM = pLmdControl->pMbsHeader;
00757 pLmdControl->pMbsHeader = (sMbsHeader *) ((char*) pM + evsz);
00758 pLmdControl->iLeftWords -= evsz/2;
00759 pLmdControl->iElements++;
00760 *event=pM;
00761 return(LMD__SUCCESS);
00762 }
00763
00764 if(pLmdControl->iOffsetEntries){
00765 if(iEvent >= pLmdControl->iOffsetEntries)return(GETLMD__OUTOF_RANGE);
00766 fseeko64(pLmdControl->fFile,fLmdOffsetGet(pLmdControl,iEvent-1)*4,SEEK_SET);
00767 i=(fLmdOffsetGet(pLmdControl,iEvent)-fLmdOffsetGet(pLmdControl,iEvent-1));
00768 iReturn=fLmdReadBuffer(pLmdControl,(char *)pLmdControl->pBuffer,i*4);
00769 if(iReturn <= 0) {printf("fLmdGetElement: EOF\n");return(GETLMD__EOFILE);}
00770 if(iReturn!=(i*4)) {
00771 printf("fLmdGetBuffer: LMD read error: unexpected EOF: %s\n",pLmdControl->cFile);
00772 return(GETLMD__EOFILE);
00773 }
00774 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)pLmdControl->pBuffer,iReturn/4);
00775 pLmdControl->pMbsHeader=(sMbsHeader *)pLmdControl->pBuffer;
00776 if((pLmdControl->pMbsHeader->iWords+4) != i*2){
00777 printf("fLmdGetElement: Error Event %d: size from table is %d, header %d\n",
00778 iEvent,i/2,pLmdControl->pMbsHeader->iWords+4);
00779 return(GETLMD__SIZE_ERROR);
00780 }
00781 pLmdControl->iBytes+=iReturn;
00782 *event=pLmdControl->pMbsHeader;
00783 return(LMD__SUCCESS);
00784 }
00785 else return(GETLMD__NOMORE);
00786
00787 }
00788
00789 uint32_t fLmdGetClose(sLmdControl *pLmdControl)
00790 {
00791 fLmdCleanup(pLmdControl);
00792 if(fclose(pLmdControl->fFile)!=0) {
00793 pLmdControl->fFile=NULL;
00794 return(LMD__CLOSE_ERR);
00795 }
00796 pLmdControl->fFile=NULL;
00797 return(LMD__SUCCESS);
00798 }
00799
00800 int32_t fLmdReadBuffer(sLmdControl *pLmdControl, char *buffer, uint32_t bytes){
00801 int32_t IObytes;
00802 IObytes=(int32_t)fread(buffer,1,bytes,pLmdControl->fFile);
00803
00804 return(IObytes);
00805 }
00806
00807 int32_t fLmdWriteBuffer(sLmdControl *pLmdControl, char *buffer, uint32_t bytes){
00808 int32_t IObytes;
00809 IObytes=(int32_t)fwrite(buffer,1,bytes,pLmdControl->fFile);
00810
00811
00812 return(IObytes);
00813 }
00814
00815 uint64_t fLmdGetBytesWritten(sLmdControl *pLmdControl){
00816 uint64_t bytes;
00817 bytes=pLmdControl->iBytes;
00818
00819 if(pLmdControl->iBufferWords > pLmdControl->iLeftWords)
00820 bytes += (pLmdControl->iBufferWords - pLmdControl->iLeftWords)*2;
00821
00822 if ((pLmdControl->pOffset4!=NULL)||(pLmdControl->pOffset8!=NULL))
00823 bytes += (pLmdControl->iElements+1)*pLmdControl->iOffsetSize;
00824 return(bytes);
00825 }
00826
00827 uint32_t fLmdCleanup(sLmdControl *pLmdControl)
00828 {
00829
00830 if(pLmdControl->pTCP != NULL)free(pLmdControl->pTCP);
00831 if(pLmdControl->cHeader != NULL)free(pLmdControl->cHeader);
00832 if(pLmdControl->pOffset4 != NULL)free(pLmdControl->pOffset4);
00833 if(pLmdControl->pOffset8 != NULL)free(pLmdControl->pOffset8);
00834 if((pLmdControl->pBuffer != NULL) && (pLmdControl->iInternBuffer>0))
00835 free(pLmdControl->pBuffer);
00836 if((pLmdControl->pMbsFileHeader != NULL) && (pLmdControl->iInternHeader>0))
00837 free(pLmdControl->pMbsFileHeader);
00838 pLmdControl->pTCP=NULL;
00839 pLmdControl->cHeader=NULL;
00840 pLmdControl->pBuffer=NULL;
00841 pLmdControl->iInternBuffer=0;
00842 pLmdControl->pOffset4=NULL;
00843 pLmdControl->pOffset8=NULL;
00844 pLmdControl->pMbsFileHeader=NULL;
00845 pLmdControl->iInternHeader=0;
00846 pLmdControl->pMbsHeader=NULL;
00847 return (LMD__SUCCESS);
00848 }
00849
00850
00851 uint32_t fLmdGetSwap(sLmdControl *pLmdControl){
00852 if(pLmdControl != NULL)
00853 return(pLmdControl->iSwap);
00854 else return(-1);
00855 }
00856
00857
00858 void fLmdSetWrittenEndian(sLmdControl *pLmdControl,uint32_t iE){
00859 if(pLmdControl->pMbsFileHeader != NULL)
00860 pLmdControl->pMbsFileHeader->iWrittenEndian=iE;
00861 else printf("fLmdSetWrittenEndian: No file header allocated!");
00862 }
00863
00864
00865 uint32_t fLmdGetWrittenEndian(sLmdControl *pLmdControl){
00866 if(pLmdControl->pMbsFileHeader != NULL)
00867 return(pLmdControl->pMbsFileHeader->iWrittenEndian);
00868 else printf("fLmdGetWrittenEndian: No file header allocated!");
00869 return(LMD__ENDIAN_UNKNOWN);
00870 }
00871
00872 sLmdControl * fLmdAllocateControl(){
00873 sLmdControl *x;
00874 x=(sLmdControl *)malloc(sizeof(sLmdControl));
00875 memset(x,0,sizeof(sLmdControl));
00876 return(x);
00877 }
00878
00879 void fLmdOffsetElements(sLmdControl *pLmdControl,
00880 uint32_t bytes,
00881 uint32_t *elements,
00882 uint32_t *used){
00883 lmdoff_t *off1,*off2;
00884 uint32_t elem=0,i,*iff1,*iff2;
00885
00886 if(pLmdControl->iOffsetSize == 4){
00887 iff1=pLmdControl->pOffset4+pLmdControl->iElements;
00888 iff2=iff1;
00889 for(i=pLmdControl->iElements;i<pLmdControl->iOffsetEntries-1;i++){
00890 if((*(iff1+1)-*iff2)>bytes/4) break;
00891 iff1++;
00892 elem++;
00893 pLmdControl->iElements++;
00894 }
00895 *used=(*iff1-*iff2)*4;
00896 *elements=elem;
00897 }
00898 else if(pLmdControl->iOffsetSize == 8){
00899 off1=pLmdControl->pOffset8+pLmdControl->iElements;
00900 off2=off1;
00901 for(i=pLmdControl->iElements;i<pLmdControl->iOffsetEntries-1;i++){
00902 if((*(off1+1)-*off2)>bytes/4) break;
00903 off1++;
00904 elem++;
00905 pLmdControl->iElements++;
00906 }
00907 *used=(*off1-*off2)*4;
00908 *elements=elem;
00909 }
00910 }
00911
00912 uint32_t fLmdOffsetRead(sLmdControl *pLmdControl)
00913 {
00914 int32_t iReturn;
00915 sMbsHeader *pTableHead;
00916
00917 pTableHead=(sMbsHeader *)malloc(16);
00918 fseeko64(pLmdControl->fFile,(lmdoff_t)pLmdControl->pMbsFileHeader->iTableOffset*4,SEEK_SET);
00919 iReturn=fLmdReadBuffer(pLmdControl, (char *)pTableHead,16);
00920 if(iReturn!=16) {
00921 printf("fLmdGetBuffer: LMD read error: unexpected EOF: %s\n",pLmdControl->cFile);
00922 free(pTableHead);
00923 return(GETLMD__NOLMDFILE);
00924 }
00925 if(pLmdControl->iSwap)fLmdSwap4((uint32_t *)pTableHead,4);
00926 if(pTableHead->iType != LMD__TYPE_FILE_INDEX_101_2){
00927 printf("fLmdOffsetTable: LMD format error: no index table: %s, type %0x\n",
00928 pLmdControl->cFile,pTableHead->iType);
00929 free(pTableHead);
00930 return(GETLMD__NOLMDFILE);
00931 }
00932
00933 free(pTableHead);
00934 pLmdControl->iOffsetEntries=pLmdControl->pMbsFileHeader->iElements+1;
00935 pLmdControl->pOffset8=(lmdoff_t *)malloc(pLmdControl->iOffsetEntries*pLmdControl->iOffsetSize);
00936 iReturn=fLmdReadBuffer(pLmdControl,
00937 (char *)pLmdControl->pOffset8,
00938 pLmdControl->iOffsetEntries*pLmdControl->iOffsetSize);
00939 if(iReturn!=pLmdControl->iOffsetEntries*pLmdControl->iOffsetSize) {
00940 printf("fLmdOffsetTable: LMD format error: no index table: %s\n",pLmdControl->cFile);
00941 pLmdControl->iOffsetEntries=0;
00942 free(pTableHead);
00943 return(GETLMD__NOLMDFILE);
00944 }
00945 if(pLmdControl->iSwap){
00946 fLmdSwap4((uint32_t *)pLmdControl->pOffset8,iReturn/4);
00947 if(pLmdControl->iOffsetSize == 8)
00948 fLmdSwap8((uint64_t *)pLmdControl->pOffset8,iReturn/8);
00949 }
00950
00951 fseeko64(pLmdControl->fFile,(lmdoff_t)sizeof(sMbsFileHeader),SEEK_SET);
00952
00953 if(pLmdControl->iOffsetSize == 4){
00954 pLmdControl->pOffset4= (uint32_t *)pLmdControl->pOffset8;
00955 pLmdControl->pOffset8=NULL;
00956 }
00957 return(LMD__SUCCESS);
00958 }
00959
00960 uint32_t fLmdOffsetWrite(sLmdControl *pLmdControl)
00961 {
00962 int32_t iReturn;
00963 char *pbuf;
00964 lmdoff_t current;
00965 sMbsHeader *pTableHead;
00966 pTableHead=(sMbsHeader *)malloc(16);
00967 memset(pTableHead,0,16);
00968 pTableHead->iWords=(pLmdControl->iElements+1)*pLmdControl->iOffsetSize/2+4;
00969 pTableHead->iType=LMD__TYPE_FILE_INDEX_101_2;
00970
00971
00972 iReturn=fgetpos64(pLmdControl->fFile,(fpos64_t *) ¤t);
00973 iReturn=fLmdWriteBuffer(pLmdControl, (char *)pTableHead,16);
00974 free(pTableHead);
00975 pbuf=(char *)pLmdControl->pOffset4;
00976 if(pbuf == NULL) pbuf=(char *)pLmdControl->pOffset8;
00977 iReturn=fLmdWriteBuffer(pLmdControl, pbuf,
00978 (pLmdControl->iElements+1)*pLmdControl->iOffsetSize);
00979 if(pLmdControl->pOffset8)
00980 pLmdControl->pMbsFileHeader->iTableOffset = *(pLmdControl->pOffset8+pLmdControl->iElements);
00981 if(pLmdControl->pOffset4)
00982 pLmdControl->pMbsFileHeader->iTableOffset = *(pLmdControl->pOffset4+pLmdControl->iElements);
00983 if(current/4 != pLmdControl->pMbsFileHeader->iTableOffset){
00984 printf("Table offset mismatch: current:%lld calculated:%lld, cur-cal %lld\n",
00985 current/4,pLmdControl->pMbsFileHeader->iTableOffset,
00986 current/4-pLmdControl->pMbsFileHeader->iTableOffset);
00987 return(LMD__FAILURE);
00988 }
00989 if(iReturn != (pLmdControl->iElements+1)*pLmdControl->iOffsetSize){
00990 printf("Table write error \n");
00991 return(LMD__FAILURE);
00992 }
00993 return(LMD__SUCCESS);
00994 }
00995
00996 uint32_t fLmdOffsetSet(sLmdControl *pLmdControl, uint32_t lwords)
00997 {
00998 int32_t iReturn;
00999 if(pLmdControl->iElements >= pLmdControl->iOffsetEntries)fLmdOffsetResize(pLmdControl,0);
01000 if(pLmdControl->pOffset8){
01001 *(pLmdControl->pOffset8+pLmdControl->iElements)=
01002 *(pLmdControl->pOffset8+pLmdControl->iElements-1)+(lmdoff_t)lwords;
01003 }
01004 if(pLmdControl->pOffset4){
01005 *(pLmdControl->pOffset4+pLmdControl->iElements)=
01006 *(pLmdControl->pOffset4+pLmdControl->iElements-1)+lwords;
01007 }
01008 return(LMD__SUCCESS);
01009 }
01010
01011 lmdoff_t fLmdOffsetGet(sLmdControl *pLmdControl, uint32_t index){
01012 if(pLmdControl->pOffset8)
01013 return(*(pLmdControl->pOffset8+index));
01014 if(pLmdControl->pOffset4)
01015 return((lmdoff_t)*(pLmdControl->pOffset4+index));
01016 return 0;
01017 }
01018
01019 void fLmdOffsetResize(sLmdControl *pLmdControl, uint32_t firstValue){
01020 lmdoff_t *new;
01021 uint32_t oldEntries,newEntries;
01022
01023 oldEntries=pLmdControl->iOffsetEntries;
01024 newEntries=oldEntries+OFFSET__ENTRIES;
01025 new=(lmdoff_t *)malloc(newEntries*pLmdControl->iOffsetSize);
01026 memset(new,0,newEntries*pLmdControl->iOffsetSize);
01027 if(oldEntries > 0){
01028
01029 if(pLmdControl->pOffset8){
01030 memcpy(new,pLmdControl->pOffset8,oldEntries*pLmdControl->iOffsetSize);
01031 free(pLmdControl->pOffset8);
01032 pLmdControl->pOffset8=new;
01033 }
01034 if(pLmdControl->pOffset4){
01035 memcpy(new,pLmdControl->pOffset4,oldEntries*pLmdControl->iOffsetSize);
01036 free(pLmdControl->pOffset4);
01037 pLmdControl->pOffset4=(uint32_t *)new;
01038 }
01039 }
01040 else {
01041
01042 if(pLmdControl->iOffsetSize==8){
01043 pLmdControl->pOffset8=new;
01044 *pLmdControl->pOffset8=(lmdoff_t)firstValue;
01045 }
01046 if(pLmdControl->iOffsetSize==4){
01047 pLmdControl->pOffset4=(uint32_t *)new;
01048 *pLmdControl->pOffset4=firstValue;
01049 }
01050 }
01051 pLmdControl->iOffsetEntries=newEntries;
01052 }
01053
01054 void fLmdPrintBufferHeader(uint32_t iVerbose, sMbsBufferHeader *pMbsBufferHeader)
01055 {
01056 if(iVerbose){
01057 if(pMbsBufferHeader){
01058 printf("BfHd: # %d, DataWords:%d Type:%08x Elements:%d sec:%d.%d MaxWords:%d\n",
01059 pMbsBufferHeader->iBuffer,
01060 pMbsBufferHeader->iUsedWords,
01061 pMbsBufferHeader->iType,
01062 pMbsBufferHeader->iElements,
01063 pMbsBufferHeader->iTimeSpecSec,
01064 pMbsBufferHeader->iTimeSpecNanoSec/1000,
01065 pMbsBufferHeader->iMaxWords);
01066 }}}
01067
01068 void fLmdPrintFileHeader(uint32_t iVerbose, sMbsFileHeader *pMbsFileHeader)
01069 {
01070 if(iVerbose){
01071 if(pMbsFileHeader){
01072 printf("FiHd: DataWords:%d Type:%d.%d Elements:%d sec:%d.%d MaxWords:%d Index: %llx[%d]\n",
01073 pMbsFileHeader->iUsedWords,
01074 pMbsFileHeader->iType&0xffff,
01075 pMbsFileHeader->iType>>16,
01076 pMbsFileHeader->iElements,
01077 pMbsFileHeader->iTimeSpecSec,
01078 pMbsFileHeader->iTimeSpecNanoSec/1000,
01079 pMbsFileHeader->iMaxWords,
01080 pMbsFileHeader->iTableOffset,
01081 pMbsFileHeader->iOffsetSize);
01082 }}}
01083
01084 void fLmdPrintHeader(uint32_t iVerbose, sMbsHeader *pMbsHeader)
01085 {
01086 if(iVerbose){
01087 if(pMbsHeader){
01088 printf("ElHd: words:%d type:%08x\n",
01089 pMbsHeader->iWords,
01090 pMbsHeader->iType);
01091 }}}
01092
01093 void fLmdPrintEvent(uint32_t iVerbose, sMbsEventHeader *pMbsEventHeader)
01094 {
01095 if(iVerbose){
01096 if(pMbsEventHeader){
01097 printf("EvHd: words:%6d type:%08x trigger:%2d #:%4d\n",
01098 pMbsEventHeader->iWords,
01099 pMbsEventHeader->iType,
01100 pMbsEventHeader->iTrigger>>16,
01101 pMbsEventHeader->iEventNumber);
01102 }}}
01103
01104 void fLmdPrintControl(uint32_t iVerbose, sLmdControl *pLmdControl)
01105 {
01106 if(iVerbose){
01107 printf("Ctrl: file:%s words:%d left:%d bytes read:%lld elements:%d\n",
01108 pLmdControl->cFile,
01109 pLmdControl->iBufferWords,
01110 pLmdControl->iLeftWords,
01111 pLmdControl->iBytes,
01112 pLmdControl->iElements
01113 );
01114 fLmdPrintFileHeader(iVerbose,pLmdControl->pMbsFileHeader);
01115 fLmdPrintEvent(iVerbose,(sMbsEventHeader *)pLmdControl->pMbsHeader);
01116 }}
01117
01118 void fLmdSwap4(uint32_t *array, uint32_t items){
01119 uint32_t i, *pp;
01120 pp=array;
01121 for(i=0;i<items;i++){
01122
01123 *pp =
01124 (*pp >> 24) +
01125 ((*pp >> 8)&0x0000ff00) +
01126 ((*pp << 8)&0x00ff0000) +
01127 (*pp << 24);
01128
01129 pp++;
01130 }}
01131
01132 void fLmdSwap8(uint64_t *array, uint32_t items){
01133 uint64_t *pp;
01134 uint32_t i,x;
01135 pp=array;
01136 for(i=0;i<items;i++){
01137
01138 *pp = (*pp << 32) + (*pp >> 32);
01139
01140 pp++;
01141 }}
01142