<commit_before>// Copyright 2013 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.

/*!

Dynamic library facilities.

A simple wrapper over the platform's dynamic library facilities

*/

use c_str::ToCStr;
use iter::Iterator;
use mem;
use ops::*;
use option::*;
use os;
use path::GenericPath;
use path;
use result::*;
use slice::{Vector,OwnedVector};
use str;
use vec::Vec;

pub struct DynamicLibrary { handle: *u8}

impl Drop for DynamicLibrary {
    fn drop(&mut self) {
        match dl::check_for_errors_in(|| {
            unsafe {
                dl::close(self.handle)
            }
        }) {
            Ok(()) => {},
            Err(str) => fail!("{}", str)
        }
    }
}

impl DynamicLibrary {
    /// Lazily open a dynamic library. When passed None it gives a
    /// handle to the calling process
    pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
        unsafe {
            let maybe_library = dl::check_for_errors_in(|| {
                match filename {
                    Some(name) => dl::open_external(name),
                    None => dl::open_internal()
                }
            });

            // The dynamic library must not be constructed if there is
            // an error opening the library so the destructor does not
            // run.
            match maybe_library {
                Err(err) => Err(err),
                Ok(handle) => Ok(DynamicLibrary { handle: handle })
            }
        }
    }

    /// Appends a path to the system search path for dynamic libraries
    pub fn add_search_path(path: &path::Path) {
        let (envvar, sep) = if cfg!(windows) {
            ("PATH", ';' as u8)
        } else if cfg!(target_os = "macos") {
            ("DYLD_LIBRARY_PATH", ':' as u8)
        } else {
            ("LD_LIBRARY_PATH", ':' as u8)
        };
        let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
        let mut newenv = newenv.move_iter().collect::<Vec<_>>();
        newenv.push_all(&[sep]);
        newenv.push_all(path.as_vec());
        os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
    }

    /// Access the value at the symbol of the dynamic library
    pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<T, ~str> {
        // This function should have a lifetime constraint of 'a on
        // T but that feature is still unimplemented

        let maybe_symbol_value = dl::check_for_errors_in(|| {
            symbol.with_c_str(|raw_string| {
                dl::symbol(self.handle, raw_string)
            })
        });

        // The value must not be constructed if there is an error so<commit_msg>std:: switch the order in which dynamic_lib adds search paths.<commit_after>// Copyright 2013 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.

/*!

Dynamic library facilities.

A simple wrapper over the platform's dynamic library facilities

*/


use c_str::ToCStr;
use mem;
use ops::*;
use option::*;
use os;
use path::GenericPath;
use path;
use result::*;
use slice::Vector;
use str;
use vec::Vec;

pub struct DynamicLibrary { handle: *u8}

impl Drop for DynamicLibrary {
    fn drop(&mut self) {
        match dl::check_for_errors_in(|| {
            unsafe {
                dl::close(self.handle)
            }
        }) {
            Ok(()) => {},
            Err(str) => fail!("{}", str)
        }
    }
}

impl DynamicLibrary {
    /// Lazily open a dynamic library. When passed None it gives a
    /// handle to the calling process
    pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
        unsafe {
            let maybe_library = dl::check_for_errors_in(|| {
                match filename {
                    Some(name) => dl::open_external(name),
                    None => dl::open_internal()
                }
            });

            // The dynamic library must not be constructed if there is
            // an error opening the library so the destructor does not
            // run.
            match maybe_library {
                Err(err) => Err(err),
                Ok(handle) => Ok(DynamicLibrary { handle: handle })
            }
        }
    }

    /// Appends a path to the system search path for dynamic libraries
    pub fn add_search_path(path: &path::Path) {
        let (envvar, sep) = if cfg!(windows) {
            ("PATH", ';' as u8)
        } else if cfg!(target_os = "macos") {
            ("DYLD_LIBRARY_PATH", ':' as u8)
        } else {
            ("LD_LIBRARY_PATH", ':' as u8)
        };
        let mut newenv = Vec::from_slice(path.as_vec());
        newenv.push(sep);
        match os::getenv_as_bytes(envvar) {
            Some(bytes) => newenv.push_all(bytes),
            None => {}
        }
        os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
    }

    /// Access the value at the symbol of the dynamic library
    pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<T, ~str> {
        // This function should have a lifetime constraint of 'a on
        // T but that feature is still unimplemented

        let maybe_symbol_value = dl::check_for_errors_in(|| {
            symbol.with_c_str(|raw_string| {
                dl::symbol(self.handle, raw_string)
            })
        });

        // The value must not be constructed if there is an error so
{'fraction_non_alphanumeric': 0.12379394930498773, 'fraction_numerical': 0.004251839738348324, 'mean_word_length': 2.0967088607594935, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6796178', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1737, 'n_words': 551}