Thread overview
opApply bug?
Mar 28, 2005
Regan Heath
Mar 28, 2005
Ben Hinkle
Mar 28, 2005
Regan Heath
March 28, 2005
Calling writefln from within an opApply does strange things..

# import std.stream;
# import std.stdio;
#
# class LineReader(Source) : Source
# {
# 	int opApply(int delegate(inout char[]) dg)
# 	{
# 		int result = 0;
# 		char[] line;
#
# 		//this call causes the bug
# 		writefln("bug");
# 		
# 		while(!eof())
# 		{
# 			line = readLine();
# 			if (!line) break;
# 			result = dg(line);
# 			if (result) break;
# 		}
# 		
# 		return result;
# 	}	
# }
#
# int main(char[][] args)
# {
# 	LineReader!(BufferedFile) f;
# 	
# 	f = new LineReader!(BufferedFile)();
# 	
# 	f.open("test.txt",FileMode.OutNew);
# 	f.writeLine("Line 1");
# 	f.writeLine("Line 2");
# 	f.writeLine("Line 3");
# 	f.writeLine("Line 4");
# 	f.close();
# 	
# 	f.open("test.txt",FileMode.In);
# 	foreach(char[] line; f)
# 	{
# 		writefln("READ[",line,"]");
# 	}
# 	f.close();
# 	
# 	return 0;	
# }

Compile:
dmd test30.d -g -debug -unittest

Output:
READ[1]
READ[Line 2]
READ[Line 3]
READ[Line 4]

Regan
March 28, 2005
> Compile:
> dmd test30.d -g -debug -unittest
>
> Output:
> READ[1]
> READ[Line 2]
> READ[Line 3]
> READ[Line 4]

There should be a check for writeable in Stream.writefln and friends that isn't there. What is happening is that the writefln in the opApply is calling the stream's writefln, not std.stdio.writefln. Since the stream is opened in read-only mode writing should be illegal. It will work if you say std.stdio.writefln or if you don't import std.stdio and put stdout.writefln.


March 28, 2005
On Mon, 28 Mar 2005 08:12:47 -0500, Ben Hinkle <ben.hinkle@gmail.com> wrote:
>> Compile:
>> dmd test30.d -g -debug -unittest
>>
>> Output:
>> READ[1]
>> READ[Line 2]
>> READ[Line 3]
>> READ[Line 4]
>
> There should be a check for writeable in Stream.writefln and friends that
> isn't there. What is happening is that the writefln in the opApply is
> calling the stream's writefln, not std.stdio.writefln. Since the stream is
> opened in read-only mode writing should be illegal. It will work if you say
> std.stdio.writefln or if you don't import std.stdio and put stdout.writefln.

*smacks head* of course! Thanks. So, the write is advancing the read pointer? Is it better to make the read/write pointers independant of each other, or not. IIRC C's FILE api has 1 pointer. What do other implementations use?

Regan