packio

view src/pathmap.c @ 1:a5728bc6a02f

moving along
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Jan 2015 02:31:29 +0200
parents a71bd70c1014
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "pathmap.h"
5 #include "rbtree.h"
7 static void map_entry_destructor(struct rbnode *node, void *data);
9 static struct rbtree *map;
11 int pkio_map(const char *from, const char *to)
12 {
13 char *myfrom, *myto;
15 if(!map) {
16 if(!(map = rb_create(RB_KEY_STRING))) {
17 fprintf(stderr, "packio: failed to create the path mapping tree\n");
18 return -1;
19 }
20 rb_set_delete_func(map, map_entry_destructor, 0);
21 }
23 if(!(myfrom = malloc(strlen(from) + 1))) {
24 fprintf(stderr, "packio: failed to allocate mapping source string\n");
25 return -1;
26 }
27 if(!(myto = malloc(strlen(to) + 1))) {
28 fprintf(stderr, "packio: failed to allocate mapping destination string\n");
29 free(myto);
30 return -1;
31 }
32 strcpy(myfrom, from);
33 strcpy(myto, to);
35 if(rb_insert(map, myfrom, myto) == -1) {
36 fprintf(stderr, "packio: failed to add path mapping\n");
37 free(myfrom);
38 free(myto);
39 return -1;
40 }
41 return 0;
42 }
44 const char *pkio_pathmap(const char *path)
45 {
46 struct rbnode *node;
48 if(!map) {
49 return path;
50 }
52 if(!(node = rb_find(map, (void*)path))) {
53 return path;
54 }
55 return node->data;
56 }
58 static void map_entry_destructor(struct rbnode *node, void *data)
59 {
60 free(node->key);
61 free(node->data);
62 }