From: u6k.yu1 Date: Sat, 30 Oct 2010 12:22:19 +0000 (+0900) Subject: 初期インポート X-Git-Url: http://git.osdn.net/view?p=u6kcommons%2Fbackup.git;a=commitdiff_plain;h=HEAD 初期インポート --- ed2f633851a111997356c7ee3d09b4fcd490e6f1 diff --git a/backup/.classpath b/backup/.classpath new file mode 100644 index 0000000..1e6d54b --- /dev/null +++ b/backup/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/backup/.project b/backup/.project new file mode 100644 index 0000000..20def52 --- /dev/null +++ b/backup/.project @@ -0,0 +1,17 @@ + + + backup + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/backup/src/main/java/Main.java b/backup/src/main/java/Main.java new file mode 100644 index 0000000..22b15f3 --- /dev/null +++ b/backup/src/main/java/Main.java @@ -0,0 +1,13 @@ +import java.io.File; + +import jp.gr.java_conf.u6k.backup.BackupRepository; + +public class Main { + + public static void main(String[] args) throws Exception { + // BackupLogRepository backupLogRepo = new BackupLogRepository(new File("D:/temp/bk"), new File("D:/game")); + BackupRepository backupLogRepo = new BackupRepository(new File("D:/temp/bk")); + backupLogRepo.backup(); + } + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/BackupLog.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/BackupLog.java new file mode 100644 index 0000000..a3ef4e2 --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/BackupLog.java @@ -0,0 +1,137 @@ + +package jp.gr.java_conf.u6k.backup; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; + +public class BackupLog { + + private int revision; + + private long backupTime; + + private List diffInfoList = new ArrayList(); + + public BackupLog() { + } + + public BackupLog(File logFile) throws ParserConfigurationException, SAXException, IOException { + // 引数を確認します。 + if (logFile == null) { + throw new NullPointerException("logFile == null"); + } + // ログ・ファイルを読み込みます。 + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(logFile); + + Element logElement = doc.getDocumentElement(); + this.revision = Integer.parseInt(logElement.getAttribute("revision")); + this.backupTime = Long.parseLong(logElement.getAttribute("time")); + + for (int i = 0; i < logElement.getChildNodes().getLength(); i++) { + Node n = logElement.getChildNodes().item(i); + if ("add".equals(n.getNodeName()) || "delete".equals(n.getNodeName()) || "modify".equals(n.getNodeName())) { + Element ele = (Element) n; + + String path = ele.getAttribute("path"); + long lastModified = Long.parseLong(ele.getAttribute("lastModified")); + long size = Long.parseLong(ele.getAttribute("size")); + String hash = ele.getAttribute("hash"); + boolean file = Boolean.parseBoolean(ele.getAttribute("file")); + DiffType type; + if ("add".equals(ele.getNodeName())) { + type = DiffType.ADDED; + } else if ("delete".equals(ele.getNodeName())) { + type = DiffType.DELETED; + } else if ("modify".equals(ele.getNodeName())) { + type = DiffType.MODIFIED; + } else if ("nochange".equals(ele.getNodeName())) { + type = DiffType.NO_CHANGE; + } else { + throw new IllegalArgumentException("type can not understand."); + } + + FileInfo fi = new FileInfo(path, lastModified, size, hash, file); + DiffInfo di = new DiffInfo(fi, type); + this.diffInfoList.add(di); + } + } + } + + public int getRevision() { + return this.revision; + } + + public void setRevision(int revision) { + this.revision = revision; + } + + public long getBackupTime() { + return this.backupTime; + } + + public void setBackupTime(long backupTime) { + this.backupTime = backupTime; + } + + public List getDiffInfoList() { + return this.diffInfoList; + } + + public void setDiffInfoList(List diffInfoList) { + this.diffInfoList = diffInfoList; + } + + public String toXmlString() { + StringBuilder sb = new StringBuilder(); + + sb.append("\r\n"); + + for (DiffInfo diffInfo : this.diffInfoList) { + sb.append(" "); + switch (diffInfo.getType()) { + case ADDED: + sb.append("\r\n"); + } + + sb.append(""); + + return sb.toString(); + } + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/BackupRepository.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/BackupRepository.java new file mode 100644 index 0000000..d1e47ea --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/BackupRepository.java @@ -0,0 +1,306 @@ + +package jp.gr.java_conf.u6k.backup; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Properties; + +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + +public class BackupRepository { + + private File repoDir; + + private File backupDir; + + private int lastRev; + + private FileInfoRepository lastRevRepo; + + public BackupRepository(File repoDir, File backupDir) throws IOException { + /* + * 引数を確認します。 + */ + if (repoDir == null) { + throw new NullPointerException("repoDir == null"); + } + if (repoDir.exists()) { + throw new IllegalArgumentException("repoDir.exists()"); + } + if (backupDir == null) { + throw new NullPointerException("backupDir == null"); + } + if (!backupDir.exists()) { + throw new IllegalArgumentException("!backupDir.exists()"); + } + if (!backupDir.isDirectory()) { + throw new IllegalArgumentException("!backupDir.isDirectory()"); + } + /* + * リポジトリを作成します。 + */ + repoDir = repoDir.getCanonicalFile(); + backupDir = backupDir.getCanonicalFile(); + + if (!repoDir.mkdirs()) { + throw new IOException("「" + repoDir.getAbsolutePath() + "」ディレクトリの作成に失敗しました。"); + } + + // 設定フォルダを作成します。 + File configDir = new File(repoDir, "config"); + if (!configDir.mkdirs()) { + throw new IOException("「" + configDir.getAbsolutePath() + "」ディレクトリの作成に失敗しました。"); + } + + File configFile = new File(configDir, "config.properties"); + FileOutputStream fout = new FileOutputStream(configFile); + try { + Properties props = new Properties(); + props.put("backupDir", backupDir.getAbsolutePath()); + props.store(fout, ""); + } finally { + fout.close(); + } + + // ログ・フォルダを作成します。 + File logDir = new File(repoDir, "log"); + if (!logDir.mkdirs()) { + throw new IOException("「" + logDir.getAbsolutePath() + "」ディレクトリの作成に失敗しました。"); + } + + // ファイル・フォルダを作成します。 + File fileDir = new File(repoDir, "file"); + if (!fileDir.mkdirs()) { + throw new IOException("「" + fileDir.getAbsolutePath() + "」ディレクトリの作成に失敗しました。"); + } + + /* + * 状態を初期化します。 + */ + this.repoDir = repoDir; + this.backupDir = backupDir; + this.lastRev = 0; + this.lastRevRepo = new FileInfoRepository(); + } + + public BackupRepository(File repoDir) throws IOException, ParserConfigurationException, SAXException { + /* + * 引数を確認します。 + */ + if (repoDir == null) { + throw new NullPointerException("repoDir == null"); + } + /* + * 状態を初期化します。 + */ + repoDir = repoDir.getCanonicalFile(); + this.repoDir = repoDir; + // 設定を読み込みます。 + File configDir = new File(repoDir, "config"); + File configFile = new File(configDir, "config.properties"); + FileInputStream fin = new FileInputStream(configFile); + try { + Properties props = new Properties(); + props.load(fin); + + this.backupDir = new File(props.getProperty("backupDir")); + } finally { + fin.close(); + } + + // 最新のファイル情報リポジトリを構築します。 + File logDir = new File(repoDir, "log"); + + this.lastRevRepo = BackupRepository.buildLastRevision(logDir); + this.lastRev = BackupRepository.getLastRevision(logDir); + } + + public void backup() throws IOException { + File logDir = new File(this.repoDir, "log"); + File fileDir = new File(this.repoDir, "file"); + + this.lastRevRepo = BackupRepository.backup(this.lastRevRepo, this.lastRev, this.backupDir, logDir, fileDir); + this.lastRev++; + } + + private static FileInfoRepository backup(FileInfoRepository lastRevRepo, int lastRev, File backupDir, File logDir, File fileDir) throws IOException { + /* + * 引数を確認します。 + */ + if (lastRevRepo == null) { + throw new NullPointerException("lastRevRepo == null"); + } + if (backupDir == null) { + throw new NullPointerException("backupDir == null"); + } + if (logDir == null) { + throw new NullPointerException("logDir == null"); + } + if (fileDir == null) { + throw new NullPointerException("fileDir == null"); + } + /* + * バックアップを行います。 + */ + FileInfoRepository nextRevRepo = new FileInfoRepository(backupDir); + + List diffInfoList = nextRevRepo.diff(lastRevRepo); + + BackupLog log = new BackupLog(); + log.setRevision(lastRev + 1); + log.setBackupTime(Calendar.getInstance().getTimeInMillis()); + log.setDiffInfoList(diffInfoList); + + BackupRepository.saveFile(backupDir, fileDir, log); + BackupRepository.saveLog(logDir, log); + + return nextRevRepo; + } + + private static void saveLog(File logDir, BackupLog log) throws IOException { + // 引数を確認します。 + if (logDir == null) { + throw new NullPointerException("logDir == null"); + } + if (log == null) { + throw new NullPointerException("log == null"); + } + // ログをファイルに保存します。 + File logFile = new File(logDir, log.getRevision() + ".log"); + + FileOutputStream fout = new FileOutputStream(logFile); + try { + fout.write("\r\n".getBytes("UTF-8")); + fout.write(log.toXmlString().getBytes("UTF-8")); + } finally { + fout.close(); + } + } + + private static void saveFile(File backupDir, File fileDir, BackupLog log) throws IOException { + // 引数を確認します。 + if (backupDir == null) { + throw new NullPointerException("backupDir == null"); + } + if (fileDir == null) { + throw new NullPointerException("fileDir == null"); + } + if (log == null) { + throw new NullPointerException("log == null"); + } + // バックアップ対象ファイルをコピーします。 + File revFileDir = new File(fileDir, Integer.toString(log.getRevision())); + if (!revFileDir.mkdirs()) { + throw new IOException("「" + revFileDir.getAbsolutePath() + "」ディレクトリの作成に失敗しました。"); + } + + for (DiffInfo diff : log.getDiffInfoList()) { + if (diff.isFile() && (diff.getType() == DiffType.ADDED || diff.getType() == DiffType.MODIFIED)) { + String path = diff.getPath().replace('/', File.separatorChar); + File srcFile = new File(backupDir, path); + File destFile = new File(revFileDir, path); + if (!destFile.getParentFile().exists()) { + if (!destFile.getParentFile().mkdirs()) { + throw new IOException("「" + destFile.getParentFile().getAbsolutePath() + "」ディレクトリの作成に失敗しました。"); + } + } + BackupRepository.copyFile(srcFile, destFile); + } + } + } + + private static void copyFile(File srcFile, File destFile) throws IOException { + // 引数を確認します。 + if (srcFile == null) { + throw new NullPointerException("srcFile == null"); + } + if (destFile == null) { + throw new NullPointerException("destFile == null"); + } + // ファイルをコピーします。 + FileInputStream fin = new FileInputStream(srcFile); + try { + FileOutputStream fout = new FileOutputStream(destFile); + try { + int len; + byte[] buf = new byte[1024 * 1024]; + while ((len = fin.read(buf)) != -1) { + fout.write(buf, 0, len); + } + } finally { + fout.close(); + } + } finally { + fin.close(); + } + } + + private static FileInfoRepository buildLastRevision(File logDir) throws ParserConfigurationException, SAXException, IOException { + // 引数を確認します。 + if (logDir == null) { + throw new NullPointerException("logDir == null"); + } + // 最新のファイル情報リポジトリを構築します。 + List logFileList = new ArrayList(); + for (File logFile : logDir.listFiles()) { + logFileList.add(logFile); + } + Collections.sort(logFileList, new LogFileOrderByNameComparator()); + + FileInfoRepository lastRevRepo = new FileInfoRepository(); + + for (File logFile : logFileList) { + BackupLog log = new BackupLog(logFile); + lastRevRepo = lastRevRepo.patch(log.getDiffInfoList()); + } + + return lastRevRepo; + } + + private static int getLastRevision(File logDir) { + // 引数を確認します。 + if (logDir == null) { + throw new NullPointerException("logDir == null"); + } + // 最新のリビジョン番号を取得します。 + List logFileList = new ArrayList(); + for (File logFile : logDir.listFiles()) { + logFileList.add(logFile); + } + Collections.sort(logFileList, new LogFileOrderByNameComparator()); + + if (logFileList.size() == 0) { + return 0; + } + File lastRevLogFile = logFileList.get(logFileList.size() - 1); + int lastRev = Integer.parseInt(lastRevLogFile.getName().substring(0, lastRevLogFile.getName().indexOf("."))); + return lastRev; + } + + private static class LogFileOrderByNameComparator implements Comparator { + + public int compare(File o1, File o2) { + int rev1 = Integer.parseInt(o1.getName().substring(0, o1.getName().indexOf("."))); + int rev2 = Integer.parseInt(o2.getName().substring(0, o2.getName().indexOf("."))); + + if (rev1 > rev2) { + return 1; + } else if (rev1 < rev2) { + return -1; + } else { + return 0; + } + } + + } + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/DiffInfo.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/DiffInfo.java new file mode 100644 index 0000000..05c3c7d --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/DiffInfo.java @@ -0,0 +1,58 @@ + +package jp.gr.java_conf.u6k.backup; + +public class DiffInfo extends FileInfo { + + private DiffType type; + + public DiffInfo(FileInfo from, FileInfo to, DiffType type) { + super(DiffInfo.superClassState(from, to, type)); + // 引数を確認します。 + if (from == null && to == null) { + throw new NullPointerException("from == null && to == null"); + } + if (type == null) { + throw new NullPointerException("type == null"); + } + // 状態を初期化します。 + this.type = type; + } + + public DiffInfo(FileInfo fileInfo, DiffType type) { + super(fileInfo); + // 引数を確認します。 + if (fileInfo == null) { + throw new NullPointerException("fileInfo == null"); + } + // 状態を初期化します。 + this.type = type; + } + + private static FileInfo superClassState(FileInfo from, FileInfo to, DiffType type) { + // 引数を確認します。 + if (from == null && to == null) { + throw new NullPointerException("from == null && to == null"); + } + if (type == null) { + throw new NullPointerException("type == null"); + } + // 親インスタンスを初期化するために正しいFileInfoインスタンスを選択します。 + switch (type) { + case ADDED: + return to; + case DELETED: + return from; + case MODIFIED: + return to; + case NO_CHANGE: + return from; + default: + throw new IllegalArgumentException("type can not understand.[type=" + type + "]"); + } + } + + public DiffType getType() { + return this.type; + } + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/DiffType.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/DiffType.java new file mode 100644 index 0000000..ff8b110 --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/DiffType.java @@ -0,0 +1,8 @@ + +package jp.gr.java_conf.u6k.backup; + +public enum DiffType { + + ADDED, MODIFIED, DELETED, NO_CHANGE, + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/FileInfo.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/FileInfo.java new file mode 100644 index 0000000..8b04ed2 --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/FileInfo.java @@ -0,0 +1,57 @@ + +package jp.gr.java_conf.u6k.backup; + +public class FileInfo { + + private String path; + + private long lastModified; + + private long size; + + private String hash; + + private boolean file; + + public FileInfo(String path, long lastModified, long size, String hash, boolean file) { + this.path = path; + this.lastModified = lastModified; + this.size = size; + this.hash = hash; + this.file = file; + } + + protected FileInfo(FileInfo fileInfo) { + // 引数を確認します。 + if (fileInfo == null) { + throw new NullPointerException("fileInfo == null"); + } + // 状態を初期化します。 + this.path = fileInfo.path; + this.lastModified = fileInfo.lastModified; + this.size = fileInfo.size; + this.hash = fileInfo.hash; + this.file = fileInfo.file; + } + + public String getPath() { + return this.path; + } + + public long getLastModified() { + return this.lastModified; + } + + public long getSize() { + return this.size; + } + + public String getHash() { + return this.hash; + } + + public boolean isFile() { + return this.file; + } + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/FileInfoRepository.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/FileInfoRepository.java new file mode 100644 index 0000000..ba961f1 --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/FileInfoRepository.java @@ -0,0 +1,265 @@ + +package jp.gr.java_conf.u6k.backup; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class FileInfoRepository { + + private List fileInfoList = new ArrayList(); + + public FileInfoRepository() { + FileInfo fi = new FileInfo("/", 0L, 0L, null, false); + + this.fileInfoList.add(fi); + } + + public FileInfoRepository(File baseDir) { + // 引数を確認します。 + if (baseDir == null) { + throw new NullPointerException("baseDir == null"); + } + if (!baseDir.isDirectory()) { + throw new IllegalArgumentException("!baseDir.isDirectory()"); + } + + // 構築します。 + this.build(baseDir, baseDir); + } + + public FileInfoRepository(List fileInfoList) { + // 引数を確認します。 + if (fileInfoList == null) { + throw new NullPointerException("fileInfoList == null"); + } + // 構築します。 + this.fileInfoList = FileInfoRepository.sort(fileInfoList); + } + + private void build(File baseDir, File targetDir) { + // 引数を確認します。 + if (baseDir == null) { + throw new NullPointerException("baseDir == null"); + } + if (!baseDir.isDirectory()) { + throw new IllegalArgumentException("!baseDir.isDirectory()"); + } + if (targetDir == null) { + throw new NullPointerException("targetDir == null"); + } + if (!targetDir.isDirectory()) { + throw new IllegalArgumentException("!targetDir.isDirectory()"); + } + + // ファイル情報を構築します。 + FileInfo fi = new FileInfo(PathUtil.buildPath(baseDir, targetDir), targetDir.lastModified(), 0, "", false); + this.fileInfoList.add(fi); + + File[] listFiles = targetDir.listFiles(); + Arrays.sort(listFiles, 0, listFiles.length, new Comparator() { + + public int compare(File o1, File o2) { + if (o1.isFile() && o2.isDirectory()) { + return 1; + } else if (o1.isDirectory() && o2.isFile()) { + return -1; + } else { + return o1.getName().compareTo(o2.getName()); + } + } + + }); + + for (File f : listFiles) { + if (f.isFile()) { + fi = new FileInfo(PathUtil.buildPath(baseDir, f), f.lastModified(), f.length(), "", true); + this.fileInfoList.add(fi); + } else { + this.build(baseDir, f); + } + } + } + + public List findAll() { + List l = new ArrayList(); + l.addAll(this.fileInfoList); + return l; + } + + public FileInfo findByPath(String path) { + return FileInfoRepository.findByPath(this.fileInfoList, path); + } + + private static FileInfo findByPath(List fileInfoList, String path) { + for (FileInfo fi : fileInfoList) { + if (fi.getPath().equals(path)) { + return fi; + } + } + return null; + } + + public List findByParent(String parentPath) { + return FileInfoRepository.findByParent(this.fileInfoList, parentPath); + } + + private static List findByParent(List fileInfoList, String parentPath) { + List l = new ArrayList(); + + for (FileInfo fi : fileInfoList) { + if (!"/".equals(fi.getPath())) { + if (PathUtil.getParent(fi.getPath()).equals(parentPath)) { + l.add(fi); + } + } + } + + return l; + } + + public List diff(FileInfoRepository fromRepo) { + return FileInfoRepository.diff(fromRepo.fileInfoList, this.fileInfoList); + } + + private static List diff(List fromRepo, List toRepo) { + // 引数を確認します。 + if (fromRepo == null) { + throw new NullPointerException("fromRepo == null"); + } + if (toRepo == null) { + throw new NullPointerException("toRepo == null"); + } + + // 更新、削除、変化無し情報を収集します。 + List diffInfoList = new ArrayList(); + + Map toRepoMap = new HashMap(); + for (FileInfo fi : toRepo) { + toRepoMap.put(fi.getPath(), fi); + } + + for (FileInfo from : fromRepo) { + if (toRepoMap.containsKey(from.getPath())) { + FileInfo to = toRepoMap.get(from.getPath()); + toRepoMap.remove(from.getPath()); + + if (from.isFile() != to.isFile()) { + diffInfoList.add(new DiffInfo(from, null, DiffType.DELETED)); + diffInfoList.add(new DiffInfo(null, to, DiffType.ADDED)); + } else if (from.getLastModified() != to.getLastModified() || from.getSize() != to.getSize() || !from.getHash().equals(to.getHash())) { + diffInfoList.add(new DiffInfo(from, to, DiffType.MODIFIED)); + } else { + diffInfoList.add(new DiffInfo(from, to, DiffType.NO_CHANGE)); + } + } else { + diffInfoList.add(new DiffInfo(from, null, DiffType.DELETED)); + } + } + + // 追加情報を収集します。 + for (FileInfo to : toRepoMap.values()) { + diffInfoList.add(new DiffInfo(null, to, DiffType.ADDED)); + } + + // 変更情報リストをソートします。 + List fileInfoList = new ArrayList(); + for (FileInfo fi : diffInfoList) { + fileInfoList.add(fi); + } + FileInfoRepository diffInfoRepo = new FileInfoRepository(fileInfoList); + fileInfoList = diffInfoRepo.findAll(); + + diffInfoList.clear(); + for (int i = 0; i < fileInfoList.size(); i++) { + diffInfoList.add((DiffInfo) fileInfoList.get(i)); + } + + return diffInfoList; + } + + public FileInfoRepository patch(List diffList) { + List patchedList = FileInfoRepository.patch(this.fileInfoList, diffList); + return new FileInfoRepository(patchedList); + } + + private static List patch(List srcList, List diffList) { + // 引数を確認します。 + if (srcList == null) { + throw new NullPointerException("srcList == null"); + } + if (diffList == null) { + throw new NullPointerException("diffList == null"); + } + // ファイル情報に差分情報を適用し、新しいファイル情報にします。 + List destList = new ArrayList(); + destList.addAll(srcList); + + for (DiffInfo diff : diffList) { + switch (diff.getType()) { + case ADDED: + FileInfo newFile = new FileInfo(diff); + destList.add(newFile); + break; + case DELETED: + FileInfo delFile = FileInfoRepository.findByPath(destList, diff.getPath()); + destList.remove(delFile); + break; + case MODIFIED: + delFile = FileInfoRepository.findByPath(destList, diff.getPath()); + destList.remove(delFile); + newFile = new FileInfo(diff); + destList.add(newFile); + break; + case NO_CHANGE: + break; + default: + throw new IllegalArgumentException("diff.getType() can not understand.[type=" + diff.getType() + "]"); + } + } + + return destList; + } + + private static List sort(List list) { + List sortedList = FileInfoRepository.sort(list, FileInfoRepository.findByPath(list, "/")); + return sortedList; + } + + private static List sort(List list, FileInfo dir) { + List l = new ArrayList(); + + l.add(dir); + + List childList = FileInfoRepository.findByParent(list, dir.getPath()); + Collections.sort(childList, new Comparator() { + + public int compare(FileInfo o1, FileInfo o2) { + if (o1.isFile() && !o2.isFile()) { + return 1; + } else if (!o1.isFile() && o2.isFile()) { + return -1; + } else { + return o1.getPath().compareTo(o2.getPath()); + } + } + + }); + + for (FileInfo fi : childList) { + if (fi.isFile()) { + l.add(fi); + } else { + l.addAll(FileInfoRepository.sort(list, fi)); + } + } + + return l; + } + +} diff --git a/backup/src/main/java/jp/gr/java_conf/u6k/backup/PathUtil.java b/backup/src/main/java/jp/gr/java_conf/u6k/backup/PathUtil.java new file mode 100644 index 0000000..da1235e --- /dev/null +++ b/backup/src/main/java/jp/gr/java_conf/u6k/backup/PathUtil.java @@ -0,0 +1,82 @@ + +package jp.gr.java_conf.u6k.backup; + +import java.io.File; + +public class PathUtil { + + public static String buildPath(File baseDir, File target) { + // 引数を確認します。 + if (baseDir == null) { + throw new NullPointerException("baseDir == null"); + } + if (!baseDir.isDirectory()) { + throw new IllegalArgumentException("!baseDir.isDirectory()"); + } + if (target == null) { + throw new NullPointerException("target == null"); + } + if (!target.getAbsolutePath().startsWith(baseDir.getAbsolutePath())) { + throw new IllegalArgumentException("!target.getAbsolutePath().startsWith(baseDir.getAbsolutePath())"); + } + + // パス文字列を構築します。 + if (baseDir.equals(target)) { + return "/"; + } + + String path = target.getAbsolutePath().substring(baseDir.getAbsolutePath().length()); + path = path.replace(File.separatorChar, '/'); + if (!path.startsWith("/")) { + path = "/" + path; + } + if (path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + + return path; + } + + public static String getParent(String path) { + // 引数を確認します。 + if (path == null) { + throw new NullPointerException("path == null"); + } + + // 親パスを返します。 + if ("/".equals(path)) { + throw new IllegalArgumentException("path is root directory."); + } else if (path.lastIndexOf("/") == 0) { + return "/"; + } else { + return path.substring(0, path.lastIndexOf("/")); + } + } + + public static boolean isSameDirLevel(String path1, String path2) { + // 引数を確認します。 + if (path1 == null) { + throw new NullPointerException("path1 == null"); + } + if (path2 == null) { + throw new NullPointerException("path2 == null"); + } + + // 親パスを比較します。 + return getParent(path1).equals(getParent(path2)); + } + + public static boolean isParent(String parentPath, String childPath) { + // 引数を確認します。 + if (parentPath == null) { + throw new NullPointerException("parentPath == null"); + } + if (childPath == null) { + throw new NullPointerException("childPath == null"); + } + + // パスを比較します。 + return parentPath.equals(getParent(childPath)); + } + +} diff --git a/bak.vbs/bak.vbs b/bak.vbs/bak.vbs new file mode 100644 index 0000000..b769ff3 --- /dev/null +++ b/bak.vbs/bak.vbs @@ -0,0 +1,2005 @@ +Option Explicit + +' ***** Ý’èî•ñ ***** + +' ƒoƒbƒNƒAƒbƒvŒ³‚̃tƒHƒ‹ƒ_B +Const gstrSourceFolderPath = "\\sse2f-fs\ƒVƒXƒeƒ€ŠJ”­•”ŠÂ‹«\’z" + +' ƒoƒbƒNƒAƒbƒv‚ÌŽí—ށB +' "Full"...Š®‘SƒoƒbƒNƒAƒbƒvB +' "Different"...·•ªƒoƒbƒNƒAƒbƒvB +' "Incremental"...‘•ªƒoƒbƒNƒAƒbƒvB +Const gstrBackupType = "Full" + +' ƒfƒoƒbƒOEƒƒO‚ðo—Í‚·‚é‚©‚Ç‚¤‚©B +' True...ƒfƒoƒbƒOEƒƒO‚ðo—Í‚·‚é(„§)B +' False...ƒfƒoƒbƒOEƒƒO‚ðo—Í‚µ‚È‚¢B +Const gblnDebugLog = True + + + + + + +' ***** Žd—l ***** + +' ƒoƒbƒNƒAƒbƒvEƒXƒNƒŠƒvƒgB +' Š®‘SƒoƒbƒNƒAƒbƒv(full backup)A·•ªƒoƒbƒNƒAƒbƒv(different backup)A‘•ªƒoƒbƒNƒAƒbƒv(incremental backup)‚ðs‚¤B +' ‚à‚¿‚ë‚ñA•œŒ³(restore)‚às‚¦‚éB + +' ƒoƒbƒNƒAƒbƒv‚ðs‚¤‚ƁAŽÀÛ‚̃tƒ@ƒCƒ‹A‹y‚уoƒbƒNƒAƒbƒvî•ñ‚ªì¬‚³‚ê‚éB +' Š®‘SƒoƒbƒNƒAƒbƒv‚̏ꍇAƒoƒbƒNƒAƒbƒvî•ñ‚̓tƒ@ƒCƒ‹‘S‚Ă̏î•ñ‚Æ‚È‚éB +' ·•ªƒoƒbƒNƒAƒbƒv‚̏ꍇAƒoƒbƒNƒAƒbƒvî•ñ‚͊“_‚Ƃ̍·•ªî•ñ‚Æ‚È‚éB +' ‘•ªƒoƒbƒNƒAƒbƒv‚̏ꍇAƒoƒbƒNƒAƒbƒvî•ñ‚Í‘O‰ñ‚Ƃ̍·•ªî•ñ‚Æ‚È‚éB +' ·•ªî•ñ‚́A‚ǂ̂悤‚ȕύX‚ª‚ ‚Á‚½‚©‚Ì‚Ý‚ð‹L‚·B +' ƒoƒbƒNƒAƒbƒvî•ñ‚̓tƒ@ƒCƒ‹EƒtƒHƒ‹ƒ_–¼‚ð•ÛŽ‚µAƒpƒX‚Í•ÛŽ‚µ‚È‚¢B‚±‚Ì‚½‚߁AƒoƒbƒNƒAƒbƒvEƒ\[ƒX‚̏ꏊ‚ª•Ï‚í‚Á‚Ä‚à‘±s‚·‚邱‚Æ‚ª‚Å‚«‚éB + +' ƒtƒHƒ‹ƒ_\¬ +' (ƒŠƒ|ƒWƒgƒŠ)\ +' +---bak.vbs +' +---log.txt +' +---rev\ +' +---rev.txt +' +---1.xml +' +---2.xml +' +---1\ +' | +---(ƒtƒ@ƒCƒ‹)\ +' +---2\ +' | +---(ƒtƒ@ƒCƒ‹)\ +' ... + +' Š®‘SƒoƒbƒNƒAƒbƒv‚Ì“®ìB +' –ˆ‰ñAƒoƒbƒNƒAƒbƒvŒ³‚Ì‘S‚Ẵtƒ@ƒCƒ‹‚ðƒRƒs[‚·‚éB + +' ·•ªƒoƒbƒNƒAƒbƒv‚Ì“®ìB +' Šî€ƒŠƒrƒWƒ‡ƒ“‚ªŽw’肳‚ꂽê‡AŠî€ƒŠƒrƒWƒ‡ƒ“‚̃oƒbƒNƒAƒbƒvî•ñ‚Ƃ̍·•ª‚ðŽZo‚µA·•ª‚̃tƒ@ƒCƒ‹‚Ì‚Ý‚ðƒRƒs[‚·‚éB +' Šî€ƒŠƒrƒWƒ‡ƒ“‚ÍŠ®‘SƒoƒbƒNƒAƒbƒv‚̏î•ñ‚ðŽ‚½‚È‚¯‚ê‚΂Ȃç‚È‚¢B +' Šî€ƒŠƒrƒWƒ‡ƒ“‚ªŽw’肳‚ê‚È‚¢ê‡AƒŠƒrƒWƒ‡ƒ“‚ð‚³‚©‚Ì‚Ú‚Á‚āA‘O‰ñ‚ÌŠ®‘SƒoƒbƒNƒAƒbƒv‚Ƃ̍·•ª‚ðƒoƒbƒNƒAƒbƒv‚·‚éB +' ‘O‰ñ‚ÌŠ®‘SƒoƒbƒNƒAƒbƒv‚ªŒ©‚‚©‚ç‚È‚¢ê‡(‚‚܂èA‰‚߂ẴoƒbƒNƒAƒbƒv‚̏ꍇ)AŠ®‘SƒoƒbƒNƒAƒbƒv‚ðs‚¤B + +' ‘•ªƒoƒbƒNƒAƒbƒv‚Ì“®ìB +' ‘O‰ñ‚܂ł̏î•ñ‚ŃoƒbƒNƒAƒbƒvî•ñ‚ð\’z‚µA‚»‚ê‚ƃoƒbƒNƒAƒbƒvŒ³‚Ƃ̍·•ª‚ðƒoƒbƒNƒAƒbƒv‚·‚éB +' ‚‚܂èAí‚É‘O‰ñ‚Ƃ̍·•ª‚ðƒoƒbƒNƒAƒbƒv‚·‚éB +' ‘O‰ñ‚̃oƒbƒNƒAƒbƒvî•ñ‚ð\’z‚Å‚«‚È‚¢ê‡(‚‚܂èA‰‚߂ẴoƒbƒNƒAƒbƒv‚̏ꍇ)AŠ®‘SƒoƒbƒNƒAƒbƒv‚ðs‚¤B + +' TODO ·•ªƒoƒbƒNƒAƒbƒv‚ðŽÀ‘•‚µ‚Ä‚Ý‚éB +' TODO œŠOŽw’è‚ð‚Å‚«‚é‚悤‚É‚·‚éB +' TODO ŠúŠÔ‚э[ƒŠƒ“ƒO‚Å‚«‚é‚悤‚É‚·‚éB +' TODO ƒƒO‚𐮔õ‚·‚éB + + + + + +' ***** ƒOƒ[ƒoƒ‹•Ï”E’萔 ***** + +Dim gfso +Dim gfsoLogStream + +Const IOMode_ForAppending = 8 +Const IOMode_ForReading = 1 +Const IOMode_ForWriting = 2 + +' ***** ˆ—ŽÀs ***** + +Call Backup() + +MsgBox "ok" + +Call WScript.Quit(0) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +' ******************************************************************************** +' ƒoƒbƒNƒAƒbƒv +' ******************************************************************************** + +Sub Backup() +Set gfso = CreateObject("Scripting.FileSystemObject") +Set gfsoLogStream = gfso.OpenTextFile(gfso.BuildPath(gfso.GetParentFolderName(WScript.ScriptFullName), "log.txt"), IOMode_ForAppending, True) + +Call StartFunction("Backup") + Dim strRepositoryFolderPath + Dim strRevisionRootFolderPath + Dim strRevisionFolderPath + Dim strRevisionTxtFilePath + Dim strRevisionDiffFilePath + Dim lngRevision + Dim lngOldRevision + Dim objFSInfo + Dim objOldFSInfo + Dim objDiffInfo + + Call WriteLog("[INFO]ƒoƒbƒNƒAƒbƒv‚ðŠJŽn‚µ‚Ü‚·B") + + ' ‰Šú‰»B + + Call WriteLog("[INFO]‰Šú‰»‚µ‚Ü‚·BƒtƒHƒ‹ƒ_Aƒtƒ@ƒCƒ‹‚Ȃǂ̍쐬AÝ’è‚̓ǂݍž‚Ý‚ðs‚¢‚Ü‚·B") + + strRepositoryFolderPath = gfso.GetParentFolderName(WScript.ScriptFullName) + + strRevisionRootFolderPath = gfso.BuildPath(strRepositoryFolderPath, "rev") + If Not gfso.FolderExists(strRevisionRootFolderPath) Then + Call CreateFolder(strRevisionRootFolderPath) + End If + + strRevisionTxtFilePath = gfso.BuildPath(strRevisionRootFolderPath, "rev.txt") + If Not gfso.FileExists(strRevisionTxtFilePath) Then + Call WriteFile(strRevisionTxtFilePath, "0") + End If + + lngRevision = CLng(ReadFile(strRevisionTxtFilePath)) + 1 + + strRevisionFolderPath = gfso.BuildPath(strRevisionRootFolderPath, CStr(lngRevision)) + Call CreateFolder(strRevisionFolderPath) + + strRevisionDiffFilePath = gfso.BuildPath(strRevisionRootFolderPath, CStr(lngRevision) & ".xml") + + lngOldRevision = GetOldRevision(strRepositoryFolderPath, lngRevision) + + Call WriteLog("[INFO]ƒoƒbƒNƒAƒbƒvŒ³ƒtƒHƒ‹ƒ_=" & gstrSourceFolderPath) + Call WriteLog("[INFO]ƒoƒbƒNƒAƒbƒv‚ÌŽí—Þ=" & gstrBackupType) + Call WriteLog("[INFO]ƒŠƒ|ƒWƒgƒŠEƒtƒHƒ‹ƒ_=" & strRepositoryFolderPath) + Call WriteLog("[INFO]ƒŠƒrƒWƒ‡ƒ“Eƒ‹[ƒgEƒtƒHƒ‹ƒ_=" & strRevisionRootFolderPath) + Call WriteLog("[INFO]ƒŠƒrƒWƒ‡ƒ“EƒtƒHƒ‹ƒ_=" & strRevisionFolderPath) + Call WriteLog("[INFO]ƒŠƒrƒWƒ‡ƒ“Ý’èƒtƒ@ƒCƒ‹=" & strRevisionTxtFilePath) + Call WriteLog("[INFO]ƒŠƒrƒWƒ‡ƒ“·•ªƒtƒ@ƒCƒ‹=" & strRevisionDiffFilePath) + Call WriteLog("[INFO]Œ»Ý‚̃ŠƒrƒWƒ‡ƒ“=" & lngRevision) + Call WriteLog("[INFO]”äŠrŒ³‚̃ŠƒrƒWƒ‡ƒ“=" & lngOldRevision) + + ' ƒoƒbƒNƒAƒbƒvB + Call WriteLog("[INFO]”äŠrŒ³ƒŠƒrƒWƒ‡ƒ“‚̃tƒ@ƒCƒ‹EƒVƒXƒeƒ€î•ñ‚ð\’z‚µ‚Ü‚·B[”äŠrŒ³‚̃ŠƒrƒWƒ‡ƒ“=" & lngOldRevision & "]") + + Set objOldFSInfo = BuildFileSystemInfoFromRevision(strRepositoryFolderPath, lngOldRevision) + + Call WriteLog("[INFO]ƒoƒbƒNƒAƒbƒvŒ³ƒtƒHƒ‹ƒ_‚̃tƒ@ƒCƒ‹EƒVƒXƒeƒ€î•ñ‚ð\’z‚µ‚Ü‚·B[ƒoƒbƒNƒAƒbƒvŒ³ƒtƒHƒ‹ƒ_=" & gstrSourceFolderPath & ", Œ»Ý‚̃ŠƒrƒWƒ‡ƒ“=" & lngRevision & "]") + + Set objFSInfo = New FileSystemInfo + Call objFSInfo.BuildFromFolderPath(gstrSourceFolderPath, lngRevision) + + Call WriteLog("[INFO]”äŠrŒ³ƒŠƒrƒWƒ‡ƒ“‚ƃoƒbƒNƒAƒbƒvŒ³ƒtƒHƒ‹ƒ_‚̃tƒ@ƒCƒ‹EƒVƒXƒeƒ€î•ñ‚̍·•ªî•ñ‚ð\’z‚µ‚Ü‚·B") + + Set objDiffInfo = New DiffInfo + Call objDiffInfo.BuildFromFileSystemInfo(objOldFSInfo, objFSInfo) + + Call WriteLog("[INFO]·•ªî•ñ‚ðƒtƒ@ƒCƒ‹‚ɏ‘‚«ž‚Ý‚Ü‚·B[·•ªî•ñƒtƒ@ƒCƒ‹=" & strRevisionDiffFilePath & "]") + + Call objDiffInfo.ToXmlFile(strRevisionDiffFilePath) + + Call WriteLog("[INFO]·•ªî•ñ‚ðŒ³‚ÉAƒoƒbƒNƒAƒbƒvŒ³ƒtƒHƒ‹ƒ_‚©‚烊ƒrƒWƒ‡ƒ“EƒtƒHƒ‹ƒ_‚Ƀtƒ@ƒCƒ‹‚ðƒRƒs[‚µ‚Ü‚·B[ƒoƒbƒNƒAƒbƒvŒ³ƒtƒHƒ‹ƒ_=" & gstrSourceFolderPath & ", ƒŠƒrƒWƒ‡ƒ“EƒtƒHƒ‹ƒ_=" & strRevisionFolderPath & "]") + + Call objDiffInfo.Backup(gstrSourceFolderPath, strRevisionFolderPath) + + ' I—¹B + Call WriteLog("[INFO]Œ»Ý‚̃ŠƒrƒWƒ‡ƒ“‚ðƒŠƒrƒWƒ‡ƒ“Ý’èƒtƒ@ƒCƒ‹‚ɏ‘‚«ž‚Ý‚Ü‚·B[ƒŠƒrƒWƒ‡ƒ“Ý’èƒtƒ@ƒCƒ‹=" & strRevisionTxtFilePath & "]") + + Call WriteFile(strRevisionTxtFilePath, lngRevision) + + Call WriteLog("[INFO]ƒoƒbƒNƒAƒbƒv‚ªŠ®—¹‚µ‚Ü‚µ‚½B") +Call EndFunction("Backup") + +Call gfsoLogStream.Close() +End Sub + +Function GetOldRevision(strRepositoryFolderPath, lngRevision) +Call StartFunction("GetOldRevision") + If gstrBackupType = "Full" Then + GetOldRevision = 0 + ElseIf gstrBackupType = "Different" Then + ' TODO ‘O‰ñ‚ÌŠ®‘SƒoƒbƒNƒAƒbƒv‚̃ŠƒrƒWƒ‡ƒ“‚ð•Ô‚·B + GetOldRevision = 0 + ElseIf gstrBackupType = "Incremental" Then + GetOldRevision = lngRevision - 1 + Else + Call Err.Raise(1, "bak.vbs", "”FŽ¯‚Å‚«‚È‚¢ƒoƒbƒNƒAƒbƒv‚ÌŽí—Þ‚Å‚·B[" & gstrBackupType & "]") + End If +Call EndFunction("GetOldRevision") +End Function + +Function BuildFileSystemInfoFromRevision(strRepositoryFolderPath, lngRevision) +Call StartFunction("BuildFileSystemInfoFromRevision") + Dim objFSInfo, objDiffInfo + + If lngRevision = 0 Then + Set objFSInfo = New FileSystemInfo + Call objFSInfo.InitializeFolderInfo() + + Set BuildFileSystemInfoFromRevision = objFSInfo + Else + Set objDiffInfo = New DiffInfo + Call objDiffInfo.BuildFromXmlFile(gfso.BuildPath(strRepositoryFolderPath, "rev\" & CStr(lngRevision) & ".xml")) + + Set objFSInfo = BuildFileSystemInfoFromRevision(strRepositoryFolderPath, objDiffInfo.OldFileSystemInfo.Revision) + + Call objDiffInfo.Merge(objFSInfo) + + objFSInfo.Revision = lngRevision + + Set BuildFileSystemInfoFromRevision = objFSInfo + End If +Call EndFunction("BuildFileSystemInfoFromRevision") +End Function + +' ƒtƒ@ƒCƒ‹(‹y‚уtƒHƒ‹ƒ_)î•ñ‚ðˆµ‚¢‚Ü‚·B +Class FileSystemInfo + Public Name ' StringŒ^Bƒtƒ@ƒCƒ‹–¼BƒpƒX‚Å‚Í‚È‚¢B + Public Attributes ' LongŒ^B‘®«B + Public DateCreated ' DateŒ^Bì¬“úŽžB + Public DateLastModified ' DateŒ^BÅIXV“úŽžB + Public Size ' LongŒ^Bƒtƒ@ƒCƒ‹EƒTƒCƒYB + Public FileType ' StringŒ^BƒIƒuƒWƒFƒNƒg‚ÌŽí—ށB‚±‚̃IƒuƒWƒFƒNƒg‚ªƒtƒ@ƒCƒ‹‚̏ꍇ‚Í"File"AƒtƒHƒ‹ƒ_‚̏ꍇ‚Í"Folder"B + Public Revision ' LongŒ^BƒoƒbƒNƒAƒbƒvŠÇ—‚É‚¨‚¯‚郊ƒrƒWƒ‡ƒ“”ԍ†B + Public Children ' DictionaryŒ^BŽqƒIƒuƒWƒFƒNƒg‚ðŠi”[‚·‚éBƒL[‚ÍNameB + + Private Sub Class_Initialize() + Call StartFunction("FileSystemInfo.Class_Initialize") + Name = "" + Attributes = 0 + DateCreated = CDate("1900/1/1 00:00:00") + DateLastModified = CDate("1900/1/1 00:00:00") + Size = 0 + FileType = "" + Revision = 0 + Set Children = Nothing + Call EndFunction("FileSystemInfo.Class_Initialize") + End Sub + + Public Sub InitializeFolderInfo() + Call StartFunction("FileSystemInfo.InitializeFolderInfo") + Name = "" + Attributes = 0 + DateCreated = CDate("1900/1/1 00:00:00") + DateLastModified = CDate("1900/1/1 00:00:00") + Size = 0 + FileType = "Folder" + Revision = 0 + Set Children = New Dictionary + Call EndFunction("FileSystemInfo.Class_Initialize") + End Sub + + Public Sub BuildFromFileSystemObject(fsoItem, lngRevision) + Call StartFunction("FileSystemInfo.BuildFromFileSystemObject") + Dim fsoChildItem, objChildFSInfo + + Name = fsoItem.Name + If TypeName(fsoItem) = "Folder" Then + If Not fsoItem.IsRootFolder Then + Attributes = fsoItem.Attributes + DateCreated = fsoItem.DateCreated + DateLastModified = fsoItem.DateLastModified + End If + Else + Attributes = fsoItem.Attributes + DateCreated = fsoItem.DateCreated + DateLastModified = fsoItem.DateLastModified + End If + + Revision = lngRevision + + If TypeName(fsoItem) = "File" Then + Size = fsoItem.Size + FileType = "File" + + Set Children = Nothing + Else + Size = 0 + FileType = "Folder" + + Set Children = New Dictionary + + For Each fsoChildItem In fsoItem.Files + Set objChildFSInfo = New FileSystemInfo + Call objChildFSInfo.BuildFromFileSystemObject(fsoChildItem, lngRevision) + Set Children.Item(objChildFSInfo.Name) = objChildFSInfo + Next + + For Each fsoChildItem In fsoItem.SubFolders + Set objChildFSInfo = New FileSystemInfo + Call objChildFSInfo.BuildFromFileSystemObject(fsoChildItem, lngRevision) + Set Children.Item(objChildFSInfo.Name) = objChildFSInfo + Next + + Call Children.Sort(New PrimitiveKeyComparator) + End If + Call EndFunction("FileSystemInfo.BuildFromFileSystemObject") + End Sub + + Public Sub BuildFromFolderPath(strFolderPath, lngRevision) + Call StartFunction("FileSystemInfo.BuildFromFolderPath") + Call BuildFromFileSystemObject(gfso.GetFolder(strFolderPath), lngRevision) + Call EndFunction("FileSystemInfo.BuildFromFolderPath") + End Sub + + Public Sub BuildFromXmlNode(xmlNode) + Call StartFunction("FileSystemInfo.BuildFromXmlNode") + Dim xmlChildNode + Dim objChildFSInfo + + Name = xmlNode.attributes.getNamedItem("name").nodeValue + Attributes = CLng(xmlNode.attributes.getNamedItem("attributes").nodeValue) + DateCreated = CDate(xmlNode.attributes.getNamedItem("dateCreated").nodeValue) + DateLastModified = CDate(xmlNode.attributes.getNamedItem("dateLastModified").nodeValue) + Size = CLng(xmlNode.attributes.getNamedItem("size").nodeValue) + FileType = xmlNode.attributes.getNamedItem("fileType").nodeValue + Revision = CLng(xmlNode.attributes.getNamedItem("revision").nodeValue) + + If FileType = "File" Then + Set Children = Nothing + Else + Set Children = New Dictionary + + For Each xmlChildNode In xmlNode.selectNodes("item") + Set objChildFSInfo = New FileSystemInfo + Call objChildFSInfo.BuildFromXmlNode(xmlChildNode) + Set Children.Item(objChildFSInfo.Name) = objChildFSInfo + Next + + Call Children.Sort(New PrimitiveKeyComparator) + End If + Call EndFunction("FileSystemInfo.BuildFromXmlNode") + End Sub + + Public Sub BuildFromXmlDocument(xmlDocument) + Call StartFunction("FileSystemInfo.BuildFromXmlDocument") + Dim xmlNode, xmlChildNode, objChildFSInfo + + Set xmlNode = xmlDocument.selectSingleNode("source") + + FileType = "Folder" + Revision = CLng(xmlNode.attributes.getNamedItem("revision").nodeValue) + + Set Children = New Dictionary + + For Each xmlChildNode In xmlNode.selectNodes("item") + Set objChildFSInfo = New FileSystemInfo + Call objChildFSInfo.BuildFromXmlNode(xmlChildNode) + Set Children.Item(objChildFSInfo.Name) = objChildFSInfo + Next + Call EndFunction("FileSystemInfo.BuildFromXmlDocument") + End Sub + + Public Sub BuildFromXmlFile(strFilePath) + Call StartFunction("FileSystemInfo.BuildFromXmlFile") + Dim xmlDocument + + Set xmlDocument = CreateObject("MSXML2.DOMDocument") + Call xmlDocument.load(strFilePath) + + Call BuildFromXmlDocument(xmlDocument) + Call EndFunction("FileSystemInfo.BuildFromXmlFile") + End Sub + + Public Function ToXmlNode(xmlDocument) + Call StartFunction("FileSystemInfo.ToXmlNode") + Dim xmlNode, objChildFSInfo + + Set xmlNode = xmlDocument.createElement("item") + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("name")) + xmlNode.attributes.getNamedItem("name").nodeValue = Name + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("attributes")) + xmlNode.attributes.getNamedItem("attributes").nodeValue = Attributes + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("dateCreated")) + xmlNode.attributes.getNamedItem("dateCreated").nodeValue = DateCreated + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("dateLastModified")) + xmlNode.attributes.getNamedItem("dateLastModified").nodeValue = DateLastModified + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("size")) + xmlNode.attributes.getNamedItem("size").nodeValue = Size + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("fileType")) + xmlNode.attributes.getNamedItem("fileType").nodeValue = FileType + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("revision")) + xmlNode.attributes.getNamedItem("revision").nodeValue = Revision + + If FileType = "Folder" Then + For Each objChildFSInfo In Children.Values + Call xmlNode.appendChild(objChildFSInfo.ToXmlNode(xmlDocument)) + Next + End If + + Set ToXmlNode = xmlNode + Call EndFunction("FileSystemInfo.ToXmlNode") + End Function + + Public Function ToXmlDocument() + Call StartFunction("FileSystemInfo.ToXmlDocument") + Dim xmlDocument + Dim xmlNode + Dim objChildFSInfo + + Set xmlDocument = CreateObject("MSXML2.DOMDocument") + + Set xmlNode = xmlDocument.createElement("source") + Call xmlDocument.appendChild(xmlNode) + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("revision")) + xmlNode.attributes.getNamedItem("revision").nodeValue = Revision + + For Each objChildFSInfo In Children.Values + Call xmlNode.appendChild(objChildFSInfo.ToXmlNode(xmlDocument)) + Next + + Set ToXmlDocument = xmlDocument + Call EndFunction("FileSystemInfo.ToXmlDocument") + End Function + + Public Sub ToXmlFile(strFilePath) + Call StartFunction("FileSystemInfo.ToXmlFile") + Call ToXmlDocument().save(strFilePath) + Call EndFunction("FileSystemInfo.ToXmlFile") + End Sub + + Public Function IsEqualProperty(objFSInfo) + Call StartFunction("FileSystemInfo.IsEqualProperty") + IsEqualProperty = (Name = objFSInfo.Name _ + And Attributes = objFSInfo.Attributes _ + And DateCreated = objFSInfo.DateCreated _ + And DateLastModified = objFSInfo.DateLastModified _ + And Size = objFSInfo.Size _ + And FileType = objFSInfo.FileType) + Call EndFunction("FileSystemInfo.IsEqualProperty") + End Function +End Class + +Class DiffInfo + Public Name ' StringŒ^Bƒtƒ@ƒCƒ‹–¼BƒpƒX‚Å‚Í‚È‚¢B + Public Status ' StringŒ^B·•ªó‹µBV‹K‚Í"Create"AXV‚Í"Update"Aíœ‚Í"Delete"AƒtƒHƒ‹ƒ_‚̏ꍇ‚̂ݕύX–³‚µ‚̏ꍇ‚Í""B + Public FileType ' StringŒ^BƒIƒuƒWƒFƒNƒg‚ÌŽí—ށB‚±‚̃IƒuƒWƒFƒNƒg‚ªƒtƒ@ƒCƒ‹‚̏ꍇ‚Í"File"AƒtƒHƒ‹ƒ_‚̏ꍇ‚Í"Folder"B + Public OldFileSystemInfo ' FileSystemInfoŒ^B‹Œƒtƒ@ƒCƒ‹î•ñB + Public NewFileSystemInfo ' FileSystemInfoŒ^BVƒtƒ@ƒCƒ‹î•ñB + Public Children ' CollectionŒ^BŽq·•ªî•ñB“¯ˆê–¼‚ō폜Aì¬‚ðs‚¤ê‡‚ª‚ ‚邽‚߁ADictionaryŒ^‚Å‚Í‚È‚¢B + + Private Sub Class_Initialize() + Call StartFunction("DiffInfo.Class_Initialize") + Name = "" + Status = "" + Set OldFileSystemInfo = Nothing + Set NewFileSystemInfo = Nothing + Set Children = Nothing + Call EndFunction("DiffInfo.Class_Initialize") + End Sub + + Public Sub BuildFromFileSystemInfo(objOldFileSystemInfo, objNewFileSystemInfo) + Call StartFunction("DiffInfo.BuildFromFileSystemInfo") + Dim strName, dicName + Dim objChildOldFSInfo, objChildNewFSInfo + + Set OldFileSystemInfo = objOldFileSystemInfo + Set NewFileSystemInfo = objNewFileSystemInfo + + If OldFileSystemInfo Is Nothing And Not NewFileSystemInfo Is Nothing Then + Name = NewFileSystemInfo.Name + Status = "Create" + FileType = NewFileSystemInfo.FileType + + If FileType = "Folder" Then + Set Children = New Collection + For Each objChildNewFSInfo In NewFileSystemInfo.Children.Values + Call AddChildDiffInfo(Nothing, objChildNewFSInfo) + Next + + Call Children.Sort(New DiffInfoComparator) + End If + ElseIf Not OldFileSystemInfo Is Nothing And NewFileSystemInfo Is Nothing Then + Name = OldFileSystemInfo.Name + Status = "Delete" + FileType = OldFileSystemInfo.FileType + Else + Name = NewFileSystemInfo.Name + If OldFileSystemInfo.IsEqualProperty(NewFileSystemInfo) Then + Status = "" + Else + Status = "Update" + End If + FileType = NewFileSystemInfo.FileType + + If FileType = "Folder" Then + Set dicName = CreateObject("Scripting.Dictionary") + For Each strName In OldFileSystemInfo.Children.Keys + dicName(strName) = strName + Next + For Each strName In NewFileSystemInfo.Children.Keys + dicName(strName) = strName + Next + + Set Children = New Collection + For Each strName In dicName.Keys + If OldFileSystemInfo.Children.ExistKey(strName) Then + Set objChildOldFSInfo = OldFileSystemInfo.Children.Item(strName) + Else + Set objChildOldFSInfo = Nothing + End If + If NewFileSystemInfo.Children.ExistKey(strName) Then + Set objChildNewFSInfo = NewFileSystemInfo.Children.Item(strName) + Else + Set objChildNewFSInfo = Nothing + End If + + If objChildOldFSInfo Is Nothing Or objChildNewFSInfo Is Nothing Then + Call AddChildDiffInfo(objChildOldFSInfo, objChildNewFSInfo) + ElseIf objChildOldFSInfo.FileType = objChildNewFSInfo.FileType Then + Call AddChildDiffInfo(objChildOldFSInfo, objChildNewFSInfo) + Else + Call AddChildDiffInfo(objChildOldFSInfo, Nothing) + Call AddChildDiffInfo(Nothing, objChildNewFSInfo) + End If + Next + + Call Children.Sort(New DiffInfoComparator) + End If + End If + Call EndFunction("DiffInfo.BuildFromFileSystemInfo") + End Sub + + Private Sub AddChildDiffInfo(objOldFSInfo, objNewFSInfo) + Call StartFunction("DiffInfo.AddChildDiffInfo") + Dim objChildDiffInfo + + Set objChildDiffInfo = New DiffInfo + Call objChildDiffInfo.BuildFromFileSystemInfo(objOldFSInfo, objNewFSInfo) + If objChildDiffInfo.Status <> "" Then + Call Children.Add(objChildDiffInfo) + ElseIf Not objChildDiffInfo.Children Is Nothing Then + If objChildDiffInfo.Children.Count > 0 Then + Call Children.Add(objChildDiffInfo) + End If + End If + Call EndFunction("DiffInfo.AddChildDiffInfo") + End Sub + + Public Sub BuildFromXmlNode(xmlNode) + Call StartFunction("DiffInfo.BuildFromXmlNode") + Dim xmlChildNode, objChildDiffInfo + + Name = xmlNode.attributes.getNamedItem("name").nodeValue + Status = xmlNode.attributes.getNamedItem("status").nodeValue + FileType = xmlNode.attributes.getNamedItem("fileType").nodeValue + + If Status = "Delete" Or Status = "Update" Or Status = "" Then + Set OldFileSystemInfo = New FileSystemInfo + + OldFileSystemInfo.Name = Name + OldFileSystemInfo.Attributes = CLng(xmlNode.attributes.getNamedItem("oldAttributes").nodeValue) + OldFileSystemInfo.DateCreated = CDate(xmlNode.attributes.getNamedItem("oldDateCreated").nodeValue) + OldFileSystemInfo.DateLastModified = CDate(xmlNode.attributes.getNamedItem("oldDateLastModified").nodeValue) + OldFileSystemInfo.Size = CLng(xmlNode.attributes.getNamedItem("oldSize").nodeValue) + OldFileSystemInfo.FileType = FileType + OldFileSystemInfo.Revision = CLng(xmlNode.attributes.getNamedItem("oldRevision").nodeValue) + End If + + If Status = "Create" Or Status = "Update" Or Status = "" Then + Set NewFileSystemInfo = New FileSystemInfo + + NewFileSystemInfo.Name = Name + NewFileSystemInfo.Attributes = CLng(xmlNode.attributes.getNamedItem("newAttributes").nodeValue) + NewFileSystemInfo.DateCreated = CDate(xmlNode.attributes.getNamedItem("newDateCreated").nodeValue) + NewFileSystemInfo.DateLastModified = CDate(xmlNode.attributes.getNamedItem("newDateLastModified").nodeValue) + NewFileSystemInfo.Size = CLng(xmlNode.attributes.getNamedItem("newSize").nodeValue) + NewFileSystemInfo.FileType = FileType + NewFileSystemInfo.Revision = CLng(xmlNode.attributes.getNamedItem("newRevision").nodeValue) + End If + + If FileType = "Folder" And Status <> "Delete" Then + Set Children = New Collection + For Each xmlChildNode In xmlNode.selectNodes("item") + Set objChildDiffInfo = New DiffInfo + Call objChildDiffInfo.BuildFromXmlNode(xmlChildNode) + Call Children.Add(objChildDiffInfo) + Next + + Call Children.Sort(New DiffInfoComparator) + End If + Call EndFunction("DiffInfo.BuildFromXmlNode") + End Sub + + Public Sub BuildFromXmlDocument(xmlDocument) + Call StartFunction("DiffInfo.BuildFromXmlDocument") + Dim xmlNode, xmlChildNode, objChildDiffInfo + + Set xmlNode = xmlDocument.selectSingleNode("diff") + + Set OldFileSystemInfo = New FileSystemInfo + + OldFileSystemInfo.FileType = "Folder" + OldFileSystemInfo.Revision = CLng(xmlNode.attributes.getNamedItem("oldRevision").nodeValue) + + Set NewFileSystemInfo = New FileSystemInfo + + NewFileSystemInfo.FileType = "Folder" + NewFileSystemInfo.Revision = CLng(xmlNode.attributes.getNamedItem("newRevision").nodeValue) + + Set Children = New Collection + For Each xmlChildNode In xmlNode.selectNodes("item") + Set objChildDiffInfo = New DiffInfo + Call objChildDiffInfo.BuildFromXmlNode(xmlChildNode) + Call Children.Add(objChildDiffInfo) + Next + Call EndFunction("DiffInfo.BuildFromXmlDocument") + End Sub + + Public Sub BuildFromXmlFile(strPath) + Call StartFunction("DiffInfo.BuildFromXmlFile") + Dim xmlDocument + + Set xmlDocument = CreateObject("MSXML2.DOMDocument") + Call xmlDocument.load(strPath) + + Call BuildFromXmlDocument(xmlDocument) + Call EndFunction("DiffInfo.BuildFromXmlFile") + End Sub + + Public Function ToXmlNode(xmlDocument) + Call StartFunction("DiffInfo.ToXmlNode") + Dim xmlNode + Dim objChildDiffInfo + + Set xmlNode = xmlDocument.createElement("item") + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("name")) + xmlNode.attributes.getNamedItem("name").nodeValue = Name + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("status")) + xmlNode.attributes.getNamedItem("status").nodeValue = Status + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("fileType")) + xmlNode.attributes.getNamedItem("fileType").nodeValue = FileType + + If Not OldFileSystemInfo Is Nothing Then + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("oldAttributes")) + xmlNode.attributes.getNamedItem("oldAttributes").nodeValue = OldFileSystemInfo.Attributes + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("oldDateCreated")) + xmlNode.attributes.getNamedItem("oldDateCreated").nodeValue = OldFileSystemInfo.DateCreated + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("oldDateLastModified")) + xmlNode.attributes.getNamedItem("oldDateLastModified").nodeValue = OldFileSystemInfo.DateLastModified + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("oldSize")) + xmlNode.attributes.getNamedItem("oldSize").nodeValue = OldFileSystemInfo.Size + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("oldRevision")) + xmlNode.attributes.getNamedItem("oldRevision").nodeValue = OldFileSystemInfo.Revision + End If + + If Not NewFileSystemInfo Is Nothing Then + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("newAttributes")) + xmlNode.attributes.getNamedItem("newAttributes").nodeValue = NewFileSystemInfo.Attributes + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("newDateCreated")) + xmlNode.attributes.getNamedItem("newDateCreated").nodeValue = NewFileSystemInfo.DateCreated + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("newDateLastModified")) + xmlNode.attributes.getNamedItem("newDateLastModified").nodeValue = NewFileSystemInfo.DateLastModified + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("newSize")) + xmlNode.attributes.getNamedItem("newSize").nodeValue = NewFileSystemInfo.Size + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("newRevision")) + xmlNode.attributes.getNamedItem("newRevision").nodeValue = NewFileSystemInfo.Revision + End If + + If Not Children Is Nothing Then + For Each objChildDiffInfo In Children.Items + Call xmlNode.appendChild(objChildDiffInfo.ToXmlNode(xmlDocument)) + Next + End If + + Set ToXmlNode = xmlNode + Call EndFunction("DiffInfo.ToXmlNode") + End Function + + Public Function ToXmlDocument() + Call StartFunction("DiffInfo.ToXmlDocument") + Dim xmlDocument, xmlNode + Dim objChildDiffInfo + + Set xmlDocument = CreateObject("MSXML2.DOMDocument") + + Set xmlNode = xmlDocument.createElement("diff") + Call xmlDocument.appendChild(xmlNode) + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("oldRevision")) + xmlNode.attributes.getNamedItem("oldRevision").nodeValue = OldFileSystemInfo.Revision + + Call xmlNode.attributes.setNamedItem(xmlDocument.createAttribute("newRevision")) + xmlNode.attributes.getNamedItem("newRevision").nodeValue = NewFileSystemInfo.Revision + + For Each objChildDiffInfo In Children.Items + Call xmlNode.appendChild(objChildDiffInfo.ToXmlNode(xmlDocument)) + Next + + Set ToXmlDocument = xmlDocument + Call EndFunction("DiffInfo.ToXmlDocument") + End Function + + Public Function ToXmlFile(strFilePath) + Call StartFunction("DiffInfo.ToXmlFile") + Call ToXmlDocument().save(strFilePath) + Call EndFunction("DiffInfo.ToXmlFile") + End Function + + Public Sub Backup(strSourceFolderPath, strDestinationFolderPath) + Call StartFunction("DiffInfo.Backup") + Dim objChildDiffInfo + Dim strSrcPath, strDstPath + + For Each objChildDiffInfo In Children.Items + If objChildDiffInfo.Status = "Create" Or objChildDiffInfo.Status = "Update" Or objChildDiffInfo.Status = "" Then + strSrcPath = gfso.BuildPath(strSourceFolderPath, objChildDiffInfo.Name) + strDstPath = gfso.BuildPath(strDestinationFolderPath, objChildDiffInfo.Name) + + If objChildDiffInfo.FileType = "File" Then + Call CopyFile(strSrcPath, strDstPath) + Else + Call CreateFolder(strDstPath) + Call objChildDiffInfo.Backup(strSrcPath, strDstPath) + End If + End If + Next + Call EndFunction("DiffInfo.Backup") + End Sub + + Public Sub Merge(objFileSystemInfo) + Call StartFunction("DiffInfo.Merge") + Dim objChildDiffInfo + + For Each objChildDiffInfo In Children.Items + If objChildDiffInfo.Status = "" Then + Call objChildDiffInfo.Merge(objFileSystemInfo.Children.Item(objChildDiffInfo.Name)) + End If + Next + + For Each objChildDiffInfo In Children.Items + If objChildDiffInfo.Status = "Delete" Then + Call objFileSystemInfo.Children.Remove(objChildDiffInfo.Name) + End If + Next + + For Each objChildDiffInfo In Children.Items + If objChildDiffInfo.Status = "Update" Then + If objChildDiffInfo.FileType = "File" Then + Set objFileSystemInfo.Children.Item(objChildDiffInfo.Name) = objChildDiffInfo.NewFileSystemInfo + Else + Set objChildDiffInfo.NewFileSystemInfo.Children = objFileSystemInfo.Children(objChildDiffInfo.Name).Children + Set objFileSystemInfo.Children.Item(objChildDiffInfo.Name) = objChildDiffInfo.NewFileSystemInfo + Call objChildDiffInfo.Merge(objChildDiffInfo.NewFileSystemInfo) + End If + End If + Next + + For Each objChildDiffInfo In Children.Items + If objChildDiffInfo.Status = "Create" Then + If objChildDiffInfo.FileType = "File" Then + Set objFileSystemInfo.Children.Item(objChildDiffInfo.Name) = objChildDiffInfo.NewFileSystemInfo + Else + Set objChildDiffInfo.NewFileSystemInfo.Children = New Dictionary + Set objFileSystemInfo.Children.Item(objChildDiffInfo.Name) = objChildDiffInfo.NewFileSystemInfo + Call objChildDiffInfo.Merge(objChildDiffInfo.NewFileSystemInfo) + End If + End If + Next + + If objFileSystemInfo.FileType = "Folder" Then + Call objFileSystemInfo.Children.Sort(New PrimitiveKeyComparator) + End If + Call EndFunction("DiffInfo.Merge") + End Sub +End Class + +Class DiffInfoComparator + Public Function Compare(objDiffInfo1, objDiffInfo2) + Call StartFunction("DiffInfoComparator.Compare") + Dim intStatus1, intStatus2 + + intStatus1 = StatusToInteger(objDiffInfo1.Status) + intStatus2 = StatusToInteger(objDiffInfo2.Status) + + If intStatus1 > intStatus2 Then + Compare = 1 + ElseIf intStatus1 < intStatus2 Then + Compare = -1 + ElseIf objDiffInfo1.Name > objDiffInfo2.Name Then + Compare = 1 + ElseIf objDiffInfo1.Name < objDiffInfo2.Name Then + Compare = -1 + Else + Compare = 0 + End If + Call EndFunction("DiffInfoComparator.Compare") + End Function + + Private Function StatusToInteger(strStatus) + Call StartFunction("DiffInfoComparator.StatusToInteger") + If strStatus = "Create" Then + StatusToInteger = 0 + ElseIf strStatus = "Update" Then + StatusToInteger = 1 + ElseIf strStatus = "Delete" Then + StatusToInteger = 2 + Else + StatusToInteger = 3 + End If + Call EndFunction("DiffInfoComparator.StatusToInteger") + End Function +End Class + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +' ******************************************************************************** +' ƒtƒ@ƒCƒ‹I/O +' ******************************************************************************** + +Sub WriteFile(strFilePath, strText) +Call StartFunction("WriteFile,strFilePath=" & strFilePath & ",Len(strText)=" & Len(strText)) + Dim fsoStream + + Set fsoStream = gfso.OpenTextFile(strFilePath, IOMode_ForWriting, True) + Call fsoStream.Write(strText) + Call fsoStream.Close() +Call EndFunction("WriteFile") +End Sub + +Sub AppendFile(strFilePath, strText) +Call StartFunction("AppendFile,strFilePath=" & strFilePath & ",Len(strText)=" & Len(strText)) + Dim fsoStream + + Set fsoStream = gfso.OpenTextFile(strFilePath, IOMode_ForAppending, True) + Call fsoStream.Write(strText) + Call fsoStream.Close() +Call EndFunction("AppendFile") +End Sub + +Function ReadFile(strFilePath) +Call StartFunction("ReadFile,strFilePath=" & strFilePath) + Dim fsoStream + + Set fsoStream = gfso.OpenTextFile(strFilePath) + ReadFile = fsoStream.ReadAll() + Call fsoStream.Close() +Call StartFunction("ReadFile,Len=" & Len(ReadFile)) +End Function + +Sub CopyFile(strSourceFilePath, strDestinationFilePath) +Call StartFunction("CopyFile,strSourceFilePath=" & strSourceFilePath & ",strDestinationFilePath=" & strDestinationFilePath) + Call gfso.CopyFile(strSourceFilePath, strDestinationFilePath) +Call EndFunction("CopyFile") +End Sub + +Function CreateFolder(strFolderPath) +Call StartFunction("CreateFolder,strFolderPath=" & strFolderPath) + Set CreateFolder = gfso.CreateFolder(strFolderPath) +Call EndFunction("CreateFolder") +End Function + +' ***** ƒƒOo—Í ***** + +Sub WriteLog(strText) + If Not gfsoLogStream Is Nothing And Not IsEmpty(gfsoLogStream) Then + Call gfsoLogStream.Write(GetNowToString() & "," & strText & vbCrLf) + End If +End Sub + +Sub StartFunction(strFunctionName) + If Not gfsoLogStream Is Nothing And Not IsEmpty(gfsoLogStream) And gblnDebugLog Then + Call gfsoLogStream.Write(GetNowToString() & ",[StartFunction]" & strFunctionName & vbCrLf) + End If +End Sub + +Sub EndFunction(strFunctionName) + If Not gfsoLogStream Is Nothing And Not IsEmpty(gfsoLogStream) And gblnDebugLog Then + Call gfsoLogStream.Write(GetNowToString() & ",[EndFunction]" & strFunctionName & vbCrLf) + End If +End Sub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +' ******************************************************************************** +' •¶Žš—ñ‘€ì +' ******************************************************************************** + +Function GetNowToString() + GetNowToString = Year(Now()) & "-" _ + & Right("00" & Month(Now()), 2) & "-" _ + & Right("00" & Day(Now()), 2) & " " _ + & Right("00" & Hour(Now()), 2) & ":" _ + & Right("00" & Minute(Now()), 2) & ":" _ + & Right("00" & Second(Now()), 2) +End Function + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +' ******************************************************************************** +' ƒRƒŒƒNƒVƒ‡ƒ“ +' ******************************************************************************** + +Class Collection + Private paryItem + + Private Sub Class_Initialize() + ReDim paryItem(-1) + End Sub + + Public Property Get Count() + Count = UBound(paryItem) + 1 + End Property + + Public Property Get Item(intIndex) + If IsObject(paryItem(intIndex)) Then + Set Item = paryItem(intIndex) + Else + Item = paryItem(intIndex) + End If + End Property + + Public Property Let Item(intIndex, varItem) + paryItem(intIndex) = varItem + End Property + + Public Property Set Item(intIndex, varItem) + Set paryItem(intIndex) = varItem + End Property + + Public Property Get Items() + Items = CopyArray(paryItem) + End Property + + Public Sub Add(varItem) + Call AddArray(paryItem, varItem) + End Sub + + Public Function Remove(intIndex) + If IsObject(paryItem(intIndex)) Then + Set Remove = paryItem(intIndex) + Else + Remove = paryItem(intIndex) + End If + + Call RemoveArray(paryItem, intIndex) + End Function + + Public Function RemoveLast() + Call Remove(Count - 1) + End Function + + Public Function Exist(varItem) + Exist = (FindItem(varItem) > -1) + End Function + + Public Function FindItem(varItem) + Dim intIndex + + FindItem = -1 + + For intIndex = 0 To UBound(paryItem) + If paryItem(intIndex) = varItem Then + FindItem = intIndex + Exit For + End If + Next + End Function + + Public Sub Sort(objComparator) + Call SortArray(paryItem, objComparator) + End Sub + + Public Function Copy() + Dim col, varItem + + Set col = New Collection + + For Each varItem In paryItem + Call col.Add(varItem) + Next + + Set Copy = col + End Function + + Public Function IsEqual(col) + Dim intIndex + + If TypeName(col) <> "Collection" Then + IsEqual = False + Exit Function + ElseIf col.Count <> Count Then + IsEqual = False + Exit Function + Else + For intIndex = 0 To Count - 1 + If Item(intIndex) <> col.Item(intIndex) Then + IsEqual = False + Exit Function + End If + Next + End If + + IsEqual = True + End Function +End Class + +Class Dictionary + Private paryItem + + Private Sub Class_Initialize() + ReDim paryItem(-1) + End Sub + + Public Property Get Count() + Count = UBound(paryItem) + 1 + End Property + + Public Property Set Item(varKey, varValue) + Call SetItem(varKey, varValue) + End Property + + Public Property Let Item(varKey, varValue) + Call SetItem(varKey, varValue) + End Property + + Public Property Get Item(varKey) + Dim intIndex + + intIndex = FindIndex(varKey) + + If intIndex <> -1 Then + If IsObject(paryItem(intIndex).Value) Then + Set Item = paryItem(intIndex).Value + Else + Item = paryItem(intIndex).Value + End If + Else + Item = Empty + End If + End Property + + Public Property Get Items() + Items = CopyArray(paryItem) + End Property + + Public Property Get Keys() + Dim aryKey, intIndex + + ReDim aryKey(UBound(paryItem)) + + For intIndex = 0 To UBound(paryItem) + If IsObject(paryItem(intIndex).Key) Then + Set aryKey(intIndex) = paryItem(intIndex).Key + Else + aryKey(intIndex) = paryItem(intIndex).Key + End If + Next + + Keys = aryKey + End Property + + Public Property Get Values() + Dim aryValue, intIndex + + ReDim aryValue(UBound(paryItem)) + + For intIndex = 0 To UBound(paryItem) + If IsObject(paryItem(intIndex).Value) Then + Set aryValue(intIndex) = paryItem(intIndex).Value + Else + aryValue(intIndex) = paryItem(intIndex).Value + End If + Next + + Values = aryValue + End Property + + Private Sub SetItem(varKey, varValue) + Dim intIndex + Dim objDicItem + + intIndex = FindIndex(varKey) + + If intIndex <> -1 Then + If IsObject(varValue) Then + Set paryItem(intIndex).Value = varValue + Else + paryItem(intIndex).Value = varValue + End If + Else + Set objDicItem = New DictionaryItem + If IsObject(varKey) Then + Set objDicItem.Key = varKey + Else + objDicItem.Key = varKey + End If + If IsObject(varValue) Then + Set objDicItem.Value = varValue + Else + objDicItem.Value = varValue + End If + + Call AddArray(paryItem, objDicItem) + End If + End Sub + + Public Function Remove(varKey) + Dim intIndex + + intIndex = FindIndex(varKey) + + If IsObject(paryItem(intIndex).Value) Then + Set Remove = paryItem(intIndex).Value + Else + Remove = paryItem(intIndex).Value + End If + + Call RemoveArray(paryItem, intIndex) + End Function + + Private Function FindIndex(varKey) + Dim intIndex + + FindIndex = -1 + + For intIndex = 0 To UBound(paryItem) + If paryItem(intIndex).Key = varKey Then + FindIndex = intIndex + End If + Next + End Function + + Public Sub Sort(objComparator) + Call SortArray(paryItem, objComparator) + End Sub + + Public Function Copy() + Dim dic, varKey + + Set dic = New Dictionary + + For Each varKey In Keys + If IsObject(Item(varKey)) Then + Set dic.Item(varKey) = Item(varKey) + Else + dic.Item(varKey) = Item(varKey) + End If + Next + + Set Copy = dic + End Function + + Public Function ExistKey(varKey) + ExistKey = (FindIndex(varKey) <> -1) + End Function + + Public Function ExistValue(varValue) + Dim var + + ExistValue = False + + For Each var In Values + If var = varValue Then + ExistValue = True + Exit For + End If + Next + End Function + + Public Function IsEqual(dic) + Dim varKey + + If TypeName(dic) <> "Dictionary" Then + IsEqual = False + Exit Function + ElseIf dic.Count <> Count Then + IsEqual = False + Exit Function + Else + For Each varKey In dic.Keys + If dic.Item(varKey) <> Item(varKey) Then + IsEqual = False + Exit Function + End If + Next + End If + + IsEqual = True + End Function +End Class + +Class DictionaryItem + Public Key + Public Value +End Class + +Sub AddArray(ary, var) + ReDim Preserve ary(UBound(ary) + 1) + + If IsObject(var) Then + Set ary(UBound(ary)) = var + Else + ary(UBound(ary)) = var + End If +End Sub + +Sub RemoveArray(ary, intRemoveIndex) + Dim intIndex + + For intIndex = intRemoveIndex + 1 To UBound(ary) + If IsObject(ary(intIndex)) Then + Set ary(intIndex - 1) = ary(intIndex) + Else + ary(intIndex - 1) = ary(intIndex) + End If + Next + + ReDim Preserve ary(UBound(ary) - 1) +End Sub + +Function CopyArray(ary) + Dim aryDst + ReDim aryDst(UBound(ary)) + Dim intIndex + + For intIndex = 0 To UBound(ary) + If IsObject(ary(intIndex)) Then + Set aryDst(intIndex) = ary(intIndex) + Else + aryDst(intIndex) = ary(intIndex) + End If + Next + + CopyArray = aryDst +End Function + +Sub SortArray(ary, objComparator) + Dim intIndex, intIndex2 + Dim varTemp + + For intIndex = 0 To UBound(ary) - 1 + For intIndex2 = intIndex + 1 To UBound(ary) + If objComparator.Compare(ary(intIndex), ary(intIndex2)) > 0 Then + If IsObject(ary(intIndex)) Then + Set varTemp = ary(intIndex) + Else + varTemp = ary(intIndex) + End If + + If IsObject(ary(intIndex2)) Then + Set ary(intIndex) = ary(intIndex2) + Else + ary(intIndex) = ary(intIndex2) + End If + + If IsObject(varTemp) Then + Set ary(intIndex2) = varTemp + Else + ary(intIndex2) = varTemp + End If + End If + Next + Next +End Sub + +Class PrimitiveComparator + Public Function Compare(var1, var2) + If var1 > var2 Then + Compare = 1 + ElseIf var1 < var2 Then + Compare = -1 + Else + Compare = 0 + End If + End Function +End Class + +Class PrimitiveKeyComparator + Public Function Compare(dicItem1, dicItem2) + If dicItem1.Key > dicItem2.Key Then + Compare = 1 + ElseIf dicItem1.Key < dicItem2.Key Then + Compare = -1 + Else + Compare = 0 + End If + End Function +End Class + +' ***** ƒeƒXƒg ***** + +Sub AssertEqual(var1, var2, strMessage) + If var1 <> var2 Then + Call Err.Raise(1, "ƒeƒXƒgŽ¸”s", strMessage) + End If +End Sub + +Sub TestAddArray() + Dim ary + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + Call AddArray(ary, "hoge") + + Call AssertEqual(UBound(ary), 3, "TestAddArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(ary(0), "foo", "TestAddArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(1), "bar", "TestAddArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(2), "boo", "TestAddArray - ”z—ñ‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(3), "hoge", "TestAddArray - ”z—ñ‚Ì—v‘f3‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestRemoveArray() + Dim ary + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + Call RemoveArray(ary, 1) + + Call AssertEqual(UBound(ary), 1, "TestRemoveArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(ary(0), "foo", "TestRemoveArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(1), "boo", "TestRemoveArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCopyArray() + Dim ary, aryDst + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + aryDst = CopyArray(ary) + + Call AssertEqual(UBound(aryDst), 2, "TestCopyArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(aryDst(0), "foo", "TestCopyArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(aryDst(1), "bar", "TestCopyArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(aryDst(2), "boo", "TestCopyArray - ”z—ñ‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestSortArray() + Dim ary + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + Call SortArray(ary, New PrimitiveComparator) + + Call AssertEqual(UBound(ary), 2, "TestSortArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(ary(0), "bar", "TestSortArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(1), "boo", "TestSortArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(2), "foo", "TestSortArray - ”z—ñ‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionClassInitialize() + Dim col + + Set col = New Collection + + Call AssertEqual(col.Count, 0, "TestCollectionCount - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionAdd() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Count, 3, "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "foo", "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "bar", "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(2), "boo", "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionRemove() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Remove(1), "bar", "TestCollectionRemove - íœ‚µ‚½’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Count, 2, "TestCollectionRemove - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "foo", "TestCollectionRemove - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "boo", "TestCollectionRemove - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionItems() + Dim col, var, intIndex + + Set col = New Collection + + For Each var In col.Items + Call AssertEqual(True, False, "TestCollectionItems - —ñ‹“”͈͂𒴂¦‚½B") + Next + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Count, 3, "TestCollectionItems - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + intIndex = 0 + + For Each var In col.Items + If intIndex = 0 Then + Call AssertEqual(var, "foo", "TestCollectionItems - —ñ‹“‚³‚ꂽ’l(0)‚ªˆÙ‚È‚éB") + ElseIf intIndex = 1 Then + Call AssertEqual(var, "bar", "TestCollectionItems - —ñ‹“‚³‚ꂽ’l(1)‚ªˆÙ‚È‚éB") + ElseIf intIndex = 2 Then + Call AssertEqual(var, "boo", "TestCollectionItems - —ñ‹“‚³‚ꂽ’l(2)‚ªˆÙ‚È‚éB") + Else + Call AssertEqual(True, False, "TestCollectionItems - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Sub TestCollectionSort() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call col.Sort(New PrimitiveComparator) + + Call AssertEqual(col.Count, 3, "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "bar", "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "boo", "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(2), "foo", "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionCopy() + Dim col, colDst + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Set colDst = col.Copy() + + Call AssertEqual(col.Count, 3, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "foo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "bar", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(2), "boo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(colDst.Count, 3, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(colDst.Item(0), "foo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(colDst.Item(1), "bar", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(colDst.Item(2), "boo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") + + Call colDst.Add("hoge") + + Call AssertEqual(col.Count, 3, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + Call AssertEqual(colDst.Count, 4, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionExist() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Exist("bar"), True, "TestCollectionExist - ‘¶Ý”»’肪ŠÔˆá‚¦‚Ä‚¢‚é(True)B") + Call AssertEqual(col.Exist("hoge"), False, "TestCollectionExist - ‘¶Ý”»’肪ŠÔˆá‚¦‚Ä‚¢‚é(False)B") +End Sub + +Sub TestCollectionFindItem() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.FindItem("bar"), 1, "TestCollectionFindItem - —v‘f‚ÌŒŸõŒ‹‰Ê‚ªˆÙ‚È‚é(1)B") + Call AssertEqual(col.FindItem("hoge"), -1, "TestCollectionFindItem - —v‘f‚ÌŒŸõŒ‹‰Ê‚ªˆÙ‚È‚é(-1)B") +End Sub + +Sub TestCollectionIsEqual() + Dim col1, col2 + + Set col1 = New Collection + + Call col1.Add("foo") + Call col1.Add("bar") + Call col1.Add("boo") + + Set col2 = New Collection + + Call col2.Add("foo") + Call col2.Add("bar") + Call col2.Add("boo") + + Call AssertEqual(col1.IsEqual(col2), True, "TestCollectionIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(True)") + + Call col2.Add("hoge") + + Call AssertEqual(col1.IsEqual(col2), False, "TestCollectionIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Public Sub TestDictionaryClassInitialize() + Dim dic + + Set dic = New Dictionary + + Call AssertEqual(dic.Count, 0, "TestDictionaryInitialize - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Public Sub TestDictionaryItem() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("bar"), "‚΁[", "TestDictionaryItem - —v‘f(bar)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("hoge"), Empty, "TestDictionaryItem - ‘¶Ý‚µ‚È‚¢—v‘f‚Ì’l‚ªEmpty‚Å‚Í‚È‚¢B") +End Sub + +Public Sub TestDictionaryRemove() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.Remove("bar"), "‚΁[", "TestDictionaryRemove - íœ‚µ‚½—v‘f‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Count, 2, "TestDictionaryRemove - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Public Sub TestDictionaryItems() + Dim dic, varItem, intIndex + + Set dic = New Dictionary + + For Each varItem In dic.Items + Call AssertEqual(True, False, "TestDictionaryItems - —ñ‹“”͈͂𒴂¦‚½B") + Next + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + intIndex = 0 + + For Each varItem In dic.Items + If intIndex = 0 Then + Call AssertEqual(varItem.Key, "foo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(0)") + Call AssertEqual(varItem.Value, "‚Ӂ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varItem.Key, "bar", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(1)") + Call AssertEqual(varItem.Value, "‚΁[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varItem.Key, "boo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(2)") + Call AssertEqual(varItem.Value, "‚ԁ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryItems - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionaryKeys() + Dim dic, varKey, intIndex + + Set dic = New Dictionary + + For Each varKey In dic.Keys + Call AssertEqual(True, False, "TestDictionaryKeys - —ñ‹“”͈͂𒴂¦‚½B") + Next + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + intIndex = 0 + + For Each varKey In dic.Keys + If intIndex = 0 Then + Call AssertEqual(varKey, "foo", "TestDictionaryKeys - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varKey, "bar", "TestDictionaryKeys - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varKey, "boo", "TestDictionaryKeys - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryKeys - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionaryValues() + Dim dic, varValue, intIndex + + Set dic = New Dictionary + + For Each varValue In dic.Values + Call AssertEqual(True, False, "TestDictionaryValues - —ñ‹“”͈͂𒴂¦‚½B") + Next + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + intIndex = 0 + + For Each varValue In dic.Values + If intIndex = 0 Then + Call AssertEqual(varValue, "‚Ӂ[", "TestDictionaryValues - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varValue, "‚΁[", "TestDictionaryValues - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varValue, "‚ԁ[", "TestDictionaryValues - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryValues - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionarySort() + Dim dic, varItem, intIndex + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call dic.Sort(New PrimitiveKeyComparator) + + intIndex = 0 + + For Each varItem In dic.Items + If intIndex = 0 Then + Call AssertEqual(varItem.Key, "bar", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(0)") + Call AssertEqual(varItem.Value, "‚΁[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varItem.Key, "boo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(1)") + Call AssertEqual(varItem.Value, "‚ԁ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varItem.Key, "foo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(2)") + Call AssertEqual(varItem.Value, "‚Ӂ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryItems - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionaryCopy() + Dim dic, dic2 + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Set dic2 = dic.Copy() + + Call AssertEqual(dic.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("bar"), "‚΁[", "TestDictionaryItem - —v‘f(bar)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic2.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic2.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic2.Item("bar"), "‚΁[", "TestDictionaryItem - —v‘f(bar)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic2.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") + + dic2.Item("hoge") = "‚Ù‚°" + + Call AssertEqual(dic.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + Call AssertEqual(dic2.Count, 4, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Public Sub TestDictionaryExistKey() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.ExistKey("foo"), True, "TestDictionaryExistKey - ‘¶Ý”»’肪ˆÙ‚È‚éB(True)") + Call AssertEqual(dic.ExistKey("hoge"), False, "TestDictionaryExistKey - ‘¶Ý”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Public Sub TestDictionaryExistValue() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.ExistValue("‚Ӂ["), True, "TestDictionaryExistValue - ‘¶Ý”»’肪ˆÙ‚È‚éB(True)") + Call AssertEqual(dic.ExistValue("foo"), False, "TestDictionaryExistValue - ‘¶Ý”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Public Sub TestDictionaryIsEqual() + Dim dic1, dic2 + + Set dic1 = New Dictionary + + dic1.Item("foo") = "‚Ӂ[" + dic1.Item("bar") = "‚΁[" + dic1.Item("boo") = "‚ԁ[" + + Set dic2 = New Dictionary + + dic2.Item("foo") = "‚Ӂ[" + dic2.Item("bar") = "‚΁[" + dic2.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic1.IsEqual(dic2), True, "TestDictionaryIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(True)") + + dic2.Item("boo") = "‚Ô‚£" + + Call AssertEqual(dic1.IsEqual(dic2), False, "TestDictionaryIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Sub AllTest() + Call TestAddArray() + Call TestRemoveArray() + Call TestCopyArray() + Call TestSortArray() + + Call TestCollectionClassInitialize() + Call TestCollectionAdd() + Call TestCollectionRemove() + Call TestCollectionItems() + Call TestCollectionSort() + Call TestCollectionCopy() + Call TestCollectionExist() + Call TestCollectionFindItem() + Call TestCollectionIsEqual() + + Call TestDictionaryClassInitialize() + Call TestDictionaryItem() + Call TestDictionaryRemove() + Call TestDictionaryItems() + Call TestDictionaryKeys() + Call TestDictionaryValues() + Call TestDictionarySort() + Call TestDictionaryCopy() + Call TestDictionaryExistKey() + Call TestDictionaryExistValue() + Call TestDictionaryIsEqual() + + MsgBox "ƒeƒXƒg¬Œ÷" +End Sub + +Call AllTest() diff --git a/bak.vbs/ƒeƒXƒg.vbs b/bak.vbs/ƒeƒXƒg.vbs new file mode 100644 index 0000000..75285c9 --- /dev/null +++ b/bak.vbs/ƒeƒXƒg.vbs @@ -0,0 +1,7 @@ +Option Explicit + +Sub AssertEqual(var1, var2, strMessage) + If var1 <> var2 Then + Call Err.Raise(1, "ƒeƒXƒgŽ¸”s", strMessage) + End If +End Sub diff --git a/bak.vbs/”z—ñEƒRƒŒƒNƒVƒ‡ƒ“.vbs b/bak.vbs/”z—ñEƒRƒŒƒNƒVƒ‡ƒ“.vbs new file mode 100644 index 0000000..f9c0dbc --- /dev/null +++ b/bak.vbs/”z—ñEƒRƒŒƒNƒVƒ‡ƒ“.vbs @@ -0,0 +1,913 @@ +Option Explicit + +Class Collection + Private paryItem + + Private Sub Class_Initialize() + ReDim paryItem(-1) + End Sub + + Public Property Get Count() + Count = UBound(paryItem) + 1 + End Property + + Public Property Get Item(intIndex) + If IsObject(paryItem(intIndex)) Then + Set Item = paryItem(intIndex) + Else + Item = paryItem(intIndex) + End If + End Property + + Public Property Let Item(intIndex, varItem) + paryItem(intIndex) = varItem + End Property + + Public Property Set Item(intIndex, varItem) + Set paryItem(intIndex) = varItem + End Property + + Public Property Get Items() + Items = CopyArray(paryItem) + End Property + + Public Sub Add(varItem) + Call AddArray(paryItem, varItem) + End Sub + + Public Function Remove(intIndex) + If IsObject(paryItem(intIndex)) Then + Set Remove = paryItem(intIndex) + Else + Remove = paryItem(intIndex) + End If + + Call RemoveArray(paryItem, intIndex) + End Function + + Public Function RemoveLast() + Call Remove(Count - 1) + End Function + + Public Function Exist(varItem) + Exist = (FindItem(varItem) > -1) + End Function + + Public Function FindItem(varItem) + Dim intIndex + + FindItem = -1 + + For intIndex = 0 To UBound(paryItem) + If paryItem(intIndex) = varItem Then + FindItem = intIndex + Exit For + End If + Next + End Function + + Public Sub Sort(objComparator) + Call SortArray(paryItem, objComparator) + End Sub + + Public Function Copy() + Dim col, varItem + + Set col = New Collection + + For Each varItem In paryItem + Call col.Add(varItem) + Next + + Set Copy = col + End Function + + Public Function IsEqual(col) + Dim intIndex + + If TypeName(col) <> "Collection" Then + IsEqual = False + Exit Function + ElseIf col.Count <> Count Then + IsEqual = False + Exit Function + Else + For intIndex = 0 To Count - 1 + If Item(intIndex) <> col.Item(intIndex) Then + IsEqual = False + Exit Function + End If + Next + End If + + IsEqual = True + End Function +End Class + +Class Dictionary + Private paryItem + + Private Sub Class_Initialize() + ReDim paryItem(-1) + End Sub + + Public Property Get Count() + Count = UBound(paryItem) + 1 + End Property + + Public Property Set Item(varKey, varValue) + Call SetItem(varKey, varValue) + End Property + + Public Property Let Item(varKey, varValue) + Call SetItem(varKey, varValue) + End Property + + Public Property Get Item(varKey) + Dim intIndex + + intIndex = FindIndex(varKey) + + If intIndex <> -1 Then + If IsObject(paryItem(intIndex).Value) Then + Set Item = paryItem(intIndex).Value + Else + Item = paryItem(intIndex).Value + End If + Else + Item = Empty + End If + End Property + + Public Property Get Items() + Items = CopyArray(paryItem) + End Property + + Public Property Get Keys() + Dim aryKey, intIndex + + ReDim aryKey(UBound(paryItem)) + + For intIndex = 0 To UBound(paryItem) + If IsObject(paryItem(intIndex).Key) Then + Set aryKey(intIndex) = paryItem(intIndex).Key + Else + aryKey(intIndex) = paryItem(intIndex).Key + End If + Next + + Keys = aryKey + End Property + + Public Property Get Values() + Dim aryValue, intIndex + + ReDim aryValue(UBound(paryItem)) + + For intIndex = 0 To UBound(paryItem) + If IsObject(paryItem(intIndex).Value) Then + Set aryValue(intIndex) = paryItem(intIndex).Value + Else + aryValue(intIndex) = paryItem(intIndex).Value + End If + Next + + Values = aryValue + End Property + + Private Sub SetItem(varKey, varValue) + Dim intIndex + Dim objDicItem + + intIndex = FindIndex(varKey) + + If intIndex <> -1 Then + If IsObject(varValue) Then + Set paryItem(intIndex).Value = varValue + Else + paryItem(intIndex).Value = varValue + End If + Else + Set objDicItem = New DictionaryItem + If IsObject(varKey) Then + Set objDicItem.Key = varKey + Else + objDicItem.Key = varKey + End If + If IsObject(varValue) Then + Set objDicItem.Value = varValue + Else + objDicItem.Value = varValue + End If + + Call AddArray(paryItem, objDicItem) + End If + End Sub + + Public Function Remove(varKey) + Dim intIndex + + intIndex = FindIndex(varKey) + + If IsObject(paryItem(intIndex).Value) Then + Set Remove = paryItem(intIndex).Value + Else + Remove = paryItem(intIndex).Value + End If + + Call RemoveArray(paryItem, intIndex) + End Function + + Private Function FindIndex(varKey) + Dim intIndex + + FindIndex = -1 + + For intIndex = 0 To UBound(paryItem) + If paryItem(intIndex).Key = varKey Then + FindIndex = intIndex + End If + Next + End Function + + Public Sub Sort(objComparator) + Call SortArray(paryItem, objComparator) + End Sub + + Public Function Copy() + Dim dic, varKey + + Set dic = New Dictionary + + For Each varKey In Keys + If IsObject(Item(varKey)) Then + Set dic.Item(varKey) = Item(varKey) + Else + dic.Item(varKey) = Item(varKey) + End If + Next + + Set Copy = dic + End Function + + Public Function ExistKey(varKey) + ExistKey = (FindIndex(varKey) <> -1) + End Function + + Public Function ExistValue(varValue) + Dim var + + ExistValue = False + + For Each var In Values + If var = varValue Then + ExistValue = True + Exit For + End If + Next + End Function + + Public Function IsEqual(dic) + Dim varKey + + If TypeName(dic) <> "Dictionary" Then + IsEqual = False + Exit Function + ElseIf dic.Count <> Count Then + IsEqual = False + Exit Function + Else + For Each varKey In dic.Keys + If dic.Item(varKey) <> Item(varKey) Then + IsEqual = False + Exit Function + End If + Next + End If + + IsEqual = True + End Function +End Class + +Class DictionaryItem + Public Key + Public Value +End Class + +Sub AddArray(ary, var) + ReDim Preserve ary(UBound(ary) + 1) + + If IsObject(var) Then + Set ary(UBound(ary)) = var + Else + ary(UBound(ary)) = var + End If +End Sub + +Sub RemoveArray(ary, intRemoveIndex) + Dim intIndex + + For intIndex = intRemoveIndex + 1 To UBound(ary) + If IsObject(ary(intIndex)) Then + Set ary(intIndex - 1) = ary(intIndex) + Else + ary(intIndex - 1) = ary(intIndex) + End If + Next + + ReDim Preserve ary(UBound(ary) - 1) +End Sub + +Function CopyArray(ary) + Dim aryDst + ReDim aryDst(UBound(ary)) + Dim intIndex + + For intIndex = 0 To UBound(ary) + If IsObject(ary(intIndex)) Then + Set aryDst(intIndex) = ary(intIndex) + Else + aryDst(intIndex) = ary(intIndex) + End If + Next + + CopyArray = aryDst +End Function + +Sub SortArray(ary, objComparator) + Dim intIndex, intIndex2 + Dim varTemp + + For intIndex = 0 To UBound(ary) - 1 + For intIndex2 = intIndex + 1 To UBound(ary) + If objComparator.Compare(ary(intIndex), ary(intIndex2)) > 0 Then + If IsObject(ary(intIndex)) Then + Set varTemp = ary(intIndex) + Else + varTemp = ary(intIndex) + End If + + If IsObject(ary(intIndex2)) Then + Set ary(intIndex) = ary(intIndex2) + Else + ary(intIndex) = ary(intIndex2) + End If + + If IsObject(varTemp) Then + Set ary(intIndex2) = varTemp + Else + ary(intIndex2) = varTemp + End If + End If + Next + Next +End Sub + +Class PrimitiveComparator + Public Function Compare(var1, var2) + If var1 > var2 Then + Compare = 1 + ElseIf var1 < var2 Then + Compare = -1 + Else + Compare = 0 + End If + End Function +End Class + +Class PrimitiveKeyComparator + Public Function Compare(dicItem1, dicItem2) + If dicItem1.Key > dicItem2.Key Then + Compare = 1 + ElseIf dicItem1.Key < dicItem2.Key Then + Compare = -1 + Else + Compare = 0 + End If + End Function +End Class + +' ***** ƒeƒXƒg ***** + +Sub AssertEqual(var1, var2, strMessage) + If var1 <> var2 Then + Call Err.Raise(1, "ƒeƒXƒgŽ¸”s", strMessage) + End If +End Sub + +Sub TestAddArray() + Dim ary + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + Call AddArray(ary, "hoge") + + Call AssertEqual(UBound(ary), 3, "TestAddArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(ary(0), "foo", "TestAddArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(1), "bar", "TestAddArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(2), "boo", "TestAddArray - ”z—ñ‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(3), "hoge", "TestAddArray - ”z—ñ‚Ì—v‘f3‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestRemoveArray() + Dim ary + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + Call RemoveArray(ary, 1) + + Call AssertEqual(UBound(ary), 1, "TestRemoveArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(ary(0), "foo", "TestRemoveArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(1), "boo", "TestRemoveArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCopyArray() + Dim ary, aryDst + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + aryDst = CopyArray(ary) + + Call AssertEqual(UBound(aryDst), 2, "TestCopyArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(aryDst(0), "foo", "TestCopyArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(aryDst(1), "bar", "TestCopyArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(aryDst(2), "boo", "TestCopyArray - ”z—ñ‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestSortArray() + Dim ary + + ReDim ary(2) + + ary(0) = "foo" + ary(1) = "bar" + ary(2) = "boo" + + Call SortArray(ary, New PrimitiveComparator) + + Call AssertEqual(UBound(ary), 2, "TestSortArray - ”z—ñ‚Ì’·‚³‚ªˆÙ‚È‚éB") + + Call AssertEqual(ary(0), "bar", "TestSortArray - ”z—ñ‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(1), "boo", "TestSortArray - ”z—ñ‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(ary(2), "foo", "TestSortArray - ”z—ñ‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionClassInitialize() + Dim col + + Set col = New Collection + + Call AssertEqual(col.Count, 0, "TestCollectionCount - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionAdd() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Count, 3, "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "foo", "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "bar", "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(2), "boo", "TestCollectionAdd - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionRemove() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Remove(1), "bar", "TestCollectionRemove - íœ‚µ‚½’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Count, 2, "TestCollectionRemove - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "foo", "TestCollectionRemove - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "boo", "TestCollectionRemove - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionItems() + Dim col, var, intIndex + + Set col = New Collection + + For Each var In col.Items + Call AssertEqual(True, False, "TestCollectionItems - —ñ‹“”͈͂𒴂¦‚½B") + Next + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Count, 3, "TestCollectionItems - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + intIndex = 0 + + For Each var In col.Items + If intIndex = 0 Then + Call AssertEqual(var, "foo", "TestCollectionItems - —ñ‹“‚³‚ꂽ’l(0)‚ªˆÙ‚È‚éB") + ElseIf intIndex = 1 Then + Call AssertEqual(var, "bar", "TestCollectionItems - —ñ‹“‚³‚ꂽ’l(1)‚ªˆÙ‚È‚éB") + ElseIf intIndex = 2 Then + Call AssertEqual(var, "boo", "TestCollectionItems - —ñ‹“‚³‚ꂽ’l(2)‚ªˆÙ‚È‚éB") + Else + Call AssertEqual(True, False, "TestCollectionItems - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Sub TestCollectionSort() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call col.Sort(New PrimitiveComparator) + + Call AssertEqual(col.Count, 3, "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "bar", "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "boo", "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(2), "foo", "TestCollectionSort - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionCopy() + Dim col, colDst + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Set colDst = col.Copy() + + Call AssertEqual(col.Count, 3, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(col.Item(0), "foo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(1), "bar", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(col.Item(2), "boo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(colDst.Count, 3, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(colDst.Item(0), "foo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f0‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(colDst.Item(1), "bar", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f1‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(colDst.Item(2), "boo", "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f2‚Ì’l‚ªˆÙ‚È‚éB") + + Call colDst.Add("hoge") + + Call AssertEqual(col.Count, 3, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") + Call AssertEqual(colDst.Count, 4, "TestCollectionCopy - ƒRƒŒƒNƒVƒ‡ƒ“‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Sub TestCollectionExist() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.Exist("bar"), True, "TestCollectionExist - ‘¶Ý”»’肪ŠÔˆá‚¦‚Ä‚¢‚é(True)B") + Call AssertEqual(col.Exist("hoge"), False, "TestCollectionExist - ‘¶Ý”»’肪ŠÔˆá‚¦‚Ä‚¢‚é(False)B") +End Sub + +Sub TestCollectionFindItem() + Dim col + + Set col = New Collection + + Call col.Add("foo") + Call col.Add("bar") + Call col.Add("boo") + + Call AssertEqual(col.FindItem("bar"), 1, "TestCollectionFindItem - —v‘f‚ÌŒŸõŒ‹‰Ê‚ªˆÙ‚È‚é(1)B") + Call AssertEqual(col.FindItem("hoge"), -1, "TestCollectionFindItem - —v‘f‚ÌŒŸõŒ‹‰Ê‚ªˆÙ‚È‚é(-1)B") +End Sub + +Sub TestCollectionIsEqual() + Dim col1, col2 + + Set col1 = New Collection + + Call col1.Add("foo") + Call col1.Add("bar") + Call col1.Add("boo") + + Set col2 = New Collection + + Call col2.Add("foo") + Call col2.Add("bar") + Call col2.Add("boo") + + Call AssertEqual(col1.IsEqual(col2), True, "TestCollectionIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(True)") + + Call col2.Add("hoge") + + Call AssertEqual(col1.IsEqual(col2), False, "TestCollectionIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Public Sub TestDictionaryClassInitialize() + Dim dic + + Set dic = New Dictionary + + Call AssertEqual(dic.Count, 0, "TestDictionaryInitialize - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Public Sub TestDictionaryItem() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("bar"), "‚΁[", "TestDictionaryItem - —v‘f(bar)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("hoge"), Empty, "TestDictionaryItem - ‘¶Ý‚µ‚È‚¢—v‘f‚Ì’l‚ªEmpty‚Å‚Í‚È‚¢B") +End Sub + +Public Sub TestDictionaryRemove() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.Remove("bar"), "‚΁[", "TestDictionaryRemove - íœ‚µ‚½—v‘f‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Count, 2, "TestDictionaryRemove - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") +End Sub + +Public Sub TestDictionaryItems() + Dim dic, varItem, intIndex + + Set dic = New Dictionary + + For Each varItem In dic.Items + Call AssertEqual(True, False, "TestDictionaryItems - —ñ‹“”͈͂𒴂¦‚½B") + Next + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + intIndex = 0 + + For Each varItem In dic.Items + If intIndex = 0 Then + Call AssertEqual(varItem.Key, "foo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(0)") + Call AssertEqual(varItem.Value, "‚Ӂ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varItem.Key, "bar", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(1)") + Call AssertEqual(varItem.Value, "‚΁[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varItem.Key, "boo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(2)") + Call AssertEqual(varItem.Value, "‚ԁ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryItems - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionaryKeys() + Dim dic, varKey, intIndex + + Set dic = New Dictionary + + For Each varKey In dic.Keys + Call AssertEqual(True, False, "TestDictionaryKeys - —ñ‹“”͈͂𒴂¦‚½B") + Next + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + intIndex = 0 + + For Each varKey In dic.Keys + If intIndex = 0 Then + Call AssertEqual(varKey, "foo", "TestDictionaryKeys - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varKey, "bar", "TestDictionaryKeys - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varKey, "boo", "TestDictionaryKeys - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryKeys - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionaryValues() + Dim dic, varValue, intIndex + + Set dic = New Dictionary + + For Each varValue In dic.Values + Call AssertEqual(True, False, "TestDictionaryValues - —ñ‹“”͈͂𒴂¦‚½B") + Next + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + intIndex = 0 + + For Each varValue In dic.Values + If intIndex = 0 Then + Call AssertEqual(varValue, "‚Ӂ[", "TestDictionaryValues - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varValue, "‚΁[", "TestDictionaryValues - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varValue, "‚ԁ[", "TestDictionaryValues - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryValues - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionarySort() + Dim dic, varItem, intIndex + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call dic.Sort(New PrimitiveKeyComparator) + + intIndex = 0 + + For Each varItem In dic.Items + If intIndex = 0 Then + Call AssertEqual(varItem.Key, "bar", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(0)") + Call AssertEqual(varItem.Value, "‚΁[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(0)") + ElseIf intIndex = 1 Then + Call AssertEqual(varItem.Key, "boo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(1)") + Call AssertEqual(varItem.Value, "‚ԁ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(1)") + ElseIf intIndex = 2 Then + Call AssertEqual(varItem.Key, "foo", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚̃L[‚ªˆÙ‚È‚éB(2)") + Call AssertEqual(varItem.Value, "‚Ӂ[", "TestDictionaryItems - —ñ‹“‚³‚ꂽ—v‘f‚Ì’l‚ªˆÙ‚È‚éB(2)") + Else + Call AssertEqual(True, False, "TestDictionaryItems - —ñ‹“”͈͂𒴂¦‚½B") + End If + + intIndex = intIndex + 1 + Next +End Sub + +Public Sub TestDictionaryCopy() + Dim dic, dic2 + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Set dic2 = dic.Copy() + + Call AssertEqual(dic.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("bar"), "‚΁[", "TestDictionaryItem - —v‘f(bar)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic2.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + + Call AssertEqual(dic2.Item("foo"), "‚Ӂ[", "TestDictionaryItem - —v‘f(foo)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic2.Item("bar"), "‚΁[", "TestDictionaryItem - —v‘f(bar)‚Ì’l‚ªˆÙ‚È‚éB") + Call AssertEqual(dic2.Item("boo"), "‚ԁ[", "TestDictionaryItem - —v‘f(boo)‚Ì’l‚ªˆÙ‚È‚éB") + + dic2.Item("hoge") = "‚Ù‚°" + + Call AssertEqual(dic.Count, 3, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") + Call AssertEqual(dic2.Count, 4, "TestDictionaryItem - ƒfƒBƒNƒVƒ‡ƒiƒŠ‚Ì—v‘f”‚ªˆÙ‚È‚éB") +End Sub + +Public Sub TestDictionaryExistKey() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.ExistKey("foo"), True, "TestDictionaryExistKey - ‘¶Ý”»’肪ˆÙ‚È‚éB(True)") + Call AssertEqual(dic.ExistKey("hoge"), False, "TestDictionaryExistKey - ‘¶Ý”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Public Sub TestDictionaryExistValue() + Dim dic + + Set dic = New Dictionary + + dic.Item("foo") = "‚Ӂ[" + dic.Item("bar") = "‚΁[" + dic.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic.ExistValue("‚Ӂ["), True, "TestDictionaryExistValue - ‘¶Ý”»’肪ˆÙ‚È‚éB(True)") + Call AssertEqual(dic.ExistValue("foo"), False, "TestDictionaryExistValue - ‘¶Ý”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Public Sub TestDictionaryIsEqual() + Dim dic1, dic2 + + Set dic1 = New Dictionary + + dic1.Item("foo") = "‚Ӂ[" + dic1.Item("bar") = "‚΁[" + dic1.Item("boo") = "‚ԁ[" + + Set dic2 = New Dictionary + + dic2.Item("foo") = "‚Ӂ[" + dic2.Item("bar") = "‚΁[" + dic2.Item("boo") = "‚ԁ[" + + Call AssertEqual(dic1.IsEqual(dic2), True, "TestDictionaryIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(True)") + + dic2.Item("boo") = "‚Ô‚£" + + Call AssertEqual(dic1.IsEqual(dic2), False, "TestDictionaryIsEqual - “™‰¿”»’肪ˆÙ‚È‚éB(False)") +End Sub + +Sub AllTest() + Call TestAddArray() + Call TestRemoveArray() + Call TestCopyArray() + Call TestSortArray() + + Call TestCollectionClassInitialize() + Call TestCollectionAdd() + Call TestCollectionRemove() + Call TestCollectionItems() + Call TestCollectionSort() + Call TestCollectionCopy() + Call TestCollectionExist() + Call TestCollectionFindItem() + Call TestCollectionIsEqual() + + Call TestDictionaryClassInitialize() + Call TestDictionaryItem() + Call TestDictionaryRemove() + Call TestDictionaryItems() + Call TestDictionaryKeys() + Call TestDictionaryValues() + Call TestDictionarySort() + Call TestDictionaryCopy() + Call TestDictionaryExistKey() + Call TestDictionaryExistValue() + Call TestDictionaryIsEqual() + + MsgBox "ƒeƒXƒg¬Œ÷" +End Sub + +Call AllTest()