/**
 * Loads a library and returns it's instance.
 *
 * @param string	The library name/path
 * @param array		The configuration
 * @return object
 */
function Lib($library, $config = null)
{
    static $libraries = array();
    if ($library === false) {
        // init default loaded classes here
        $classes = array('config' => 'config', 'input' => 'input', 'benchmark' => 'benchmark', 'uri' => 'uri', 'output' => 'output', 'lang' => 'lang', 'router' => 'router');
        $CI =& get_instance();
        foreach ($classes as $var) {
            $libraries[$var] =& $CI->{$var};
        }
        $libraries['load'] =& $CI->load;
        $libraries['loader'] =& $CI->load;
        return null;
    }
    $library = strtolower($library);
    // already loaded?
    if (isset($libraries[$library])) {
        return $libraries[$library];
    }
    if (!($res = Finder::load_file($library, 'libraries'))) {
        log_message('error', "Unable to load the requested class file: libraries/" . $library);
        show_error("Unable to load the requested class file: libraries/" . $library);
    }
    if ($config === null) {
        foreach (Finder::get_config_file($library) as $c) {
            include_once $c;
        }
    }
    if (($p = strrpos($library, '/')) !== false) {
        $library = substr($library, $p + 1);
    }
    if ($res === 'SUBCLASS') {
        $name = Finder::$subclass_prefix . $library;
    } else {
        if (class_exists('CI_' . $library)) {
            $name = 'CI_' . $library;
        } else {
            $name = $library;
        }
    }
    // Is the class name valid?
    if (!class_exists($name)) {
        log_message('error', "Non-existent class: " . $name);
        show_error("Non-existent class: " . $name);
    }
    if ($config !== null) {
        $libraries[$library] = new $name($config);
    } else {
        $libraries[$library] = new $name();
    }
    return $libraries[$library];
}