//On Error GoTo GotError

var FileError = false;

var InfoLen,
var MultiCode,
var BurstCount,
var RepeatStart,
var RepeatEnd,
var IRName,
var MultiCodeCount,
var Carrier,
var Code,
var Cn,
var N,
var Nn,
var OldTmp;

//Erase IR()
//Erase OldCode()

//IR(Cn, MultiCode).IRError = 0 'reset all errors to 0

//Open IRFile For Binary Access Read As #1
var str = 'data';

Header.Manufacturer = Trim$((CleanTxt(Input$(30, #1))))
Get #1, , NulByte  ' get 1 byte and Null it

Header.HandControl = Trim$((CleanTxt(Input$(20, #1))))
Get #1, , NulByte  ' get 1 byte and Null it

Header.Device = Trim$((CleanTxt(Input$(20, #1))))
Get #1, , NulByte  ' get 1 byte and Null it

Get #1, , ByteL
Get #1, , ByteH
InfoLen = Bytes2Long(ByteH, ByteL)

If InfoLen Then   'read Info if it has it
	Header.Info = Input$(InfoLen, #1)
	 
	'(file may have just Lf or both make all just Lf)
	Header.Info = Replace(Header.Info, vbCrLf, vbLf)   'replace all CrLf with Lf
	Header.Info = Replace(Header.Info, vbLf, vbCrLf)   'now make all CrLf
Else
	Header.Info = ""
End If

	'****************************** get IR commands ***********
Do Until Seek(1) >= LOF(1)
	
	MultiCode = 0
	
	MultiCodeReturn:           'if it has Multicode then come back here
	
	IRName = Input(20, #1)   'get IR name
	Get #1, , NulByte  ' get 1 byte and Null it
	
	Get #1, , ByteL     'get channel number
	Get #1, , NulByte  ' get 1 byte and Null it
	
	If ByteL Then Cn = ByteL
	
	Get #1, , ByteL
	Get #1, , ByteH
	BurstCount = Bytes2Long(ByteH, ByteL)  ' number of burst
	OldTmp = Chr$(ByteL) & Chr$(ByteH)        ' save old
	
	Get #1, , ByteL
	Get #1, , ByteH
	RepeatStart = Bytes2Long(ByteH, ByteL) ' place where burst start to repeat (holding button)
	OldTmp = OldTmp & Chr$(ByteL) & Chr$(ByteH)      ' save old
	
	Get #1, , ByteL
	Get #1, , ByteH
	RepeatEnd = Bytes2Long(ByteH, ByteL)   ' get here and go back to repeat start
	OldTmp = OldTmp & Chr$(ByteL) & Chr$(ByteH)      ' save old

	Get #1, , ByteL    ' Carrier
	Get #1, , ByteH
	If ByteL = 0 And ByteH = 0 Then Carrier = 38 Else Carrier = 1000 / (ByteH + (ByteL / 256))
	OldTmp = OldTmp & Chr$(ByteL) & Chr$(ByteH)      ' save old
	
	Get #1, , ByteL
	Get #1, , NulByte  ' get 1 byte and Null it
	IR(Cn, MultiCode).Repeat = ByteL     'Repeat (how many times to send command)
	OldTmp = OldTmp & Chr$(ByteL) & Chr$(NulByte)        ' save old
	
	Get #1, , MultiCodeCount                     ' number of MultiCode if any
	Get #1, , NulByte  ' get 1 byte and Null it
	OldTmp = OldTmp & Chr$(1) & Chr$(NulByte)
	
	Code = Input$(BurstCount * 2, #1)  'get burst and save in Code
  
	OldCode(Cn, MultiCode) = OldTmp & Code  ' save old code for amx to amx convert or undo
	
		'====================== convert to IRF format ==============================
	
		'--------------- IR name -------------------------------
	IR(Cn, MultiCode).Name = IRName
	
		'--------------- channel number -------------------------------
	IR(Cn, MultiCode).Channel = Cn
	
		'--------------- multi code -------------------------------
	IR(Cn, 0).Multi = MultiCodeCount 'set MultiCode count in IR(Cn, 0).Multi only
	
		'-------------- carrier -----------------------------------
	IR(Cn, MultiCode).Carrier = Carrier
	
		'--------------- RepeatStart -------------------------------
	
	If IR(Cn, MultiCode).Repeat > 0 And RepeatStart = 0 Then
		IR(Cn, MultiCode).RepeatStart = 1
	Else
		IR(Cn, MultiCode).RepeatStart = RepeatStart
	End If
	
		'--------------- RepeatEnd -------------------------------
	IR(Cn, MultiCode).RepeatEnd = RepeatEnd
		 
		'-------------------- bursts ends with a light on byte (make a off byte) --------------------
	var LastLight As String
	MadeDark = False
	
	If BurstCount < RepeatEnd Then   ' check for uneven byte

		LastLight = Right(Code, 2) 'last light burst
		
		For N = 1 To (BurstCount * 2) - 4 Step 2 'try to find a burst pair that has last light burst in it
				'found a burst with same light timing now add its dark time to end
			If LastLight = Mid(Code, N, 2) Then
				Code = Code & Mid(Code, N + 2, 2)
				MadeDark = True
				Exit For
			End If
		Next
		
			' did not find burst pair match so get the 1st dark burst and use that
		If MadeDark = False Then Code = Code & Mid(Code, 3, 2)
		
		BurstCount = BurstCount + 1   'adjust BurstCount for newly added burst
		
	End If
	
		'---------------------- get unique burst times ----------------------
	
	var TestBurst As Long
	var Nibble1 As Integer
	
	IR(Cn, MultiCode).IDCount = 0
	
	Nibble1 = -1
	IR(Cn, MultiCode).Unique = 0
	
	For N = 1 To BurstCount * 2 Step 2
		
		TestBurst = Bytes2Long(Asc(Mid$(Code, N + 1, 1)), Asc(Mid$(Code, N, 1)))
		TestBurst = TestBurst * 4 'TestBurst now has timing in micro sec
		
		TestBurst = (TestBurst / (1000 / IR(Cn, MultiCode).Carrier))  'TestBurst now has loop carrier count ... to make burst time
		
		For Nn = 0 To IR(Cn, MultiCode).Unique - 1
			
			If IR(Cn, MultiCode).Bursts(Nn) = TestBurst Then  '========= had this one so just get ID
				
				If Nibble1 = -1 Then     'wait to have 2 nibbles
					Nibble1 = Nn * 16  'save high ID
				Else 'now have both nibbles store in .code
					IR(Cn, MultiCode).Code = IR(Cn, MultiCode).Code & Chr(Nibble1 + Nn)  ' save IDs High and Low
					IR(Cn, MultiCode).IDCount = IR(Cn, MultiCode).IDCount + 2  ' adjust ID count
					Nibble1 = -1  'reset
				End If
				
				GoTo GetNext
				
			End If

		Next Nn
		
			'=============== got here so then its new =======================================
		
		IR(Cn, MultiCode).Bursts(IR(Cn, MultiCode).Unique) = TestBurst           ' new burst add to burst list
		IR(Cn, MultiCode).Unique = IR(Cn, MultiCode).Unique + 1                ' adjust Unique count
		
		If Nibble1 = -1 Then     'wait to have 2 nibbles
			Nibble1 = Nn * 16  'save high ID
		Else 'now have both nibbles store in .code
			IR(Cn, MultiCode).Code = IR(Cn, MultiCode).Code & Chr(Nibble1 + Nn)  ' save IDs High and Low
			IR(Cn, MultiCode).IDCount = IR(Cn, MultiCode).IDCount + 2   ' adjust ID count
			Nibble1 = -1  'reset
		End If

		GetNext:
		
	Next N
	
	'if it has Multicode then adjust count and loop to get next block of data
	If (MultiCodeCount - 1) > MultiCode Then MultiCode = MultiCode + 1: GoTo MultiCodeReturn
	
	If Cn < 255 Then Cn = Cn + 1
	
Loop

If MultiCode = 0 Then ListCount = Cn     'adj for public

Close #1

Exit Property

GotError:     '================ Had Error ===================
	FileError = True
	Close #1