/*
 * Util.java
 * általános célú hasznos függvények, akár a Java részei is lehettek volna
 * by pts@fazekas.hu at Sun Apr 22 19:23:53 CEST 2001
 * további doksi: a README-ben és e file legkülső class-ának fejkommentjében
 *
 * Kincskereső Kisgömböc (C) Early May 2001 by eNTitánok (Rév Szilvia,
 * Szabó Péter <pts@inf.bme.hu>, Szurdi Miklós, Weizengruber Attila).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or   
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package eNTitanok.util;
import java.util.Vector;
import java.util.Calendar;

/**
 * Általános célú, hasznos függvények.
 */
public class Util {
  /**
   * Az `n' nemnegatív számot adja vissza pontosan `hossz' db hexa számjeggyel.
   */
  public static String hexformat(int n, int hossz) {
    // Assert: n>=0
    // gagyi java, meg egy sprintf()-et se tud, es a stringkezelo fuggvenyei
    // teljesen hasznalhatatlanok, lassuak, nehezkesek.
    String s=Integer.toHexString(n);
    int l=s.length();
    return l>=hossz ? s : ("00000000"+s).substring(8+l-hossz,8+l);
  }
  /**
   * Az `n' nemnegatív számot adja vissza pontosan `hossz' db decimális
   * számjeggyel.
   */
  public static String decformat(int n, int hossz) {
    // Assert: n>=0
    // gagyi java, meg egy sprintf()-et se tud, es a stringkezelo fuggvenyei
    // teljesen hasznalhatatlanok, lassuak, nehezkesek.
    String s=Integer.toString(n);
    int l=s.length();
    return l>=hossz ? s : ("00000000000000"+s).substring(14+l-hossz,14+l);
  }
  /**
   * A String-ben található egész számokat egy tömbbe fűzi.
   */
  public static int[] szamokInt(String s) {
    int i, j;
    char c;
    Vector v=new Vector();
    for (i=0;i<s.length();i=j) {
      while (i<s.length() && (c=s.charAt(i))!='-' && (c<'0' || c>'9')) i++;
      if (i==s.length()) break;
      j=i+1;
      while (j<s.length() && (c=s.charAt(j))>='0' && c<='9') j++;
      v.addElement(new Integer(Integer.parseInt(s.substring(i,j))));
    }
    int[] t=new int[v.size()];
    for (i=t.length-1;i>=0;i--) t[i]=((Integer)v.elementAt(i)).intValue();
    return t;
  }
  /**
   * A String-ben található egész számokat egy tömbbe fűzi.
   */
  public static long[] szamokLong(String s) {
    int i, j;
    char c;
    Vector v=new Vector();
    for (i=0;i<s.length();i=j) {
      i=0;
      while (i<s.length() && (c=s.charAt(i))!='-' && (c<'0' || c>'9')) i++;
      if (i==s.length()) break;
      j=i+1;
      while (j<s.length() && (c=s.charAt(j))>='0' && c<='9') j++;
      v.addElement(new Long(Long.parseLong(s.substring(i,j))));
    }
    long[] t=new long[v.size()];
    for (i=t.length-1;i>=0;i--) t[i]=((Long)v.elementAt(i)).longValue();
    return t;
  }
  /**
   * Pontosan akkor `true', ha a tömb csupa nemnagatív értékekből áll, melyek
   * összege pozitív.
   */
  public static boolean eloszlasp(int t[]) {
    int osszeg=0;
    for (int i=t.length-1;i>=0;i--) { 
      if (t[i]<0) return false;
      osszeg+=t[i];
    }
    return osszeg>0;
  }
  /**
   * Pontosan akkor `true', ha a tömb csupa nemnagatív értékekből áll, melyek
   * összege pozitív, és a tömb `n' elemű.
   */
  public static boolean eloszlasp(int t[]int n) {
    return t.length==n && eloszlasp(t);
  }

  /**
   * ÉÉÉÉ-HH-NN ÓÓ:PP:MM alakban visszaad egy dátumot.
   */
  public static String mysqlDateFormat(Calendar calendar) {
    return decformat(calendar.get(Calendar.YEAR),4)+"-"+
           decformat(calendar.get(Calendar.MONTH)+1,2)+"-"+
           decformat(calendar.get(Calendar.DAY_OF_MONTH),2)+" "+
           decformat(calendar.get(Calendar.HOUR_OF_DAY),2)+":"+
           decformat(calendar.get(Calendar.MINUTE),2)+":"+
           decformat(calendar.get(Calendar.SECOND),2);
  }
} /// class Util