• Category Archives Code
  • Buffered file access – Bfile

    There may already be a method or library for handling files in memory but I wrote this to easily add and remove bytes without constantly reading or writing to a file. Bfile reads the whole file into memory before working on it by using the bfread, bfwrite, bfclip and bfinsert functions.

    bfopen() opens a file and sets the initial values in the Bfile struct.
    bfclose() flushes the buffer to the file and closes the file.
    bfflush() flushes the buffer to the file.
    bfread() reads the entire file into a Bfile buffer.
    bfwrite() writes from memory to the Bfile buffer, but not to the file.
    bfclip() removes data from the Bfile and adjusts the file size.
    bfinsert() inserts data from memory into the Bfile and adjusts the file size.

    Writing to the file needs to end with a bfflush and this is done automatically when the file is closed with bfclose.


    Source: bfile.hbfile.c

  • Project: hexed

    Well the current project is hexed, a command line HEXadecimal file EDitor. The first update is a simple test for cmdargs, a command line argument parser. Cmdargs is released as public domain software so you can use it in other projects without restrictions. Cmdargs can handle both long and short arguments with options. For long arguments cmdargs can accept either ‘‐’ or ‘‐‐’ as the argument switch. Any short arguments that don’t require an option can be set in the same switch (-a -b -c  or -abc). Options can be set either by ‘:’, ‘=’ or with whitespace as in (-e:10 -b=100 -f filename). The accepted arguments are passed as a single struct array called Arglist with longer args listed before shorter ones in alphabetical order.

    Arglist arglist[] = {
    {"bar", 0},       // set the bar arg
    {"b", 0},         // set the b arg
    {"foobar", 0},    // set the foobar arg
    {"foo", 0},       // set the foo arg
    {"f", 0},         // set the f arg
    {NULL, 0}         // must be the last entry

    This struct and argv from main() are sent to parsecmdargs() in a loop until the end of the arguments are reached. parsecmdargs() returns a Cmdargs struct with the argument string, an options string, the arg ID, and some flags about the current arg. The source code is documented more in the comments for cmdargs.c

    I also spent the last few days learning a bit about make and svn. Between them I’ve created a simple way to get the revision number for exported versions or on machines that don’t have svnversion installed. It uses bash to update a version make file when the working copy and the file $Rev$ revision numbers are different. This means that the version makefile is updated on every commit but it’s only for a single comment with the updated time and date at the top of the file.


    Source: cmdargs.hcmdargs.c