sys

A set of unix utils in hare!
Log | Files | Refs | README

split.ha (894B)


      1 use fmt;
      2 use io;
      3 use os;
      4 use strings;
      5 use bufio;
      6 use encoding::utf8;
      7 use getopt;
      8 
      9 use util;
     10 
     11 let delim = " ";
     12 
     13 fn run() void = {
     14 	let sc = bufio::newscanner(os::stdin);
     15 	defer bufio::finish(&sc);
     16 
     17 	for (true) {
     18 		match (bufio::scan_string(&sc, delim)) {
     19 		case let s: str => fmt::println(s)!;
     20 		case io::EOF => break;
     21 		case let e: io::error => util::die(io::strerror(e));
     22 		};
     23 	};
     24 
     25 	match (bufio::scan_line(&sc)) {
     26 	case let s: str => fmt::println(s)!;
     27 	case io::EOF => return;
     28 	case let e: io::error => util::die(io::strerror(e));
     29 	case let e: utf8::invalid => util::die(utf8::strerror(e));
     30 	};
     31 };
     32 
     33 export fn main() void = {
     34 	let cmd = getopt::parse(os::args,
     35 		"split",
     36 		('d', "delim", "set a deliminer between tokens"),
     37 		);
     38 	defer getopt::finish(&cmd);
     39 
     40 	for (let opt .. cmd.opts) {
     41 		switch (opt.0) {
     42 		case 'd' =>
     43 			delim = util::escape(opt.1);
     44 		case => abort();
     45 		};
     46 	};
     47 
     48 	run();
     49 };