Files
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

163 lines
3.6 KiB
C

/*
* McdeMenuModel.c -- MCDE: the text form of a menu bar (Mcde/GlobalMenu.h)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* C99 and POSIX only, no X: test_menumodel.c tests it (make check).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Mcde/GlobalMenu.h>
static int
ValidKind(int k)
{
return k == MCDE_MENU_CASCADE || k == MCDE_MENU_PUSH ||
k == MCDE_MENU_TOGGLE || k == MCDE_MENU_SEPARATOR ||
k == MCDE_MENU_LABEL;
}
/* append S to BUF at *AT; RAW 0: tabs and newlines as spaces */
static int
Put(char *buf, size_t len, size_t *at, const char *s, int raw)
{
for (; *s != '\0'; s++) {
if (*at + 1 >= len)
return 0;
buf[(*at)++] = (!raw &&
(*s == '\t' || *s == '\n' || *s == '\r')) ? ' ' : *s;
}
buf[*at] = '\0';
return 1;
}
int
McdeMenuWrite(const McdeMenuItem *items, int n, char *buf, size_t len)
{
size_t at = 0;
char head[64], flags[4], mn[2];
int i, f;
if (len == 0)
return -1;
buf[0] = '\0';
for (i = 0; i < n; i++) {
const McdeMenuItem *m = &items[i];
if (!ValidKind(m->kind) || m->depth < 0 ||
m->depth > (i == 0 ? 0 : items[i - 1].depth + 1))
return -1;
f = 0;
if (m->flags & MCDE_MENU_SENSITIVE)
flags[f++] = 's';
if (m->flags & MCDE_MENU_CHECKED)
flags[f++] = 'x';
if (m->flags & MCDE_MENU_RADIO)
flags[f++] = 'r';
if (f == 0)
flags[f++] = '-';
flags[f] = '\0';
mn[0] = (m->mnemonic > ' ' && m->mnemonic != 0x7f) ?
m->mnemonic : '-';
mn[1] = '\0';
snprintf(head, sizeof head, "%d\t%c\t%s\t%s\t", m->depth,
m->kind, flags, mn);
if (!Put(buf, len, &at, head, 1) ||
!Put(buf, len, &at, m->accelerator[0] != '\0' ?
m->accelerator : "-", 0) ||
!Put(buf, len, &at, "\t", 1) ||
!Put(buf, len, &at, m->label, 0) ||
!Put(buf, len, &at, "\n", 1))
return -1;
}
return (int) at;
}
/* the next field of *P up to a tab (or the end of the line), in DST */
static int
Field(const char **p, char *dst, size_t len, int last)
{
const char *s = *p, *e = s;
size_t n;
while (*e != '\0' && *e != '\n' && (last || *e != '\t'))
e++;
if (!last && *e != '\t')
return 0;
n = (size_t) (e - s);
if (n >= len)
n = len - 1; /* a long label is cut, not refused */
memcpy(dst, s, n);
dst[n] = '\0';
*p = (*e == '\t') ? e + 1 : e;
return 1;
}
int
McdeMenuParse(const char *text, McdeMenuItem *items, int max)
{
const char *p = text;
char f[4][16], *end;
int n = 0, i;
long depth;
while (*p != '\0') {
McdeMenuItem *m;
if (*p == '\n') {
p++;
continue;
}
if (n >= max)
return -1;
m = &items[n];
memset(m, 0, sizeof *m);
for (i = 0; i < 4; i++)
if (!Field(&p, f[i], sizeof f[i], 0))
return -1;
if (!Field(&p, m->accelerator, sizeof m->accelerator, 0) ||
!Field(&p, m->label, sizeof m->label, 1))
return -1;
if (*p == '\n')
p++;
depth = strtol(f[0], &end, 10);
if (*end != '\0' || end == f[0] || depth < 0 ||
depth > (n == 0 ? 0 : items[n - 1].depth + 1))
return -1;
m->depth = (int) depth;
if (strlen(f[1]) != 1 || !ValidKind(f[1][0]))
return -1;
m->kind = f[1][0];
for (end = f[2]; *end != '\0'; end++) {
if (*end == 's')
m->flags |= MCDE_MENU_SENSITIVE;
else if (*end == 'x')
m->flags |= MCDE_MENU_CHECKED;
else if (*end == 'r')
m->flags |= MCDE_MENU_RADIO;
else if (*end != '-')
return -1;
}
if (strlen(f[3]) != 1)
return -1;
m->mnemonic = f[3][0] == '-' ? '\0' : f[3][0];
if (strcmp(m->accelerator, "-") == 0)
m->accelerator[0] = '\0';
n++;
}
return n;
}
int
McdeMenuEnd(const McdeMenuItem *items, int n, int i)
{
int j;
for (j = i + 1; j < n && items[j].depth > items[i].depth; j++)
;
return j;
}