// 二分法 // 根据给出的Key得到Value // return: // -1 not found public int Search(string strKeyParam, out string strValue) { strValue = ""; int k; // 区间左 int m; // 区间右 int j = -1; // 区间中 string strKey; int nComp; k = 0; m = (int)this.Count - 1; while (k <= m) { j = (k + m) / 2; // 取得j位置的值 CharsetItem item = (CharsetItem)this[j]; strKey = item.Name; nComp = String.Compare(strKey, strKeyParam); if (nComp == 0) { strValue = item.Value; break; } if (nComp > 0) { // strKeyParam较小 m = j - 1; } else { k = j + 1; } } if (k > m) { return(-1); // not found } return(j); }
int BuildCharsetTable(out string strError) { strError = ""; CharsetTable charsettable_e2u = new CharsetTable(); CharsetTable charsettable_u2e = new CharsetTable(); charsettable_e2u.Open(true); charsettable_u2e.Open(true); if (this.textBox_unihanFilenames.Text == "") { strError = "尚未指定输入文件名"; return(-1); } stop.OnStop += new StopEventHandler(this.DoStop); stop.SetMessage("正在创建码表文件 ..."); stop.BeginLoop(); EnableControls(false); this.Update(); this.MainForm.Update(); try { for (int i = 0; i < this.textBox_unihanFilenames.Lines.Length; i++) { StreamReader sr = null; try { sr = new StreamReader(this.textBox_unihanFilenames.Lines[i]); } catch (Exception ex) { strError = "文件 " + this.textBox_unihanFilenames.Lines[i] + " 打开失败: " + ex.Message; return(-1); } this.MainForm.ToolStripProgressBar.Minimum = 0; this.MainForm.ToolStripProgressBar.Maximum = (int)sr.BaseStream.Length; this.MainForm.ToolStripProgressBar.Value = 0; try { for (; ;) { Application.DoEvents(); // 出让界面控制权 if (stop != null) { if (stop.State != 0) { strError = "用户中断"; return(-1); } } string strLine = sr.ReadLine(); if (strLine == null) { break; } if (strLine.Length < 1) { goto CONTINUE; } // 注释行 if (strLine[0] == '#') { goto CONTINUE; } int nRet = strLine.IndexOf("\t", 0); if (nRet == -1) { goto CONTINUE; // 格式有问题 } string strPart1 = strLine.Substring(0, nRet).Trim(); strLine = strLine.Substring(nRet + 1); nRet = strLine.IndexOf("\t", 0); if (nRet == -1) { goto CONTINUE; // 格式有问题 } string strPart2 = strLine.Substring(0, nRet).Trim(); string strPart3 = strLine.Substring(nRet + 1).Trim(); strPart1 = strPart1.Substring(2); // 去掉'U+' if (strPart2 != "kEACC") { goto CONTINUE; // 不相关的行 } strLine = strPart1 + "\t" + strPart3; CharsetItem item = new CharsetItem(); item.Content = strLine; charsettable_u2e.Add(item); strLine = strPart3 + "\t" + strPart1; item = new CharsetItem(); item.Content = strLine; charsettable_e2u.Add(item); // ANSI字符集 stop.SetMessage(strLine); CONTINUE: // 显示进度条 this.MainForm.ToolStripProgressBar.Value = (int)sr.BaseStream.Position; } } finally { sr.Close(); } } stop.SetMessage("正在复制和排序..."); string strDataFileName = ""; string strIndexFileName = ""; if (String.IsNullOrEmpty(this.textBox_e2uFilename.Text) == false) { charsettable_e2u.Sort(); charsettable_e2u.Detach(out strDataFileName, out strIndexFileName); File.Delete(this.textBox_e2uFilename.Text); File.Move(strDataFileName, this.textBox_e2uFilename.Text); File.Delete(this.textBox_e2uFilename.Text + ".index"); File.Move(strIndexFileName, this.textBox_e2uFilename.Text + ".index"); } // if (String.IsNullOrEmpty(this.textBox_u2eFilename.Text) == false) { charsettable_u2e.Sort(); charsettable_u2e.Detach(out strDataFileName, out strIndexFileName); File.Delete(this.textBox_u2eFilename.Text); File.Move(strDataFileName, this.textBox_u2eFilename.Text); File.Delete(this.textBox_u2eFilename.Text + ".index"); File.Move(strIndexFileName, this.textBox_u2eFilename.Text + ".index"); } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); EnableControls(true); } return(0); }
// 实现IComparable接口的CompareTo()方法, // 根据ID比较两个对象的大小,以便排序, // 按右对齐方式比较 // obj: An object to compare with this instance // 返回值 A 32-bit signed integer that indicates the relative order of the comparands. The return value has these meanings: // Less than zero: This instance is less than obj. // Zero: This instance is equal to obj. // Greater than zero: This instance is greater than obj. // 异常: ArgumentException,obj is not the same type as this instance. public override int CompareTo(object obj) { CharsetItem item = (CharsetItem)obj; return(String.Compare(this.Name, item.Name)); }
int BuildCharsetTable(out string strError) { strError = ""; CharsetTable charsettable_e2u = new CharsetTable(); charsettable_e2u.Open(true); if (this.textBox_unihanFilenames.Text == "") { strError = "尚未指定输入文件名"; return -1; } stop.OnStop += new StopEventHandler(this.DoStop); stop.SetMessage("正在创建码表文件 ..."); stop.BeginLoop(); EnableControls(false); this.Update(); this.MainForm.Update(); try { int nRet = 0; for (int i = 0; i < this.textBox_unihanFilenames.Lines.Length; i++) { string strSourceFileName = this.textBox_unihanFilenames.Lines[i]; if (String.IsNullOrEmpty(strSourceFileName) == true) continue; string strStyle = ""; nRet = strSourceFileName.IndexOf(" "); if (nRet != -1) { strStyle = strSourceFileName.Substring(nRet + 1).Trim(); strSourceFileName = strSourceFileName.Substring(0, nRet).Trim(); } StreamReader sr = null; try { sr = new StreamReader(strSourceFileName); } catch (Exception ex) { strError = "文件 " + strSourceFileName + " 打开失败: " + ex.Message; return -1; } this.MainForm.ToolStripProgressBar.Minimum = 0; this.MainForm.ToolStripProgressBar.Maximum = (int)sr.BaseStream.Length; this.MainForm.ToolStripProgressBar.Value = 0; try { for (; ; ) { Application.DoEvents(); // 出让界面控制权 if (stop != null) { if (stop.State != 0) { strError = "用户中断"; return -1; } } string strLine = sr.ReadLine(); if (strLine == null) break; if (strLine.Length < 1) goto CONTINUE; // 注释行 if (strLine[0] == '#') goto CONTINUE; if (String.IsNullOrEmpty(strStyle) == true) { nRet = strLine.IndexOf("\t", 0); if (nRet == -1) goto CONTINUE; // 格式有问题 string strPart1 = strLine.Substring(0, nRet).Trim().ToUpper(); strLine = strLine.Substring(nRet + 1); nRet = strLine.IndexOf("\t", 0); if (nRet == -1) goto CONTINUE; // 格式有问题 string strPart2 = strLine.Substring(0, nRet).Trim(); string strPart3 = strLine.Substring(nRet + 1).Trim().ToUpper(); strPart1 = strPart1.Substring(2); // 去掉'U+' if (strPart2 != "kEACC") goto CONTINUE; // 不相关的行 strLine = strPart1 + "\t" + strPart3; CharsetItem item = new CharsetItem(); item.Content = strLine; charsettable_e2u.Add(item); // charsettable_u2e.Add(item); strLine = strPart3 + "\t" + strPart1; item = new CharsetItem(); item.Content = strLine; charsettable_e2u.Add(item); //charsettable_e2u.Add(item); // ANSI字符集 stop.SetMessage(strLine.Replace("\t", " ")); } if (strStyle == "6+4") { nRet = strLine.IndexOfAny(new char[] { '\t', ' ' }, 0); if (nRet == -1) goto CONTINUE; // 格式有问题 string strPart1 = strLine.Substring(0, nRet).Trim().ToUpper(); string strPart2 = strLine.Substring(nRet + 1).Trim().ToUpper(); CharsetItem item = new CharsetItem(); strLine = strPart1 + "\t" + strPart2; item.Content = strLine; charsettable_e2u.Add(item); // charsettable_u2e.Add(item); item = new CharsetItem(); strLine = strPart2 + "\t" + strPart1; item.Content = strLine; charsettable_e2u.Add(item); //charsettable_e2u.Add(item); // ANSI字符集 stop.SetMessage(strLine.Replace("\t", " ")); } CONTINUE: // 显示进度条 this.MainForm.ToolStripProgressBar.Value = (int)sr.BaseStream.Position; } } finally { sr.Close(); } } stop.SetMessage("正在复制和排序..."); this.Update(); this.MainForm.Update(); string strDataFileName = ""; string strIndexFileName = ""; if (String.IsNullOrEmpty(this.textBox_e2uFilename.Text) == false) { charsettable_e2u.Sort(); charsettable_e2u.Detach(out strDataFileName, out strIndexFileName); File.Delete(this.textBox_e2uFilename.Text); File.Move(strDataFileName, this.textBox_e2uFilename.Text); File.Delete(this.textBox_e2uFilename.Text + ".index"); File.Move(strIndexFileName, this.textBox_e2uFilename.Text + ".index"); } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); EnableControls(true); } return 0; }