//============================================================================== // BuiltinFuncs.java //============================================================================== package tribble.net.ftp.shell; // System imports import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import java.lang.Exception; import java.lang.IllegalArgumentException; import java.lang.Integer; import java.lang.Long; import java.lang.String; import java.lang.StringBuffer; /******************************************************************************* * FTP command interpreter built-in functions. * * * @version $Revision: 1.3 $ $Date: 2007/08/12 16:35:05 $ * @since API 1.0, 2007-04-12 * @author David R. Tribble (david@tribble.com). *

* Copyright ©2007 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated by * by the United States Department of State as a terrorist, or terrorist * government or agency, to use and distribute this source code provided * that the original copyright notice remains present and unaltered. * * @see Interp */ abstract class BuiltinFuncs implements CommandCodes { // Identification /** Revision information. */ static final String REV = "@(#)tribble/net/ftp/shell/BuiltinFuncs.java $Revision: 1.3 $ $Date: 2007/08/12 16:35:05 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static methods /*************************************************************************** * Evaluate built-in function expression index(sub,src). * *

* Format *

* (CMD_FUNC_INDEX, expr1, expr2) * * @return * Index of the leftmost character within string s that matches * sub, or zero (0) if the substring does not occur within the * string. * * @since 1.1, 2007-04-12 */ static String funcIndex(String sub, String src) { // Find the leftmost matching substring if (src == null || sub == null) return (VAL_ZERO); return ("n" + (src.indexOf(sub, 1) + 1)); } /*************************************************************************** * Evaluate built-in function expression len(src). * *

* Format *

* (CMD_FUNC_LEN, expr) * * @return * Length of the string expr. * * @since 1.1, 2007-04-12 */ static String funcLen(String src) { // Determine the length of the string argument if (src == null) return (VAL_ZERO); return ("n" + (src.length() - 1)); } /*************************************************************************** * Evaluate built-in function expression lc(src). * *

* Format *

* (CMD_FUNC_LCASE, expr) * * @return * The string expr converted to all lower case. * * @since 1.2, 2007-05-04 */ static String funcLowerCase(String src) { // Convert the string contents to lower case if (src == null) return (VAL_EMPTY); return ('s' + src.substring(1).toLowerCase()); } /*************************************************************************** * Evaluate built-in function expression norm(src). * *

* Format *

* (CMD_FUNC_NORM, expr1) * * @return * String src as a normalized native filename. * * @since 1.1, 2007-04-12 */ static String funcNorm(String src) { // Normalize the string into a native filename if (src == null) return (VAL_EMPTY); ///+REDO return (src); } /*************************************************************************** * Evaluate built-in function expression repl(old,rep,src). * *

* Format *

* (CMD_FUNC_REPL, expr1, expr2, expr3) * * @return * String src with the first (leftmost) occurrence of substring * old replaced with substring rep. * * @since 1.1, 2007-04-12 */ static String funcRepl(String old, String rep, String src) { StringBuffer buf; int oldLen; int repLen; // Sanity checks if (src == null) return (VAL_EMPTY); if (rep == null || rep.length() <= 1) return (src); if (old == null) old = VAL_EMPTY; // Replace a substring within the string buf = new StringBuffer(src); old = old.substring(1); rep = rep.substring(1); oldLen = old.length(); repLen = rep.length(); for (int o = 1; o <= buf.length() - oldLen; ) { int p; // Replace a single substring occurrence p = buf.indexOf(old, o); if (p < 0) break; buf.replace(p, p + oldLen, rep); o = p + repLen; } return (buf.toString()); } /*************************************************************************** * Evaluate built-in function expression rindex(p,s). * *

* Format *

* (CMD_FUNC_RINDEX, expr1, expr2) * * @return * Index of the rightmost character within string s that matches * sub, or zero (0) if the substring does not occur within the * string. * * @since 1.1, 2007-04-12 */ static String funcRindex(String sub, String s) { // Find the rightmost matching substring if (s == null || sub == null) return (VAL_ZERO); return ("n" + (s.lastIndexOf(sub, 1) + 1)); } /*************************************************************************** * Evaluate built-in function expression sub(src,pos). * *

* Format *

* (CMD_FUNC_SUB, expr1, expr2) * * @return * Substring of src starting at position pos up to the end * of the string. Note that the leftmost character is at position 1. If * pos is negative, the substring is extracted from the right side * of src. * * @since 1.1, 2007-04-12 */ static String funcSubstr(String src, int pos) { int max; // Extract a substring from the string if (src == null) return (VAL_EMPTY); max = src.length(); if (pos >= 0) { if (pos == 0) pos = 1; if (pos > max) return (VAL_EMPTY); return ('s' + src.substring(pos)); } else { pos = pos+max; if (pos > max) return (VAL_EMPTY); if (pos > 0) return ('s' + src.substring(pos)); if (src.charAt(0) != 's') return ('s' + src.substring(1)); return (src); } } /*************************************************************************** * Evaluate built-in function expression sub(src,pos,len). * *

* Format *

* (CMD_FUNC_SUB, expr1, expr2, expr3) * * @return * Substring of src starting at position pos for * len characters. Note that the leftmost character is at * position 1. If pos is negative, the substring is extracted from * the right side of src. If len is less than 1, an empty * string ('') is returned. * * @since 1.1, 2007-04-12 */ static String funcSubstr(String src, int pos, int len) { int max; // Extract a substring from the string if (src == null) return (VAL_EMPTY); if (len <= 0) return (VAL_EMPTY); max = src.length(); if (pos >= 0) { if (pos == 0) pos = 1; if (pos+len > max) return ('s' + src.substring(pos)); return ('s' + src.substring(pos, pos+len)); } else { pos = pos+max; if (pos > max) return (VAL_EMPTY); if (pos > 0) return ('s' + src.substring(pos, pos+len)); return ('s' + src.substring(1, len)); } } /*************************************************************************** * Evaluate built-in function expression trim(s). * *

* Format *

* (CMD_FUNC_TRIM, expr) * * @return * String expr, stripped of all leading and trailing spaces and * control characters. * * @since 1.1, 2007-04-12 */ static String funcTrim(String s) { // Strip all leading and trailing spaces and control chars if (s == null) return (VAL_EMPTY); return ('s' + s.substring(1).trim()); } /*************************************************************************** * Evaluate built-in function expression uc(src). * *

* Format *

* (CMD_FUNC_UCASE, expr) * * @return * The string expr converted to all upper case. * * @since 1.2, 2007-05-04 */ static String funcUpperCase(String src) { // Convert the string contents to upper case if (src == null) return (VAL_EMPTY); return ('s' + src.substring(1).toUpperCase()); } } // End BuiltinFuncs.java