[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/shockfish/tools/java_tc65/src/com/shockfish/tinyos/util CldcMath.java, NONE, 1.1 OtapCommand.java, NONE, 1.1 ToolBox.java, NONE, 1.1 Unsigned.java, NONE, 1.1

rogmeier rogmeier at users.sourceforge.net
Mon Sep 11 06:40:06 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/shockfish/tools/java_tc65/src/com/shockfish/tinyos/util
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv28926/src/com/shockfish/tinyos/util

Added Files:
	CldcMath.java OtapCommand.java ToolBox.java Unsigned.java 
Log Message:
TC65/TinyNode foundations.

--- NEW FILE: CldcMath.java ---
package com.shockfish.tinyos.util;

public class CldcMath {

	// TODO this implementation should check for NaN
	public static double pow(double a, int b) {
		double r = 1;
		for (int i=0;i<Math.abs(b);i++) {
			r = r * a;	
		}
		if (b<0)
			return 1/r;
		return r;
	}
	
}

--- NEW FILE: OtapCommand.java ---
package com.shockfish.tinyos.util;


public class OtapCommand {
    
    public final static String ARG_SEP = ";";

    public static String[] getArgs(String argStr) {
	try {
	    // find nb of args
	    int nbArgs = 0;
	    for (int i=0;i<argStr.length();i++) {
		if (argStr.substring(i,i+1).equals(ARG_SEP)) {
		    nbArgs++;
		}
	    }
	    // 2nd pass
	    String[] args = new String[nbArgs];
	    int aidx = 0;
	    int lastidx = 0;
	    for (int i=0;i<argStr.length();i++) {
		if (argStr.substring(i,i+1).equals(ARG_SEP)) {
		    args[aidx] = argStr.substring(lastidx, i);
		    aidx++;
		}
	    }
	    return args;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }
    

}
--- NEW FILE: ToolBox.java ---
package com.shockfish.tinyos.util;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import com.shockfish.tinyos.tools.CldcLogger;

public class ToolBox {
  
  public final static char PAIRS_SEP = ';'; // to separate couple in NodeMap
  public final static char VALUES_SEP = ',';

  /**
   * @return , if str is null, null is return
   */
  public static String[] split (String str, char c) {
    if (str == null)
      return null;
    
    // search for all the c in str
    Vector posOfC = new Vector();
    int lastPos = -1;
    while (str.indexOf(c, lastPos + 1) != -1) {
      lastPos = str.indexOf(c, lastPos + 1);
      posOfC.addElement(new Integer(lastPos));
    }
    
    String[] response = new String[posOfC.size() + 1];
    
    // make the substring
    int subStringBegin = 0;
    Integer subStringLast;
    for (int i = 0; i < response.length; i++) {
      if (posOfC.size() != 0)
        subStringLast = (Integer) posOfC.firstElement();
      else
        subStringLast = new Integer(str.length());
      
      posOfC.removeElement(subStringLast);
      response[i] = str.substring(subStringBegin, subStringLast.intValue());
      subStringBegin = subStringLast.intValue() + 1;
    }
    
    return response;
  }
  
  /* the return hashtable may be empty! 
   * Hashtabel format (Integer as key | String as data) */
  public static Hashtable nodeMapSpliter(String nodeMap) {
    Hashtable ht = new Hashtable();
    if (nodeMap == null) {
      return ht;
    }
    
    String[] values = ToolBox.split(nodeMap, ';');
    
    for (int i = 0; i < values.length; i++) {
      String[] pair = ToolBox.split(values[i], ',');
      if (pair.length == 2)
        try {
          ht.put(new Integer(Integer.parseInt(pair[0])),
              pair[1]);
        } catch (NumberFormatException e) {
          CldcLogger.warning("Couple (" + pair[0] + "|" + pair[1] + ") ignored" +
              " to make IDNode/placeNum hashtable, src:ToolBox");
        }
    }
    return ht;
  }
  
  public static String buildNodeMapProp(Hashtable nodeMap) {
    StringBuffer buf = new StringBuffer();
    for (Enumeration e = nodeMap.keys(); e.hasMoreElements();) {
      Object i = e.nextElement();
      if (i instanceof Integer) {
        buf.append(((Integer) i).toString() + VALUES_SEP + nodeMap.get(i) +
          PAIRS_SEP);
      }
    }
    return buf.toString();
  }
}

--- NEW FILE: Unsigned.java ---
package com.shockfish.tinyos.util;


public class Unsigned {



  public static int convertunsignedBytetoInt(byte data) {
    return (data & 0xff);
  }
  public static int convertunsignedBytestoInt(byte dataLow, byte dataHigh) {
    return ((dataLow&0xff) | ((dataHigh&0xff) <<8));
  }

  public static int convertunsignedShorttoInt (short data) {
  return (data & 0xffff);
}


  public static long convertunsignedBytestoInt(byte dataFirst, byte dataSecond, byte dataThird, byte dataFourth) {
   return ((dataFirst &0xff )| ((dataSecond&0xff) <<8) | ((dataThird&0xff) <<16) | ((dataFourth&0xff) <<24));
 }

 public static long convertUnsignedInttoLong (int data) {
   return (data & 0xffffffffl);
 }

 public static byte [] convertInttoUnsignedBytes (int data) {
   byte [] res = new byte[4];
   int temp=data;
   int i;
   for (i=0; i<res.length;i++) {
     res[i] = convertInttoUnsignedByte(temp);
     temp=temp>>8;
   }
   return res;
 }
 
 public static byte [] convertLongtoUnsignedBytes (long data) {
	   byte [] res = new byte[8];
	   long temp=data;
	   int i;
	   for (i=0; i<res.length;i++) {
		 
	     res[i] = convertLongtoUnsignedByte(temp);
	     temp=temp>>8;
	   }
	   return res;
	 }

 public static long convertunsignedBytestoLong(byte [] data) {
	 long result=0;
	 for (int i=0; i<data.length;i++) {
	     result=result |((long)(data[i]&0xff)<<i*8);
	   }
	   return result;
	 }

 public static void main(String args[]) throws Exception {
	 long essai=286423;
	 System.out.println("Nombre a convertir: "+essai);
	 byte [] data=Unsigned.convertLongtoUnsignedBytes(essai);
	 long resu=Unsigned.convertunsignedBytestoLong(data);
	 System.out.println("Nombre a converti: "+resu);
	 
 }
  public static byte convertInttoUnsignedByte(int data) {
    //byte temp= data && 0xff;
    return (byte) (data & 0xff);
  }
  
  public static byte convertLongtoUnsignedByte(long data) {
	    //byte temp= data && 0xff;
	    return (byte) (data & 0xff);
	  }

  public static short convertInttoUnsignedShort(int data) {
    //byte temp= data && 0xff;
    return (short) (data & 0xffff);
  }

  public static int convertLongtoUnsignedInt (long data) {
    return (int) (data & 0xffffffff);
  }


}



More information about the Tinyos-contrib-commits mailing list