aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorLibravatar Comrade Martin Miroslavovich Michalec <martin@michalec.dev>2023-11-26 21:34:11 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 02:13:38 +0000
commit252df6c74c7be9b82aa89cdb4972f91c87b06184 (patch)
tree988497c67eb3d8d8789aa9bdea47bafd300e25b2 /src/utils.c
downloadcoursework-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..49306bb
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,48 @@
1#include "utils.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <regex.h>
6#include <string.h>
7
8int
9input (const char *message, const char *regex, const char *template, ...)
10{
11 char *line = NULL;
12 size_t line_size = 0;
13 regex_t compiled;
14 int matches = 0;
15 int c;
16 va_list ap;
17
18 va_start (ap, template);
19 vprintf (message, ap);
20 printf (" (\"%s\"): ", regex);
21 getline (&line, &line_size, stdin);
22 line[strlen (line) - 1] = '\0';
23
24 regcomp (&compiled, regex, REG_EXTENDED | REG_NOSUB);
25 if (regexec (&compiled, line, 0, NULL, 0))
26 goto defer;
27
28 matches = vsscanf (line, template, ap);
29 va_end (ap);
30
31 defer:
32 free (line);
33 return matches;
34}
35
36uint
37log10ui (uint ui)
38{
39 return ((ui >= 1000000000u) ? 9 :
40 (ui >= 100000000u) ? 8 :
41 (ui >= 10000000u) ? 7 :
42 (ui >= 1000000u) ? 6 :
43 (ui >= 100000u) ? 5 :
44 (ui >= 10000u) ? 4 :
45 (ui >= 1000u) ? 3 :
46 (ui >= 100u) ? 2 :
47 (ui >= 10u) ? 1 : 0u);
48}