Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions FastDBF/DbfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ namespace SocialExplorer.IO.FastDBF
/// This class represents a DBF file. You can create new, open, update and save DBF files using this class and supporting classes.
/// Also, this class supports reading/writing from/to an internet forward only type of stream!
/// </summary>
/// <remarks>
/// TODO: add end of file byte '0x1A' !!!
/// We don't relly on that byte at all, and everything works with or without that byte, but it should be there by spec.
/// </remarks>
public class DbfFile
{

Expand All @@ -42,6 +38,10 @@ public class DbfFile
/// </summary>
protected bool _headerWritten = false;

/// <summary>
/// flag that indicates whether a footer update is required
/// </summary>
protected bool _footerUpdateNeeded = false;

/// <summary>
/// Streams to read and write to the DBF file.
Expand Down Expand Up @@ -204,13 +204,19 @@ public void Close()
if (_header.IsDirty)
WriteHeader();


//try to update the footer if the file has been udpated
//------------------------------------------
if (_footerUpdateNeeded)
WriteFooter();

//Empty header...
//--------------------------------
_header = new DbfHeader(encoding);
_headerWritten = false;

//Clear the footer required flag
//------------------------------------------
_footerUpdateNeeded = false;

//reset current record index
//--------------------------------
Expand Down Expand Up @@ -485,6 +491,7 @@ public void Write(DbfRecord orec)
else
Update(orec);

_footerUpdateNeeded = true;
}

public void Write(DbfRecord orec, bool bClearRecordAfterWrite)
Expand Down Expand Up @@ -544,8 +551,8 @@ public void Update(DbfRecord orec)

//write
orec.Write(_dbfFile);


_footerUpdateNeeded = true;
}


Expand All @@ -561,11 +568,13 @@ public bool WriteHeader()
//--------------------------------
if (_dbfFileWriter != null)
{

if (_dbfFileWriter.BaseStream.CanSeek)
{
_dbfFileWriter.Seek(0, SeekOrigin.Begin);
_header.Write(_dbfFileWriter);
_headerWritten = true;
_footerUpdateNeeded = true;
return true;
}
else
Expand All @@ -575,15 +584,52 @@ public bool WriteHeader()
_header.Write(_dbfFileWriter);

_headerWritten = true;

_footerUpdateNeeded = true;
}
}

return false;

}

public bool WriteFooter()
{
// return false if it's not possible to write.
if (_dbfFileWriter == null)
{
return false;
}

//if stream can not seek, then just write it out and that's it.
if (!_dbfFileWriter.BaseStream.CanSeek)
{
_dbfFileWriter.Write((byte)0x1A);
return true;
}

var streamLength = _dbfFileWriter.BaseStream.Length;
var expectedLength = Header.HeaderLength + Header.RecordCount * Header.RecordLength + 1;

// If the length of the stream is the same as the expected length, seek ready to re-write the last byte.
if (streamLength == expectedLength)
{
_dbfFileWriter.BaseStream.Seek(-1, SeekOrigin.End);
}
// If the length of the stream is one less than expected length, seek ready to append the last byte.
else if (streamLength == expectedLength - 1)
{
_dbfFileWriter.BaseStream.Seek(0, SeekOrigin.End);
}
// Otherwise, something isn't right. return false.
else
{
return false;
}

// Write the last byte and return true
_dbfFileWriter.Write((byte)0x1A);
return true;
}

/// <summary>
/// Access DBF header with information on columns. Use this object for faster access to header.
Expand Down