*** hfaopen_old.cpp	Wed Apr 11 01:16:18 2007
--- hfaopen.cpp	Wed May 16 08:19:13 2007
***************
*** 2210,2230 ****
          if( columnDataPtr == 0 )
              continue;
! 
  /* -------------------------------------------------------------------- */
! /*      read up to 500 bytes from the indicated location.               */
  /* -------------------------------------------------------------------- */
!         char szMDValue[501];
!         int  nMDBytes = sizeof(szMDValue)-1;
! 
!         if( VSIFSeekL( hHFA->fp, columnDataPtr, SEEK_SET ) != 0 )
!             continue;
! 
!         nMDBytes = VSIFReadL( szMDValue, 1, nMDBytes, hHFA->fp );
!         if( nMDBytes == 0 )
!             continue;
! 
!         szMDValue[nMDBytes] = '\0';
! 
!         papszMD = CSLSetNameValue( papszMD, poColumn->GetName(), szMDValue );
      }
  
--- 2210,2245 ----
          if( columnDataPtr == 0 )
              continue;
!             
!         int nMaxNumChars = poColumn->GetIntField( "maxNumChars" );
  /* -------------------------------------------------------------------- */
! /*      read up to nMaxNumChars bytes from the indicated location.      */
! /*      allocate required space temporarily                             */
! /*      nMaxNumChars should have been set by GDAL orginally so we can   */
! /*      trust it.                                                       */
  /* -------------------------------------------------------------------- */
!         if( nMaxNumChars == 0 )
!         {
!            papszMD = CSLSetNameValue( papszMD, poColumn->GetName(), "" );
!         }
!         else
!         {
!           char *pszMDValue = (char*) CPLMalloc(nMaxNumChars);
! 
!           if( VSIFSeekL( hHFA->fp, columnDataPtr, SEEK_SET ) != 0 )
!               continue;
! 
!           int nMDBytes = VSIFReadL( pszMDValue, 1, nMaxNumChars, hHFA->fp );
!           if( nMDBytes == 0 )
!           {
!               CPLFree( pszMDValue );
!               continue;
!           }
! 
!           pszMDValue[nMaxNumChars-1] = '\0';
! 
!           papszMD = CSLSetNameValue( papszMD, poColumn->GetName(), pszMDValue );
!         
!           CPLFree( pszMDValue );
!        }
      }
  
***************
*** 2259,2268 ****
  /* -------------------------------------------------------------------- */
  /*      Create the Descriptor table.                                    */
  /* -------------------------------------------------------------------- */
      HFAEntry	*poEdsc_Table;
  
!     poEdsc_Table = new HFAEntry( hHFA, "GDAL_MetaData", "Edsc_Table",
!                                  poNode );
  
      poEdsc_Table->SetIntField( "numrows", 1 );
  
--- 2274,2288 ----
  /* -------------------------------------------------------------------- */
  /*      Create the Descriptor table.                                    */
+ /*      Check we have no table with this name already                   */
  /* -------------------------------------------------------------------- */
      HFAEntry	*poEdsc_Table;
  
!     for( poEdsc_Table = poNode->GetChild(); poEdsc_Table != NULL && !EQUAL(poEdsc_Table->GetName(),"GDAL_MetaData");
!          poEdsc_Table = poEdsc_Table->GetNext() ) {}
  
+     if( poEdsc_Table == NULL || !EQUAL(poEdsc_Table->GetType(),"Edsc_Table") )
+         poEdsc_Table = new HFAEntry( hHFA, "GDAL_MetaData", "Edsc_Table",
+                                  poNode );
+         
      poEdsc_Table->SetIntField( "numrows", 1 );
  
***************
*** 2270,2278 ****
  /*      Create the Binning function node.  I am not sure that we        */
  /*      really need this though.                                        */
  /* -------------------------------------------------------------------- */
      HFAEntry       *poEdsc_BinFunction;
  
!     poEdsc_BinFunction =
!         new HFAEntry( hHFA, "#Bin_Function#", "Edsc_BinFunction",
                        poEdsc_Table );
  
--- 2290,2302 ----
  /*      Create the Binning function node.  I am not sure that we        */
  /*      really need this though.                                        */
+ /*      Check it doesn't exist already                                  */
  /* -------------------------------------------------------------------- */
      HFAEntry       *poEdsc_BinFunction;
  
!     for( poEdsc_BinFunction = poEdsc_Table->GetChild(); poEdsc_BinFunction != NULL && !EQUAL(poEdsc_BinFunction->GetName(),"#Bin_Function#");
!            poEdsc_BinFunction = poEdsc_BinFunction->GetNext() ) {}
!            
!     if( poEdsc_BinFunction == NULL || !EQUAL(poEdsc_BinFunction->GetType(),"Edsc_BinFunction") )
!       poEdsc_BinFunction = new HFAEntry( hHFA, "#Bin_Function#", "Edsc_BinFunction",
                        poEdsc_Table );
  
***************
*** 2300,2305 ****
  /* -------------------------------------------------------------------- */
  /*      Create the Edsc_Column.                                         */
  /* -------------------------------------------------------------------- */
!         poEdsc_Column = new HFAEntry( hHFA, pszKey, "Edsc_Column",
                                        poEdsc_Table );
          poEdsc_Column->SetIntField( "numRows", 1 );
--- 2324,2334 ----
  /* -------------------------------------------------------------------- */
  /*      Create the Edsc_Column.                                         */
+ /*      Check it doesn't exist already                                  */
  /* -------------------------------------------------------------------- */
!         for( poEdsc_Column = poEdsc_Table->GetChild(); poEdsc_Column != NULL && !EQUAL(poEdsc_Column->GetName(),pszKey);
!            poEdsc_Column = poEdsc_Column->GetNext() ) {}
!          
!         if( poEdsc_Column == NULL || !EQUAL(poEdsc_Column->GetType(),"Edsc_Column") )
!             poEdsc_Column = new HFAEntry( hHFA, pszKey, "Edsc_Column",
                                        poEdsc_Table );
          poEdsc_Column->SetIntField( "numRows", 1 );

