Index: trunk/Poubelle/archTOI.old/archeopsfile.cc
===================================================================
--- trunk/Poubelle/archTOI.old/archeopsfile.cc	(revision 310)
+++ trunk/Poubelle/archTOI.old/archeopsfile.cc	(revision 315)
@@ -1,6 +1,12 @@
 #define utilitaires_de_block_archeops
 #include "archeopsfile.h"
+extern "C" {
 #include "compress.h"
+}
 #include <iostream.h>
+
+// BlockSet is the current "state" of the file reader automate.
+// It contains the last blocks of each kind, the current block and
+// the look-ahead block.
 
 class BlockSet {
@@ -27,4 +33,9 @@
   block_type_gyro_comprime* lastGyroComp;
   block_type_sst_comprime*  lastSSTComp;
+    
+  block_type_modele         curBlock;
+  block_type_modele         peekBlock;
+  
+  SSTHandler sstHandler;
 };
 
@@ -45,4 +56,7 @@
   lastGyroComp   = NULL;
   lastSSTComp    = NULL;
+  
+  memset(&curBlock,0,sizeof(block_type_modele));
+  memset(&peekBlock,0,sizeof(block_type_modele));
 }
 
@@ -65,5 +79,7 @@
 }
 
-BlockSet::BlockSet(BlockSet const& x) {
+BlockSet::BlockSet(BlockSet const& x) 
+: sstHandler(x.sstHandler)
+{
   lastParam      = NULL;
   lastJournal    = NULL;
@@ -81,4 +97,5 @@
   lastGyroComp   = NULL;
   lastSSTComp    = NULL;
+  
   if (x.lastParam) {
     lastParam = new block_type_param;
@@ -141,4 +158,8 @@
     *lastSSTComp = *x.lastSSTComp;
   }
+  
+  memcpy(&curBlock,&x.curBlock,sizeof(block_type_modele));
+  memcpy(&peekBlock,&x.peekBlock,sizeof(block_type_modele));
+
 }
 
@@ -232,5 +253,6 @@
   fseek(f,0,SEEK_END);
   fLen = ftell(f);
-  curPos = 0;
+  curPos = -1;
+  peekPos = -1;
   curKind = -1;
   curRawKind = -1;
@@ -256,8 +278,9 @@
   }
 
-  curBlock   = x.curBlock;
+  //curBlock   = x.curBlock;
   curKind    = x.curKind;
   curRawKind = x.curRawKind;
   curPos     = x.curPos;
+  peekPos    = x.peekPos;
   fLen       = x.fLen;
   fn         = x.fn;
@@ -275,4 +298,7 @@
   delete blockSet;
   blockSet = new BlockSet(*oldFile.blockSet);
+  //curBlock = oldFile.curBlock;
+  //peekBlock = oldFile.peekBlock;
+  peekPos = 0;
   setUTCOffset(oldFile.utcOffset);
 }
@@ -282,15 +308,28 @@
 
 bool ArcheopsFile::nextBlock() {
-  //if (curPos>0) saveCurBlock();
-  //if (curPos<0) curPos = 0;
-  if (curPos+12 > fLen) return false;
-  fseek(f,curPos,SEEK_SET);
-  size_t read = fread(&curBlock,1,sizeof(curBlock),f);
-  if (read < longueur_block(&curBlock)) return false;
-  if (verifie_block(&curBlock) != block_correct) {
+  // si pas de peek, alors en lire un pour commencer...
+  if (peekPos<0) {
+    peekPos=0; if (!nextBlock()) return false;
+  }
+  block_type_modele* curBlock=currentBlock();
+  block_type_modele* peekBlock=aheadBlock();
+  
+  memcpy(curBlock, peekBlock, sizeof(block_type_modele));
+  
+  if (peekPos+12 > fLen) return false;
+  fseek(f,peekPos,SEEK_SET);
+  size_t read = fread(peekBlock,1,sizeof(block_type_modele),f);
+  swapEntete(peekBlock);
+  if (read < longueur_block(peekBlock)) return false;
+  swapContent(peekBlock);
+  if (verifie_block(peekBlock) != block_correct) {
   printf("block invalide\n"); throw ArchExc("invalid block");
   }
-  curRawKind = curKind = type_block(&curBlock);
-  curPos += longueur_block(&curBlock);
+  peekPos += longueur_block(peekBlock);
+  if (curPos < 0) {
+    curPos = 0; return true;
+  }
+  curPos += longueur_block(curBlock);
+  curRawKind = curKind = type_block(curBlock);
   //printf("block %d  :  %s \n",numero_block(&curBlock),nom_block[curKind]);
   postProcessBlock();
@@ -316,5 +355,5 @@
 
 int  ArcheopsFile::blockNum() {
-  return numero_block(&curBlock);
+  return numero_block(currentBlock());
 }
 
@@ -348,7 +387,18 @@
 
 block_type_modele*  ArcheopsFile::currentBlock() {
-  if (curPos<0) return NULL;
-  return &curBlock;
-}
+  //if (curPos<0) return NULL;
+  return &blockSet->curBlock;
+}
+
+block_type_modele*  ArcheopsFile::aheadBlock() {
+  //if (peekPos<0) return NULL;
+  return &blockSet->peekBlock;
+}
+
+bool ArcheopsFile::sameBlockNumAhead() {
+  if (curPos<0) return false;
+  return (numero_block(&blockSet->curBlock) == numero_block(&blockSet->peekBlock));
+}
+
 
 block_type_param* ArcheopsFile::lastParam() {
@@ -401,7 +451,7 @@
   switch (curKind) {
      case block_bolo_comprime: {
-       blockSet->setRawBloc(curBlock);
+       blockSet->setRawBloc(blockSet->curBlock);
        block_type_bolo blk2;
-       block_type_bolo_comprime* blk = (block_type_bolo_comprime*) &curBlock;
+       block_type_bolo_comprime* blk = (block_type_bolo_comprime*) &blockSet->curBlock;
        for(int j=0;j<nb_bolo_util;j++)	
 		{
@@ -409,17 +459,23 @@
 		}
        valide_block((block_type_modele*)&blk2,block_bolo,numero_block(blk));	
-       memcpy(&curBlock,&blk2,sizeof(blk2));
+       memcpy(&blockSet->curBlock,&blk2,sizeof(blk2));
        curKind = block_bolo;
        break;
      }
+     case block_sst_comprime: {  // $CHECK$ TBD
+     }
+     case block_sst : {
+       blockSet->sstHandler.ProcessBlock((block_type_sst*)&blockSet->curBlock);
+     }
   }
 }
 
 void ArcheopsFile::saveCurBlock() {
-  blockSet->setBloc(curBlock);
+  blockSet->setBloc(blockSet->curBlock);
 }
 
 void ArcheopsFile::pushMark() {    // push current file position, and "last" blocks`
   fposStack.push(curPos);
+  fposStack.push(peekPos);
   blockStack.push(new BlockSet(*blockSet));
 }
@@ -429,9 +485,78 @@
     delete blockSet;
     blockSet = blockStack.top();
+    blockStack.pop();
+    peekPos = fposStack.top();
+    fposStack.pop();
     curPos = fposStack.top();
-    blockStack.pop();
     fposStack.pop();
   }
 }
+
+typedef unsigned int4 uint_4;
+typedef unsigned short uint_2;
+
+static inline void bswap4(void* p)
+{
+  uint_4 tmp = *(uint_4*)p;
+  *(uint_4*)p = ((tmp >> 24) & 0x000000FF) | 
+                ((tmp >> 8)  & 0x0000FF00) |
+                ((tmp & 0x0000FF00) << 8)  |
+                ((tmp & 0x000000FF) << 24);
+}
+
+static inline void bswap2(void* p)
+{
+  uint_2 tmp = *(uint_2*)p;
+  *(uint_2*)p = ((tmp >> 8) & 0x00FF) | 
+                ((tmp & 0x00FF) << 8);
+}
+
+#ifdef __DECCXX
+#define SWAP
+#endif
+#if defined(Linux)  || defined(linux)
+#define SWAP
+#endif
+
+void ArcheopsFile::swapEntete(block_type_modele* blk) {
+#ifdef SWAP
+  bswap4(&(blk->debut));
+  bswap4(&(blk->code1));
+  bswap4(&(blk->code2));
+  long lg = longueur_block(blk);
+  bswap4(((char*)blk) + lg - 4);
+#endif
+}
+
+void ArcheopsFile::swapContent(block_type_modele* blk) {
+#ifdef SWAP
+  int typ = type_block(blk);
+
+  if (typ == block_gps) return; // char only, no swap
+  if (typ == block_une_periode) { // une_periode, des shorts
+    block_type_une_periode* b = (block_type_une_periode*) blk;
+    for (int i=0; i<nb_max_bolo; i++)
+      for (int j=0; j<nb_max_mes_per; j++)
+	bswap2(&b->bol_per[i][j]);
+    return;
+  }
+
+  for (int i=0; i<longueur_block(blk)/4-4; i++)
+    bswap4(blk->mot + i);
+  // On deswappe ce qui ne devait pas etre swappe...
+  switch (typ) {
+  case block_param: {
+    block_type_param* b = (block_type_param*) blk;
+    for (int i=0; i<nb_max_bolo; i++)
+      for (int j=0; j<8; j++)
+	bswap4(&b->param.bolo[i].bolo_nom[4*j]);
+  }
+  
+  }
+#endif
+}
+  
+  
+
 
 static int mlen[] = {31,28,31,30,31,30,31,31,30,31,30,31};
@@ -474,4 +599,15 @@
 }
 
+int ArcheopsFile::getBoloBlockNum() {
+  if (!lastBolo()) return -1;
+  return numero_block(lastBolo());
+}
+
+int ArcheopsFile::getSSTBlockNum() {
+  if (!lastSST()) return -1;
+  return numero_block(lastSST());
+}
+
+
 // GPS
 // $GPGGA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,
@@ -484,7 +620,7 @@
 
 double ArcheopsFile::getGPSUTC() {  // en secondes depuis minuit UTC jour courant
-  if (!lastGPS()) return -1;
+  if (!lastGPS()) return -9.e99;
   char* p = lastGPS()->gps;
-  if (strncmp(p, "$GPGGA,", 7)) return -1;
+  if (strncmp(p, "$GPGGA,", 7)) return -9.e99;
   p += 7;
   double t;
@@ -506,11 +642,11 @@
 
 double ArcheopsFile::getGPSLat() {  // degres, +  = NORD
-  if (!lastGPS()) return 99999;
+  if (!lastGPS()) return -9.e99;
   char* p = lastGPS()->gps;
-  if (strncmp(p, "$GPGGA,", 7)) return 99999;
+  if (strncmp(p, "$GPGGA,", 7)) return -9.e99;
   char* fence = p+80;
   p += 7;
   while (*p != ',' && p<fence) p++;
-  if (*p != ',') return 99999; 
+  if (*p != ',') return -9.e99; 
   p++;
   double t;
@@ -519,5 +655,5 @@
   t = d + t/60;
   while (*p != ',' && p<fence) p++;
-  if (*p != ',') return 99999; 
+  if (*p != ',') return -9.e99; 
   p++;
   if (*p == 'S') t = -t;
@@ -526,12 +662,12 @@
 
 double ArcheopsFile::getGPSLong() { // degres, +  = EST
-  if (!lastGPS()) return -1;
+  if (!lastGPS()) return -9.e99;
   char* p = lastGPS()->gps;
-  if (strncmp(p, "$GPGGA,", 7)) return 99999;
+  if (strncmp(p, "$GPGGA,", 7)) return -9.e99;
   char* fence = p+80;
   p += 7;
   for (int i=0; i<3; i++) {
     while (*p != ',' && p<fence) p++;
-    if (*p != ',') return 99999; 
+    if (*p != ',') return -9.e99; 
     p++;
   }
@@ -541,5 +677,5 @@
   t = d + t/60;
   while (*p != ',' && p<fence) p++;
-  if (*p != ',') return 99999; 
+  if (*p != ',') return -9.e99; 
   p++;
   if (*p == 'W') t = -t;
@@ -570,11 +706,21 @@
 
 double ArcheopsFile::getMuVBolo(int ibolo, int imesure) { // microvolts, filtre avec filtre carre
-  double y = (getRawBolo(ibolo, imesure-1) + getRawBolo(ibolo, imesure))/2.;
+  double y = (getRawBolo(ibolo, imesure-1) - getRawBolo(ibolo, imesure))/2.;
+  if (imesure%2) y=-y;
   block_type_reglage* reglage = lastReglage();
-  return ((1e5*y)/(65536.*gain(reglage->reglage.bolo[ibolo])));
+  return ((1e5*y)/(65536.*gain_ampli(reglage->reglage.bolo[ibolo])));
 }
 
 
 // SST, gyros...
+
+void ArcheopsFile::needSSTProcessMask(int mask) {
+  blockSet->sstHandler.NeedProcess(mask);
+}
+
+
+long ArcheopsFile::getSSTSignal(int idiode, int imesure) {
+  return blockSet->sstHandler.getSignal(imesure, idiode);
+}
 // $CHECK$ TBD
 
