日期:2011-04-24  浏览次数:20606 次

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(stri