wsh:jscriptで、ADODB.Streamの対応Charsetを調べる

Aliasがあるので、Aliasのない名前を中心として調べてみました。
どこかで、HKCR\MIME\Database\Charsetだとみたので、そこから取得する。

// http://passing.breeze.cc/mt/archives/2009/07/jscript-wmi.html
(function() {
	var Registry = function () {
		this.initialize.apply(this, arguments);
	};
	Registry.prototype = {
		initialize: function(computer) {
			if(!computer) computer = ".";
			var locator = new ActiveXObject("WbemScripting.SWbemLocator");
			var server = locator.ConnectServer(computer, "root\\default");
			this.stdregprov = server.Get("StdRegProv");
			this.HKCR = 0x80000000;	// HKEY_CLASSES_ROOT
			this.HKCU = 0x80000001;	// HKEY_CURRENT_USER
			this.HKLM = 0x80000002;	// HKEY_LOCAL_MACHINE
			this.HKUS = 0x80000003;	// HKEY_USERS
			this.HKCC = 0x80000005;	// HKEY_CURRENT_CONFIG
			// WinNT.h
			this.REG_SZ = 1;		// Unicode nul terminated string
			this.REG_EXPAND_SZ = 2;	// Unicode nul terminated string (with environment variable references)
			this.REG_BINARY = 3;	// Free form binary
			this.REG_DWORD = 4;		// 32-bit number
			this.REG_MULTI_SZ = 7;	// Multiple Unicode strings
			this.REG_QWORD = 11;	// 64-bit number
			this.REG_KEY = 0x7fff;	// 便宜上のタイプ(実際にはない)。また以下は用途不明
			this.REG_NON = 0;						// No value type
			this.REG_DWORD_LITTLE_ENDIAN = 4;		// 32-bit number (same as REG_DWORD)
			this.REG_DWORD_BIG_ENDIAN = 5;			// 32-bit number
			this.REG_LINK = 6;						// Symbolic Link (unicode)
			this.REG_RESOURCE_LIST = 8;				// Resource list in the resource map
			this.REG_FULL_RESOURCE_DESCRIPTOR = 9;	// Resource list in the hardware description
			this.REG_RESOURCE_REQUIREMENTS_LIST = 10;
			this.REG_QWORD_LITTLE_ENDIAN = 11;		// 64-bit number (same as REG_QWORD)
		},
		do_method: function(method_name, hkey, key, value_name) {
			var in_param = this.stdregprov.Methods_.Item(method_name).InParameters.SpawnInstance_();
			in_param.hDefKey = hkey;
			in_param.sSubKeyName = key;
			if(value_name != null) {
				in_param.sValueName = value_name;
			}
			var out = this.stdregprov.ExecMethod_(method_name, in_param);
			return	out;
		},
		EnumKey: function(hkey, key) {
			var out_param = this.do_method("EnumKey", hkey, key);
			var names = [];
			if(out_param.sNames != null) {
				names = out_param.sNames.toArray();
			}
			return	names;
		},
		EnumValues: function(hkey, key) {
			var out_param = this.do_method("EnumValues", hkey, key);
			var value_names = [];
			if(out_param.sNames != null) {
				value_names = out_param.sNames.toArray();
			}
			var value_types = [];
			if(out_param.Types != null) {
				value_types = out_param.Types.toArray();
			}
			return {
				Names: value_names,
				Types: value_types
			};
		},
		GetStringValue: function(hkey, key, name) {
			// REG_SZ
			var out_param = this.do_method("GetStringValue", hkey, key, name);
			// 値が存在しない場合null
			return out_param.sValue;
		},
		GetExpandedStringValue: function(hkey, key, name) {
			// REG_EXPAND_SZ
			var out_param = this.do_method("GetExpandedStringValue", hkey, key, name);
			// 値が存在しない場合null
			return out_param.sValue;
		},
		GetDWORDValue: function(hkey, key, name) {
			// REG_DWORD
			var out_param = this.do_method("GetDWORDValue", hkey, key, name);
			// 値が存在しない場合null
			return out_param.uValue;
		},
		GetBinaryValue: function(hkey, key, name) {
			// REG_BINARY
			var out_param = this.do_method("GetBinaryValue", hkey, key, name);
			// 値が存在しない場合null
			// TODO: Binaryの戻りがなんなのか不明。MSXMLで変換できなかった(Byte()ではなさそう)
			return out_param.uValue;
		},

		// 未対応メソッド
		// http://www.wmifun.net/library/stdregprov.html
		// CreateKey / DeleteKey / DeleteValue / Set*Value(Get系対応) / CheckAccess / SetSecurityDescriptor / GetSecurityDescriptor
		// Get系: GetQWORDValue(REG_QWORD) / GetMultiStringValue(REG_MULTI_SZ)

		// 追加メソッド
		GetValue: function(hkey, key, name, type) {
			var val = null;
			switch(type) {
				case this.REG_SZ:
					val = this.GetStringValue(hkey, key, name);
					break;
				case this.REG_EXPAND_SZ:
					val = this.GetExpandedStringValue(hkey, key, name);
					break;
				case this.REG_DWORD:
					val = this.GetDWORDValue(hkey, key, name);
					break;
				case this.REG_BINARY:
					val = this.GetBinaryValue(hkey, key, name);
					break;
			}
			return val;
		},
		GetValues: function(hkey, key) {
			var results = new Array();
			var values = this.EnumValues(hkey, key);
			for(var i = 0; i < values.Names.length; i++) {
				var val = this.GetValue(hkey, key, values.Names[i], values.Types[i]);
				results.push({ key:values.Names[i], type:values.Types[i], value:val });
			}
			return results;
		},
		GetKeyValues: function(hkey, key) {
			var results = new Array();
			var keys = this.EnumKey(hkey, key);
			for(var i = 0; i < keys.length; i++) {
				results.push({ key:keys[i], type:this.REG_KEY, value:this.GetKeyValues(hkey, key + "\\" + keys[i]) });
			}
			return results.concat(this.GetValues(hkey, key));
		}
	};



	var enumCharset = function(reg) {
		var results = {};
		var chars = reg.GetKeyValues(reg.HKCR, "MIME\\Database\\Charset");
		for(var i = 0 ; i < chars.length; i++) {
			if(chars[i].type != reg.REG_KEY) continue;
			var values = chars[i].value;
			for(var j = 0 ; j < values.length; j++) {
				var name = chars[i].key.toLowerCase();
				var value_name = values[j].key;
				var parent = results[name];
				var val = values[j].value;
				if(val == null) val = "";
				else val = val.toString().toLowerCase();
				if(value_name == "AliasForCharset") {
					parent = results[val];
					if(parent == null) {
						parent = {};
						results[val] = parent;
					}
					var sub = parent.sub;
					if(sub == null) {
						sub = new Array();
						parent.sub = sub;
					}
					sub.push(name);
				} else {
					if(parent == null) {
						parent = {};
						results[name] = parent;
					}
					parent[value_name] = val;
				}
			}
		}
		var charsets = new Array();
		for(var charset in results) {
			charsets.push(charset);
		}
		charsets.sort();
		for(var i = 0; i < charsets.length; i++) {
			var charset = charsets[i];
			WScript.Echo(charset);
			var sub = results[charset].sub;
			if(sub != null) WScript.Echo("\tAliasForCharset: " + sub.join(" "));
			var eles = ""
			for(var ele in results[charset]) {
				if(ele != "sub") {
					eles += "\t" + ele + ":" + results[charset][ele]
				}
			}
			if(eles != "") WScript.Echo(eles);
			WScript.Echo();
		}
	};
	enumCharset(new Registry(WScript.Arguments.Named("computer")));

})();