annotate libcore/core/util.c @ 240:d7e5e02f70a1

doc: misc
author David Demelier <markand@malikania.fr>
date Fri, 27 Nov 2020 21:34:07 +0100
parents 4896bb07a8db
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
185
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
19 #include <assert.h>
236
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
20 #include <limits.h>
185
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
21 #include <stdlib.h>
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
22
39
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include <SDL.h>
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
24
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include "util.h"
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
26
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 void
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 delay(unsigned int ms)
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 {
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 SDL_Delay(ms);
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 }
185
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
32
236
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
33 const char *
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
34 pprintf(const char *fmt, ...)
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
35 {
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
36 static char path[PATH_MAX];
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
37 va_list ap;
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
38
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
39 va_start(ap, fmt);
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
40 vsnprintf(path, sizeof (path), fmt, ap);
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
41 va_end(ap);
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
42
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
43 return path;
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
44 }
4896bb07a8db core: add pprintf function
David Demelier <markand@malikania.fr>
parents: 234
diff changeset
45
185
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
46 unsigned int
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
47 nrand(unsigned int lower, unsigned int upper)
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
48 {
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
49 assert(upper <= RAND_MAX);
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
50
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
51 return (rand() % (upper - lower + 1)) + lower;
7103d6574062 core: add nrand function
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
52 }