Menu

Month: October 2010

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.cmore...

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.… more...