1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A wrapper around LLVM's archive (.a) code
15 use std::ffi::CString;
20 pub struct ArchiveRO { ptr: ArchiveRef }
23 archive: &'a ArchiveRO,
24 ptr: ::ArchiveIteratorRef,
27 pub struct Child<'a> {
28 name: Option<&'a str>,
33 /// Opens a static archive for read-only purposes. This is more optimized
34 /// than the `open` method because it uses LLVM's internal `Archive` class
35 /// rather than shelling out to `ar` for everything.
37 /// If this archive is used with a mutable method, then an error will be
39 pub fn open(dst: &Path) -> Option<ArchiveRO> {
41 let s = path2cstr(dst);
42 let ar = ::LLVMRustOpenArchive(s.as_ptr());
46 Some(ArchiveRO { ptr: ar })
51 fn path2cstr(p: &Path) -> CString {
52 use std::os::unix::prelude::*;
54 let p: &OsStr = p.as_ref();
55 CString::new(p.as_bytes()).unwrap()
58 fn path2cstr(p: &Path) -> CString {
59 CString::new(p.to_str().unwrap()).unwrap()
63 pub fn iter(&self) -> Iter {
65 Iter { ptr: ::LLVMRustArchiveIteratorNew(self.ptr), archive: self }
70 impl Drop for ArchiveRO {
73 ::LLVMRustDestroyArchive(self.ptr);
78 impl<'a> Iterator for Iter<'a> {
79 type Item = Child<'a>;
81 fn next(&mut self) -> Option<Child<'a>> {
83 let ptr = ::LLVMRustArchiveIteratorCurrent(self.ptr);
88 let name_ptr = ::LLVMRustArchiveChildName(ptr, &mut name_len);
90 let data_ptr = ::LLVMRustArchiveChildData(ptr, &mut data_len);
92 name: if name_ptr.is_null() {
95 let name = slice::from_raw_parts(name_ptr as *const u8,
97 str::from_utf8(name).ok().map(|s| s.trim())
99 data: slice::from_raw_parts(data_ptr as *const u8,
102 ::LLVMRustArchiveIteratorNext(self.ptr);
108 impl<'a> Drop for Iter<'a> {
111 ::LLVMRustArchiveIteratorFree(self.ptr);
117 pub fn name(&self) -> Option<&'a str> { self.name }
118 pub fn data(&self) -> &'a [u8] { self.data }