Sfoglia il codice sorgente

ncd: value_utils.h: add ncd_read_time()

ambrop7 13 anni fa
parent
commit
f0b394d70e
1 ha cambiato i file con 21 aggiunte e 0 eliminazioni
  1. 21 0
      ncd/value_utils.h

+ 21 - 0
ncd/value_utils.h

@@ -31,9 +31,11 @@
 #define NCD_VALUE_UTILS_H
 #define NCD_VALUE_UTILS_H
 
 
 #include <stdint.h>
 #include <stdint.h>
+#include <limits.h>
 
 
 #include <misc/debug.h>
 #include <misc/debug.h>
 #include <misc/parse_number.h>
 #include <misc/parse_number.h>
+#include <system/BTime.h>
 #include <ncd/NCDVal.h>
 #include <ncd/NCDVal.h>
 #include <ncd/NCDStringIndex.h>
 #include <ncd/NCDStringIndex.h>
 #include <ncd/static_strings.h>
 #include <ncd/static_strings.h>
@@ -42,6 +44,7 @@ static int ncd_is_none (NCDValRef val);
 static NCDValRef ncd_make_boolean (NCDValMem *mem, int value, NCDStringIndex *string_index);
 static NCDValRef ncd_make_boolean (NCDValMem *mem, int value, NCDStringIndex *string_index);
 static int ncd_read_boolean (NCDValRef val);
 static int ncd_read_boolean (NCDValRef val);
 static int ncd_read_uintmax (NCDValRef string, uintmax_t *out) WARN_UNUSED;
 static int ncd_read_uintmax (NCDValRef string, uintmax_t *out) WARN_UNUSED;
+static int ncd_read_time (NCDValRef string, btime_t *out) WARN_UNUSED;
 static NCD_string_id_t ncd_get_string_id (NCDValRef string, NCDStringIndex *string_index);
 static NCD_string_id_t ncd_get_string_id (NCDValRef string, NCDStringIndex *string_index);
 static NCDValRef ncd_make_uintmax (NCDValMem *mem, uintmax_t value);
 static NCDValRef ncd_make_uintmax (NCDValMem *mem, uintmax_t value);
 
 
@@ -84,6 +87,24 @@ static int ncd_read_uintmax (NCDValRef string, uintmax_t *out)
     return parse_unsigned_integer_bin(NCDVal_StringValue(string), NCDVal_StringLength(string), out);
     return parse_unsigned_integer_bin(NCDVal_StringValue(string), NCDVal_StringLength(string), out);
 }
 }
 
 
+static int ncd_read_time (NCDValRef string, btime_t *out)
+{
+    ASSERT(NCDVal_IsString(string))
+    ASSERT(out)
+    
+    uintmax_t n;
+    if (!ncd_read_uintmax(string, &n)) {
+        return 0;
+    }
+    
+    if (n > INT64_MAX) {
+        return 0;
+    }
+    
+    *out = n;
+    return 1;
+}
+
 static NCD_string_id_t ncd_get_string_id (NCDValRef string, NCDStringIndex *string_index)
 static NCD_string_id_t ncd_get_string_id (NCDValRef string, NCDStringIndex *string_index)
 {
 {
     ASSERT(NCDVal_IsString(string))
     ASSERT(NCDVal_IsString(string))