Xbase.DbtWriter (Xbase v0.1.0)

View Source

Module for writing and managing memo content in DBT files.

Provides comprehensive memo writing capabilities including append, update, deletion, block management, and file compaction. Coordinates with DBF record operations to maintain consistency.

Summary

Functions

Analyzes DBT file fragmentation and returns statistics.

Closes a DBT file opened for writing.

Compacts a DBT file by removing unused blocks and rebuilding the file.

Creates a new DBT file with initial header.

Marks a memo block as deleted by writing empty content.

Opens a DBT file for writing operations.

Updates existing memo content at a specific block.

Writes new memo content to the DBT file and returns the block number.

Functions

analyze_fragmentation(dbt_file)

Analyzes DBT file fragmentation and returns statistics.

Parameters

  • dbt_file - DbtFile structure

Returns

  • {:ok, %{total_blocks: n, used_blocks: n, free_blocks: n, fragmentation_ratio: f}}
  • {:error, reason} - Error analyzing file

close_dbt(dbt_file)

Closes a DBT file opened for writing.

Parameters

  • dbt_file - DbtFile structure

Returns

  • :ok - File closed successfully

compact_dbt(dbt_file, output_path)

Compacts a DBT file by removing unused blocks and rebuilding the file.

Parameters

  • dbt_file - DbtFile structure to compact
  • output_path - Path for the compacted file (can be same as input)

Returns

  • {:ok, compacted_dbt_file} - Successfully compacted DBT file
  • {:error, reason} - Error during compaction

create_dbt(file_path, opts \\ [])

Creates a new DBT file with initial header.

Parameters

  • file_path - Path for the new DBT file
  • opts - Options for DBT creation
    • :version - DBT format version (default: :dbase_iii)
    • :block_size - Block size in bytes (default: 512)

Returns

  • {:ok, DbtFile.t()} - Successfully created DBT file
  • {:error, reason} - Error creating file

delete_memo(dbt_file, block_number)

Marks a memo block as deleted by writing empty content.

Parameters

  • dbt_file - DbtFile structure opened for writing
  • block_number - Block number to delete

Returns

  • {:ok, dbt_file} - Successfully deleted memo
  • {:error, reason} - Error deleting memo

open_dbt_for_writing(file_path, version \\ :dbase_iii)

Opens a DBT file for writing operations.

Parameters

  • file_path - Path to the DBT file
  • version - DBT format version (optional, defaults to :dbase_iii)

Returns

  • {:ok, DbtFile.t()} - Successfully opened DBT file for writing
  • {:error, reason} - Error opening file

update_memo(dbt_file, block_number, content)

Updates existing memo content at a specific block.

Parameters

  • dbt_file - DbtFile structure opened for writing
  • block_number - Block number to update
  • content - New memo content (string)

Returns

  • {:ok, dbt_file} - Successfully updated memo
  • {:error, reason} - Error updating memo

write_memo(dbt_file, content)

Writes new memo content to the DBT file and returns the block number.

Parameters

  • dbt_file - DbtFile structure opened for writing
  • content - Memo content to write (string)

Returns

  • {:ok, {block_number, updated_dbt_file}} - Successfully written memo
  • {:error, reason} - Error writing memo