Apple2-IO-RPi/RaspberryPi/apple2driver/handlers/loadFile.go

52 lines
1.1 KiB
Go
Raw Normal View History

2022-01-11 04:00:58 +00:00
// Copyright Terence J. Boldt (c)2020-2022
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
2022-01-11 04:00:58 +00:00
// This file contains the handler for loading files directly from the
// Raspberry Pi onto the Apple II
2021-11-03 09:33:09 +00:00
2021-05-30 11:18:39 +00:00
package handlers
import (
"fmt"
"os"
)
2021-11-03 09:33:09 +00:00
// LoadFileCommand handles requests to direct read files from Linux to the Apple II
2021-05-30 11:18:39 +00:00
func LoadFileCommand() {
2021-10-30 11:03:18 +00:00
fileName, _ := comm.ReadString()
2021-05-30 11:18:39 +00:00
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
2021-10-30 11:03:18 +00:00
comm.WriteByte(0)
comm.WriteByte(0)
2021-05-30 11:18:39 +00:00
return
}
fileInfo, _ := file.Stat()
fileSize := int(fileInfo.Size())
fmt.Printf("FileSize: %d\n", fileSize)
fileSizeHigh := byte(fileSize >> 8)
fileSizeLow := byte(fileSize & 255)
2021-10-30 11:03:18 +00:00
err = comm.WriteByte(fileSizeLow)
2021-06-01 23:08:21 +00:00
if err != nil {
return
}
2021-10-30 11:03:18 +00:00
err = comm.WriteByte(fileSizeHigh)
2021-06-01 23:08:21 +00:00
if err != nil {
return
}
2021-05-30 11:18:39 +00:00
buffer := make([]byte, fileSize)
fmt.Printf("Read file %s SizeHigh: %d SizeLow: %d\n", fileName, fileSizeHigh, fileSizeLow)
file.Read(buffer)
2021-10-30 11:03:18 +00:00
comm.WriteBuffer(buffer)
2021-05-30 11:18:39 +00:00
}