using System;
using System.Collections;
/*
此類的功能,是讀取ISO2709數據
得到ISO2709數據三個段,頭標\目次\數據
獲得字段信息
獲得子字段信息
*/
namespace Nosi.Library
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
public class Marc
{
#region 常量定義
public const char FLDEND = (char)30; // 字段結束符
public const char RECEND = (char)29; // 記錄結束符
public const char SUBFLD = (char)31; // 子字段指示符
public const int FLDNAME_LEN = 3; // 字段名長度
public const int MAX_MARCREC_LEN = 100000; // MARC記錄的最大長度
#endregion
string m_strMarc = ""; // MARC記錄體
public Marc()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
//獲得頭標
private string GetHeader()
{
string strHeader = null;
strHeader = m_strMarc.Substring(0,24);
return strHeader;
}
//獲得目次
private string GetMuci()
{
char[] charr = m_strMarc.ToCharArray();
string strMuci = null;
int i = 24; // 頭標字符不再讀取
while(i < m_strMarc.Length)
{
strMuci += charr[i].ToString();
if(charr[i] == FLDEND) break; //發現字段標識
i++;
}
return strMuci;
}
// 獲得數據區
private string GetData()
{
string strData = null;
int iMuci = this.GetMuci().Length;
int iHeader = this.GetHeader().Length;
int iMarc = m_strMarc.Length;
strData = m_strMarc.Substring(iMuci + iHeader,iMarc - iMuci - iHeader);
return strData;
}
// 獲得目次區中的字段名
// -1 error
// 0 no found
// 1 found
private int GetFieldPos(string strFIEldName,
int nIndex,
out string strFIEldPos)
{
string strMuci = this.GetMuci();
strFIEldPos = null;
int i = 0;
int nRet = 0;
if(strMuci == null)
return -1;
if((strMuci.Length - 1) % 12 != 0) // 減1是由於目次區結束標識符
return -1; // length error
do
{
if(strMuci.Substring(i,3) == strFIEldName)
nRet ++;
if(nRet == nIndex)// from zero add
{
strFIEldPos = strMuci.Substring(i,12);
break;
}
i += 12;
} while(i<strMuci.Length);
if (strFIEldPos == null)
return 0; // no found
return 1;
}
// 通過字段名,字段中出現次數獲得字段內容
// 次數從 1 開始計數
// -1 error
// 0 no found
// 1 found
public int GetFIEld(string strFldName,
int nIndex,
out string strFld)
{
strFld = null;
string strFldPos = null;
int nRet = this.GetFIEldPos(strFldName,nIndex,out strFldPos);
if (nRet != 1)
return nRet;
if(strFldName.Length != 3 )
return -1; // subfIEld must 3 chars
int nLength = int.Parse( strFldPos.Substring(3,4));
int nBeginPos = int.Parse( strFldPos.Substring(7,5));
char[] chData = this.GetData().ToCharArray();
int nPos =0;
int i = 0;
while( nPos < chData.Length)
{
i += GetCharLength(chData[nPos]);
if((i >= nBeginPos) && i<= (nBeginPos + nLength))
strFld += chData[nPos].ToString();
nPos ++;
}
if(strFld == null)
return 0;
return 1;
}
//從字段中獲得出現次數的子字段
// -1 error
// 0 not found
// 1 found
public int GetSubFIEld(string strFld,
string strSubName,
int nIndex,
out string strSubFld)
{
strSubFld = null;
if(strSubName.Length != 1)
return -1; // subfIEld'symbol char must 1 char
if(strFld == null)
return -1;
char[] chData = strFld.ToCharArray();
int nPos = 0;
bool isNewSub = false;
int nFound = 0; // 0: not 1: first time found 2: second time found
while( nPos < chData.Length)
{
nPos ++;
if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSubName))
nFound ++; // found
if ((nFound == nIndex) && (isNewSub == false))
{
if(chData[nPos] == SUBFLD)
{
isNewSub = true;
break;
}
strSubFld += chData[nPos].ToString();
}
}
if(strSubFld == null)
return 0;
return 1;
}
//從字段組中獲得子字段
// -1 error
// 0 not found
// 1 found
public int GetSubFIEld(string strGroup,
string strSubName,
out string strSubFld)
{
strSubFld = null;
if(strSubName.Length != 1)
return -1; // subfIEld'symbol char must 1 char
if(strGroup == null)
return -1;
char[] chData = strGroup.ToCharArray();
int nPos = 0;
bool isNewSub = false;
int nFound = 0; // 0: not 1: first time found 2: second time found
while( nPos < chData.Length)
{
nPos ++;
if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSubName))
nFound ++; // found
if (isNewSub == false)
{
if(chData[nPos] == SUBFLD)
{
isNewSub = true;
break;
}
strSubFld += chData[nPos].ToString();
}
}
if(strSubFld == null)
return 0;
return 1;
}
//從字段中獲得出現次數字段組
// -1 error
// 0 not found
// 1 found
public int GetFIEldGroup(string strFld,
int nIndex,
out string strGroup)
{
strGroup = null;
if(strFld == null)
return -1;
char[] chData = strFld.ToCharArray();
int nPos = 0;
string strSplit = "a"; // 一般以a子字段為字段組區分
int nFound = 0; // 0: not 1: first time found 2: second time found
while( nPos < chData.Length)
{
nPos ++;
if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSplit))
nFound ++; // found
if (nFound == nIndex)
strGroup += chData[nPos].ToString();
if(nFound > nIndex)
break;
}
if(strGroup == null)
return 0;
return 1;
}
//獲得字符的長度
//
private int GetCharLength(char ch)
{
int nLength = 0;
if((short)ch < 0 || (short)ch > 128)
{
nLength += 2;
}
else
{
nLength = nLength + 1;
}
return nLength;
}
public string strMarc
{
get
{
return m_strMarc;
}
set
{
m_strMarc = value;
}
}
}
}