1
0
mirror of https://github.com/pfusik/xasm.git synced 2024-06-15 03:29:31 +00:00

Get rid of the deprecated std.stream.

This commit is contained in:
Piotr Fusik 2013-03-19 11:12:12 +01:00
parent 406354e06c
commit 3fc9b5c0f5

87
xasm.d
View File

@ -21,7 +21,6 @@ import std.conv;
import std.math; import std.math;
import std.path; import std.path;
import std.stdio; import std.stdio;
import std.stream;
import std.string; import std.string;
version (Windows) { version (Windows) {
@ -159,12 +158,12 @@ struct IfContext {
IfContext[] ifContexts; IfContext[] ifContexts;
Stream listingStream = null; File listingStream;
char[32] listingLine; char[32] listingLine;
int listingColumn; int listingColumn;
string lastListedFilename = null; string lastListedFilename = null;
Stream objectStream = null; File objectStream;
int objectBytes = 0; int objectBytes = 0;
@ -1067,34 +1066,34 @@ string makeEscape(string s) {
return s.replace("$", "$$"); return s.replace("$", "$$");
} }
Stream openInputFile(string filename) { File openInputFile(string filename) {
makeSources ~= ' ' ~ makeEscape(filename); makeSources ~= ' ' ~ makeEscape(filename);
try { try {
return new BufferedFile(filename, FileMode.In); return File(filename);
} catch (OpenException e) { } catch (Exception e) {
throw new AssemblyError(e.msg); throw new AssemblyError(e.msg);
} }
} }
Stream openOutputFile(char letter, string defaultExt) { File openOutputFile(char letter, string defaultExt) {
string filename = optionParameters[letter - 'a']; string filename = optionParameters[letter - 'a'];
if (filename is null) if (filename is null)
filename = sourceFilename.setExtension(defaultExt); filename = sourceFilename.setExtension(defaultExt);
if (letter == 'o') if (letter == 'o')
makeTarget = makeEscape(filename); makeTarget = makeEscape(filename);
try { try {
return new BufferedFile(filename, FileMode.OutNew); return File(filename, "wb");
} catch (OpenException e) { } catch (Exception e) {
throw new AssemblyError(e.msg); throw new AssemblyError(e.msg);
} }
} }
void ensureListingFileOpen(char letter, string msg) { void ensureListingFileOpen(char letter, string msg) {
if (listingStream is null) { if (!listingStream.isOpen) {
listingStream = openOutputFile(letter, "lst"); listingStream = openOutputFile(letter, "lst");
if (!getOption('q')) if (!getOption('q'))
write(msg); write(msg);
listingStream.writeLine(TITLE); listingStream.writeln(TITLE);
} }
} }
@ -1122,7 +1121,7 @@ void listLine() {
return; return;
ensureListingFileOpen('l', "Writing listing file...\n"); ensureListingFileOpen('l', "Writing listing file...\n");
if (currentLocation.filename != lastListedFilename) { if (currentLocation.filename != lastListedFilename) {
listingStream.writefln("Source: %s", currentLocation.filename); listingStream.writeln("Source: ", currentLocation.filename);
lastListedFilename = currentLocation.filename; lastListedFilename = currentLocation.filename;
} }
int i = 4; int i = 4;
@ -1150,12 +1149,10 @@ void listCommentLine() {
} }
void listLabelTable() { void listLabelTable() {
if (optionParameters['t' - 'a'] !is null && listingStream !is null) { if (optionParameters['t' - 'a'] !is null && listingStream.isOpen)
listingStream.close(); listingStream.close();
listingStream = null;
}
ensureListingFileOpen('t', "Writing label table...\n"); ensureListingFileOpen('t', "Writing label table...\n");
listingStream.writefln("Label table:"); listingStream.writeln("Label table:");
foreach (string name; labelTable.keys.sort) { foreach (string name; labelTable.keys.sort) {
Label l = labelTable[name]; Label l = labelTable[name];
listingStream.write(l.unused ? 'n' : ' '); listingStream.write(l.unused ? 'n' : ' ');
@ -1180,14 +1177,16 @@ void objectByte(ubyte b) {
} else { } else {
assert(pass2); assert(pass2);
if (!optionObject) return; if (!optionObject) return;
if (objectStream is null) { if (!objectStream.isOpen) {
objectStream = openOutputFile('o', "obx"); objectStream = openOutputFile('o', "obx");
if (!getOption('q')) if (!getOption('q'))
writeln("Writing object file..."); writeln("Writing object file...");
} }
ubyte[1] buffer;
buffer[0] = b;
try { try {
objectStream.write(b); objectStream.rawWrite(buffer);
} catch (WriteException e) { } catch (Exception e) {
throw new AssemblyError("Error writing object file"); throw new AssemblyError("Error writing object file");
} }
} }
@ -2281,23 +2280,25 @@ void assemblyIns() {
length = value; length = value;
} }
} }
Stream stream = openInputFile(filename); File stream = openInputFile(filename);
scope (exit) stream.close(); scope (exit) stream.close();
try { try {
stream.seek(offset, offset >= 0 ? SeekPos.Set : SeekPos.End); stream.seek(offset, offset >= 0 ? SEEK_SET : SEEK_END);
} catch (SeekException e) { } catch (Exception e) {
throw new AssemblyError("Error seeking file"); throw new AssemblyError("Error seeking file");
} }
while (length != 0) { while (length != 0) {
ubyte b; ubyte[1] buffer;
try { try {
stream.read(b); if (stream.rawRead(buffer) == null) {
} catch (ReadException e) { if (length > 0)
if (length > 0) throw new AssemblyError("File is too short");
throw new AssemblyError("File is too short"); break;
break; }
} catch (Exception e) {
throw new AssemblyError("Error reading file");
} }
putByte(b); putByte(buffer[0]);
if (length > 0) length--; if (length > 0) length--;
} }
} }
@ -2787,27 +2788,21 @@ void assemblyFile(string filename) {
filename = absolutePath(filename); filename = absolutePath(filename);
currentLocation = new Location(filename); currentLocation = new Location(filename);
foundEnd = false; foundEnd = false;
Stream stream = openInputFile(filename); File stream = openInputFile(filename);
scope (exit) stream.close(); scope (exit) stream.close();
line = ""; line = "";
readChar: while (!foundEnd) { readChar: while (!foundEnd) {
ubyte c; ubyte[1] buffer;
try { if (stream.rawRead(buffer) == null)
stream.read(c);
} catch (ReadException e) {
break; break;
} switch (buffer[0]) {
switch (c) {
case '\r': case '\r':
assemblyLine(); assemblyLine();
line = ""; line = "";
try { if (stream.rawRead(buffer) == null)
stream.read(c);
} catch (ReadException e) {
break readChar; break readChar;
} if (buffer[0] != '\n')
if (c != '\n') line ~= cast(char) buffer[0];
line ~= cast(char) c;
break; break;
case '\n': case '\n':
case '\x9b': case '\x9b':
@ -2815,7 +2810,7 @@ void assemblyFile(string filename) {
line = ""; line = "";
break; break;
default: default:
line ~= cast(char) c; line ~= cast(char) buffer[0];
break; break;
} }
} }
@ -2961,10 +2956,8 @@ int main(string[] args) {
warning(e.msg, true); warning(e.msg, true);
exitCode = 2; exitCode = 2;
} }
if (listingStream !is null) listingStream.close();
listingStream.close(); objectStream.close();
if (objectStream !is null)
objectStream.close();
if (exitCode <= 1) { if (exitCode <= 1) {
if (!getOption('q')) { if (!getOption('q')) {
writefln("%d lines of source assembled", totalLines); writefln("%d lines of source assembled", totalLines);