|
| 1 | +use std::ptr::{self, NonNull}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + ffi::{ |
| 5 | + php_register_url_stream_wrapper, php_register_url_stream_wrapper_volatile, php_stream, |
| 6 | + php_stream_context, php_stream_locate_url_wrapper, php_stream_wrapper, |
| 7 | + php_stream_wrapper_ops, php_unregister_url_stream_wrapper, |
| 8 | + php_unregister_url_stream_wrapper_volatile, zend_string, |
| 9 | + }, |
| 10 | + types::ZendStr, |
| 11 | +}; |
| 12 | + |
| 13 | +pub type StreamWrapper = php_stream_wrapper; |
| 14 | + |
| 15 | +pub type StreamOpener = unsafe extern "C" fn( |
| 16 | + *mut StreamWrapper, |
| 17 | + *const std::ffi::c_char, |
| 18 | + *const std::ffi::c_char, |
| 19 | + i32, |
| 20 | + *mut *mut zend_string, |
| 21 | + *mut php_stream_context, |
| 22 | + i32, |
| 23 | + *const std::ffi::c_char, |
| 24 | + u32, |
| 25 | + *const std::ffi::c_char, |
| 26 | + u32, |
| 27 | +) -> *mut Stream; |
| 28 | + |
| 29 | +impl StreamWrapper { |
| 30 | + pub fn get(name: &str) -> Option<&Self> { |
| 31 | + unsafe { |
| 32 | + let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0); |
| 33 | + Some(NonNull::new(result)?.as_ref()) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + pub fn get_mut(name: &str) -> Option<&mut Self> { |
| 38 | + unsafe { |
| 39 | + let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0); |
| 40 | + Some(NonNull::new(result)?.as_mut()) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn register(self, name: &str) -> Result<Self, ()> { |
| 45 | + // We have to convert it to a static so owned streamwrapper doesn't get dropped. |
| 46 | + let copy = Box::new(self); |
| 47 | + let copy = Box::leak(copy); |
| 48 | + let name = std::ffi::CString::new(name).unwrap(); |
| 49 | + let result = unsafe { php_register_url_stream_wrapper(name.as_ptr(), copy) }; |
| 50 | + if result == 0 { |
| 51 | + Ok(*copy) |
| 52 | + } else { |
| 53 | + Err(()) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + pub fn register_volatile(self, name: &str) -> Result<Self, ()> { |
| 58 | + // We have to convert it to a static so owned streamwrapper doesn't get dropped. |
| 59 | + let copy = Box::new(self); |
| 60 | + let copy = Box::leak(copy); |
| 61 | + let name = ZendStr::new(name, false); |
| 62 | + let result = |
| 63 | + unsafe { php_register_url_stream_wrapper_volatile((*name).as_ptr() as _, copy) }; |
| 64 | + if result == 0 { |
| 65 | + Ok(*copy) |
| 66 | + } else { |
| 67 | + Err(()) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + pub fn unregister(name: &str) -> Result<(), ()> { |
| 72 | + let name = std::ffi::CString::new(name).unwrap(); |
| 73 | + match unsafe { php_unregister_url_stream_wrapper(name.as_ptr()) } { |
| 74 | + 0 => Ok(()), |
| 75 | + _ => Err(()), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn unregister_volatile(name: &str) -> Result<(), ()> { |
| 80 | + let name = ZendStr::new(name, false); |
| 81 | + match unsafe { php_unregister_url_stream_wrapper_volatile((*name).as_ptr() as _) } { |
| 82 | + 0 => Ok(()), |
| 83 | + _ => Err(()), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + pub fn wops(&self) -> &php_stream_wrapper_ops { |
| 88 | + unsafe { &*self.wops } |
| 89 | + } |
| 90 | + |
| 91 | + pub fn wops_mut(&mut self) -> &mut php_stream_wrapper_ops { |
| 92 | + unsafe { &mut *(self.wops as *mut php_stream_wrapper_ops) } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub type Stream = php_stream; |
| 97 | + |
| 98 | +pub type StreamWrapperOps = php_stream_wrapper_ops; |
| 99 | + |
| 100 | +impl StreamWrapperOps {} |
0 commit comments