Index: trunk/SophyaLib/BaseTools/ppfbinstream.cc
===================================================================
--- trunk/SophyaLib/BaseTools/ppfbinstream.cc	(revision 2475)
+++ trunk/SophyaLib/BaseTools/ppfbinstream.cc	(revision 2476)
@@ -50,9 +50,9 @@
 
 
-PPFBinaryInputStream::PPFBinaryInputStream()
-{
-  s = NULL;
-  version = 0;
-  bigEndian = true;
+PPFBinaryInputStream::PPFBinaryInputStream(RawInOutStream * is, bool ad, bool scan)
+{
+  s = is;
+  _ads = ad;
+  Init(scan);
 }
 
@@ -64,5 +64,16 @@
 {
   s = new RawInFileStream(flnm.c_str());
-
+  _ads = true;
+  Init(scan);
+}
+
+PPFBinaryInputStream::~PPFBinaryInputStream()
+{
+  if (_ads && (s!=NULL) ) delete s;
+}
+
+void
+PPFBinaryInputStream::Init(bool scan)
+{
   // Read and check header
 
@@ -75,5 +86,5 @@
   version = atoi(rbuf+25);
   if (version < 2) {
-    cerr << "PPFBinaryInputStream::PPFBinaryInputStream(" << flnm << ") Version(=" << version 
+    cerr << "PPFBinaryInputStream::PPFBinaryInputStream(" << FileName() << ") Version(=" << version 
 	 << ") < 2 not supported !" << endl;
     throw FileFormatExc("PPFBinaryInputStream::PPFBinaryInputStream() - Unsupported (Old) Version");
@@ -104,15 +115,13 @@
 
   creationdate = mktime(&tm);
-  filename = flnm;  // keep the filename
   seqread = true;   // To flag non sequential reads
-  if (scan && s->isSeekable()) Scan();
-}
-
-
-
-PPFBinaryInputStream::~PPFBinaryInputStream()
-{
-  if (s) delete s;
-}
+  if (scan && s->isSeekable()) { 
+    if (Version() >= 3) ReadNameTagTable();
+    else ReadNameTagTableV2();
+  }
+}
+
+
+
 
 
@@ -126,8 +135,6 @@
 
 void
-PPFBinaryInputStream::Scan()
-{
-  // On cherche la liste des tags, a la fin du fichier
-
+PPFBinaryInputStream::ReadNameTagTable()
+{
   unsigned char ppstype;
   int_8 debut;
@@ -138,5 +145,46 @@
   GetTypeTag(ppstype);
   if (ppstype != PPS_EOF)
-    throw FileFormatExc("PPFBinaryInputStream::Scan   corrupted file, no eof entry at end of file");
+    throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTable()  Corrupted file, no EOF tag at end of file");
+
+  // Lecture position NameTagTable
+  int_8 pos;
+  GetRawI8(pos);
+  if (pos < 0) {  // no tags
+    s->seekg(debut);
+    return;
+  }
+  
+  // On se positionne au debut du NameTagTable
+  s->seekg(pos);
+  GetTypeTag(ppstype);
+  if (ppstype != PPS_NAMETAG_TABLE)
+    throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTable()  Corrupted file PPS_NAMETAG_TABLE not found");
+  uint_8 ttsz,it;
+  GetU8(ttsz);
+  for(it=0; it<ttsz; it++) {
+    int_8 tpos;
+    string tname;
+    GetI8(tpos);
+    GetStr(tname);
+    tags[tname] = tpos;
+  }
+  // On revient au debut du float, juste apres l'entete
+  s->seekg(debut);
+}
+
+void
+PPFBinaryInputStream::ReadNameTagTableV2()
+{
+  // On cherche la liste des tags, a la fin du fichier
+
+  unsigned char ppstype;
+  int_8 debut;
+  debut = s->tellg();
+
+  s->seekg(-(sizeof(int_8)+1), ios::end);
+
+  GetTypeTag(ppstype);
+  if (ppstype != PPS_EOF)
+    throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, no eof entry at end of file");
 
   int_8 pos;
@@ -154,5 +202,5 @@
     
     if (ppstype != PPS_NAMETAG_TABLE)
-      throw FileFormatExc("PPFBinaryInputStream::Scan   corrupted file, bad tag entry");
+      throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, bad tag entry");
 
     GetRawI8(pos);
@@ -160,5 +208,5 @@
     GetRawI4(len);
     if (len > MAXTAGLEN_V2) 
-      throw FileFormatExc("PPFBinaryInputStream::Scan   corrupted file, tag name too long");
+      throw FileFormatExc("PPFBinaryInputStream::ReadNameTagTableV2() Corrupted file, tag name too long");
     GetRawBytes(buffer, len);
     buffer[len] = '\0';
@@ -242,8 +290,22 @@
 
 bool
-PPFBinaryInputStream::SkipItem()
+PPFBinaryInputStream::SkipNextItem()
 {
   // A FAIRE NOV 2003 - REZA
   return true;
+}
+
+char
+PPFBinaryInputStream::NextItemTag(short datasz, size_t sz)
+{
+  datasz = 0;
+  sz = 0;
+  return 0;
+}
+
+void 
+PPFBinaryInputStream::SkipItem(bool fgrdt, char itag)
+{
+  return;
 }
 
@@ -888,11 +950,27 @@
 //--
 
-PPFBinaryOutputStream::PPFBinaryOutputStream()
-{
-  s = NULL;
-  version = 0;
+PPFBinaryOutputStream::PPFBinaryOutputStream(RawInOutStream* os, bool ad, int endianness)
+{
+  s = os;
+  _ads = ad;
+  Init(endianness);
 }
 
 PPFBinaryOutputStream::PPFBinaryOutputStream(string const& flnm, int endianness)
+{
+  // Output stream creation
+  s = new RawOutFileStream(flnm.c_str());
+  _ads = true;
+  Init(endianness);
+}
+
+PPFBinaryOutputStream::~PPFBinaryOutputStream()
+{
+  WriteNameTagTable();
+  if (_ads && (s != NULL)) delete s;   // Close the stream
+}
+
+void
+PPFBinaryOutputStream::Init(int endianness)
 {
   if (endianness == -1)
@@ -901,6 +979,4 @@
     bigEndian = endianness;
 
-  // Output stream creation
-  s = new RawOutFileStream(flnm.c_str());
   version = 3;
   // Header
@@ -917,8 +993,31 @@
   datestring[32] = '\0'; 
   PutRawBytes(datestring, 32);
-  filename = flnm;
-}
-
-PPFBinaryOutputStream::~PPFBinaryOutputStream()
+}
+
+void
+PPFBinaryOutputStream::WriteNameTagTable()
+{  
+  if (tags.size() == 0) {
+    PutRawUByte(PPS_EOF);
+    PutRawI8(-1);
+  } 
+  else {
+    int_8 tagPos;
+    tagPos = s->tellp();
+    PutRawUByte(PPS_NAMETAG_TABLE);                       // This is a tag
+    PutU8((uint_8)tags.size());    // Number of tags
+    for (map<string,int_8>::iterator i = tags.begin(); i != tags.end(); i++) {
+      int_8 pos = (*i).second;
+      PutI8(pos);
+      PutStr((*i).first);
+    }
+    PutRawUByte(PPS_EOF);
+    PutRawI8(tagPos);
+  }
+  
+}
+
+void
+PPFBinaryOutputStream::WriteNameTagTableV2()
 {
   if (tags.size() == 0) {
@@ -940,6 +1039,6 @@
   }
 
-  delete s;   // Close the stream
-}
+}
+
 
 int_8
