diff --git a/src/project.ts b/src/project.ts index 5d2d73b4..8a3a75c5 100644 --- a/src/project.ts +++ b/src/project.ts @@ -84,7 +84,7 @@ export class CodeProject { } else { // for .asm -- [.]include "file" // for .c -- #include "file" - var re2 = /^\s+([.#]?include)\s+"(.+?)"/gmi; + var re2 = /^\s+([.#]?include|incbin)\s+"(.+?)"/gmi; while (m = re2.exec(text)) { this.pushAllFiles(files, m[2]); } diff --git a/src/ui.ts b/src/ui.ts index da07f3d0..c0520c96 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -5,13 +5,13 @@ import $ = require("jquery"); import * as bootstrap from "bootstrap"; import { CodeProject } from "./project"; -import { WorkerResult, WorkerOutput, VerilogOutput, SourceFile, WorkerError } from "./workertypes"; +import { WorkerResult, WorkerOutput, VerilogOutput, SourceFile, WorkerError, FileData } from "./workertypes"; import { ProjectWindows } from "./windows"; import { Platform, Preset, DebugSymbols } from "./baseplatform"; import { PLATFORMS } from "./emu"; import * as Views from "./views"; import { createNewPersistentStore } from "./store"; -import { getFilenameForPath, getFilenamePrefix, highlightDifferences, invertMap, byteArrayToString, compressLZG } from "./util"; +import { getFilenameForPath, getFilenamePrefix, highlightDifferences, invertMap, byteArrayToString, compressLZG, byteArrayToUTF8 } from "./util"; import { StateRecorderImpl } from "./recorder"; // external libs (TODO) @@ -259,19 +259,31 @@ function _uploadNewFile(e) { function handleFileUpload(files: File[]) { console.log(files); var index = 0; + var gotoMainFile = (files.length == 1); function uploadNextFile() { var f = files[index++]; if (!f) { console.log("Done uploading"); - gotoNewLocation(); + if (gotoMainFile) + gotoNewLocation(); // TODO: upload w/o starting new project? + else + alert("Files uploaded."); } else { var path = "local/" + f.name; var reader = new FileReader(); reader.onload = function(e) { - var data = (e.target).result; + var arrbuf = (e.target).result as ArrayBuffer; + var data : FileData = new Uint8Array(arrbuf); + // convert to UTF8, unless it's a binary file (TODO) + if (path.endsWith("bin")) { + gotoMainFile = false; + } else { + data = byteArrayToUTF8(data); + } + // store in local forage store.setItem(path, data, function(err, result) { if (err) - console.log(err); + alert("Error uploading " + path + ": " + err); else { console.log("Uploaded " + path + " " + data.length + " bytes"); if (index == 1) @@ -280,7 +292,7 @@ function handleFileUpload(files: File[]) { } }); } - reader.readAsText(f); + reader.readAsArrayBuffer(f); // read as binary } } if (files) uploadNextFile(); @@ -460,6 +472,7 @@ function _downloadAllFilesZipFile(e) { store.keys( (err, keys : string[]) => { if (err) throw err; keys.forEach((path) => { + // TODO: handle binary files store.getItem(path, (err, text) => { if (text) { zip.file(fixFilename(getFilenameForPath(path)), text); diff --git a/src/util.ts b/src/util.ts index 188cd382..246cc0f0 100644 --- a/src/util.ts +++ b/src/util.ts @@ -258,39 +258,8 @@ export function lzgmini() { } // Get the decoded string from an UTF-8 encoded array - this.getStringUTF8 = function():string - { - var str = ""; - if (outdata != null) - { - var charLUT = new Array(); - for (var i = 0; i < 128; ++i) - charLUT[i] = String.fromCharCode(i); - var c; - var outlen = outdata.length; - for (var i = 0; i < outlen;) - { - c = outdata[i++]; - if (c < 128) - { - str += charLUT[c]; - } - else - { - if ((c > 191) && (c < 224)) - { - c = ((c & 31) << 6) | (outdata[i++] & 63); - } - else - { - c = ((c & 15) << 12) | ((outdata[i] & 63) << 6) | (outdata[i+1] & 63); - i += 2; - } - str += String.fromCharCode(c); - } - } - } - return str; + this.getStringUTF8 = function():string { + return byteArrayToUTF8(outdata); } } @@ -314,6 +283,38 @@ export function byteArrayToString(outdata : number[] | Uint8Array) : string { return str; } +export function byteArrayToUTF8(outdata : number[] | Uint8Array) : string { + var str = ""; + var charLUT = new Array(); + for (var i = 0; i < 128; ++i) + charLUT[i] = String.fromCharCode(i); + var c; + var outlen = outdata.length; + for (var i = 0; i < outlen;) + { + c = outdata[i++]; + if (c < 128) + { + str += charLUT[c]; + } + else + { + if ((c > 191) && (c < 224)) + { + c = ((c & 31) << 6) | (outdata[i++] & 63); + } + else + { + c = ((c & 15) << 12) | ((outdata[i] & 63) << 6) | (outdata[i+1] & 63); + i += 2; + if (c == 0xfeff) continue; // ignore BOM + } + str += String.fromCharCode(c); + } + } + return str; +} + export function removeBOM(s:string) { if (s.charCodeAt(0) === 0xFEFF) { s = s.substr(1);