package ga050426;

public class Segments {
	Segment head, tail;
	
	public void enter(String name, int beg, int len, boolean p){
		if (tail != null)
			tail = tail.next = new Segment(name, beg, len, p, tail, null);
		else
			tail = head = new Segment(name, beg, len, p, tail, null);
	}
	Segments(){}
	
	public int Ttotal(){
		int total = 0;
		for (Segment cur = head; cur != null; cur = cur.next) {
			if(cur.name.contains("txt"))total += cur.len;
			}
		return total;
	}
	public int Dtotal(){
		int total = 0;
		for (Segment cur = head; cur != null; cur = cur.next) {
			if(cur.name.contains("dat"))total += cur.len;
			}
		return total;
	}
	public int Btotal(){
		int total = 0;
		for (Segment cur = head; cur != null; cur = cur.next) {
			if(cur.name.contains("bss"))total += cur.len;
			}
		return total;
	}
	
	public static Segments instance = new Segments();
	
	public int numOfPSeg(){
		int total = 0;
		for (Segment cur = head; cur != null; cur = cur.next) {
			if(cur.p) total++;
			}
		return total;
	}
	
	public String toString() {
		StringBuffer buf = new StringBuffer(String
				.format("#Segments (dec)%n"));
		for (Segment cur = head; cur != null; cur = cur.next) {
			buf.append(String.format(" %s  %d  %d  %s %n", cur.name, cur.beg, cur.len, cur.p));
		}
		return new String(buf);
	}
}
