2013-08-03 19:13:14 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use cast;
|
|
|
|
use iterator::Iterator;
|
|
|
|
use libc;
|
|
|
|
use ops::Drop;
|
|
|
|
use option::{Option, Some, None};
|
|
|
|
use ptr::RawPtr;
|
|
|
|
use ptr;
|
|
|
|
use str::StrSlice;
|
|
|
|
use vec::ImmutableVector;
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// The representation of a C String.
|
|
|
|
///
|
|
|
|
/// This structure wraps a `*libc::c_char`, and will automatically free the
|
|
|
|
/// memory it is pointing to when it goes out of scope.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub struct CString {
|
|
|
|
priv buf: *libc::c_char,
|
2013-08-04 20:37:55 -05:00
|
|
|
priv owns_buffer_: bool,
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:55:07 -05:00
|
|
|
impl CString {
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Create a C String from a pointer.
|
2013-08-06 23:11:10 -05:00
|
|
|
pub unsafe fn new(buf: *libc::c_char, owns_buffer: bool) -> CString {
|
2013-08-04 20:37:55 -05:00
|
|
|
CString { buf: buf, owns_buffer_: owns_buffer }
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
|
|
|
|
pub unsafe fn unwrap(self) -> *libc::c_char {
|
|
|
|
let mut c_str = self;
|
|
|
|
c_str.owns_buffer_ = false;
|
|
|
|
c_str.buf
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Calls a closure with a reference to the underlying `*libc::c_char`.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if the CString is null.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub fn with_ref<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
2013-08-04 20:37:55 -05:00
|
|
|
if self.buf.is_null() { fail!("CString is null!"); }
|
2013-08-03 19:13:14 -05:00
|
|
|
f(self.buf)
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Calls a closure with a mutable reference to the underlying `*libc::c_char`.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if the CString is null.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub fn with_mut_ref<T>(&mut self, f: &fn(*mut libc::c_char) -> T) -> T {
|
2013-08-04 20:37:55 -05:00
|
|
|
if self.buf.is_null() { fail!("CString is null!"); }
|
2013-08-06 23:08:39 -05:00
|
|
|
f(unsafe { cast::transmute_mut_unsafe(self.buf) })
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Returns true if the CString is a null.
|
|
|
|
pub fn is_null(&self) -> bool {
|
2013-08-03 19:13:14 -05:00
|
|
|
self.buf.is_null()
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Returns true if the CString is not null.
|
|
|
|
pub fn is_not_null(&self) -> bool {
|
2013-08-03 19:13:14 -05:00
|
|
|
self.buf.is_not_null()
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Returns whether or not the `CString` owns the buffer.
|
|
|
|
pub fn owns_buffer(&self) -> bool {
|
|
|
|
self.owns_buffer_
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the CString into a `&[u8]` without copying.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if the CString is null.
|
2013-08-05 21:55:07 -05:00
|
|
|
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
|
2013-08-04 20:37:55 -05:00
|
|
|
if self.buf.is_null() { fail!("CString is null!"); }
|
2013-08-03 19:13:14 -05:00
|
|
|
unsafe {
|
|
|
|
let len = libc::strlen(self.buf) as uint;
|
|
|
|
cast::transmute((self.buf, len + 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Return a CString iterator.
|
2013-08-05 21:55:07 -05:00
|
|
|
fn iter<'a>(&'a self) -> CStringIterator<'a> {
|
2013-08-03 19:13:14 -05:00
|
|
|
CStringIterator {
|
|
|
|
ptr: self.buf,
|
|
|
|
lifetime: unsafe { cast::transmute(self.buf) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for CString {
|
|
|
|
fn drop(&self) {
|
2013-08-06 23:08:39 -05:00
|
|
|
if self.owns_buffer_ {
|
2013-08-03 19:13:14 -05:00
|
|
|
unsafe {
|
|
|
|
libc::free(self.buf as *libc::c_void)
|
2013-08-04 20:37:55 -05:00
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// A generic trait for converting a value to a CString.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub trait ToCStr {
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Create a C String.
|
2013-08-03 19:13:14 -05:00
|
|
|
fn to_c_str(&self) -> CString;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> ToCStr for &'self str {
|
2013-08-04 20:37:55 -05:00
|
|
|
#[inline]
|
2013-08-03 19:13:14 -05:00
|
|
|
fn to_c_str(&self) -> CString {
|
|
|
|
self.as_bytes().to_c_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> ToCStr for &'self [u8] {
|
|
|
|
fn to_c_str(&self) -> CString {
|
|
|
|
do self.as_imm_buf |self_buf, self_len| {
|
|
|
|
unsafe {
|
2013-08-06 23:19:38 -05:00
|
|
|
let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
|
2013-08-03 19:13:14 -05:00
|
|
|
if buf.is_null() {
|
|
|
|
fail!("failed to allocate memory!");
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr::copy_memory(buf, self_buf, self_len);
|
|
|
|
*ptr::mut_offset(buf, self_len as int) = 0;
|
2013-08-04 20:37:55 -05:00
|
|
|
|
|
|
|
CString::new(buf as *libc::c_char, true)
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// External iterator for a CString's bytes.
|
|
|
|
///
|
|
|
|
/// Use with the `std::iterator` module.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub struct CStringIterator<'self> {
|
|
|
|
priv ptr: *libc::c_char,
|
|
|
|
priv lifetime: &'self libc::c_char, // FIXME: #5922
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Iterator<libc::c_char> for CStringIterator<'self> {
|
|
|
|
fn next(&mut self) -> Option<libc::c_char> {
|
2013-08-06 23:06:12 -05:00
|
|
|
let ch = unsafe { *self.ptr };
|
|
|
|
if ch == 0 {
|
2013-08-03 19:13:14 -05:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.ptr = ptr::offset(self.ptr, 1);
|
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use libc;
|
|
|
|
use ptr;
|
2013-08-06 23:06:12 -05:00
|
|
|
use option::{Some, None};
|
2013-08-03 19:13:14 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_c_str() {
|
|
|
|
do "".to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do "hello".to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 5), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_is_null() {
|
2013-08-06 23:11:10 -05:00
|
|
|
let c_str = unsafe { CString::new(ptr::null(), false) };
|
2013-08-04 20:37:55 -05:00
|
|
|
assert!(c_str.is_null());
|
|
|
|
assert!(!c_str.is_not_null());
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_unwrap() {
|
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
unsafe { libc::free(c_str.unwrap() as *libc::c_void) }
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_with_ref() {
|
2013-08-03 19:13:14 -05:00
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
let len = unsafe { c_str.with_ref(|buf| libc::strlen(buf)) };
|
2013-08-04 20:37:55 -05:00
|
|
|
assert!(!c_str.is_null());
|
|
|
|
assert!(c_str.is_not_null());
|
2013-08-03 19:13:14 -05:00
|
|
|
assert_eq!(len, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[ignore(cfg(windows))]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_with_ref_empty_fail() {
|
2013-08-06 23:06:12 -05:00
|
|
|
let c_str = unsafe { CString::new(ptr::null(), false) };
|
2013-08-03 19:13:14 -05:00
|
|
|
c_str.with_ref(|_| ());
|
|
|
|
}
|
2013-08-06 23:06:12 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
|
|
|
let c_str = "".to_c_str();
|
|
|
|
let mut iter = c_str.iter();
|
|
|
|
assert_eq!(iter.next(), None);
|
|
|
|
|
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
let mut iter = c_str.iter();
|
|
|
|
assert_eq!(iter.next(), Some('h' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('e' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('l' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('l' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('o' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), None);
|
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|