본문 바로가기

프로그래밍/코드 조각

풀패스에서 폴더명, 파일명 얻어 오는 함수

    typedef std::basic_string<TCHAR> tstring;

    tstring ExtractStringWithDelim(LPCTSTR dump, const TCHAR delim, bool isLast)
    {
        if(!dump) { return _T(""); }

        tstring ret(dump);
        size_t pos = ret.rfind(delim);
        if(pos != tstring::npos) {
            if(isLast) {
                ret = ret.substr(pos+1, ret.length());
            }
            else {
                ret = ret.substr(0, pos);
            }
        }
        return ret;
    }

    tstring ExtractPathFromUrl(LPCTSTR path)
    {
        return ExtractStringWithDelim(path, _T('/'), false);
    }

    tstring ExtractFilenameFromUrl(LPCTSTR path)
    {
        return ExtractStringWithDelim(path, _T('/'), true);
    }

    tstring ExtractPathFromFullPath(LPCTSTR path)
    {
        return ExtractStringWithDelim(path, _T('\\'), false);
    }

    tstring ExtractFilenameFromFullPath(LPCTSTR path)
    {
        return ExtractStringWithDelim(path, _T('\\'), true);
    }