summaryrefslogtreecommitdiff
path: root/src/libaudcore/util.cc
blob: 4d92dedbffd09344acd0f994220bfeb065c48bfe (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * util.c
 * Copyright 2009-2013 John Lindgren and Michał Lipski
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "internal.h"

#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include <glib/gstdio.h>

#include "audstrings.h"
#include "runtime.h"

const char * get_home_utf8 ()
{
    static pthread_once_t once = PTHREAD_ONCE_INIT;
    static char * home_utf8;

    auto init = [] ()
        { home_utf8 = g_filename_to_utf8 (g_get_home_dir (), -1, nullptr, nullptr, nullptr); };

    pthread_once (& once, init);
    return home_utf8;
}

bool dir_foreach (const char * path, DirForeachFunc func, void * user)
{
    GDir * dir = g_dir_open (path, 0, nullptr);
    if (! dir)
        return false;

    const char * name;
    while ((name = g_dir_read_name (dir)))
    {
        if (func (filename_build ({path, name}), name, user))
            break;
    }

    g_dir_close (dir);
    return true;
}

String write_temp_file (const void * data, int64_t len)
{
    StringBuf name = filename_build ({g_get_tmp_dir (), "audacious-temp-XXXXXX"});

    int handle = g_mkstemp (name);
    if (handle < 0)
    {
        AUDERR ("Error creating temporary file: %s\n", strerror (errno));
        return String ();
    }

    while (len)
    {
        int64_t written = write (handle, data, len);
        if (written < 0)
        {
            AUDERR ("Error writing %s: %s\n", (const char *) name, strerror (errno));
            close (handle);
            return String ();
        }

        data = (char *) data + written;
        len -= written;
    }

    if (close (handle) < 0)
    {
        AUDERR ("Error closing %s: %s\n", (const char *) name, strerror (errno));
        return String ();
    }

    return String (name);
}

bool same_basename (const char * a, const char * b)
{
    const char * dot_a = strrchr (a, '.');
    const char * dot_b = strrchr (b, '.');
    int len_a = dot_a ? dot_a - a : strlen (a);
    int len_b = dot_b ? dot_b - b : strlen (b);

    return len_a == len_b && ! strcmp_nocase (a, b, len_a);
}

const char * last_path_element (const char * path)
{
    const char * slash = strrchr (path, G_DIR_SEPARATOR);
    return (slash && slash[1]) ? slash + 1 : nullptr;
}

void cut_path_element (char * path, int pos)
{
#ifdef _WIN32
    if (pos > 3)
#else
    if (pos > 1)
#endif
        path[pos - 1] = 0; /* overwrite slash */
    else
        path[pos] = 0; /* leave [drive letter and] leading slash */
}

bool is_cuesheet_entry (const char * filename)
{
    const char * ext, * sub;
    uri_parse (filename, nullptr, & ext, & sub, nullptr);
    return sub[0] && sub - ext == 4 && ! strcmp_nocase (ext, ".cue", 4);
}

bool is_subtune (const char * filename)
{
    const char * sub;
    uri_parse (filename, nullptr, nullptr, & sub, nullptr);
    return sub[0];
}

StringBuf strip_subtune (const char * filename)
{
    const char * sub;
    uri_parse (filename, nullptr, nullptr, & sub, nullptr);
    return str_copy (filename, sub - filename);
}

/* Thomas Wang's 32-bit mix function.  See:
 * http://web.archive.org/web/20070307172248/http://www.concentric.net/~Ttwang/tech/inthash.htm */

unsigned int32_hash (unsigned val)
{
    val = ~val + (val << 15);
    val = val ^ (val >> 12);
    val = val + (val << 2);
    val = val ^ (val >> 4);
    val = val * 2057;
    val = val ^ (val >> 16);
    return val;
}

unsigned ptr_hash (const void * ptr)
{
    unsigned addr_low = (uint64_t) (uintptr_t) ptr;
    unsigned addr_high = (uint64_t) (uintptr_t) ptr >> 32;
    return int32_hash (addr_low + addr_high);
}