c2a4061997-02-06Fredrik Hübinette (Hubbe) #include <string.h> import files; constant File=file; constant Port=port; object stderr=File("stderr"); object stdout=File("stdout"); #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) ) class FILE { #define BUFSIZE 16384 inherit File : file; /* Private functions / buffers etc. */ private string b=""; private int bpos=0; inline private static nomask int get_data() { string s; b = b[bpos .. ]; bpos=0; s = file::read(BUFSIZE,1); if(!s || !strlen(s)) return 0; b += s; return 1; } inline private static nomask string extract(int bytes, int|void skip) { string s; s=b[bpos..bpos+bytes-1]; bpos += bytes+skip; return s; } /* Public functions. */ string gets() {
b6f2fb1997-05-03Fredrik Hübinette (Hubbe)  int p,tmp=0; while((p=search(b, "\n", bpos+tmp)) == -1)
5ae2381997-03-16Fredrik Hübinette (Hubbe)  {
b6f2fb1997-05-03Fredrik Hübinette (Hubbe)  tmp=strlen(b)-bpos;
5ae2381997-03-16Fredrik Hübinette (Hubbe)  if(!get_data()) return 0; }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return extract(p-bpos, 1); } int seek(int pos) { bpos=0; b=""; return file::seek(pos); }
2eedf71997-05-16Wilhelm Köhler  int tell() { return bpos; }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  int close(void|string mode) { bpos=0; b=""; if(!mode) mode="rw"; file::close(mode); } int open(string file, void|string mode) { bpos=0; b=""; if(!mode) mode="rwc"; return file::open(file,mode); } int open_socket() { bpos=0; b=""; return file::open_socket(); } object pipe() { bpos=0; b=""; return file::pipe(); } object dup() { object o; o=object_program(this_object())(); o->assign(file::dup()); return o; }
54dd9b1997-04-10Fredrik Hübinette (Hubbe)  int assign(object foo) { bpos=0; b=""; return ::assign(foo); }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  void set_nonblocking() { error("Cannot use nonblocking IO with buffered files.\n"); } int printf(string fmt, mixed ... data) { if(!sizeof(data)) return file::write(fmt); else return file::write(sprintf(fmt,@data)); } string read(int bytes) { while(strlen(b) - bpos < bytes) if(!get_data()) break; return extract(bytes); } string ungets(string s) { b=s+b[bpos..]; bpos=0; } int getchar() { if(strlen(b) - bpos < 1) if(!get_data()) return -1; return b[bpos++]; } };
e965921997-07-02Henrik Grubbström (Grubba) object(FILE) stdin=FILE("stdin");
c2a4061997-02-06Fredrik Hübinette (Hubbe)  string read_file(string filename,void|int start,void|int len) {
e965921997-07-02Henrik Grubbström (Grubba)  object(FILE) f; string ret, tmp;
c2a4061997-02-06Fredrik Hübinette (Hubbe)  f=FILE(); if(!f->open(filename,"r")) return 0; switch(query_num_arg()) { case 1: ret=f->read(0x7fffffff); break; case 2: len=0x7fffffff; case 3: while(start-- && f->gets());
e965921997-07-02Henrik Grubbström (Grubba)  object(String_buffer) buf=String_buffer();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  while(len-- && (tmp=f->gets())) { buf->append(tmp); buf->append("\n"); } ret=buf->get_buffer(); destruct(buf); } destruct(f); return ret; } string read_bytes(string filename,void|int start,void|int len) { string ret;
e965921997-07-02Henrik Grubbström (Grubba)  object(File) f = File(); if(!f->open(filename,"r"))
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return 0; switch(query_num_arg()) { case 1: case 2: len=0x7fffffff; case 3:
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  if(start) f->seek(start);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  }
e965921997-07-02Henrik Grubbström (Grubba)  ret=f->read(len); f->close();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return ret; } int write_file(string filename,string what) { int ret;
e965921997-07-02Henrik Grubbström (Grubba)  object(File) f = File(); if(!f->open(filename,"awc"))
c2a4061997-02-06Fredrik Hübinette (Hubbe)  error("Couldn't open file "+filename+".\n");
e965921997-07-02Henrik Grubbström (Grubba)  ret=f->write(what); f->close();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return ret; } int file_size(string s) { int *stat; stat=file_stat(s); if(!stat) return -1; return stat[1]; } void perror(string s) { #if efun(strerror) stderr->write(s+": "+strerror(predef::errno())+"\n"); #else stderr->write(s+": errno: "+predef::errno()+"\n"); #endif }
5ae2381997-03-16Fredrik Hübinette (Hubbe)  mixed `[](string index) { mixed x=`->(this_object(),index); if(x) return x; switch(index) { case "readline": return master()->resolv("readline"); default: return ([])[0]; } }