80 lines
1.7 KiB
Lua
80 lines
1.7 KiB
Lua
ls.add_snippets('c', {
|
|
s('trip', {
|
|
-- cond ? then : else statment
|
|
i(1, 'cond'), t(' ? '), i(2, 'then'), t(' : '), i(3, 'else')
|
|
}),
|
|
|
|
s('stdlibs', {
|
|
-- the normal stuff
|
|
t('#include <stdio.h>'),
|
|
t({'', '#include <stdlib.h>'})
|
|
}),
|
|
|
|
s('die', {
|
|
-- message provieder when program is exiting
|
|
t({
|
|
'void die(const char *fmt, ...) {', '',
|
|
'va_list ap;',
|
|
'',
|
|
'va_start(ap, fmt);',
|
|
'vfprintf(stderr, fmt, ap);',
|
|
'va_end(ap);',
|
|
'',
|
|
[[if (fmt[0] && fmt[strlen(fmt)-1] == ':') {]],
|
|
[[ fputc(' ', stderr);]],
|
|
' perror(NULL);',
|
|
'} else',
|
|
[[ fputc('\n', stderr);]],
|
|
' exit(1);',
|
|
'}',
|
|
})
|
|
}),
|
|
|
|
s({
|
|
name = 'get them opts!',
|
|
trig = 'getopt',
|
|
dscr = 'standard argument parser',
|
|
},
|
|
fmta(
|
|
[[
|
|
int c;
|
|
|
|
while ((c = getopt(argc, argv, "<OPTS>")) != -1) {
|
|
switch (c) {
|
|
case '<OPT1>': <OPT1RUN> break;
|
|
default: <DEFRUN> break;
|
|
}
|
|
}
|
|
]],
|
|
{
|
|
OPTS = i(1, 'h'),
|
|
OPT1 = i(2, 'h'),
|
|
OPT1RUN = i(3, 'printf("help text\\n");'),
|
|
DEFRUN = i(4, 'printf("run %s -h for help\\n", argv[0]); return 1;'),
|
|
}
|
|
)
|
|
),
|
|
s({
|
|
name = 'Variadic function parser',
|
|
trig = 'infinite vars',
|
|
dscr = 'Parse an infinite number of arguments passed to a function',
|
|
},
|
|
{
|
|
t({
|
|
"/*",
|
|
" * NOTE: the function must have a int before the ... argument",
|
|
" * and you need to include <stdarg.h> for this to work",
|
|
" */",
|
|
"va_list ptr;",
|
|
"va_start(ptr, ", i(1, "n"),
|
|
");",
|
|
"for (int i = 0; i < ", ri(1),
|
|
"; i++) {",
|
|
i(2),
|
|
"}",
|
|
"va_end(ptr);",
|
|
}),
|
|
}
|
|
)
|
|
})
|