diff --git a/qb.js b/qb.js index eceac3a..4e0ac8c 100644 --- a/qb.js +++ b/qb.js @@ -1064,6 +1064,12 @@ var QB = new function() { }; this.sub_Close = function(fh) { + if (!fh) { + for (const key in _fileHandles) { + delete _fileHandles[key]; + } + return; + } if (!_fileHandles[fh]) { throw new Error("Invalid file handle"); } @@ -2441,11 +2447,16 @@ var QB = new function() { return; } 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 data = null; if (type == "SINGLE") { @@ -2489,7 +2500,12 @@ var QB = new function() { return (new DataView(data)).getUint32(0, true); } 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") { // mimicking QB64 error message here