Subject: [Thuban-list] Re: Shapelib: dbfopen.c addition

From: Frank Warmerdam <[email-masked]>
Date: Thu, 04 Dec 2003 09:09:47 -0500

Markus Neteler wrote:
> Frank,
> 
> while trying to get Thuban (from Intevation) running,
> I found that they have added a function to dbfopen.c in shapelib.
> Maybe interesting for you to add into CVS?
> Author should be Bernhard Herzog.
> 
> Function attached.
> 
> Best regards
> 
>  Markus
> 
> 
> 
> ------------------------------------------------------------------------
> 
> /************************************************************************/
> /*                          DBFCommit()	                                */
> /*                                                                      */
> /*      Write any changes made into the file.                           */
> /*                                                                      */
> /************************************************************************/
> int SHPAPI_CALL
> DBFCommit( DBFHandle psDBF )
> 
> {
>     DBFFlushRecord( psDBF );
>     if (fflush( psDBF->fp ) == EOF)
> 	return FALSE;
> 
>     return TRUE;
> }

Markus,

I see there is already a DBFUpdateHeader() function which accomplishing
pretty much the same as DBFCommit(), but also ensures that the header is
in a consistent state (the record count appears there).  I think it would
be best if Thuban used that instead if possible.  I have cc:ed the Thuban
list in case they want to change their code.

DBFUpdateHeader() looks like:

/************************************************************************/
/*                          DBFUpdateHeader()                           */
/************************************************************************/

void SHPAPI_CALL
DBFUpdateHeader( DBFHandle psDBF )

{
     unsigned char		abyFileHeader[32];

     if( psDBF->bNoHeader )
         DBFWriteHeader( psDBF );

     DBFFlushRecord( psDBF );

     fseek( psDBF->fp, 0, 0 );
     fread( abyFileHeader, 32, 1, psDBF->fp );

     abyFileHeader[4] = (unsigned char) (psDBF->nRecords % 256);
     abyFileHeader[5] = (unsigned char) ((psDBF->nRecords/256) % 256);
     abyFileHeader[6] = (unsigned char) ((psDBF->nRecords/(256*256)) % 256);
     abyFileHeader[7] = (unsigned char) ((psDBF->nRecords/(256*256*256)) % 256);

     fseek( psDBF->fp, 0, 0 );
     fwrite( abyFileHeader, 32, 1, psDBF->fp );

     fflush( psDBF->fp );
}

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [email-masked]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

---

From: Bernhard Herzog <[email-masked]>
Date: Thu, 04 Dec 2003 20:16:23 +0100

Frank Warmerdam <[email-masked]> writes:

> in a consistent state (the record count appears there). 

I ran into that (wrong record count when appending records) when I
hacked on the drawshape extension :)

> I think it would be best if Thuban used that instead if possible.

Indeed. This is only available in CVS so far AFAICT. Is it safe to
simply copy that function into our code or would it be better to use
dbfopen.c from CVS?

  Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
Thuban                                  http://thuban.intevation.org/

---

From: Frank Warmerdam <[email-masked]>
Date: Thu, 04 Dec 2003 14:31:56 -0500

> simply copy that function into our code or would it be better to use

Bernhard,

The best would likely to be grab dbfopen.c, shpopen.c, and shapefil.h from
CVS though it should be fine to just copy in that one function and the
corresponding prototype.  I do intend to do a new shapelib release sometime
soon but haven't really gotten around to it.

Best regards,