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

public class ProcParser{

    public ProcParser(){
 getProcesses();
    }

    public Vector getProcesses(){
 Process process = null;
 Vector procies = new Vector();
 File dir = new File("/proc");

 FileReader stat = null;
 FileReader statm = null;
 FileReader status = null;

 String[] procs = dir.list();
 int pos = 0;
 char c = 0;
 char cp = 0;
 int d,dp = 0;

 String[] statString = new String[38];
 String[] statmString = new String[7];
 String[] statusString = new String[47];

 StringBuffer buffer = null;
 for (int i = 0; i < procs.length;i++){
     buffer = new StringBuffer();
     try {
  new Integer(procs[i]);
  try {
      stat = new FileReader("/proc/"+procs[i]+"/stat");
      pos = 0;
      d = 0;
      while (d != -1 & d != '\n'){
   try {
       d = stat.read();
       c = (char)d;
       if (c == ' '){

    statString[pos] = new String(buffer);
    pos++;
    buffer = new StringBuffer();
    //System.out.print("_");
       }
       else if (d != -1 & d != '\n'){
    buffer.append(c);
    //System.out.print(c);
       }
       else if (d == '\n'){
    statString[pos] = new String(buffer);
    //System.out.println("fired " + procString[pos]);
       }
   }
   catch (IOException ioe){
       //System.out.println("oops");
   }
      }
      //finished while loop create new Process sts info with statString
 

  }
  catch (FileNotFoundException fnfe){
      //System.out.println("didn't find "+procs[i]);
  }
  try {
      stat.close();
  }
  catch (IOException ioe){
      //System.out.println("lost it!");
  }
  //*******************  finished with /proc/[pid]/stat ********************************
  buffer = new StringBuffer();
  try {
      statm = new FileReader("/proc/"+procs[i]+"/statm");
      pos = 0;
      d = 0;
      c = 0;
      while (d != -1 & d != '\n'){
   try {
       d = statm.read();
       c = (char)d;
       if (c == ' '){
    statmString[pos] = new String(buffer);
    pos++;
    buffer = new StringBuffer();
       }
       else if (d != -1 & d != '\n'){
    buffer.append(c);
       }
       else if (d == '\n'){
    statmString[pos] = new String(buffer);
       }
   }
   catch (IOException ioe){
   }
      }
  }
  catch (FileNotFoundException fnfe){
  }
  try {
      statm.close();
  }
  catch (IOException ioe){
  }
  //*************** finished with /proc/[pid]/statm *************************
  buffer = new StringBuffer();
  try {
      status = new FileReader("/proc/"+procs[i]+"/status");
      pos = 0;
      d = 0;
      c = 0;
      cp = 0;
      // System.out.println(c);
      while (d != -1 & pos < 47){
   try {
       d = status.read();
       cp = c;
       c = (char)d;
       if ((c == ' ' & cp != ' ') || c == '\t' || c == '\n'){
    statusString[pos] = new String(buffer);
    //System.out.println(statusString[pos]);
    pos++;
    buffer = new StringBuffer();
       }
       else if (d != -1 & c != '\n' & c != ' '){
    if (c != '\n' ){
        buffer.append(c);
        // System.out.println(c);
    }
       }
       else if (d == '\n'){
    statusString[pos] = new String(buffer);
       }
   }
   catch (IOException ioe){
   }
      }
  }
  catch (FileNotFoundException fnfe){
  }
  try {
      status.close();
  }
  catch (IOException ioe){
  }
 

  //************** finished with /proc/[pid]/status *************************
  process = new Process(statString,statmString,statusString);
  procies.addElement(process);
  System.out.println("done with "+process);
 

     }
     catch (NumberFormatException nfe){
  /** do nothing if not a number **/
  //System.out.print(".");
     }

 }

 return procies;
    }
    public Vector getDevices(){
 return new Vector();
    }
    public Vector getSockets(){
 return new Vector();
    }
    public Vector getNetDevices(){
 return new Vector();
    }
    /*  public MemInfo getMemInfo(){
    }
    public CpuInfo getCpuInfo(){
    }
    public OsInfo getOsInfo(){
    }*/
}