/*
 *  Basic string operations class
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jTMassPlatform.jTServices;

import jTMass.TMass;
import jTMass.core.jALLL.jTObjects.TAtom;
import jTMass.core.jALLL.jTObjects.TFail;
import jTMass.core.jALLL.jTObjects.TList;
import jTMass.core.jALLL.jTObjects.TObject;
import jTMass.core.jTMassPlatform.TPlatform;


public class TSStringServices extends TASSynchronousService{

    public TObject processService(TList parameters, String clientName, String serviceName){
        super.processService(parameters,clientName,serviceName);
        try{
            String operator, param1, param2;
            Integer n1,n2;
            String opcode;
            operator=getIth(1,parameters).getContent();
            param1=getIth(2,parameters).getContent();

            if(operator.contentEquals("cnt")){
                String result="";
                for(int i=2;i<=parameters.size();i++)
                    result=result.concat(getIth(i,parameters).getContent());
                return(new TAtom(result));
            }

            if(operator.contentEquals("lng")){
                return(new TAtom(String.valueOf(param1.length())));
            }

            if(operator.contentEquals("idx")){
                String idxS=new String();
                char[] name=param1.toCharArray();
                int i;
                int l;
                for(l=0,i=param1.length()-1;((i>=0)
                                &&(name[i]>='0')&&(name[i]<='9'));i--,l++);
                idxS=String.copyValueOf(name, i+1, l);
                return(new TAtom(idxS));
            }

        }catch(Exception e){
            TMass._Terminal.reportError("String service failed");
            return(new TFail());
        }
        return(null);
    }

    // ===== CONSTRUCTOR =====

    public TSStringServices(TPlatform platform){
        super(platform);
    }
}
