1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-09-20 04:24:45 +00:00

Merge pull request #47 from FellippeHeitor/main

Tweaks and improvements to file operations
This commit is contained in:
boxgaming 2023-06-01 15:37:10 -05:00 committed by GitHub
commit d4c1f03a0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

22
qb.js
View file

@ -1064,6 +1064,12 @@ var QB = new function() {
}; };
this.sub_Close = function(fh) { this.sub_Close = function(fh) {
if (!fh) {
for (const key in _fileHandles) {
delete _fileHandles[key];
}
return;
}
if (!_fileHandles[fh]) { if (!_fileHandles[fh]) {
throw new Error("Invalid file handle"); throw new Error("Invalid file handle");
} }
@ -2441,11 +2447,16 @@ var QB = new function() {
return; return;
} }
else { else {
valueObj.value = _readDataElement(fh, position, type); if (type == "STRING") {
var bytestoread = String(value).length;
} else {
var bytestoread = 0;
}
valueObj.value = _readDataElement(fh, position, type, bytestoread);
} }
}; };
function _readDataElement(fh, position, type) { function _readDataElement(fh, position, type, bytestoread) {
var vfs = GX.vfs(); var vfs = GX.vfs();
var data = null; var data = null;
if (type == "SINGLE") { if (type == "SINGLE") {
@ -2489,7 +2500,12 @@ var QB = new function() {
return (new DataView(data)).getUint32(0, true); return (new DataView(data)).getUint32(0, true);
} }
else if (type == "STRING") { else if (type == "STRING") {
throw new Error("Unsupported data type: " + type); if (bytestoread > 0) {
data = vfs.readData(fh.file, position, bytestoread);
fh.offset = position + data.byteLength;
return String.fromCharCode.apply(null, new Uint8Array(data));
}
return '';
} }
else if (type == "_BIT" || type == "_UNSIGNED _BIT") { else if (type == "_BIT" || type == "_UNSIGNED _BIT") {
// mimicking QB64 error message here // mimicking QB64 error message here