add parse to f64/f32
This commit is contained in:
parent
8cdfe4829b
commit
02b55a6f35
32
rtcore.h
32
rtcore.h
@ -260,11 +260,25 @@ typedef struct
|
||||
i32 i;
|
||||
b32 ok;
|
||||
} s8_parse_i32_result;
|
||||
typedef struct
|
||||
{
|
||||
f64 f;
|
||||
b32 ok;
|
||||
} s8_parse_f64_result;
|
||||
typedef struct
|
||||
{
|
||||
f32 f;
|
||||
b32 ok;
|
||||
} s8_parse_f32_result;
|
||||
|
||||
/* Parses a integer from string s */
|
||||
RTC_API s8_parse_i64_result S8ParseI64(s8 s, int base);
|
||||
/* Parses an integer from string s */
|
||||
RTC_API s8_parse_i32_result S8ParseI32(s8 s, int base);
|
||||
/* Parses a float from string s */
|
||||
RTC_API s8_parse_f64_result S8ParseF64(s8 s);
|
||||
/* Parses a float from string s */
|
||||
RTC_API s8_parse_f32_result S8ParseF32(s8 s);
|
||||
|
||||
/* Basic file io */
|
||||
typedef struct file_buffer
|
||||
@ -415,6 +429,8 @@ RTC_API THREAD_RETURN_TYPE JoinThread(thread *t);
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <pthread.h>
|
||||
@ -628,6 +644,22 @@ S8ParseI32(s8 s, int base)
|
||||
.ok = 1,
|
||||
};
|
||||
}
|
||||
RTC_API s8_parse_f64_result
|
||||
S8ParseF64(s8 s)
|
||||
{
|
||||
f64 f = strtod((const char *)s.data, NULL);
|
||||
if (errno == ERANGE)
|
||||
return (s8_parse_f64_result){.f = 0.0, .ok = 0};
|
||||
return (s8_parse_f64_result){.f = f, .ok = 1};
|
||||
}
|
||||
RTC_API s8_parse_f32_result
|
||||
S8ParseF32(s8 s)
|
||||
{
|
||||
f32 f = strtof((const char *)s.data, NULL);
|
||||
if (errno == ERANGE)
|
||||
return (s8_parse_f32_result){.f = 0.0, .ok = 0};
|
||||
return (s8_parse_f32_result){.f = f, .ok = 1};
|
||||
}
|
||||
|
||||
/* Basic file io */
|
||||
RTC_API file_buffer
|
||||
|
||||
Loading…
Reference in New Issue
Block a user