summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_value_util.c
blob: 3d35a47838445bc40781281d847a154fc6e88829 (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
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2013, 2014 Mike Blumenkrantz
 *
 * 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/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <time.h>

#ifdef _WIN32
# include <Evil.h>
#endif

#include "eina_safety_checks.h"
#include "eina_value.h"
#include "eina_stringshare.h"


typedef struct _Eina_Value_Util_Struct_Desc
{
   Eina_Value_Struct_Desc base;
   int refcount;
} Eina_Value_Util_Struct_Desc;

static void *
_ops_malloc(const Eina_Value_Struct_Operations *ops EINA_UNUSED, const Eina_Value_Struct_Desc *desc)
{
   Eina_Value_Util_Struct_Desc *edesc = (Eina_Value_Util_Struct_Desc*)desc;
   edesc->refcount++;
   //DBG("%p refcount=%d", edesc, edesc->refcount);
   return malloc(desc->size);
}

static void
_ops_free(const Eina_Value_Struct_Operations *ops EINA_UNUSED, const Eina_Value_Struct_Desc *desc, void *memory)
{
   Eina_Value_Util_Struct_Desc *edesc = (Eina_Value_Util_Struct_Desc*) desc;
   edesc->refcount--;
   free(memory);
   //DBG("%p refcount=%d", edesc, edesc->refcount);
   if (edesc->refcount <= 0)
     {
        unsigned i;
        for (i = 0; i < edesc->base.member_count; i++)
          eina_stringshare_del((char *)edesc->base.members[i].name);
        free((Eina_Value_Struct_Member *)edesc->base.members);
        free(edesc);
     }
}

static Eina_Value_Struct_Operations operations =
{
   EINA_VALUE_STRUCT_OPERATIONS_VERSION,
   _ops_malloc,
   _ops_free,
   NULL,
   NULL,
   NULL
};

EAPI Eina_Value_Struct_Desc *
eina_value_util_struct_desc_new(void)
{
   Eina_Value_Util_Struct_Desc *st_desc;

   st_desc = calloc(1, sizeof(Eina_Value_Util_Struct_Desc));
   EINA_SAFETY_ON_NULL_RETURN_VAL(st_desc, NULL);
   st_desc->base.version = EINA_VALUE_STRUCT_DESC_VERSION;
   st_desc->base.ops = &operations;
   return (Eina_Value_Struct_Desc*)st_desc;
}

EAPI Eina_Value *
eina_value_util_time_string_new(const char *timestr)
{
   Eina_Value *v;
   struct tm tm;
   time_t t;

   if (!strptime(timestr, "%Y%m%dT%H:%M:%S", &tm)) return NULL;
   t = mktime(&tm);
   v = eina_value_new(EINA_VALUE_TYPE_TIMESTAMP);
   if (v) eina_value_set(v, t);
   return v;
}