package com.kzw.core.util;
// jcommon.java
//
// -------------------------------------------------------------------------
// This class is a container of common functions
// Usage:
// 1. Call the class methods: jcommon.pad(telcolsize, '0' -4)
// -------------------------------------------------------------------------
//
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.text.Collator;
import java.text.SimpleDateFormat;
import java.io.*;
public class FunctionsUtil implements Serializable{
// -----------------------------
// Pad function - CLASS METHOD
// -----------------------------
// This function pads a string up to the required length with the
// pad character specified.
// If len is negative, this function pads on the left
// -----------------------------
public static char crchar[] = { 0x0D, 0x0A };
public static String crlf = new String(crchar);
public static char cr = 0x0D;
public static char lf = 0x0A;
public static char quchar[] = { '"' };
public static char quBlank[] = { '"', '"' };
public static String quote = new String(quchar);
public static String blankQuote = new String(quBlank);
public static String fs = File.separator;
private static char[] hex40 = null;
static {
hex40 = new char[4000];
for (int i = 0; i < 4000; i++) {
hex40[i] = 0x40;
}
}
// chopstr
// This function chops a string up into strings tokens
public static String chopstr(String line, int maxlen) {
StringTokenizer strtoken = new StringTokenizer(line, " ", true);
int curlen = 0;
boolean firstoken = true;
String retline = "";
while (strtoken.hasMoreTokens()) {
String token = strtoken.nextToken();
curlen += token.length();
if (curlen <= maxlen | firstoken) {
retline += token;
} else {
retline += crlf + token;
curlen = token.length();
}
if (firstoken)
firstoken = false;
}
return retline;
}
public static String firstToUpperCase(String string) {
String post = string.substring(1, string.length());
String first = ("" + string.charAt(0)).toUpperCase();
return first + post;
}
// append one file to another
public static void copyAppend(String fromFile, String toFile)
throws IOException {
String lineIn;
PrintWriter out = new PrintWriter(new FileWriter(toFile, true));
BufferedReader in = new BufferedReader(new FileReader(fromFile));
while ((lineIn = in.readLine()) != null)
out.println(lineIn);
out.close();
in.close();
}
// convert from decimal string to hex string value
public static String d2x(String numstr, int precision, int scale) {
String retstr = "";
String signstr = "";
String pstr = "";
String sstr = "";
boolean beginscale = false;
char onechar;
if (numstr.indexOf("-") >= 0)
signstr = "D";
else
signstr = "C";
int strlen = numstr.length();
for (int i = 0; i < strlen; i++) {
onechar = numstr.charAt(i);
if (onechar == '+' || onechar == '-')
;
else if (onechar == '.')
beginscale = true;
else if (beginscale)
sstr += onechar;
else
pstr += onechar;
}
int diflen = precision - scale;
if (pstr.length() < diflen)
pstr = FunctionsUtil.Pad(pstr, '0', -1 * diflen);
// pad precision portion if precision is an even number
if ((precision % 2) == 0)
pstr = "0" + pstr;
if (sstr.length() < scale)
sstr = FunctionsUtil.Pad(sstr, '0', scale);
retstr = s2x(pstr + sstr + signstr);
return retstr;
}
public static int exp(int in, int power) {
int retval = in;
if (power > 0) {
for (int i = 1; i < power; i++) {
retval = retval * in;
}
} else
retval = 1;
return retval;
}
// return the argument, converted to upper case, at a specific position
// (counting from zero)
public static String getArg(String[] arg, int argNum) {
return ((argNum >= 0 && argNum < arg.length) ? arg[argNum]
.toUpperCase() : "");
}
// return the concatenation of arguments starting from a specific position
// to the last one
public static String getArg(String[] arg, int argNum, boolean lastArg) {
String retstr = "";
retstr = getArg(arg, argNum);
if (lastArg && argNum >= 0)
for (int i = argNum + 1; i < arg.length; i++)
retstr += " " + arg[i];
return retstr.toUpperCase();
}
/**
* This method was created in VisualAge.
*/
public static String getFmtString(String varName, String fmt) {
int numDigits = 0;
int numDecimals = 0;
StringTokenizer tline = new StringTokenizer(fmt, " (),");
String tokstr = "";
boolean isNumDigits = true;
while (tline.hasMoreTokens()) {
tokstr = tline.nextToken().trim();
try {
Integer num = new Integer(tokstr);
if (isNumDigits) {
numDigits = num.intValue();
isNumDigits = false;
} else
numDecimals = num.intValue();
} catch (Exception e) {
}
} // end while
return getFmtString(varName, numDigits, numDecimals);
}
// search an option from the options string and return the value given for
// that option termination by a " " or "/"
/**
* @param varName
* @param numDigits
* @param numDecimals
* @return
*/
public static String getFmtString(String varName, int numDigits,
int numDecimals) {
// the number in the data type DECIMAL(A,B) has A-B integer digits and B
// decimal digits
int numSig = numDigits - numDecimals;
StringBuffer retStr = new StringBuffer("");
int numThree = 0;
for (int c = 0; c < numSig; c++) {
if (numThree == 3) {
retStr.insert(0, ",");
numThree = 0;
}
if (c == 0)
retStr.insert(0, "0");
else
retStr.insert(0, "#");
++numThree;
}
for (int c = 0; c < numDecimals; c++) {
if (c == 0)
retStr.append(".0");
else
retStr.append("0");
}
return (" private static String " + varName + "Fmt " + " = "
+ FunctionsUtil.quote + retStr.toString() + FunctionsUtil.quote + ";");
}
public static String getFmtString(String varName, String digits,
String decimals) {
int numDigits = new Integer(digits).intValue();
int numDecimals = new Integer(decimals).intValue();
return getFmtString(varName, numDigits, numDecimals);
}
public static String getOpt(String options, String searchStr) {
String retstr = "";
int i = options.indexOf(searchStr);
if (i >= 0) {
int e = options.length();
int s = i + searchStr.length();
int temp = options.indexOf(' ', s);
if (temp >= 0 && temp < e)
e = temp;
temp = options.indexOf('/', s);
if (temp >= 0 && temp < e)
e = temp;
retstr = options.substring(s, e);
} // end if
return retstr;
}
// search parmStr for searchStr=value and return value if found, otherwise
// return blank
public static String getParameter(String parmStr, String searchStr,
String separator) {
String result = "";
if (searchStr == null || parmStr == null)
return result;
//
StringTokenizer tline = new StringTokenizer(parmStr, separator);
while (tline.hasMoreTokens()) {
String value = tline.nextToken().trim();
if (value.equals(searchStr)) {
if (tline.hasMoreTokens())
result = tline.nextToken().trim();
break;
}
}
return result;
}
/**
* This method was created in VisualAge.
*/
public static String getScale(String fmt) {
StringBuffer retStr = new StringBuffer("");
int numDigits = 0;
int numDecimals = 0;
StringTokenizer tline = new StringTokenizer(fmt, " (),");
String tokstr = "";
boolean isNumDigits = true;
while (tline.hasMoreTokens()) {
tokstr = tline.nextToken().trim();
try {
Integer num = new Integer(tokstr);
if (isNumDigits) {
numDigits = num.intValue();
isNumDigits = false;
} else
numDecimals = num.intValue();
} catch (Exception e) {
}
} // end while
Integer numDec = new Integer(numDecimals);
return (numDec.toString());
}
/**
* This method was created in VisualAge. Search and replace text in all
* files in a directory or a single file
*