]> git.sesse.net Git - vlc/blob - src/posix/error.c
wasapi: audio capture client module (fixes #7205)
[vlc] / src / posix / error.c
1 /*****************************************************************************
2  * error.c: POSIX error messages formatting
3  *****************************************************************************
4  * Copyright © 2013 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <assert.h>
29
30 #include <vlc_common.h>
31
32 static const char *vlc_strerror_l(int errnum, const char *lname)
33 {
34     int saved_errno = errno;
35     locale_t loc = newlocale(LC_MESSAGES_MASK, lname, (locale_t)0);
36
37     if (unlikely(loc == (locale_t)0))
38     {
39         if (errno == ENOENT) /* fallback to POSIX locale */
40             loc = newlocale(LC_MESSAGES_MASK, "C", (locale_t)0);
41
42         if (unlikely(loc == (locale_t)0))
43         {
44             assert(errno != EINVAL && errno != ENOENT);
45             errno = saved_errno;
46             return "Error message unavailable";
47         }
48         errno = saved_errno;
49     }
50
51     const char *buf = strerror_l(errnum, loc);
52
53     freelocale(loc);
54     return buf;
55 }
56
57 /**
58  * Formats an error message in the current locale.
59  * @param errnum error number (as in errno.h)
60  * @return A string pointer, valid until the next call to a function of the
61  * strerror() family in the same thread. This function cannot fail.
62  */
63 const char *vlc_strerror(int errnum)
64 {
65     /* We cannot simply use strerror() here, since it is not thread-safe. */
66     return vlc_strerror_l(errnum, "");
67 }
68
69 /**
70  * Formats an error message in the POSIX/C locale (i.e. American English).
71  * @param errnum error number (as in errno.h)
72  * @return A string pointer, valid until the next call to a function of the
73  * strerror() family in the same thread. This function cannot fail.
74  */
75 const char *vlc_strerror_c(int errnum)
76 {
77     return vlc_strerror_l(errnum, "C");
78 }