// TBL_Id：通し番号
// TBL_FormatId：項目のフォーマット番号
// TBL_FieldName：項目名
// TBL_FieldNameNew：結合後フォーマットの項目名
// TBL_FieldSize：対象項目の長さ(バイト)
// TBL_StartPos：切り出し位置
// TBL_InitVal：初期値
// TBL_Proc：処理内容
// TBL_Size：テーブルのサイズ
// TBL_RecodeLength：レコード長

// 内部テーブルの初期化を行います。
function TBL_Initialize()
{
  TBL_Id[0] = 0
  TBL_FormatId[0] = 0
  TBL_FieldName[0] = ""
  TBL_FieldNameNew[0] = ""
  TBL_FieldSize[0] = 0
  TBL_StartPos[0] = 0
  TBL_Size = 0;
  TBL_FormatIdMax = 0;
  TBL_RecodeLength = 0;
}

// 内部テーブルの解放を行います。
function TBL_Terminate()
{
  delete TBL_Id
  delete TBL_FormatId
  delete TBL_FieldName
  delete TBL_FieldNameNew
  delete TBL_FieldSize
  delete TBL_StartPos
  TBL_Size = 0;
  TBL_FormatIdMax = 0;

  delete TBL_IdFromFName
  delete TBL_IdFromFNameNew
}

// テーブル構造に追加
function TBL_Add(formatId, fieldName, fieldNameNew, fieldSize, startPos)
{
  TBL_Id[TBL_Size] = TBL_Size
  TBL_FormatId[TBL_Size] = formatId
  TBL_FieldName[TBL_Size] = fieldName
  TBL_FieldNameNew[TBL_Size] = fieldNameNew
  TBL_FieldSize[TBL_Size] = fieldSize
  TBL_StartPos[TBL_Size] = startPos

  if(TBL_FormatIdMax < formatId)
    TBL_FormatIdMax = formatId;

  // 項目名->Idの逆引き。重複したら"*"
  if(TBL_IdFromFName[fieldName] == "")
  {
    TBL_IdFromFName[fieldName] = TBL_Size
  }
  else
  {
    TBL_IdFromFName[fieldName "," TBL_FormatId[TBL_IdFromFName[fieldName]]] = TBL_IdFromFName[fieldName]
    TBL_IdFromFName[fieldName "," formatId] = TBL_Size
    TBL_IdFromFName[fieldName] = "*"
  }

  // 結合後フォーマットの項目名->Idを逆引き。重複したら"*"
  lText = TBL_IdFromFNameNew[fieldNameNew]
  if(TBL_IdFromFNameNew[fieldNameNew] == "")
  {
//    MsgBox(fieldNameNew " not Exist fieldNameNew", "TBL_Add")
    TBL_IdFromFNameNew[fieldNameNew] = TBL_Size
  }
  else
  {
//    MsgBox(fieldNameNew " exist fieldNameNew[" lText "]", "TBL_Add")

    TBL_IdFromFNameNew[fieldNameNew "," TBL_FormatId[TBL_IdFromFNameNew[fieldNameNew]]] = TBL_IdFromFNameNew[fieldNameNew]
    TBL_IdFromFNameNew[fieldNameNew "," formatId] = TBL_Size
    TBL_IdFromFNameNew[fieldNameNew] = "*"
  }

  TBL_Size += 1
}

// 結合後の項目名から、通し番号を引く
function TBL_GetFIDNew(fieldNameNew, formatId)
{
  if(TBL_IdFromFNameNew[fieldNameNew] == "*")
  {
    if(formatId != "")
    {
      return TBL_IdFromFNameNew[fieldNameNew "," formatId]
    }
    return "*"
  }
  return TBL_IdFromFNameNew[fieldNameNew]
}

// 項目名から、通し番号を引く
function TBL_GetFID(fieldName, formatId)
{
  if(TBL_IdFromFName[fieldName] == "*")
  {
    if(formatId != "")
    {
      return TBL_IdFromFName[fieldName "," formatId]
    }
    return "*"
  }
  return TBL_IdFromFName[fieldName]
}

// OBFから、結合後項目名に相当する文字を抜き出す
function OBF_GetTextF(fieldNameNew)
{
  l_field = ""
  l_id = TBL_GetFIDNew(fieldNameNew)
  l_fs = TBL_FieldSize[l_id]
  l_sp = TBL_StartPos[l_id]

  if(l_id != "" && l_id != "*" && l_fs != "" && l_sp != "")
  {
    l_field = Mid(OBF, l_sp, l_fs) // 1文字ずれたら調整
  }
//MsgBox(l_id ":" l_fs ":" l_sp ":" l_field, "OBF_GetField")
  return l_field
}

function OBF_SetField(fieldNameNew, text)
{
  OBF_Fields[fieldNameNew] = text
}

function OBF_SetFieldOBF()
{
  l_fnameNew = ""
  l_OBF = OBF
  l_OBF_part1 = ""
  l_OBF_part2 = ""
  l_id = 0
  for(l_id = 0 ; l_id < TBL_Size ; l_id++)
  {
    l_fnameNew = TBL_FieldNameNew[l_id]
    if(OBF_Fields[l_fnameNew] != "")
    {
      l_rno = RNO - TBL_FormatIdMax + TBL_FormatId[l_id]
      l_fno = GetFNO(TBL_FormatId[l_id], TBL_FieldName[l_id])
      SetField(l_rno, l_fno, OBF_Fields[l_fnameNew])

// TBL_FieldSize：対象項目の長さ(バイト)
// TBL_StartPos：切り出し位置(最初は1？)

      l_partPos = TBL_StartPos[l_id] + TBL_FieldSize[l_id]
      if(TBL_RecodeLength - l_partPos + 1 <= 0)
      {
        l_OBF_part2 = ""
      }
      else
      {
        l_OBF_part2 = Mid(l_OBF, l_partPos, TBL_RecodeLength - l_partPos + 1)
      }
      l_OBF_part1 = Mid(l_OBF, 1, TBL_StartPos[l_id] - 1)

      l_OBF = l_OBF_part1 GetTextF(l_rno, l_fno) l_OBF_part2
    }
  }
  OBF = l_OBF
  // サイズは変わらないはず
  
  delete OBF_Fields
}


function old_OBF_SetField(fieldNameNew, text)
{
  l_id = TBL_GetFIDNew(fieldNameNew)
  l_rno = RNO - TBL_FormatIdMax + TBL_FormatId[l_id]
  l_fno = GetFNO(TBL_FormatId[l_id], TBL_FieldName[l_id])
  SetField(l_rno, l_fno, text)
  OBF_Fields[fieldNameNew] = GetTextF(l_rno, l_fno)

//MsgBox(l_id ":" l_rno ":" l_fno ":" GetTextF(l_rno, l_fno), "/OBF_SetField")
}

function old_OBF_SetFieldOBF()
{
  l_fnameNew = ""
  l_OBF = ""
  l_id = 0
  for(l_id = 0 ; l_id < TBL_Size ; l_id++)
  {
    l_fnameNew = TBL_FieldNameNew[l_id]
    if(OBF_Fields[l_fnameNew] == "")
    {
      l_OBF = l_OBF OBF_GetTextF(l_fnameNew)
    }
    else
    {
      l_OBF = l_OBF OBF_Fields[l_fnameNew]
    }
  }
  OBF = l_OBF
  // サイズは変わらないはず
  
  delete OBF_Fields
}
