iopipe code to count lines in gzipped file works with v 0.1.7 but fails with 0.2.1

Steven Schveighoffer schveiguy at gmail.com
Sat Aug 8 00:57:40 UTC 2020


On 8/7/20 9:40 AM, Andrew wrote:
> Hi,
> 
> This code to count lines in a gzipped file exits with "Program exited 
> with code -9" when run with the latest version of the library, I guess 
> because I am doing unsafe things. Could someone tell me how to change it 
> to make it work? The actual program I'm writing processes a file line by 
> line, so ideally I'd like to keep the structure of open a file, then 
> foreach over it.
> 
> Thanks very much

As of iopipe v0.2.0, io is no longer a required dependency, it's 
optional. So you must also add a dependency for io.

I tried adding

dependency "io" version="~>0.3.0"

But it fails with:

Got no configuration for dependency io ~>0.3.1 of hello ~master!?

If I add

dependency "io" version="*"

it works.

I think this is an issue with dub when using an inline recipe file, but 
I don't know?

Note that in this simple example, the line count is stored in the line 
pipe, you can retreive the number of lines by accessing the `segments` 
member of the pipe (undocumented, I have to fix that). So my code looks 
like:

---
/+ dub.sdl:
     name "hello"
     dependency "iopipe" version="~>0.2.0"
     dependency "io" version="*"
+/

import std.stdio;
//import std.typecons; // refCounted not @safe
import iopipe.textpipe;
import iopipe.zip;
import iopipe.bufpipe;
import iopipe.refc; // refCounted that is @safe
import std.io : File = File; // just a note, I don't know why you are 
renaming here...

void main() @safe // yay @safe!
{
   auto counter = 0;
   auto fileToRead = File("file.gz").refCounted.bufd
       .unzip(CompressionFormat.gzip)
       .assumeText
       .byLine;

   fileToRead.process();
   writeln(fileToRead.segments);
}
---

FYI, I noticed that in my simple test, this outputs one less than the 
actual lines. I'll have to look into *that* too.

That dependency on writeln also irks me ;) I need to get working on that 
iopipe replacement for it...

-Steve


More information about the Digitalmars-d-learn mailing list