Apply K&R brace style and unwrap function signatures

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Scott Duensing 2026-03-04 19:22:02 -06:00
parent 17394e4f5d
commit e25428bb8b
3 changed files with 37 additions and 82 deletions

View file

@ -123,8 +123,7 @@ static void usage(const char *progName);
// Low-level DFM readers // Low-level DFM readers
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static int32_t readByte(const uint8_t *data, int32_t size, int32_t *pos) static int32_t readByte(const uint8_t *data, int32_t size, int32_t *pos) {
{
if (*pos >= size) { if (*pos >= size) {
fprintf(stderr, "Error: unexpected end of file at offset %d\n", *pos); fprintf(stderr, "Error: unexpected end of file at offset %d\n", *pos);
exit(1); exit(1);
@ -133,8 +132,7 @@ static int32_t readByte(const uint8_t *data, int32_t size, int32_t *pos)
} }
static int32_t readInt16LE(const uint8_t *data, int32_t size, int32_t *pos) static int32_t readInt16LE(const uint8_t *data, int32_t size, int32_t *pos) {
{
if (*pos + 2 > size) { if (*pos + 2 > size) {
fprintf(stderr, "Error: unexpected end of file at offset %d\n", *pos); fprintf(stderr, "Error: unexpected end of file at offset %d\n", *pos);
exit(1); exit(1);
@ -145,8 +143,7 @@ static int32_t readInt16LE(const uint8_t *data, int32_t size, int32_t *pos)
} }
static int32_t readInt32LE(const uint8_t *data, int32_t size, int32_t *pos) static int32_t readInt32LE(const uint8_t *data, int32_t size, int32_t *pos) {
{
if (*pos + 4 > size) { if (*pos + 4 > size) {
fprintf(stderr, "Error: unexpected end of file at offset %d\n", *pos); fprintf(stderr, "Error: unexpected end of file at offset %d\n", *pos);
exit(1); exit(1);
@ -158,8 +155,7 @@ static int32_t readInt32LE(const uint8_t *data, int32_t size, int32_t *pos)
} }
static bool readStr(const uint8_t *data, int32_t size, int32_t *pos, char *buf, int32_t bufSize) static bool readStr(const uint8_t *data, int32_t size, int32_t *pos, char *buf, int32_t bufSize) {
{
int32_t len = readByte(data, size, pos); int32_t len = readByte(data, size, pos);
if (*pos + len > size) { if (*pos + len > size) {
fprintf(stderr, "Error: string overflows file at offset %d\n", *pos); fprintf(stderr, "Error: string overflows file at offset %d\n", *pos);
@ -173,8 +169,7 @@ static bool readStr(const uint8_t *data, int32_t size, int32_t *pos, char *buf,
} }
static int32_t readIntValue(const uint8_t *data, int32_t size, int32_t *pos, uint8_t tag) static int32_t readIntValue(const uint8_t *data, int32_t size, int32_t *pos, uint8_t tag) {
{
switch (tag) { switch (tag) {
case vaInt8: case vaInt8:
return (int8_t)readByte(data, size, pos); return (int8_t)readByte(data, size, pos);
@ -192,8 +187,7 @@ static int32_t readIntValue(const uint8_t *data, int32_t size, int32_t *pos, uin
// Skip unknown property values // Skip unknown property values
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void skipValue(const uint8_t *data, int32_t size, int32_t *pos, uint8_t tag) static void skipValue(const uint8_t *data, int32_t size, int32_t *pos, uint8_t tag) {
{
int32_t len; int32_t len;
char buf[256]; char buf[256];
@ -272,8 +266,7 @@ static void skipValue(const uint8_t *data, int32_t size, int32_t *pos, uint8_t t
// Case-insensitive string compare // Case-insensitive string compare
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static int stricmp_local(const char *a, const char *b) static int stricmp_local(const char *a, const char *b) {
{
while (*a && *b) { while (*a && *b) {
int ca = tolower((unsigned char)*a); int ca = tolower((unsigned char)*a);
int cb = tolower((unsigned char)*b); int cb = tolower((unsigned char)*b);
@ -291,8 +284,7 @@ static int stricmp_local(const char *a, const char *b)
// Map Delphi class name to control type // Map Delphi class name to control type
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static CtrlTypeE mapClassName(const char *className) static CtrlTypeE mapClassName(const char *className) {
{
if (stricmp_local(className, "TLabel") == 0) { if (stricmp_local(className, "TLabel") == 0) {
return ctLabel; return ctLabel;
} }
@ -322,8 +314,7 @@ static CtrlTypeE mapClassName(const char *className)
// Init helpers // Init helpers
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void initCtrl(DfmCtrlT *ctrl) static void initCtrl(DfmCtrlT *ctrl) {
{
memset(ctrl, 0, sizeof(DfmCtrlT)); memset(ctrl, 0, sizeof(DfmCtrlT));
ctrl->enabled = 1; ctrl->enabled = 1;
ctrl->visible = 1; ctrl->visible = 1;
@ -332,8 +323,7 @@ static void initCtrl(DfmCtrlT *ctrl)
} }
static void initForm(DfmFormT *form) static void initForm(DfmFormT *form) {
{
memset(form, 0, sizeof(DfmFormT)); memset(form, 0, sizeof(DfmFormT));
} }
@ -342,9 +332,7 @@ static void initForm(DfmFormT *form)
// Parse properties from DFM binary // Parse properties from DFM binary
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void parseProperties(const uint8_t *data, int32_t size, int32_t *pos, static void parseProperties(const uint8_t *data, int32_t size, int32_t *pos, DfmFormT *form, DfmCtrlT *ctrl, bool isForm) {
DfmFormT *form, DfmCtrlT *ctrl, bool isForm)
{
char propName[64]; char propName[64];
char strBuf[4096]; char strBuf[4096];
@ -617,9 +605,7 @@ static void parseProperties(const uint8_t *data, int32_t size, int32_t *pos,
// Parse a component (form or child control) recursively // Parse a component (form or child control) recursively
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static bool parseComponent(const uint8_t *data, int32_t size, int32_t *pos, static bool parseComponent(const uint8_t *data, int32_t size, int32_t *pos, DfmFormT *form, bool isRoot) {
DfmFormT *form, bool isRoot)
{
// Check for flags byte (Delphi 1.0 rarely uses this but handle it) // Check for flags byte (Delphi 1.0 rarely uses this but handle it)
uint8_t peek = data[*pos]; uint8_t peek = data[*pos];
if ((peek & 0xF0) == 0xF0) { if ((peek & 0xF0) == 0xF0) {
@ -706,8 +692,7 @@ static bool parseComponent(const uint8_t *data, int32_t size, int32_t *pos,
// Escape a string for protocol output // Escape a string for protocol output
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void escapeStr(const char *src, char *dst, int32_t dstSize) static void escapeStr(const char *src, char *dst, int32_t dstSize) {
{
int32_t di = 0; int32_t di = 0;
for (int32_t si = 0; src[si] != '\0' && di < dstSize - 2; si++) { for (int32_t si = 0; src[si] != '\0' && di < dstSize - 2; si++) {
@ -745,8 +730,7 @@ static void escapeStr(const char *src, char *dst, int32_t dstSize)
// Emit protocol commands for a single control // Emit protocol commands for a single control
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void emitCtrl(FILE *out, int32_t formId, int32_t ctrlId, DfmCtrlT *ctrl) static void emitCtrl(FILE *out, int32_t formId, int32_t ctrlId, DfmCtrlT *ctrl) {
{
static const char *typeNames[] = { static const char *typeNames[] = {
"Unknown", "Label", "Edit", "Button", "Unknown", "Label", "Edit", "Button",
"CheckBox", "ListBox", "ComboBox", "Memo" "CheckBox", "ListBox", "ComboBox", "Memo"
@ -842,8 +826,7 @@ static void emitCtrl(FILE *out, int32_t formId, int32_t ctrlId, DfmCtrlT *ctrl)
// Emit the complete form // Emit the complete form
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void emitForm(FILE *out, int32_t formId, DfmFormT *form) static void emitForm(FILE *out, int32_t formId, DfmFormT *form) {
{
char escaped[512]; char escaped[512];
escapeStr(form->caption, escaped, sizeof(escaped)); escapeStr(form->caption, escaped, sizeof(escaped));
@ -862,8 +845,7 @@ static void emitForm(FILE *out, int32_t formId, DfmFormT *form)
// Usage // Usage
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void usage(const char *progName) static void usage(const char *progName) {
{
fprintf(stderr, "Usage: %s <input.dfm> [output.form]\n", progName); fprintf(stderr, "Usage: %s <input.dfm> [output.form]\n", progName);
exit(1); exit(1);
} }
@ -873,8 +855,7 @@ static void usage(const char *progName)
// Main // Main
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{
const char *inputPath = NULL; const char *inputPath = NULL;
const char *outputPath = NULL; const char *outputPath = NULL;

View file

@ -42,8 +42,7 @@ static bool skipSpaces(const char **p);
// Internal helpers // Internal helpers
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static bool parseToken(const char **p, char *buf, int32_t bufSize) static bool parseToken(const char **p, char *buf, int32_t bufSize) {
{
skipSpaces(p); skipSpaces(p);
if (**p == '\0') { if (**p == '\0') {
return false; return false;
@ -94,8 +93,7 @@ static bool parseToken(const char **p, char *buf, int32_t bufSize)
} }
static void rewriteFormId(char *line, int32_t lineSize, int32_t formId) static void rewriteFormId(char *line, int32_t lineSize, int32_t formId) {
{
// Protocol lines have the form: COMMAND <formId> <rest...> // Protocol lines have the form: COMMAND <formId> <rest...>
// Skip command prefix (non-space chars) // Skip command prefix (non-space chars)
char *p = line; char *p = line;
@ -138,8 +136,7 @@ static void rewriteFormId(char *line, int32_t lineSize, int32_t formId)
} }
static void sendCommand(FormServerT *server, const char *fmt, ...) static void sendCommand(FormServerT *server, const char *fmt, ...) {
{
char buf[MAX_MSG_LEN]; char buf[MAX_MSG_LEN];
va_list args; va_list args;
@ -151,8 +148,7 @@ static void sendCommand(FormServerT *server, const char *fmt, ...)
} }
static bool skipSpaces(const char **p) static bool skipSpaces(const char **p) {
{
while (**p == ' ' || **p == '\t') { while (**p == ' ' || **p == '\t') {
(*p)++; (*p)++;
} }
@ -164,15 +160,12 @@ static bool skipSpaces(const char **p)
// Public API // Public API
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
void formServerBindEvent(FormServerT *server, int32_t formId, int32_t ctrlId, void formServerBindEvent(FormServerT *server, int32_t formId, int32_t ctrlId, const char *eventName) {
const char *eventName)
{
sendCommand(server, "EVENT.BIND %d %d %s", formId, ctrlId, eventName); sendCommand(server, "EVENT.BIND %d %d %s", formId, ctrlId, eventName);
} }
FormServerT *formServerCreate(FormTransportT *transport) FormServerT *formServerCreate(FormTransportT *transport) {
{
FormServerT *server = (FormServerT *)calloc(1, sizeof(FormServerT)); FormServerT *server = (FormServerT *)calloc(1, sizeof(FormServerT));
if (server == NULL) { if (server == NULL) {
return NULL; return NULL;
@ -183,8 +176,7 @@ FormServerT *formServerCreate(FormTransportT *transport)
} }
void formServerDestroy(FormServerT *server) void formServerDestroy(FormServerT *server) {
{
if (server == NULL) { if (server == NULL) {
return; return;
} }
@ -192,20 +184,17 @@ void formServerDestroy(FormServerT *server)
} }
void formServerDestroyForm(FormServerT *server, int32_t formId) void formServerDestroyForm(FormServerT *server, int32_t formId) {
{
sendCommand(server, "FORM.DESTROY %d", formId); sendCommand(server, "FORM.DESTROY %d", formId);
} }
void formServerHideForm(FormServerT *server, int32_t formId) void formServerHideForm(FormServerT *server, int32_t formId) {
{
sendCommand(server, "FORM.HIDE %d", formId); sendCommand(server, "FORM.HIDE %d", formId);
} }
bool formServerPollEvent(FormServerT *server) bool formServerPollEvent(FormServerT *server) {
{
int bytesRead = server->transport->readMessage( int bytesRead = server->transport->readMessage(
server->msgBuf, MAX_MSG_LEN - 1, server->transport->ctx); server->msgBuf, MAX_MSG_LEN - 1, server->transport->ctx);
@ -257,8 +246,7 @@ bool formServerPollEvent(FormServerT *server)
} }
int32_t formServerSendForm(FormServerT *server, const char *path) int32_t formServerSendForm(FormServerT *server, const char *path) {
{
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "formsrv: cannot open '%s': %s\n", path, strerror(errno)); fprintf(stderr, "formsrv: cannot open '%s': %s\n", path, strerror(errno));
@ -289,29 +277,22 @@ int32_t formServerSendForm(FormServerT *server, const char *path)
} }
void formServerSetEventCallback(FormServerT *server, EventCallbackT cb, void formServerSetEventCallback(FormServerT *server, EventCallbackT cb, void *userData) {
void *userData)
{
server->eventCallback = cb; server->eventCallback = cb;
server->eventUserData = userData; server->eventUserData = userData;
} }
void formServerSetProp(FormServerT *server, int32_t formId, int32_t ctrlId, void formServerSetProp(FormServerT *server, int32_t formId, int32_t ctrlId, const char *prop, const char *value) {
const char *prop, const char *value)
{
sendCommand(server, "CTRL.SET %d %d %s=%s", formId, ctrlId, prop, value); sendCommand(server, "CTRL.SET %d %d %s=%s", formId, ctrlId, prop, value);
} }
void formServerShowForm(FormServerT *server, int32_t formId) void formServerShowForm(FormServerT *server, int32_t formId) {
{
sendCommand(server, "FORM.SHOW %d", formId); sendCommand(server, "FORM.SHOW %d", formId);
} }
void formServerUnbindEvent(FormServerT *server, int32_t formId, int32_t ctrlId, void formServerUnbindEvent(FormServerT *server, int32_t formId, int32_t ctrlId, const char *eventName) {
const char *eventName)
{
sendCommand(server, "EVENT.UNBIND %d %d %s", formId, ctrlId, eventName); sendCommand(server, "EVENT.UNBIND %d %d %s", formId, ctrlId, eventName);
} }

View file

@ -31,9 +31,7 @@ typedef struct {
// Event callback // Event callback
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
typedef void (*EventCallbackT)(int32_t formId, int32_t ctrlId, typedef void (*EventCallbackT)(int32_t formId, int32_t ctrlId, const char *eventName, const char *data, void *userData);
const char *eventName, const char *data,
void *userData);
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Server handle (opaque) // Server handle (opaque)
@ -58,21 +56,16 @@ void formServerHideForm(FormServerT *server, int32_t formId);
void formServerDestroyForm(FormServerT *server, int32_t formId); void formServerDestroyForm(FormServerT *server, int32_t formId);
// Send a CTRL.SET command to update a property on a control. // Send a CTRL.SET command to update a property on a control.
void formServerSetProp(FormServerT *server, int32_t formId, void formServerSetProp(FormServerT *server, int32_t formId, int32_t ctrlId, const char *prop, const char *value);
int32_t ctrlId, const char *prop,
const char *value);
// Send an EVENT.BIND command. // Send an EVENT.BIND command.
void formServerBindEvent(FormServerT *server, int32_t formId, void formServerBindEvent(FormServerT *server, int32_t formId, int32_t ctrlId, const char *eventName);
int32_t ctrlId, const char *eventName);
// Send an EVENT.UNBIND command. // Send an EVENT.UNBIND command.
void formServerUnbindEvent(FormServerT *server, int32_t formId, void formServerUnbindEvent(FormServerT *server, int32_t formId, int32_t ctrlId, const char *eventName);
int32_t ctrlId, const char *eventName);
// Set the callback for incoming events. // Set the callback for incoming events.
void formServerSetEventCallback(FormServerT *server, void formServerSetEventCallback(FormServerT *server, EventCallbackT cb, void *userData);
EventCallbackT cb, void *userData);
// Poll for one incoming event. Returns true if an event was processed. // Poll for one incoming event. Returns true if an event was processed.
bool formServerPollEvent(FormServerT *server); bool formServerPollEvent(FormServerT *server);