/* -*- Mode: C -*- */
/*======================================================================
  FILE: icalvalue.c
  CREATOR: eric 02 May 1999
  
  $Id: icalderivedvalue.c.in,v 1.3 2002/06/24 22:02:34 djarvie Exp $


 (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org

 This program is free software; you can redistribute it and/or modify
 it under the terms of either: 

    The LGPL as published by the Free Software Foundation, version
    2.1, available at: http://www.fsf.org/copyleft/lesser.html

  Or:

    The Mozilla Public License Version 1.0. You may obtain a copy of
    the License at http://www.mozilla.org/MPL/

  The original code is icalvalue.c

  Contributions from:
     Graham Davison (g.m.davison@computer.org)


======================================================================*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "icalerror.h"
#include "icalmemory.h"
#include "icalparser.h"
#include "icalenums.h"

#include "icalvalueimpl.h"

#include <stdlib.h> /* for malloc */
#include <stdio.h> /* for sprintf */
#include <string.h> /* For memset, others */
#include <stddef.h> /* For offsetof() macro */
#include <errno.h>
#include <time.h> /* for mktime */
#include <stdlib.h> /* for atoi and atof */
#include <limits.h> /* for SHRT_MAX */         



#define TMP_BUF_SIZE 1024

struct icalvalue_impl*  icalvalue_new_impl(icalvalue_kind kind);

/* This map associates each of the value types with its string
   representation */
struct icalvalue_kind_map {
	icalvalue_kind kind;
	char name[20];
};

extern struct icalvalue_kind_map value_map[]; 

const char* icalvalue_kind_to_string(icalvalue_kind kind)
{
    int i;

    for (i=0; value_map[i].kind != ICAL_NO_VALUE; i++) {
	if (value_map[i].kind == kind) {
	    return value_map[i].name;
	}
    }

    return 0;
}

icalvalue_kind icalvalue_string_to_kind(const char* str)
{
    int i;

    for (i=0; value_map[i].kind != ICAL_NO_VALUE; i++) {
	if (strcmp(value_map[i].name,str) == 0) {
	    return value_map[i].kind;
	}
    }

    return  value_map[i].kind;

}

icalvalue* icalvalue_new_x (const char* v){
   struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_X_VALUE);
   icalerror_check_arg_rz( (v!=0),"v");

   icalvalue_set_x((icalvalue*)impl,v);
   return (icalvalue*)impl;
}
void icalvalue_set_x(icalvalue* value, const char* v) {
    struct icalvalue_impl* impl; 
    icalerror_check_arg_rv( (value!=0),"value");
    icalerror_check_arg_rv( (v!=0),"v");

    impl = (struct icalvalue_impl*)value;
    if(impl->x_value!=0) {free((void*)impl->x_value);}

    impl->x_value = icalmemory_strdup(v);

    if (impl->x_value == 0){
      errno = ENOMEM;
    }
 
 }
const char* icalvalue_get_x(icalvalue* value) {

    icalerror_check_arg( (value!=0),"value");
    icalerror_check_value_type(value, ICAL_X_VALUE);
    return ((struct icalvalue_impl*)value)->x_value;
}

/* Attachment is a special case, so it is not auto generated. */
icalvalue*
icalvalue_new_attach (struct icalattachtype *v)
{
   struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_ATTACH_VALUE);
    
   icalvalue_set_attach((icalvalue*)impl,v);

   return (icalvalue*)impl;
}

void
icalvalue_set_attach(icalvalue* value, struct icalattachtype *v)
{
    struct icalvalue_impl* impl; 
    
    icalerror_check_arg_rv( (value!=0),"value");
    icalerror_check_value_type(value, ICAL_ATTACH_VALUE);

    impl = (struct icalvalue_impl*)value;

    if (impl->data.v_attach != 0){
	icalattachtype_free(impl->data.v_attach);
    }

    impl->data.v_attach = v;
    icalattachtype_add_reference(v);
}

struct icalattachtype*
icalvalue_get_attach(icalvalue* value)
{
    icalerror_check_arg( (value!=0),"value");
    icalerror_check_value_type(value, ICAL_ATTACH_VALUE);
  
    return ((struct icalvalue_impl*)value)->data.v_attach;
}


/* Recur is a special case, so it is not auto generated. */
icalvalue*
icalvalue_new_recur (struct icalrecurrencetype v)
{
   struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_RECUR_VALUE);
    
   icalvalue_set_recur((icalvalue*)impl,v);

   return (icalvalue*)impl;
}

void
icalvalue_set_recur(icalvalue* value, struct icalrecurrencetype v)
{
    struct icalvalue_impl* impl; 
    
    icalerror_check_arg_rv( (value!=0),"value");
    icalerror_check_value_type(value, ICAL_RECUR_VALUE);

    impl = (struct icalvalue_impl*)value;

    if (impl->data.v_recur != 0){
	free(impl->data.v_recur);
	impl->data.v_recur = 0;
    }

    impl->data.v_recur = malloc(sizeof(struct icalrecurrencetype));

    if (impl->data.v_recur == 0){
	icalerror_set_errno(ICAL_NEWFAILED_ERROR);
	return;
    } else {
	memcpy(impl->data.v_recur, &v, sizeof(struct icalrecurrencetype));
    }
	       
}

struct icalrecurrencetype
icalvalue_get_recur(icalvalue* value)
{
    icalerror_check_arg( (value!=0),"value");
    icalerror_check_value_type(value, ICAL_RECUR_VALUE);
  
    return *(((struct icalvalue_impl*)value)->data.v_recur);
}




icalvalue*
icalvalue_new_trigger (struct icaltriggertype v)
{
   struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_TRIGGER_VALUE);
 
   icalvalue_set_trigger((icalvalue*)impl,v);

   return (icalvalue*)impl;
}

void
icalvalue_set_trigger(icalvalue* value, struct icaltriggertype v)
{
    struct icalvalue_impl* impl; 
    
    icalerror_check_arg_rv( (value!=0),"value");
    
    impl = (struct icalvalue_impl*)value;

   if(!icaltime_is_null_time(v.time)){
       icalvalue_set_datetime((icalvalue*)impl,v.time);
       impl->kind = ICAL_DATETIME_VALUE;
   } else {
       icalvalue_set_duration((icalvalue*)impl,v.duration);
       impl->kind = ICAL_DURATION_VALUE;
   }

}

struct icaltriggertype
icalvalue_get_trigger(icalvalue* value)
{
    struct icalvalue_impl *impl = (struct icalvalue_impl*)value;
    struct icaltriggertype tr;

    icalerror_check_arg( (value!=0),"value");
    icalerror_check_arg( (value!=0),"value");

    if(impl->kind == ICAL_DATETIME_VALUE){
	 tr.duration = icaldurationtype_from_int(0);
	 tr.time = impl->data.v_time;
    } else if(impl->kind == ICAL_DURATION_VALUE){
	tr.time = icaltime_null_time();
	tr.duration = impl->data.v_duration;
    } else {
	tr.duration = icaldurationtype_from_int(0);
	tr.time = icaltime_null_time();
	icalerror_set_errno(ICAL_BADARG_ERROR);
    }

    return tr;
}

/* DATE-TIME-PERIOD is a special case, and is not auto generated */

icalvalue*
icalvalue_new_datetimeperiod (struct icaldatetimeperiodtype v)
{
   struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_DATETIMEPERIOD_VALUE);

   icalvalue_set_datetimeperiod((icalvalue*)impl,v);

   return (icalvalue*)impl;
}

void
icalvalue_set_datetimeperiod(icalvalue* value, struct icaldatetimeperiodtype v)
{
    struct icalvalue_impl* impl = (struct icalvalue_impl*)value;
    
    icalerror_check_arg_rv( (value!=0),"value");
    
    icalerror_check_value_type(value, ICAL_DATETIMEPERIOD_VALUE);

    if(!icaltime_is_null_time(v.time)){
	if(!icaltime_is_valid_time(v.time)){
	    icalerror_set_errno(ICAL_BADARG_ERROR);
	    return;
	}
	impl->kind = ICAL_DATETIME_VALUE;
	icalvalue_set_datetime(impl,v.time);
    } else if (!icalperiodtype_is_null_period(v.period)) {
	if(!icalperiodtype_is_valid_period(v.period)){
	    icalerror_set_errno(ICAL_BADARG_ERROR);
	    return;
	}
	impl->kind = ICAL_PERIOD_VALUE;
	icalvalue_set_period(impl,v.period);
    } else {
	icalerror_set_errno(ICAL_BADARG_ERROR);
    }
}

struct icaldatetimeperiodtype
icalvalue_get_datetimeperiod(icalvalue* value)
{
  struct icaldatetimeperiodtype dtp;
  
  struct icalvalue_impl* impl = (struct icalvalue_impl*)value;
  icalerror_check_arg( (value!=0),"value");
  icalerror_check_value_type(value, ICAL_DATETIMEPERIOD_VALUE);
  
  if(impl->kind == ICAL_DATETIME_VALUE){
      dtp.period = icalperiodtype_null_period();
      dtp.time = impl->data.v_time;
  } else if(impl->kind == ICAL_PERIOD_VALUE) {
      dtp.period = impl->data.v_period;
      dtp.time = icaltime_null_time();
  } else {
      dtp.period = icalperiodtype_null_period();
      dtp.time = icaltime_null_time();
      icalerror_set_errno(ICAL_BADARG_ERROR);
  }	

  return dtp;
}









/* The remaining interfaces are 'new', 'set' and 'get' for each of the value
   types */


/* Everything below this line is machine generated. Do not edit. */
