struct cxpr cxtrunc (struct cxpr z) { struct cxpr w; w.re = xtrunc (z.re); w.im = xtrunc (z.im); return w; }
static uint64_t NGS_StringToU64( const NGS_String * str, ctx_t ctx ) { /* have to guarantee NUL-termination for strtou64/strtod */ char buf[4096]; if ( sizeof(buf) > NGS_StringSize ( str, ctx ) ) { char* end; uint64_t value; string_copy ( buf, sizeof(buf), NGS_StringData ( str, ctx ), NGS_StringSize ( str, ctx ) ); errno = 0; value = strtou64 ( buf, &end, 10 ); if ( *end == 0 ) { if ( errno == 0 ) { return value; } } else { /* attempt to parse as a double */ double dbl; errno = 0; dbl = strtod ( buf, &end ); if ( *end == 0 && errno == 0 && dbl >= 0 && dbl <= ULLONG_MAX ) { return ( uint64_t ) xtrunc ( dbl ); } } } INTERNAL_ERROR ( xcUnexpected, "cannot convert dictionary value '%.*s' from string to uint64", NGS_StringSize ( str, ctx ), NGS_StringData ( str, ctx ) ); return 0; }
uint64_t SRA_StatisticsGetAsU64 ( const SRA_Statistics * self, ctx_t ctx, const char * path ) { FUNC_ENTRY ( ctx, rcSRA, rcDatabase, rcAccessing ); assert ( self ); if ( path == NULL ) INTERNAL_ERROR ( xcParamNull, "path is NULL" ); else { DictionaryEntry * node = ( DictionaryEntry * ) BSTreeFind ( & self -> dictionary, ( const void * ) path, DictionaryEntryFind ); if ( node == NULL ) { INTERNAL_ERROR ( xcUnexpected, "dictionary item '%s' is not found", path ); } else { switch ( node -> type ) { case NGS_StatisticValueType_Int64: if ( node -> value . i64 < 0 ) { INTERNAL_ERROR ( xcUnexpected, "cannot convert dictionary item '%s' from in64_t to uint64_t", path ); } else { return ( uint64_t ) node -> value . i64; } break; case NGS_StatisticValueType_UInt64: return node -> value . i64; case NGS_StatisticValueType_Real: if ( node -> value . real < 0 || node -> value . real > ULLONG_MAX ) { INTERNAL_ERROR ( xcUnexpected, "cannot convert dictionary item '%s' from double to uint64_t", path ); } else { return ( uint64_t ) xtrunc ( node -> value . real ); } break; case NGS_StatisticValueType_String: return NGS_StringToU64 ( node -> value . str, ctx ); default : INTERNAL_ERROR ( xcUnexpected, "unexpected type %u for dictionary item '%s'", node -> type, path ); break; } } } return 0; }