d0e239e9c2ffb913a3fe3c30a61d93da3310958c
[captive.git] / src / client / fuse / gnomevfsresult.c
1 /* $Id$
2  * Client fuse interface handling of 'GnomeVFSResult' for libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <libgnomevfs/gnome-vfs-result.h>
23 #include <glib/ghash.h>
24 #include <glib/gmessages.h>
25 #include <errno.h>
26 #include <stdio.h>
27
28 #include "gnomevfsresult.h"     /* self */
29
30
31 /* Config: */
32 #define ERRNO_VALUE_MAX 0x400
33
34
35 /* map: GINT_TO_POINTER(GnomeVFSResult) -> GINT_TO_POINTER(errno) */
36 static GHashTable *gnomevfsresult_to_errno_hash;
37
38 static void gnomevfsresult_to_errno_hash_init(void)
39 {
40 int errno_i;
41
42         if (gnomevfsresult_to_errno_hash)
43                 return;
44         gnomevfsresult_to_errno_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
45         for (errno_i=1;errno_i<ERRNO_VALUE_MAX;errno_i++) {
46 GnomeVFSResult gnomevfsresult;
47
48                 /* gnome_vfs_result_from_errno_code() returns 'GNOME_VFS_ERROR_GENERIC'
49                  * for invalid input values.
50                  */
51                 gnomevfsresult=gnome_vfs_result_from_errno_code(errno_i);
52                 if (GNOME_VFS_ERROR_GENERIC==gnomevfsresult)
53                         continue;
54                 g_hash_table_insert(
55                                 gnomevfsresult_to_errno_hash,   /* hash_table */
56                                 GINT_TO_POINTER(gnomevfsresult),        /* key */
57                                 GINT_TO_POINTER(errno_i));      /* value */
58                 }
59 }
60
61 int gnomevfsresult_to_errno(GnomeVFSResult gnomevfsresult)
62 {
63 int r;
64
65         g_return_val_if_fail(sizeof(GnomeVFSResult)<=sizeof(gint),EINVAL);
66
67         /* 'GNOME_VFS_OK' may map to NULL or fallback somehow or whatever weird. */
68         if (GNOME_VFS_OK==gnomevfsresult)
69                 return 0;
70
71         gnomevfsresult_to_errno_hash_init();
72         if (!(r=GPOINTER_TO_INT(g_hash_table_lookup(gnomevfsresult_to_errno_hash,GINT_TO_POINTER(gnomevfsresult))))) {
73                 g_assert_not_reached();
74                 return EINVAL;
75                 }
76         return r;
77 }