DCalendar

git clone git://xatko.vsos.ethz.ch/DCalendar.git
Log | Files | Refs

dcalendar.d (968B)


      1 module dcalendar;
      2 import std.stdio;
      3 import std.algorithm;
      4 import std.datetime;
      5 import std.string;
      6 
      7 class Calendar{
      8 	private immutable string filename;
      9 	private File f;
     10 	private static char buf[6];
     11 	this(in string filename){
     12 		this.filename=filename;
     13 	}
     14 	public void open(){
     15 		f.open(filename,"r");
     16 	}
     17 	public void close(){
     18 		f.close();
     19 	}
     20 	char[] format(inout ref SysTime t){
     21 		return sformat(buf,"%02d/%02d", t.month, t.day);
     22 	}
     23 	public auto getEntries(inout SysTime t)
     24 	in{
     25 		assert(f.isOpen, "File not open");
     26 	}
     27 	body{
     28 		char[] form=this.format(t);
     29 		return f.byLine.filter!(a=>startsWith(a, form)).map!(a=>a[6..$]);
     30 	}
     31 }
     32 
     33 struct CalendarList{
     34 	Calendar calendars[];
     35 	
     36 	void add(string filename){
     37 		calendars~=new Calendar(filename);
     38 	}
     39 	bool run(inout SysTime t, void delegate(char[]) f){
     40 		bool ret=false;
     41 		foreach(ref Calendar c; calendars){
     42 			c.open();
     43 			foreach(char[] s; c.getEntries(t)){
     44 				ret=true;
     45 				f(s);
     46 			}
     47 			c.close();
     48 		}
     49 		return ret;
     50 	}
     51 }