summaryrefslogtreecommitdiffstats
path: root/src/lib/edoors_iota.c
blob: 4735ba3b550cc92aca55706ff135bda8f0143826 (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
/* EDOORS
 * Copyright (C) 2012 Jérémy Zurcher
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#include "edoors_iota.h"
#include "edoors_spin.h"
#include "edoors_room.h"
#include "edoors_door.h"
#include "edoors_board.h"
#include "edoors_private.h"

int edoors_iota_init(Edoors_Iota *iota, const char *name, Edoors_Iota *parent, Edoors_Type type)
{
    int ln, lp;
    char tmp[EDOORS_MAX_PATH_LENGTH];

    ln = strlen(name);
    if(name==NULL || ln==0) {
        ERR("name can't be NULL or empty");
        return 1;
    }

    iota->type = type;
    iota->spin = ( parent ? parent->spin : iota );
    iota->parent = parent;
    if(parent) {
        lp = eina_stringshare_strlen(parent->path);
        if((ln+lp+2)>EDOORS_MAX_PATH_LENGTH) {
            ERR("buffer overflow (%d)",(ln+lp+2));
            return 1;
        }
        memcpy(tmp,parent->path,lp);
        tmp[lp]='/';
        memcpy((tmp+lp+sizeof(char)),name,ln+sizeof(char));
        iota->name = eina_stringshare_add(name);
        iota->path = eina_stringshare_add(tmp);
    } else {
        iota->name = eina_stringshare_add(name);
        iota->path = eina_stringshare_add(name);
    }

    return 0;
}

void edoors_iota_desinit(Edoors_Iota *iota)
{
    STRINGSHARE_FREE(iota->name);
    STRINGSHARE_FREE(iota->path);
}

void edoors_iota_free(Edoors_Iota *iota)
{
    switch(iota->type) {
        case EDOORS_TYPE_SPIN:
            edoors_spin_free((Edoors_Spin*)iota);
            break;
        case EDOORS_TYPE_ROOM:
            edoors_room_free((Edoors_Room*)iota);
            break;
        case EDOORS_TYPE_DOOR:
            edoors_door_free((Edoors_Door*)iota);
            break;
        case EDOORS_TYPE_BOARD:
            edoors_board_free((Edoors_Board*)iota);
            break;
        default:
            CRITICAL("SPANK SPANK: 0x%X type %d is unknown, memory leak",PRINTPTR(iota),iota->type);
            break;
    }
}

EAPI void edoors_iota_show(Edoors_Iota *iota)
{
    DBG("Iota(%d) 0x%X : %s (%s) [0x%X] {0x%X}",iota->type,PRINTPTR(iota),iota->name,iota->path,
            PRINTPTR(iota->parent),PRINTPTR(iota->spin));
}