summaryrefslogtreecommitdiff
path: root/libmailutils/property/create.c
blob: 9d6e7c8c263b8ff94d3bf6743ff2891f71fe7373 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999-2001, 2004-2005, 2007-2008, 2010-2012, 2014-2016
   Free Software Foundation, Inc.

   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 3 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 <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/property.h>

int
mu_property_create (mu_property_t *pprop)
{
  mu_property_t prop = calloc (1, sizeof (prop[0]));
  if (!prop)
    return ENOMEM;
  *pprop = prop;
  return 0;
}

int
mu_property_create_init (mu_property_t *pprop,
			 int (*initfun) (mu_property_t), void *initdata)
{
  mu_property_t prop;
  int rc = mu_property_create (&prop);
  if (rc == 0)
    {
      mu_property_set_init (prop, initfun, initdata);
      *pprop = prop;
    }
  return rc;
}

int
mu_property_set_init (mu_property_t prop,
		      int (*initfun) (mu_property_t), void *initdata)
{
  if (!prop)
    return ENOMEM;
  if (prop->_prop_flags & MU_PROP_INIT)
    return MU_ERR_SEQ;
  prop->_prop_init = initfun;
  prop->_prop_init_data = initdata;
  return 0;
}

int
mu_property_set_init_data (mu_property_t prop, void *data, void **old_data)
{
  if (!prop)
    return ENOMEM;
  if (prop->_prop_flags & MU_PROP_INIT)
    return MU_ERR_SEQ;
  if (old_data)
    *old_data = prop->_prop_init_data;
  prop->_prop_init_data = data;
  return 0;
}

void
mu_property_destroy (mu_property_t *pprop)
{
  if (pprop)
    {
      mu_property_t prop = *pprop;
      if (prop && (prop->_prop_ref_count == 0 || --prop->_prop_ref_count == 0))
	{
	  mu_property_save (prop);
	  if (prop->_prop_done)
	    prop->_prop_done (prop);
	  free (prop);
	  *pprop = NULL;
	}
    }
}

void
mu_property_ref (mu_property_t prop)
{
  if (prop)
    prop->_prop_ref_count++;
}

void
mu_property_unref (mu_property_t prop)
{
  mu_property_destroy (&prop);
}

int
mu_property_save (mu_property_t prop)
{
  int rc = 0;
      
  if (!prop)
    return EINVAL;
  if (prop->_prop_flags & MU_PROP_MODIFIED)
    {
      if (prop->_prop_save)
	rc = prop->_prop_save (prop);

      if (rc == 0)
	prop->_prop_flags &= ~MU_PROP_MODIFIED;
    }
  return rc;
}

int
_mu_property_init (mu_property_t prop)
{
  int rc = 0;
  if (!(prop->_prop_flags & MU_PROP_INIT))
    {
      if (prop->_prop_init)
	rc = prop->_prop_init (prop);
      if (rc == 0)
	prop->_prop_flags |= MU_PROP_INIT;
    }
  return rc;
}

static int
_mu_property_fill (mu_property_t prop)
{
  int rc = 0;
  if (!(prop->_prop_flags & MU_PROP_FILL))
    {
      if (prop->_prop_fill)
	rc = prop->_prop_fill (prop);
      if (rc == 0)
	prop->_prop_flags |= MU_PROP_FILL;
    }
  return rc;
}

int
_mu_property_check (mu_property_t prop)
{
  int rc;
  
  if (!prop)
    return EINVAL;
  rc = _mu_property_init (prop);
  if (rc == 0)
    rc = _mu_property_fill (prop);
  return rc;
}

  

Return to:

Send suggestions and report system problems to the System administrator.