Back to index

lightning-sunbird  0.9+nobinonly
icalattach.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: icalattach.c
00004   CREATOR: acampi 28 May 02
00005   
00006   $Id: icalattach.c,v 1.2 2002/10/09 19:54:59 acampi Exp $
00007   $Locker:  $
00008     
00009 
00010  (C) COPYRIGHT 2000, Andrea Campi
00011 
00012  This program is free software; you can redistribute it and/or modify
00013  it under the terms of either: 
00014 
00015     The LGPL as published by the Free Software Foundation, version
00016     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00017 
00018   Or:
00019 
00020     The Mozilla Public License Version 1.0. You may obtain a copy of
00021     the License at http://www.mozilla.org/MPL/
00022 
00023   The original code is icaltypes.c
00024 
00025  ======================================================================*/
00026 #ifdef HAVE_CONFIG_H
00027 #include "config.h"
00028 #endif
00029 
00030 #include "icaltypes.h"
00031 #include "icalerror.h"
00032 #include "icalmemory.h"
00033 #include "icalattachimpl.h"
00034 #include <stdlib.h> /* for malloc and abs() */
00035 #include <errno.h> /* for errno */
00036 #include <string.h> /* for icalmemory_strdup */
00037 #include <assert.h>
00038 
00039 icalattach *
00040 icalattach_new_from_url (const char *url)
00041 {
00042     icalattach *attach;
00043     char *url_copy;
00044 
00045     icalerror_check_arg_rz ((url != NULL), "url");
00046 
00047     if ((attach = malloc (sizeof (icalattach))) == NULL) {
00048        errno = ENOMEM;
00049        return NULL;
00050     }
00051 
00052     if ((url_copy = strdup (url)) == NULL) {
00053        free (attach);
00054        errno = ENOMEM;
00055        return NULL;
00056     }
00057 
00058     attach->refcount = 1;
00059     attach->is_url = 1;
00060     attach->u.url.url = url_copy;
00061 
00062     return attach;
00063 }
00064 
00065 icalattach *
00066 icalattach_new_from_data (unsigned char *data, icalattach_free_fn_t free_fn,
00067                        void *free_fn_data)
00068 {
00069     icalattach *attach;
00070 
00071     icalerror_check_arg_rz ((data != NULL), "data");
00072 
00073     if ((attach = malloc (sizeof (icalattach))) == NULL) {
00074        errno = ENOMEM;
00075        return NULL;
00076     }
00077 
00078     attach->refcount = 1;
00079     attach->is_url = 0;
00080     attach->u.data.data = data;
00081     attach->u.data.free_fn = free_fn;
00082     attach->u.data.free_fn_data = free_fn_data;
00083 
00084     return attach;
00085 }
00086 
00087 void
00088 icalattach_ref (icalattach *attach)
00089 {
00090     icalerror_check_arg_rv ((attach != NULL), "attach");
00091     icalerror_check_arg_rv ((attach->refcount > 0), "attach->refcount > 0");
00092 
00093     attach->refcount++;
00094 }
00095 
00096 void
00097 icalattach_unref (icalattach *attach)
00098 {
00099     icalerror_check_arg_rv ((attach != NULL), "attach");
00100     icalerror_check_arg_rv ((attach->refcount > 0), "attach->refcount > 0");
00101 
00102     attach->refcount--;
00103 
00104     if (attach->refcount != 0)
00105        return;
00106 
00107     if (attach->is_url)
00108        free (attach->u.url.url);
00109     else if (attach->u.data.free_fn)
00110        (* attach->u.data.free_fn) (attach->u.data.data, attach->u.data.free_fn_data);
00111 
00112     free (attach);
00113 }
00114 
00115 int
00116 icalattach_get_is_url (icalattach *attach)
00117 {
00118     icalerror_check_arg_rz ((attach != NULL), "attach");
00119 
00120     return attach->is_url ? 1 : 0;
00121 }
00122 
00123 const char *
00124 icalattach_get_url (icalattach *attach)
00125 {
00126     icalerror_check_arg_rz ((attach != NULL), "attach");
00127     icalerror_check_arg_rz ((attach->is_url), "attach->is_url");
00128 
00129     return attach->u.url.url;
00130 }
00131 
00132 unsigned char *
00133 icalattach_get_data (icalattach *attach)
00134 {
00135     icalerror_check_arg_rz ((attach != NULL), "attach");
00136     icalerror_check_arg_rz ((!attach->is_url), "!attach->is_url");
00137 
00138     return attach->u.data.data;
00139 }