商业源码热门下载www.html.org.cn
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;
}
}
}
}
新闻热点
疑难解答