summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c33
1 files changed, 11 insertions, 22 deletions
diff --git a/src/plist.c b/src/plist.c
index ef1d7a2..af64ed1 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -27,6 +27,7 @@
27#include "plist.h" 27#include "plist.h"
28#include <stdlib.h> 28#include <stdlib.h>
29#include <stdio.h> 29#include <stdio.h>
30#include <math.h>
30 31
31#include <node.h> 32#include <node.h>
32#include <node_iterator.h> 33#include <node_iterator.h>
@@ -214,9 +215,8 @@ PLIST_API plist_t plist_new_date(int32_t sec, int32_t usec)
214{ 215{
215 plist_data_t data = plist_new_plist_data(); 216 plist_data_t data = plist_new_plist_data();
216 data->type = PLIST_DATE; 217 data->type = PLIST_DATE;
217 data->timeval.tv_sec = sec; 218 data->realval = (double)sec + (double)usec / 1000000;
218 data->timeval.tv_usec = usec; 219 data->length = sizeof(double);
219 data->length = sizeof(struct timeval);
220 return plist_new_node(data); 220 return plist_new_node(data);
221} 221}
222 222
@@ -567,6 +567,7 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
567 *((uint64_t *) value) = data->intval; 567 *((uint64_t *) value) = data->intval;
568 break; 568 break;
569 case PLIST_REAL: 569 case PLIST_REAL:
570 case PLIST_DATE:
570 *((double *) value) = data->realval; 571 *((double *) value) = data->realval;
571 break; 572 break;
572 case PLIST_KEY: 573 case PLIST_KEY:
@@ -577,11 +578,6 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
577 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t)); 578 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t));
578 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t)); 579 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t));
579 break; 580 break;
580 case PLIST_DATE:
581 //exception : here we use memory on the stack since it is just a temporary buffer
582 ((struct timeval*) value)->tv_sec = data->timeval.tv_sec;
583 ((struct timeval*) value)->tv_usec = data->timeval.tv_usec;
584 break;
585 case PLIST_ARRAY: 581 case PLIST_ARRAY:
586 case PLIST_DICT: 582 case PLIST_DICT:
587 default: 583 default:
@@ -670,12 +666,12 @@ PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
670{ 666{
671 plist_type type = plist_get_node_type(node); 667 plist_type type = plist_get_node_type(node);
672 uint64_t length = 0; 668 uint64_t length = 0;
673 struct timeval val = { 0, 0 }; 669 double val = 0;
674 if (PLIST_DATE == type) 670 if (PLIST_DATE == type)
675 plist_get_type_and_value(node, &type, (void *) &val, &length); 671 plist_get_type_and_value(node, &type, (void *) &val, &length);
676 assert(length == sizeof(struct timeval)); 672 assert(length == sizeof(double));
677 *sec = val.tv_sec; 673 *sec = (int32_t)val;
678 *usec = val.tv_usec; 674 *usec = (int32_t)fabs((val - (int64_t)val) * 1000000);
679} 675}
680 676
681int plist_data_compare(const void *a, const void *b) 677int plist_data_compare(const void *a, const void *b)
@@ -700,6 +696,7 @@ int plist_data_compare(const void *a, const void *b)
700 case PLIST_BOOLEAN: 696 case PLIST_BOOLEAN:
701 case PLIST_UINT: 697 case PLIST_UINT:
702 case PLIST_REAL: 698 case PLIST_REAL:
699 case PLIST_DATE:
703 case PLIST_UID: 700 case PLIST_UID:
704 if (val_a->length != val_b->length) 701 if (val_a->length != val_b->length)
705 return FALSE; 702 return FALSE;
@@ -730,11 +727,6 @@ int plist_data_compare(const void *a, const void *b)
730 else 727 else
731 return FALSE; 728 return FALSE;
732 break; 729 break;
733 case PLIST_DATE:
734 if (!memcmp(&(val_a->timeval), &(val_b->timeval), sizeof(struct timeval)))
735 return TRUE;
736 else
737 return FALSE;
738 default: 730 default:
739 break; 731 break;
740 } 732 }
@@ -782,6 +774,7 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val
782 data->intval = *((uint64_t *) value); 774 data->intval = *((uint64_t *) value);
783 break; 775 break;
784 case PLIST_REAL: 776 case PLIST_REAL:
777 case PLIST_DATE:
785 data->realval = *((double *) value); 778 data->realval = *((double *) value);
786 break; 779 break;
787 case PLIST_KEY: 780 case PLIST_KEY:
@@ -792,10 +785,6 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val
792 data->buff = (uint8_t *) malloc(length); 785 data->buff = (uint8_t *) malloc(length);
793 memcpy(data->buff, value, length); 786 memcpy(data->buff, value, length);
794 break; 787 break;
795 case PLIST_DATE:
796 data->timeval.tv_sec = ((struct timeval*) value)->tv_sec;
797 data->timeval.tv_usec = ((struct timeval*) value)->tv_usec;
798 break;
799 case PLIST_ARRAY: 788 case PLIST_ARRAY:
800 case PLIST_DICT: 789 case PLIST_DICT:
801 default: 790 default:
@@ -840,7 +829,7 @@ PLIST_API void plist_set_data_val(plist_t node, const char *val, uint64_t length
840 829
841PLIST_API void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) 830PLIST_API void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
842{ 831{
843 struct timeval val = { sec, usec }; 832 double val = (double)sec + (double)usec / 1000000;
844 plist_set_element_val(node, PLIST_DATE, &val, sizeof(struct timeval)); 833 plist_set_element_val(node, PLIST_DATE, &val, sizeof(struct timeval));
845} 834}
846 835