package com.brilliance.common.syspth.service.impl;

import com.brilliance.common.syspth.service.SyspthService;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

public class SyspthServiceImpl implements SyspthService {

    public String getPathofName(String argPth) {
        //取得上下文
//        IContext ctx = MdaEnv.getContext();
//        ctx.setErrorCode(NO_ERROR);
        //! Function to extract the path portion of a filename or a field reference.
        //! The part of the string up to the last Backslash is returned.
        //! On absolute names the leading backslash is not eliminated.
        //! If no backslash is found an empty string is returned.
        //!
        //! Corresponding functions: AddExtensionToName, AddNodeToPath, GetLastNode, Basename (TradeDesign function), GetFileExtension, GetNormalizedNodename
        // search for a backslash starting at the end
        String chr = NULLSTR;
        for (int idx = Strings.len(argPth); idx >= 1; idx --) {
            chr = Strings.mid(argPth, idx, 1);
            if (MdaUtils.compareTo(chr, "\\") == 0 || MdaUtils.compareTo(chr, "/") == 0) {
                if (idx > 1) {
                    // return all characters up to the character before the found backslash
                    return Strings.trim(Strings.mid(argPth, 1, idx - 1));
                } else {
                    // if the only backslash is the first character, return it
                    return chr;
                }
            }
        }
        // When no backslash is found, return an empty path
        return "";

    }
}
