package com.axis.innovators.box.tools;

/**
 * 在程序链接库文件中加载指定链接库
 * @author tzdwindows 7
 */
public class LibraryLoad {
    public static final String LIBRARY_NAME = FolderCreator.getLibraryFolder() + "\\";

    /**
     * 加载链接库
     * @param libraryName 链接库名称
     */
    public static void loadLibrary(String libraryName) {
        String libraryWithExtension = addLibraryExtension(libraryName);
        System.load(LIBRARY_NAME + libraryWithExtension);
    }

    /**
     * 判断并添加正确的库文件后缀
     * @param libraryName 链接库名称
     * @return 带有后缀名的库文件名称
     */
    private static String addLibraryExtension(String libraryName) {
        String os = System.getProperty("os.name").toLowerCase();

        if (libraryName.toLowerCase().endsWith(".dll")
            || libraryName.toLowerCase().endsWith(".so")) {
            return libraryName;
        }

        if (os.contains("win")) {
            return libraryName + ".dll";
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
            return libraryName + ".so";
        } else {
            return libraryName;
        }
    }
}
