using System;
using System.Collections;
/*
此类的功能,是读取ISO2709数据
得到ISO2709数据三个段,头标\目次\数据
获得字段信息
获得子字段信息
*/
namespace Nosi.Library
{
///
1<summary>
2/// Class1 的摘要说明。
3/// </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
1<strmuci.length); !="3" (nret="" (strfieldpos="null)" )="" +="GetCharLength(chData[nPos]);" -1="" -1;="" 0="" 0;="" 1="" 1;="" 3="" <="" char[]="" chars="" chdata="this.GetData().ToCharArray();" chdata.length)="" error="" found="" getfield(string="" i="" if="" if((i="" if(strfldname.length="" int="" must="" nbeginpos="int.Parse(" nindex,="" nlength="int.Parse(" no="" npos="" nret="this.GetFieldPos(strFldName,nIndex,out" nret;="" out="" public="" return="" strfld="null;" strfld)="" strfldname,="" strfldpos="null;" strfldpos);="" strfldpos.substring(3,4));="" strfldpos.substring(7,5));="" string="" subfield="" while(="" {="" }="" 开始计数="" 次数从="" 通过字段名,字段中出现次数获得字段内容="">= nBeginPos) && i<= (nBeginPos + nLength))
2strFld += chData[nPos].ToString();
3nPos ++;
4
5}
6if(strFld == null)
7return 0;
8return 1;
9}
10//从字段中获得出现次数的子字段
11// -1 error
12// 0 not found
13// 1 found
14public int GetSubField(string strFld,
15string strSubName,
16int nIndex,
17out string strSubFld)
18{
19strSubFld = null;
20if(strSubName.Length != 1)
21return -1; // subfield'symbol char must 1 char
22if(strFld == null)
23return -1;
24
25char[] chData = strFld.ToCharArray();
26int nPos = 0;
27bool isNewSub = false;
28int nFound = 0; // 0: not 1: first time found 2: second time found
29while( nPos < chData.Length)
30{
31nPos ++;
32
33if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSubName))
34nFound ++; // found
35
36if ((nFound == nIndex) && (isNewSub == false))
37{
38if(chData[nPos] == SUBFLD)
39{
40isNewSub = true;
41break;
42}
43strSubFld += chData[nPos].ToString();
44}
45
46}
47if(strSubFld == null)
48return 0;
49return 1;
50}
51
52//从字段组中获得子字段
53// -1 error
54// 0 not found
55// 1 found
56public int GetSubField(string strGroup,
57string strSubName,
58out string strSubFld)
59{
60strSubFld = null;
61if(strSubName.Length != 1)
62return -1; // subfield'symbol char must 1 char
63if(strGroup == null)
64return -1;
65
66char[] chData = strGroup.ToCharArray();
67int nPos = 0;
68bool isNewSub = false;
69int nFound = 0; // 0: not 1: first time found 2: second time found
70while( nPos < chData.Length)
71{
72nPos ++;
73
74if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSubName))
75nFound ++; // found
76
77if (isNewSub == false)
78{
79if(chData[nPos] == SUBFLD)
80{
81isNewSub = true;
82break;
83}
84strSubFld += chData[nPos].ToString();
85}
86
87}
88if(strSubFld == null)
89return 0;
90return 1;
91}
92
93//从字段中获得出现次数字段组
94// -1 error
95// 0 not found
96// 1 found
97public int GetFieldGroup(string strFld,
98int nIndex,
99out string strGroup)
100{
101strGroup = null;
102
103if(strFld == null)
104return -1;
105
106char[] chData = strFld.ToCharArray();
107int nPos = 0;
108string strSplit = "a"; // 一般以a子字段为字段组区分
109int nFound = 0; // 0: not 1: first time found 2: second time found
110while( nPos < chData.Length)
111{
112nPos ++;
113
114if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSplit))
115nFound ++; // found
116
117if (nFound == nIndex)
118strGroup += chData[nPos].ToString();
119if(nFound > nIndex)
120break;
121}
122if(strGroup == null)
123return 0;
124return 1;
125}
126//获得字符的长度
127//
128private int GetCharLength(char ch)
129{
130int nLength = 0;
131
132if((short)ch < 0 || (short)ch > 128)
133{
134nLength += 2;
135}
136else
137{
138nLength = nLength + 1;
139}
140return nLength;
141}
142
143public string strMarc
144{
145get
146{
147return m_strMarc;
148}
149set
150{
151m_strMarc = value;
152}
153
154}
155}
156}</strmuci.length);>