Added a build script.
The build script was added to obtain the correct values of the errno C preprocessor definitions. This way no matter what the platform has defined them as, they will be correct in the libraries code.
This commit is contained in:
167
src/c/errno.rs
Normal file
167
src/c/errno.rs
Normal file
@ -0,0 +1,167 @@
|
||||
use weave::Error;
|
||||
use binding::CInt;
|
||||
|
||||
|
||||
|
||||
/// This signifies that there is no current error.
|
||||
/// This can be used for clearing an error.
|
||||
pub const NO_ERROR: CInt = 0;
|
||||
|
||||
/// Include the generated C error values.
|
||||
include!(concat!(env!("OUT_DIR"), "/errno_values.rs"));
|
||||
|
||||
|
||||
|
||||
///
|
||||
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub enum CError
|
||||
{
|
||||
/// There is no Error.
|
||||
None,
|
||||
|
||||
/// Some mathematical functions are only defined
|
||||
/// for certain real values, which is called its domain,
|
||||
/// for example the square root function is only defined for
|
||||
/// non-negative numbers, therefore the sqrt function sets
|
||||
/// errno to EDOM if called with a negative argument.
|
||||
Domain,
|
||||
|
||||
/// The range of values that can be represented
|
||||
/// with a variable is limited. For example, mathematical functions
|
||||
/// such as pow can easily outbound the range representable by a
|
||||
/// floating point variable, or functions such as strtod can
|
||||
/// encounter sequences of digits longer than the range
|
||||
/// representable values. In these cases, errno is set to ERANGE.
|
||||
Range,
|
||||
|
||||
/// Multibyte character sequence may have a
|
||||
/// restricted set of valid sequences. When a set of multibyte
|
||||
/// characters is translated by functions such as mbrtowc,
|
||||
/// errno is set to EILSEQ when an invalid sequence is encountered.
|
||||
IllegalSequence,
|
||||
|
||||
/// There was a detected error, but it
|
||||
/// is not one handled by CError.
|
||||
Unhandled
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl CError
|
||||
{
|
||||
/// Returns if the given value is a handled CError.
|
||||
pub fn is_handled_error(errno: i32) -> bool
|
||||
{
|
||||
let error: CError;
|
||||
|
||||
// Turn the error value into a CError and
|
||||
// check if it is an Unhandled error.
|
||||
error = CError::from(errno);
|
||||
if error != CError::Unhandled
|
||||
{
|
||||
true
|
||||
}
|
||||
else
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Check the last Error that was posted.
|
||||
pub fn get_current_error() -> CError
|
||||
{
|
||||
CError::from(get_errno())
|
||||
}
|
||||
|
||||
/// Set the current Error for the system.
|
||||
pub fn set_current_error(error: CError)
|
||||
{
|
||||
set_errno(error as i32);
|
||||
}
|
||||
|
||||
/// Clear the current Error.
|
||||
pub fn clear()
|
||||
{
|
||||
CError::set_current_error(CError::None);
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for CError
|
||||
{
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
|
||||
{
|
||||
::std::fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for CError
|
||||
{
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
|
||||
{
|
||||
//write!(f, "{}", self.to_str())
|
||||
write!(f, "{}", self.get_description())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for CError
|
||||
{
|
||||
fn from(val: i32) -> CError
|
||||
{
|
||||
match val
|
||||
{
|
||||
NO_ERROR => { CError::None }
|
||||
EDOM => { CError::Domain }
|
||||
ERANGE => { CError::Range }
|
||||
EILSEQ => { CError::IllegalSequence }
|
||||
_ => { CError::Unhandled }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for CError
|
||||
{
|
||||
fn get_description(&self) -> &str
|
||||
{
|
||||
match *self
|
||||
{
|
||||
CError::None => { "There was no error" }
|
||||
CError::Domain => { "Math argument out of domain of function" }
|
||||
CError::Range => { "Math result not representable" }
|
||||
CError::IllegalSequence => { "Illegal byte sequence" }
|
||||
CError::Unhandled => { "Detected an Error not handled by CError" }
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cause(&self) -> Option<&Error>
|
||||
{
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// A helper function for getting the current error.
|
||||
fn get_errno() -> i32
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
*__errno_location() as i32
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper function for setting the current error.
|
||||
fn set_errno(errnum: i32)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
*__errno_location() = errnum as CInt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// The function to get the errno memory location.
|
||||
extern
|
||||
{
|
||||
fn __errno_location() -> *mut CInt;
|
||||
}
|
@ -3,10 +3,12 @@
|
||||
//! this library is as up-to-date as it can be.
|
||||
|
||||
|
||||
mod errno;
|
||||
mod string;
|
||||
mod time;
|
||||
|
||||
|
||||
|
||||
pub use self::errno::*;
|
||||
pub use self::string::*;
|
||||
pub use self::time::*;
|
||||
|
@ -18,6 +18,8 @@ extern crate scribe;
|
||||
#[macro_use]
|
||||
extern crate binding;
|
||||
|
||||
extern crate weave;
|
||||
|
||||
|
||||
|
||||
#[cfg(not(feature="rust_lib"))]
|
||||
|
Reference in New Issue
Block a user