summaryrefslogtreecommitdiffstats
path: root/src/lib/eiotas_iota.c
blob: 1a1f7f87629fb41b62ef6508b9a4f361925516ba (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
/* EIOTAS
 * 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 "eiotas_iota.h"
#include "eiotas_spin.h"
#include "eiotas_room.h"
#include "eiotas_door.h"
#include "eiotas_board.h"
#include "eiotas_private.h"

int eiotas_iota_init(Eiotas_Iota *iota, const char *name, Eiotas_Iota *parent, Eiotas_Type type)
{
    int ln, lp;
    char tmp[EIOTAS_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)>EIOTAS_MAX_PATH_LENGTH) {
            ERR("path length %d is > EIOTAS_MAX_PATH_LENGTH (%d)",(ln+lp+2),EIOTAS_MAX_PATH_LENGTH);
            return 1;
        }
        memcpy(tmp,parent->path,lp);
        tmp[lp]='/';
        memcpy((tmp+lp+1),name,ln+1);
        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 eiotas_iota_desinit(Eiotas_Iota *iota)
{
    eina_stringshare_del(iota->name);
    eina_stringshare_del(iota->path);
}

void eiotas_iota_free(Eiotas_Iota *iota)
{
    switch(iota->type) {
        case EIOTAS_TYPE_SPIN:
            eiotas_spin_free((Eiotas_Spin*)iota);
            break;
        case EIOTAS_TYPE_ROOM:
            eiotas_room_free((Eiotas_Room*)iota);
            break;
        case EIOTAS_TYPE_DOOR:
            eiotas_door_free((Eiotas_Door*)iota);
            break;
        case EIOTAS_TYPE_BOARD:
            eiotas_board_free((Eiotas_Board*)iota);
            break;
        default:
            CRITICAL("SPANK SPANK: 0x%X type %d is unknown, memory leak",PRINTPTR(iota),iota->type);
            break;
    }
}

EAPI void eiotas_iota_show(Eiotas_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));
}