import java.util.*;
import java.io.*;

public class CfileList {
	Vector vec;
	int index;
	CfileList() {
		vec = new Vector();
		index = -1;
	}
	public void add(Cfile cfile) {
		String newName = cfile.simpleName();
		for(int i=0; i<vec.size(); i++) {
			Cfile oldCfile = (Cfile)vec.elementAt(i);
			String oldName = oldCfile.simpleName();
			int cond = newName.compareTo(oldName);
			if(cond < 0) {
				vec.insertElementAt(cfile,i);
				return;
			}
		}
		vec.addElement(cfile);
	}
	public void dump(PrintWriter out) {
		Cfile cfile = getFirst();
		while(cfile != null) {
			out.println();
			cfile.dump(out);
			cfile = getNext();
		}
	}
	public void dumpKeywordShort(PrintWriter out) {
		CtagList taglist = new CtagList();
		Cfile cfile = getFirst();
		while(cfile != null) {
			CfunctionList cfnlist = cfile.getCfnlist();
			Cfunction cfn = cfnlist.getFirst();
			while(cfn != null) {
				String[] list = cfn.getKeyList();
				for(int i=0; i<list.length; i++) {
					taglist.add(list[i],cfn);
				}
				cfn = cfnlist.getNext();
			}
			cfile = getNext();
		}
		Ctag tag = taglist.getFirst();
		while(tag != null) {
			out.println();
			out.println(tag.getTag());
			Cfunction cfn = tag.getFirst();
			while(cfn != null) {
				cfn.dumpNoParmsNoType(out);
				cfn = tag.getNext();
			}
			tag = taglist.getNext();
		}
	}
	public void dumpKeywordFull(PrintWriter out) {
		CtagList taglist = new CtagList();
		Cfile cfile = getFirst();
		while(cfile != null) {
			CfunctionList cfnlist = cfile.getCfnlist();
			Cfunction cfn = cfnlist.getFirst();
			while(cfn != null) {
				String[] list = cfn.getKeyList();
				for(int i=0; i<list.length; i++) {
					taglist.add(list[i],cfn);
				}
				cfn = cfnlist.getNext();
			}
			cfile = getNext();
		}
		Ctag tag = taglist.getFirst();
		while(tag != null) {
			out.println();
			out.println(tag.getTag());
			Cfunction cfn = tag.getFirst();
			while(cfn != null) {
				cfn.dump(out);
				cfn = tag.getNext();
			}
			tag = taglist.getNext();
		}
	}
	public void dumpFunctions(PrintWriter out) {
		CfunctionList masterList = new CfunctionList();
		Cfile cfile = getFirst();
		while(cfile != null) {
			CfunctionList cfnlist = cfile.getCfnlist();
			Cfunction cfn = cfnlist.getFirst();
			while(cfn != null) {
				masterList.add(cfn);
				cfn = cfnlist.getNext();
			}
			cfile = getNext();
		}
		Cfunction cfn = masterList.getFirst();
		while(cfn != null) {
			cfn.dump(out);
			cfn = masterList.getNext();
		}
	}
	public void dumpReturnval(PrintWriter out) {
		CtagList taglist = new CtagList();
		Cfile cfile = getFirst();
		while(cfile != null) {
			CfunctionList cfnlist = cfile.getCfnlist();
			Cfunction cfn = cfnlist.getFirst();
			while(cfn != null) {
				String retval = cfn.getReturnType();
				taglist.add(retval,cfn);
				cfn = cfnlist.getNext();
			}
			cfile = getNext();
		}
		Ctag tag = taglist.getFirst();
		while(tag != null) {
			out.println();
			out.println(tag.getTag());
			Cfunction cfn = tag.getFirst();
			while(cfn != null) {
				cfn.dumpNoType(out);
				cfn = tag.getNext();
			}
			tag = taglist.getNext();
		}
	}
	public Cfile getFirst() {
		index = -1;
		return(getNext());
	}
	public Cfile getNext() {
		if(++index < vec.size())
			return((Cfile)vec.elementAt(index));
		return(null);
	}
}
