using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace CS_ScanSample1
{
/// <summary>
/// Logic 的摘要說明。
/// </summary>
public class Logic
{
/// <summary>
/// 從排序文本中查找指定關鍵字的行
/// </summary>
/// <param name="path">路徑</param>
/// <param name="keyString">關鍵字</param>
/// <param name="rowLength">一行的長度</param>
/// <param name="keyStart">開始位置</param>
/// <param name="keyLength">取的長度</param>
public static DataTable Find(string path, string keyString, int rowLength, int keyStart, int keyLength)
{
DataTable ddt=new DataTable();
ddt.Columns.Add("Titlex");
ddt.Columns.Add("BookNum");
long s = DateTime.Now.Ticks;
ArrayList list = new ArrayList();
FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
long fileLength = fs.Length;
int recCount = (int)(fileLength / rowLength);
int recStart = 0;
int recEnd = recCount;
string rowKeyString;
int curRowNo = 0;
int result;
curRowNo = (recStart + recEnd) / 2;
int seekCount = 0;
byte[] rowBytes;
do
{
seekCount++;
rowBytes = ReadRow(fs, curRowNo, rowLength);
rowKeyString = Encoding.Default.GetString(rowBytes, keyStart, keyLength).Trim();
result = rowKeyString.CompareTo(keyString);
if (result == 0)
&nb
sp; {
// 提取該記錄前後相同的數據
int recMore = curRowNo;
do
{
// list.Add(Encoding.Default.GetString(rowBytes));
ddt = InStrOutTable(Encoding.Default.GetString(rowBytes,0,rowBytes.Length),ddt);
recMore++;
rowBytes = ReadRow(fs, recMore, rowLength);
rowKeyString = Encoding.Default.GetString(rowBytes, keyStart, keyLength).Trim();
result = rowKeyString.CompareTo(keyString);
} while (result == 0 && recMore <= recCount);
recMore = curRowNo - 1;
do
{
rowBytes = ReadRow(fs, recMore, rowLength);
rowKeyString = Encoding.Default.GetString(rowBytes, keyStart, keyLength).Trim();
result = rowKeyString.CompareTo(keyString);
recMore--;
if (result == 0)
{
// list.Add(Encoding.Default.GetString(rowBytes));
ddt = InStrOutTable( Encoding.Default.GetString(rowBytes,0,rowBytes.Length),ddt);
}
} while (result == 0 && recMore >= 0);
break;
}
else if (result > 0) // 當前記錄Key大於要查找的Key,則向前查找
{
if (recEnd <= recStart) break;
recEnd = curRowNo - 1;
}
else // 當前記錄Key小於要查找的Key,
fs.Close();
long e = DateTime.Now.Ticks;
//Console.WriteLine("Time={0},Times={1}", TimeSpan.FromTicks(e - s), seekCount);
return ddt;
// return list;
}
private static byte[] ReadRow(FileStream fs, int recno, int rowLength)
{
byte[] rowBytes = new byte[rowLength - 2];
if(recno <0) return rowBytes;
int position = recno * rowLength;
fsposition, SeekOrigin.Begin);
fs.Read(rowBytes, 0, rowLength - 2);
return rowBytes;
}
public static DataTable InStrOutTable( string arowStr,DataTable Dt)
{
string ztm = MySubstring(arowStr, 20, 60).Trim().Replace("''", "’");
int fbs = Convert.ToInt32((MySubstring(arowStr, 120, 10).Trim().Replace("''", "’")=="") ? "0" :MySubstring(arowStr, 120, 10).Trim().Replace("''", "’"));
return InputDataTable( ztm, fbs,Dt);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 把一行加入DataTable裡
/// </summary>
/// <param name="ZykDt1">DataTable</param>
/// <param name="idxstr">IdxStr</param>
/// <param name="Titlexstr">Title</param>
/// <;param name="booknumstr">nCopIEs</param>
/// <returns></returns>
private static DataTable InputDataTable( string Titlexstr, int booknumstr,DataTable ZykDt1)
{
string Titlex = Titlexstr;
int BookNum = booknumstr;
DataRow row = ZykDt1.NewRow();
row["Titlex"] = Titlex;
row["BookNum"] = BookNum;
ZykDt1.Rows.Add(row);
return ZykDt1;
}
/// <summary>
/// 取字符
/// </summary>
/// <param name="string1">字符串</param>
/// <param name="start">開始位置</param>
/// <param name="length">長度</param>
/// <returns></returns>
private static string MySubstring(string string1, int start, int length)
{
if (string1 == "" || string1 == null)
{
return "";
}
else
{
byte[] s = Encoding.Default.GetBytes(string1);
if (s.Length >= start + length)
{
return Encoding.Default.GetString(s, start, length);
}
else
{
if (s.Length > start)
{
return Encoding.Default.GetString(s, start, s.Length - start);
}
else
{
return "";
}
}
}
}
}
}