Mercurial > molko
annotate libcore/core/util.c @ 226:dd7c8d4321a3
misc: miscellaneous cleanups
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 19 Nov 2020 10:17:04 +0100 |
parents | d6f217a5e4b1 |
children | befa2e855d3b |
rev | line source |
---|---|
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * util.c -- utilities |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
198
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
19 #include <sys/stat.h> |
185
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
20 #include <assert.h> |
198
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
21 #include <errno.h> |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
22 #include <fcntl.h> |
185
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
23 #include <stdlib.h> |
198
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
24 #include <unistd.h> |
185
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
25 |
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 #include <SDL.h> |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 |
198
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
28 #include "alloc.h" |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
29 #include "error.h" |
39
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
30 #include "util.h" |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
32 void |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
33 delay(unsigned int ms) |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
34 { |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
35 SDL_Delay(ms); |
9d1421c09dfb
core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
36 } |
185
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
37 |
198
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
38 char * |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
39 readall(const char *path) |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
40 { |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
41 int fd; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
42 struct stat st; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
43 char *str = NULL; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
44 ssize_t nr; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
45 |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
46 if ((fd = open(path, O_RDONLY)) < 0 || fstat(fd, &st) < 0) |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
47 goto io_error; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
48 if (!(str = alloc_zero(1, st.st_size + 1))) |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
49 goto alloc_error; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
50 if ((nr = read(fd, str, st.st_size)) != st.st_size) |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
51 goto io_error; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
52 |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
53 return str; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
54 |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
55 io_error: |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
56 errorf("%s", strerror(errno)); |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
57 |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
58 alloc_error: |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
59 close(fd); |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
60 free(str); |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
61 |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
62 return NULL; |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
63 } |
d6f217a5e4b1
molko-js: add support for modules, closes #2514 @1h
David Demelier <markand@malikania.fr>
parents:
185
diff
changeset
|
64 |
185
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
65 unsigned int |
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
66 nrand(unsigned int lower, unsigned int upper) |
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
67 { |
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
68 assert(upper <= RAND_MAX); |
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
69 |
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
70 return (rand() % (upper - lower + 1)) + lower; |
7103d6574062
core: add nrand function
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
71 } |