import java.util.StringTokenizer;

public
class TestString {

    private String m_options;
    private String m_fileName;
    
    private void parse1(String argument) {
        String lsFileName = "./";
        String options = "";
        
        // find options and file name (may have regular expression)
        if(argument != null) {
            argument = argument.trim();
            StringBuffer optionsSb = new StringBuffer(4);
            StringTokenizer st = new StringTokenizer(argument, " ");
            while(st.hasMoreTokens()) {
                String token = st.nextToken();
                if(token.charAt(0) == '-') {
                    if (token.length() > 1) {
                        optionsSb.append(token.substring(1));
                    }
                }
                else {
                   lsFileName = token;
                }
            }
            options = optionsSb.toString();
        }
        m_options = options;
        m_fileName = lsFileName;
    } 
    
    private void parse2(String argument) {
        String lsFileName = "./";
        String options = "";
        
        // find options and file name (may have regular expression)
        if(argument != null) {
            argument = argument.trim();
            StringBuffer optionsSb = new StringBuffer(4);
            StringTokenizer st = new StringTokenizer(argument, " ");
            while(st.hasMoreTokens()) {
                String token = st.nextToken();
                if(token.charAt(0) == '-') {
                    if (token.length() > 1) {
                        optionsSb.append(token.substring(1));
                    }
                }
                else {
                   lsFileName += token;
                }
            }
            options = optionsSb.toString();
        }
        m_options = options;
        m_fileName = lsFileName;
    }
    
    private void parse3(String argument) {
        String lsFileName = "./";
        String options = "";
        
        // find options and file name (may have regular expression)
        if(argument != null) {
            argument = argument.trim();
            StringBuffer optionsSb = new StringBuffer(4);
            StringBuffer lsFileNameSb = new StringBuffer(16);
            StringTokenizer st = new StringTokenizer(argument, " ", true);
            while(st.hasMoreTokens()) {
                String token = st.nextToken();
                
                if(lsFileNameSb.length() != 0) {
                    // file name started - append to file name buffer
                    lsFileNameSb.append(token);
                }
                else if(token.equals(" ")) {
                    // delimiter and file not started - ignore
                    continue;
                } 
                else if(token.charAt(0) == '-') {
                    // token and file name not started - append to options buffer
                    if (token.length() > 1) {
                        optionsSb.append(token.substring(1));
                    }
                }
                else {
                    // filename - append to the filename buffer
                    lsFileNameSb.append(token);
                }
            }
            
            if(lsFileNameSb.length() != 0) {
                lsFileName = lsFileNameSb.toString();
            }
            options = optionsSb.toString();
        }
        
        m_options = options;
        m_fileName = lsFileName;
    }
    
    public static void main(String args[]) {
    
        TestString tstStr = null;
        String argument = null;
        
        tstStr = new TestString();
        argument = "-ls /abcd";
        tstStr.parse1(argument);
        System.out.println(" ---------------------------");
        System.out.println("Method  : parse1()");
        System.out.println("Argument: >" + argument + "<");
        System.out.println("Options : >" + tstStr.m_options + "<");
        System.out.println("File    : >" + tstStr.m_fileName + "<");
        
        tstStr = new TestString();
        argument = "-ls /abcd";
        tstStr.parse2(argument);
        System.out.println(" ---------------------------");
        System.out.println("Method  : parse2()");
        System.out.println("Argument: >" + argument + "<");
        System.out.println("Options : >" + tstStr.m_options + "<");
        System.out.println("File    : >" + tstStr.m_fileName + "<");
        
        tstStr = new TestString();
        argument = "-ls /abcd";
        tstStr.parse3(argument);
        System.out.println(" ---------------------------");
        System.out.println("Method  : parse3()");
        System.out.println("Argument: >" + argument + "<");
        System.out.println("Options : >" + tstStr.m_options + "<");
        System.out.println("File    : >" + tstStr.m_fileName + "<");
        
        tstStr = new TestString();
        argument = "-ls /ab cd";
        tstStr.parse1(argument);
        System.out.println(" ---------------------------");
        System.out.println("Method  : parse1()");
        System.out.println("Argument: >" + argument + "<");
        System.out.println("Options : >" + tstStr.m_options + "<");
        System.out.println("File    : >" + tstStr.m_fileName + "<");
        
        tstStr = new TestString();
        argument = "-ls /ab cd";
        tstStr.parse2(argument);
        System.out.println(" ---------------------------");
        System.out.println("Method  : parse2()");
        System.out.println("Argument: >" + argument + "<");
        System.out.println("Options : >" + tstStr.m_options + "<");
        System.out.println("File    : >" + tstStr.m_fileName + "<");
        
        tstStr = new TestString();
        argument = "-ls /ab cd";
        tstStr.parse3(argument);
        System.out.println(" ---------------------------");
        System.out.println("Method  : parse3()");
        System.out.println("Argument: >" + argument + "<");
        System.out.println("Options : >" + tstStr.m_options + "<");
        System.out.println("File    : >" + tstStr.m_fileName + "<");
    }
    
}