1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: icalattach.c
4   CREATOR: acampi 28 May 02
5 
6   $Id: icalattach.c,v 1.3 2007-04-30 13:57:47 artcancro Exp $
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 *
icalattach_new_from_url(const char * url)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 *
icalattach_new_from_data(const char * data,icalattach_free_fn_t free_fn,void * free_fn_data)66 icalattach_new_from_data (const char *data, icalattach_free_fn_t free_fn,
67 			  void *free_fn_data)
68 {
69     icalattach *attach;
70     char *data_copy;
71 
72     icalerror_check_arg_rz ((data != NULL), "data");
73 
74     if ((attach = malloc (sizeof (icalattach))) == NULL) {
75 	errno = ENOMEM;
76 	return NULL;
77     }
78 
79     if ((data_copy = strdup (data)) == NULL) {
80 	free (attach);
81 	errno = ENOMEM;
82 	return NULL;
83     }
84 
85     attach->refcount = 1;
86     attach->is_url = 0;
87     attach->u.data.data = data_copy;
88     attach->u.data.free_fn = free_fn;
89     attach->u.data.free_fn_data = free_fn_data;
90 
91     return attach;
92 }
93 
94 void
icalattach_ref(icalattach * attach)95 icalattach_ref (icalattach *attach)
96 {
97     icalerror_check_arg_rv ((attach != NULL), "attach");
98     icalerror_check_arg_rv ((attach->refcount > 0), "attach->refcount > 0");
99 
100     attach->refcount++;
101 }
102 
103 void
icalattach_unref(icalattach * attach)104 icalattach_unref (icalattach *attach)
105 {
106     icalerror_check_arg_rv ((attach != NULL), "attach");
107     icalerror_check_arg_rv ((attach->refcount > 0), "attach->refcount > 0");
108 
109     attach->refcount--;
110 
111     if (attach->refcount != 0)
112 	return;
113 
114     if (attach->is_url) {
115 	free (attach->u.url.url);
116     } else {
117 	free (attach->u.data.data);
118 /* unused for now
119 	if (attach->u.data.free_fn)
120 	   (* attach->u.data.free_fn) (attach->u.data.data, attach->u.data.free_fn_data);
121 */
122     }
123 
124     free (attach);
125 }
126 
127 int
icalattach_get_is_url(icalattach * attach)128 icalattach_get_is_url (icalattach *attach)
129 {
130     icalerror_check_arg_rz ((attach != NULL), "attach");
131 
132     return attach->is_url ? 1 : 0;
133 }
134 
135 const char *
icalattach_get_url(icalattach * attach)136 icalattach_get_url (icalattach *attach)
137 {
138     icalerror_check_arg_rz ((attach != NULL), "attach");
139     icalerror_check_arg_rz ((attach->is_url), "attach->is_url");
140 
141     return attach->u.url.url;
142 }
143 
144 unsigned char *
icalattach_get_data(icalattach * attach)145 icalattach_get_data (icalattach *attach)
146 {
147     icalerror_check_arg_rz ((attach != NULL), "attach");
148     icalerror_check_arg_rz ((!attach->is_url), "!attach->is_url");
149 
150     return (unsigned char*)attach->u.data.data;
151 }
152