$darkmode
Libical API Documentation 4.0 STABLE VERSION Visit the v3.0 documentation
icalattach.c
Go to the documentation of this file.
1 /*======================================================================
2  FILE: icalattach.c
3  CREATOR: acampi 28 May 02
4 
5  SPDX-FileCopyrightText: 2002, Andrea Campi <a.campi@inet.it>
6  SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7  ======================================================================*/
8 
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17 
18 #include "icalattachimpl.h"
19 #include "icalerror_p.h"
20 #include "icalmemory.h"
21 
22 #include <errno.h>
23 #include <stdlib.h>
24 
26 {
27  icalattach *attach;
28  char *url_copy;
29 
30  icalerror_check_arg_rz((url != NULL), "url");
31 
32  if ((attach = icalmemory_new_buffer(sizeof(icalattach))) == NULL) {
33  errno = ENOMEM;
34  return NULL;
35  }
36 
37  if ((url_copy = icalmemory_strdup(url)) == NULL) {
38  icalmemory_free_buffer(attach);
39  errno = ENOMEM;
40  return NULL;
41  }
42 
43  attach->refcount = 1;
44  attach->is_url = 1;
45  attach->u.url.url = url_copy;
46 
47  return attach;
48 }
49 
50 static void attach_data_free(char *data, void *free_fn_data)
51 {
52  _unused(free_fn_data);
54 }
55 
57  void *free_fn_data)
58 {
59  icalattach *attach;
60 
61  icalerror_check_arg_rz((data != NULL), "data");
62 
63  if ((attach = icalmemory_new_buffer(sizeof(icalattach))) == NULL) {
64  errno = ENOMEM;
65  return NULL;
66  }
67 
68  if (!free_fn) {
69  data = icalmemory_strdup(data);
70  if (!data) {
71  icalmemory_free_buffer(attach);
72  errno = ENOMEM;
73  return NULL;
74  }
75  free_fn = attach_data_free;
76  }
77 
78  attach->refcount = 1;
79  attach->is_url = 0;
80  attach->u.data.data = (char *)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 
88 {
89  icalerror_check_arg_rv((attach != NULL), "attach");
90  icalerror_check_arg_rv((attach->refcount > 0), "attach->refcount > 0");
91 
92  attach->refcount++;
93 }
94 
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  if (attach->refcount != 0) {
103  return;
104  }
105 
106  if (attach->is_url) {
107  icalmemory_free_buffer(attach->u.url.url);
108  } else if (attach->u.data.free_fn) {
109  (*attach->u.data.free_fn)(attach->u.data.data, attach->u.data.free_fn_data);
110  }
111 
112  icalmemory_free_buffer(attach);
113 }
114 
116 {
117  icalerror_check_arg_rz((attach != NULL), "attach");
118 
119  return attach->is_url;
120 }
121 
122 const char *icalattach_get_url(icalattach *attach)
123 {
124  icalerror_check_arg_rz((attach != NULL), "attach");
125  icalerror_check_arg_rz((attach->is_url), "attach->is_url");
126 
127  return attach->u.url.url;
128 }
129 
130 unsigned char *icalattach_get_data(icalattach *attach)
131 {
132  icalerror_check_arg_rz((attach != NULL), "attach");
133  icalerror_check_arg_rz((!attach->is_url), "!attach->is_url");
134 
135  return (unsigned char *)attach->u.data.data;
136 }
bool icalattach_get_is_url(const icalattach *attach)
Determines if attach is an URL.
Definition: icalattach.c:115
Common memory management routines.
char * icalmemory_strdup(const char *s)
Creates a duplicate of a string.
Definition: icalmemory.c:242
void icalmemory_free_buffer(void *buf)
Releases a buffer.
Definition: icalmemory.c:355
icalattach * icalattach_new_from_data(const char *data, icalattach_free_fn_t free_fn, void *free_fn_data)
Creates new icalattach object from data.
Definition: icalattach.c:56
unsigned char * icalattach_get_data(icalattach *attach)
Returns the data of the icalattach object.
Definition: icalattach.c:130
void(* icalattach_free_fn_t)(char *data, void *user_data)
Definition: icalattach.h:46
void icalattach_unref(icalattach *attach)
Decrements reference count of the icalattach.
Definition: icalattach.c:95
void icalattach_ref(icalattach *attach)
Increments reference count of the icalattach.
Definition: icalattach.c:87
const char * icalattach_get_url(icalattach *attach)
Returns the URL of the icalattach object.
Definition: icalattach.c:122
void * icalmemory_new_buffer(size_t size)
Creates new buffer with the specified size.
Definition: icalmemory.c:315
icalattach * icalattach_new_from_url(const char *url)
Creates new icalattach object from a URL.
Definition: icalattach.c:25
struct icalattach_impl icalattach
An iCal attach object representing a link to a document object.
Definition: icalattach.h:36