Extend util
This commit is contained in:
parent
9a6ffa69c8
commit
c027d9dc90
3
build.sh
3
build.sh
|
@ -18,7 +18,7 @@ cbuild() {
|
|||
target="$1"
|
||||
targetdest="bin/$(prettyname "$target")"
|
||||
shift
|
||||
(set -x; cc $CFLAGS -o "$targetdest" "$target" "$@")
|
||||
(set -x; cc $CFLAGS -o "$targetdest" "$target" src/util.c "$@")
|
||||
}
|
||||
|
||||
fbuild() {
|
||||
|
@ -31,6 +31,7 @@ bin() {
|
|||
[ ! -d bin ] && mkdir bin
|
||||
|
||||
for f in src/*.c; do
|
||||
[ "$f" = "src/util.c" ] && continue
|
||||
cbuild "$f"
|
||||
done
|
||||
|
||||
|
|
10
src/line.c
10
src/line.c
|
@ -21,14 +21,18 @@
|
|||
|
||||
#include "util.h"
|
||||
|
||||
const char* argv0;
|
||||
const char *argv0;
|
||||
|
||||
void usage() {
|
||||
void
|
||||
usage()
|
||||
{
|
||||
fprintf(stderr, "usage: %s ln\n", argv0);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SET_ARGV0();
|
||||
|
||||
if (argc < 1) usage();
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
void
|
||||
die(const char *fmt, ...)
|
||||
{
|
||||
fmt = str_concat(2, fmt, "\n");
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void
|
||||
edie(const char *fmt, ...)
|
||||
{
|
||||
fmt = str_concat(2, fmt, strerror(errno));
|
||||
va_list ap;
|
||||
die(fmt, ap);
|
||||
}
|
||||
|
||||
void *
|
||||
ecalloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *ptr = calloc(nmemb, size);
|
||||
if (ptr == NULL) edie("calloc: ");
|
||||
return ptr;
|
||||
}
|
||||
|
||||
size_t
|
||||
str_pushc(char *s, char c, size_t s_size, size_t realloc_amount)
|
||||
{
|
||||
/* Realloc s if needed */
|
||||
size_t s_len = strlen(s);
|
||||
if (s_len + 1 > s_size) {
|
||||
s_size += realloc_amount;
|
||||
s = realloc(s, s_size);
|
||||
if (s == NULL) edie("realloc: ");
|
||||
}
|
||||
|
||||
s[s_len] = c;
|
||||
s[s_len + 1] = '\0';
|
||||
return s_size;
|
||||
}
|
||||
|
||||
int
|
||||
str_trimr(char *s, char c, int max_num)
|
||||
{
|
||||
int removed = 0;
|
||||
int i = strlen(s)-1;
|
||||
while (s[i] == c && removed <= max_num) {
|
||||
s[i] = '\0';
|
||||
i--;
|
||||
removed++;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
char *
|
||||
str_concat(int count, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int new_len = 1;
|
||||
|
||||
/* Total length of all strings */
|
||||
va_start(ap, count);
|
||||
for (int i = 0; i < count; i++)
|
||||
new_len += strlen(va_arg(ap, char*));
|
||||
va_end(ap);
|
||||
|
||||
char *new_str = calloc(new_len, sizeof(char));
|
||||
if (new_str == NULL) edie("calloc: ");
|
||||
|
||||
/* Concat strings into newstr */
|
||||
va_start(ap, count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
char *s = va_arg(ap, char*);
|
||||
strcat(new_str, s);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
return new_str;
|
||||
}
|
10
src/util.h
10
src/util.h
|
@ -1,3 +1,13 @@
|
|||
#define SHIFT_ARGS() argv++; argc--;
|
||||
#define SET_ARGV0() argv0 = argv[0]; SHIFT_ARGS();
|
||||
|
||||
void die(const char *fmt, ...);
|
||||
void edie(const char *fmt, ...);
|
||||
|
||||
void *ecalloc(size_t nmemb, size_t size);
|
||||
|
||||
size_t str_pushc(char *s, char c, size_t size, size_t realloc_amount);
|
||||
int str_trimr(char *s, char c, int max_num);
|
||||
char *str_concat(int count, ...);
|
||||
#define STR_EALLOC(s) ecalloc((s), sizeof(char))
|
||||
#define STR_MALLOC(s) calloc((s), sizeof(char))
|
||||
|
|
Loading…
Reference in New Issue