library name fixups
[libmvfs.git] / libmvfs / arglist.c
blob60c8b491e5e8304cd0660e724695846a5bd733d2
1 /*
2 libmvfs - metux Virtual Filesystem Library
4 Argument list handling
6 Copyright (C) 2008 Enrico Weigelt, metux IT service <weigelt@metux.de>
7 This code is published under the terms of the GNU Public License 2.0
8 */
10 #include "mvfs-internal.h"
12 #include <string.h>
13 #include <malloc.h>
14 #include <hash.h>
15 #include <stdio.h>
16 #include <errno.h>
18 #include <mvfs/mvfs.h>
19 #include <mvfs/url.h>
20 #include <mvfs/_utils.h>
22 struct __mvfs_args
24 hash hashtable;
27 // not very efficient yet, but should work for now
29 MVFS_ARGS* mvfs_args_alloc()
31 MVFS_ARGS* args = calloc(1,sizeof(MVFS_ARGS));
32 if (!hash_initialise(&(args->hashtable), 997U, hash_hash_string, hash_compare_string, hash_copy_string, free, free))
34 ERRMSG("hash init failed");
35 free(args);
36 return NULL;
39 return args;
42 const char* mvfs_args_get(MVFS_ARGS* args, const char* name)
44 if (args==NULL)
45 return NULL;
47 const char* str = NULL;
48 if (hash_retrieve(&(args->hashtable), (char*)name, (void**)&str))
49 return str;
51 return NULL;
54 int mvfs_args_set(MVFS_ARGS* args, const char* name, const char* value)
56 if (args == NULL)
57 return -1;
59 hash_delete(&(args->hashtable), (char*)name);
60 if (value==NULL)
61 return 0;
63 if (!hash_insert(&(args->hashtable), strdup(name), strdup(value)))
64 ERRMSG("failed for key %s", name);
66 return 1;
69 int mvfs_args_setn(MVFS_ARGS* args, const char* name, const char* value, int sz)
71 if (args == NULL)
72 return -1;
74 hash_delete(&(args->hashtable), (char*)name);
75 if (value==NULL)
76 return 0;
78 if (!hash_insert(&(args->hashtable), strdup(name), strndup(value,sz)))
79 ERRMSG("failed for key %s", name);
81 return 1;
84 int mvfs_args_free(MVFS_ARGS* args)
86 if (args==NULL)
87 return -EFAULT;
89 hash_deinitialise(&(args->hashtable));
90 return 0;
93 MVFS_ARGS* mvfs_args_from_url(const char* url)
95 if (url==NULL)
96 return NULL;
98 MVFS_URL* u = mvfs_url_parse(url);
99 MVFS_ARGS* args = mvfs_args_alloc();
101 mvfs_args_set(args,"url", url);
102 mvfs_args_set(args,"path", u->pathname);
103 mvfs_args_set(args,"host", u->hostname);
104 mvfs_args_set(args,"port", u->port);
105 mvfs_args_set(args,"username", u->username);
106 mvfs_args_set(args,"secret", u->secret);
108 if ((!(u->type)) || (!(u->type[0])))
109 mvfs_args_set(args,"type", "local");
110 else
111 mvfs_args_set(args,"type", u->type);
113 return args;