aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/utils.php
blob: 7092f4dd8c2a8c8cd55d444cb6055ec1a9929a27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/*
 * File: ratatoeskr/sys/utils.php
 *
 * Various useful helper functions.
 *
 * License:
 * This file is part of Ratatöskr.
 * Ratatöskr is licensed unter the MIT / X11 License.
 * See "ratatoeskr/licenses/ratatoeskr" for more information.
 */

/*
 * Function: array_repeat
 *
 * Parameters:
 *
 *  $val -
 *  $n   -
 *
 * Returns:
 *
 *  An array with $val $n-times repeated.
 */
function array_repeat($val, $n)
{
    $rv = [];
    for ($i = 0; $i < $n; ++$i) {
        array_push($rv, $val);
    }
    return $rv;
}

/*
 * Function: intcmp
 * Compare integers (equavilent to strcmp)
 */
function intcmp($a, $b)
{
    return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
}
/*
 * Function: delete_directory
 * Delete a directory and all of its content.
 */
function delete_directory($dir)
{
    $dir_content = scandir($dir);
    foreach ($dir_content as $f) {
        if (($f == "..") or ($f == ".")) {
            continue;
        }

        $f = "$dir/$f";

        if (is_dir($f)) {
            delete_directory($f);
        } else {
            unlink($f);
        }
    }
    rmdir($dir);
}

/*
 * Constant: SITE_BASE_PATH
 * The Base path of this ratatoeskr site.
 */
define("SITE_BASE_PATH", dirname(dirname(dirname(__FILE__))));