Skip to content
Snippets Groups Projects
Verified Commit ee40035c authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Add asprintf

parent 63b079c2
No related branches found
No related tags found
No related merge requests found
Pipeline #2159 passed with warnings
...@@ -9,6 +9,7 @@ typedef struct FILE FILE; ...@@ -9,6 +9,7 @@ typedef struct FILE FILE;
extern "C" { extern "C" {
#endif #endif
int asprintf(char **strp, const char * fmt, ...);
int fprintf(FILE * stream, const char * fmt, ...); int fprintf(FILE * stream, const char * fmt, ...);
int printf(const char * fmt, ...); int printf(const char * fmt, ...);
int snprintf(char *s, size_t n, const char * fmt, ...); int snprintf(char *s, size_t n, const char * fmt, ...);
......
#include <openlibm_math.h> #include <openlibm_math.h>
// Included to fix mesa issues
#define M_PI_2 (M_PI/2.0)
#define M_PI_4 (M_PI/4.0)
...@@ -3,6 +3,17 @@ ...@@ -3,6 +3,17 @@
typedef struct FILE FILE; typedef struct FILE FILE;
int vasprintf(char ** strp, const char * fmt, va_list ap);
int asprintf(char ** strp, const char * fmt, ...) {
int ret;
va_list ap;
va_start(ap, fmt);
ret = vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
int vfprintf(FILE * stream, const char * fmt, va_list ap); int vfprintf(FILE * stream, const char * fmt, va_list ap);
int fprintf(FILE * stream, const char * fmt, ...) { int fprintf(FILE * stream, const char * fmt, ...) {
......
...@@ -920,6 +920,14 @@ pub unsafe extern "C" fn vprintf(format: *const c_char, ap: va_list) -> c_int { ...@@ -920,6 +920,14 @@ pub unsafe extern "C" fn vprintf(format: *const c_char, ap: va_list) -> c_int {
vfprintf(&mut *stdout, format, ap) vfprintf(&mut *stdout, format, ap)
} }
#[no_mangle]
pub unsafe extern "C" fn vasprintf(strp: *mut *mut c_char, format: *const c_char, ap: va_list) -> c_int {
let mut alloc_writer = platform::AllocStringWriter(ptr::null_mut(), 0);
let ret = printf::printf(&mut alloc_writer, format, ap);
*strp = alloc_writer.0 as *mut c_char;
ret
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn vsnprintf( pub unsafe extern "C" fn vsnprintf(
s: *mut c_char, s: *mut c_char,
......
...@@ -99,6 +99,47 @@ impl Read for FileReader { ...@@ -99,6 +99,47 @@ impl Read for FileReader {
} }
} }
pub struct AllocStringWriter(pub *mut u8, pub usize);
impl Write for AllocStringWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let ptr = unsafe {
realloc(self.0 as *mut c_void, self.1 + buf.len() + 1) as *mut u8
};
if ptr.is_null() {
return Err(io::Error::new(
io::ErrorKind::Other,
"AllocStringWriter::write failed to allocate"
));
}
self.0 = ptr;
unsafe {
ptr::copy_nonoverlapping(buf.as_ptr(), self.0.add(self.1), buf.len());
self.1 += buf.len();
*self.0.add(self.1) = 0;
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl fmt::Write for AllocStringWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
// can't fail
self.write(s.as_bytes()).unwrap();
Ok(())
}
}
impl WriteByte for AllocStringWriter {
fn write_u8(&mut self, byte: u8) -> fmt::Result {
// can't fail
self.write(&[byte]).unwrap();
Ok(())
}
}
pub struct StringWriter(pub *mut u8, pub usize); pub struct StringWriter(pub *mut u8, pub usize);
impl Write for StringWriter { impl Write for StringWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment