Diffstat (limited to 'libical/src/libical/icalattach.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libical/src/libical/icalattach.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/libical/src/libical/icalattach.c b/libical/src/libical/icalattach.c new file mode 100644 index 0000000..8da9d43 --- a/dev/null +++ b/libical/src/libical/icalattach.c | |||
@@ -0,0 +1,139 @@ | |||
1 | /* -*- Mode: C -*- | ||
2 | ====================================================================== | ||
3 | FILE: icalattach.c | ||
4 | CREATOR: acampi 28 May 02 | ||
5 | |||
6 | $Id$ | ||
7 | $Locker$ | ||
8 | |||
9 | |||
10 | (C) COPYRIGHT 2000, Andrea Campi | ||
11 | |||
12 | This program is free software; you can redistribute it and/or modify | ||
13 | it under the terms of either: | ||
14 | |||
15 | The LGPL as published by the Free Software Foundation, version | ||
16 | 2.1, available at: http://www.fsf.org/copyleft/lesser.html | ||
17 | |||
18 | Or: | ||
19 | |||
20 | The Mozilla Public License Version 1.0. You may obtain a copy of | ||
21 | the License at http://www.mozilla.org/MPL/ | ||
22 | |||
23 | The original code is icaltypes.c | ||
24 | |||
25 | ======================================================================*/ | ||
26 | #ifdef HAVE_CONFIG_H | ||
27 | #include "config.h" | ||
28 | #endif | ||
29 | |||
30 | #include "icaltypes.h" | ||
31 | #include "icalerror.h" | ||
32 | #include "icalmemory.h" | ||
33 | #include "icalattachimpl.h" | ||
34 | #include <stdlib.h> /* for malloc and abs() */ | ||
35 | #include <errno.h> /* for errno */ | ||
36 | #include <string.h> /* for icalmemory_strdup */ | ||
37 | #include <assert.h> | ||
38 | |||
39 | icalattach * | ||
40 | icalattach_new_from_url (const char *url) | ||
41 | { | ||
42 | icalattach *attach; | ||
43 | char *url_copy; | ||
44 | |||
45 | icalerror_check_arg_rz ((url != NULL), "url"); | ||
46 | |||
47 | if ((attach = malloc (sizeof (icalattach))) == NULL) { | ||
48 | errno = ENOMEM; | ||
49 | return NULL; | ||
50 | } | ||
51 | |||
52 | if ((url_copy = strdup (url)) == NULL) { | ||
53 | free (attach); | ||
54 | errno = ENOMEM; | ||
55 | return NULL; | ||
56 | } | ||
57 | |||
58 | attach->refcount = 1; | ||
59 | attach->is_url = 1; | ||
60 | attach->u.url.url = url_copy; | ||
61 | |||
62 | return attach; | ||
63 | } | ||
64 | |||
65 | icalattach * | ||
66 | icalattach_new_from_data (unsigned char *data, icalattach_free_fn_t free_fn, | ||
67 | void *free_fn_data) | ||
68 | { | ||
69 | icalattach *attach; | ||
70 | |||
71 | icalerror_check_arg_rz ((data != NULL), "data"); | ||
72 | |||
73 | if ((attach = malloc (sizeof (icalattach))) == NULL) { | ||
74 | errno = ENOMEM; | ||
75 | return NULL; | ||
76 | } | ||
77 | |||
78 | attach->refcount = 1; | ||
79 | attach->is_url = 0; | ||
80 | attach->u.data.data = data; | ||
81 | attach->u.data.free_fn = free_fn; | ||
82 | attach->u.data.free_fn_data = free_fn_data; | ||
83 | |||
84 | return attach; | ||
85 | } | ||
86 | |||
87 | void | ||
88 | icalattach_ref (icalattach *attach) | ||
89 | { | ||
90 | icalerror_check_arg_rv ((attach != NULL), "attach"); | ||
91 | icalerror_check_arg_rv ((attach->refcount > 0), "attach->refcount > 0"); | ||
92 | |||
93 | attach->refcount++; | ||
94 | } | ||
95 | |||
96 | void | ||
97 | icalattach_unref (icalattach *attach) | ||
98 | { | ||
99 | icalerror_check_arg_rv ((attach != NULL), "attach"); | ||
100 | icalerror_check_arg_rv ((attach->refcount > 0), "attach->refcount > 0"); | ||
101 | |||
102 | attach->refcount--; | ||
103 | |||
104 | if (attach->refcount != 0) | ||
105 | return; | ||
106 | |||
107 | if (attach->is_url) | ||
108 | free (attach->u.url.url); | ||
109 | else if (attach->u.data.free_fn) | ||
110 | (* attach->u.data.free_fn) (attach->u.data.data, attach->u.data.free_fn_data); | ||
111 | |||
112 | free (attach); | ||
113 | } | ||
114 | |||
115 | int | ||
116 | icalattach_get_is_url (icalattach *attach) | ||
117 | { | ||
118 | icalerror_check_arg_rz ((attach != NULL), "attach"); | ||
119 | |||
120 | return attach->is_url ? 1 : 0; | ||
121 | } | ||
122 | |||
123 | const char * | ||
124 | icalattach_get_url (icalattach *attach) | ||
125 | { | ||
126 | icalerror_check_arg_rz ((attach != NULL), "attach"); | ||
127 | icalerror_check_arg_rz ((attach->is_url), "attach->is_url"); | ||
128 | |||
129 | return attach->u.url.url; | ||
130 | } | ||
131 | |||
132 | unsigned char * | ||
133 | icalattach_get_data (icalattach *attach) | ||
134 | { | ||
135 | icalerror_check_arg_rz ((attach != NULL), "attach"); | ||
136 | icalerror_check_arg_rz ((!attach->is_url), "!attach->is_url"); | ||
137 | |||
138 | return attach->u.data.data; | ||
139 | } | ||