• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Translating PB to C/C++

Started by Patrice Terrier, July 25, 2013, 12:23:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

ASC() ANSI version:

long asc(IN string s, IN long nPos) {
    long nRet = -1;
    long nLen = (long) s.length();
    if ((nLen == 0) || (nPos > nLen) || (nPos == 0)) { return nRet; }
    if (nPos > 0) {
        --nPos; }
    else {
        nPos = nLen + nPos; // Don't be fooled, despite the plus here, we remove the nPos value.
        if (nPos < 0) { return nRet; }
    }
    return (long) s[nPos];
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#1
TIME$() Unicode version:

wstring TIME$() {
    time_t now = time(0);
    tm tstruct;
    WCHAR buf[16];
    if (localtime_s(&tstruct, &now) == 0) {;
        wcsftime(buf, sizeof(buf), L"%X", &tstruct);
    }
    return buf;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#2
REPLACE() Unicode version:

wstring REPLACE(IN wstring MainString, IN wstring MatchString, IN wstring NewString) {
    size_t pos = 0;
    while((pos = MainString.find(MatchString, pos)) != std::string::npos) {
         MainString.replace(pos, MatchString.length(), NewString);
         pos += NewString.length();
    }
    return MainString;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#3
CHR$() ANSI version:

string chr$(IN long n) {
    char bb[2] = {0};
    bb[0] = n % 255;
    return (char*) bb;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

EXTRACT$() Unicode version:

wstring EXTRACT$(IN long nIndex, IN wstring sMain, IN wstring sSearch) {
    wstring sResult = sMain;
    long nLength = (long) sMain.length();
    long nRet = -1; // Not found
    if (nLength && (sSearch.length())) {
        if (nIndex < 0) {
            nRet = (long) sMain.rfind(sSearch, nLength + nIndex + 1); }
        else {
            nRet = (long) sMain.find(sSearch, nIndex);
        }
    }
    if (nRet > -1) { sResult = LEFT$(sMain, nRet); }
    return sResult;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

A few macro:

#define XOR ^
#define OR |
#define MOD %
#define ENDIF }
#define NEXT }


A few Unicode constants:

static wchar_t  *$NULL              = L"";
const wchar_t   *$DOT               = L".";
const wchar_t   *$ANTI              = L"\\";
const wchar_t   *$COMMA             = L",";
const wchar_t   *$SPACE             = L" ";
const wchar_t   *$ZLIM              = L"|";
const wchar_t   *$CHARNULL          = L"\0";
const wchar_t   *$CR                = L"\r";
const wchar_t   *$LF                = L"\n";
const wchar_t   *$TAB               = L"\t";
const wchar_t   *$SQUOTE            = L"'";
const wchar_t   *$DQUOTE            = L"\"";
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

#6
Good stuff Patrice keep em coming.

I am not a seasoned c++ coder but wouldn't wstring::npos be more politically correct?

wstring REPLACE(IN wstring MainString, IN wstring MatchString, IN wstring NewString) {
    size_t pos = 0;
    while((pos = MainString.find(MatchString, pos)) != wstring::npos) {
         MainString.replace(pos, MatchString.length(), NewString);
         pos += NewString.length();
    }
    return MainString;
}

James


Patrice Terrier

#7
wstring::npos, this constant means until the end of the wstring.
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#8
UCASE$() Unicode version:

wstring UCASE$(IN wstring sBuf) {
    std::transform(sBuf.begin(), sBuf.end(), sBuf.begin(), ::toupper);
    return sBuf;
}



LCASE$() Unicode version:

wstring LCASE$(IN wstring sBuf) {
    std::transform(sBuf.begin(), sBuf.end(), sBuf.begin(), ::tolower);
    return sBuf;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

RTRIM$() Unicode version:

wstring RTRIM$(IN wstring sBuf, IN wstring sChar) {
    wstring sResult = sBuf;
    LONG_PTR nLength = sBuf.length();
    if (nLength && (sChar.length())) {
        //sChar = sChar.substr(0, 1);
        while (nLength > 0) {
            // if (*sBuf.substr(nLength - 1, 1).c_str() == *sChar.c_str()) {
            // This is the sChar ANY search version
            if (std::wstring::npos != sChar.find(sBuf.substr(nLength - 1, 1))) {
                --nLength; }
            else {
                break;
            }
        }
        sResult = sBuf.substr(0, nLength);
    }
    return sResult;
}


rtrim$() ANSI version:

string rtrim$(IN string sBuf, IN string sChar) {
    string sResult = sBuf;
    long nLength = sBuf.length();
    if (nLength && (sChar.length())) {
        //sChar = sChar.substr(0, 1);
        while (nLength > 0) {
            // if (*sBuf.substr(nLength - 1, 1).c_str() == *sChar.c_str()) {
            // This is the sChar ANY search version
            if (std::string::npos != sChar.find(sBuf.substr(nLength - 1, 1))) {
                --nLength; }
            else {
                break;
            }
        }
        sResult = sBuf.substr(0, nLength);
    }
    return sResult;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#10
UCODE$() convert from ANSI to UNICODE.

wstring UCODE$(IN char* src) {
    return wstring(src, src + strlen(src));
}


Added:
And here is another variation based on a macro

#define UCODE$(str) ((WCHAR*) wstring(str.begin(), str.end()).c_str())
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

ISFOLDER() Unicode version:

BOOL ISFOLDER(IN WCHAR* wszPath) { // dllexport
    BOOL bRet = FALSE;
    DWORD ftyp = GetFileAttributes(wszPath);
    if ((ftyp != INVALID_FILE_ATTRIBUTES) && (ftyp & FILE_ATTRIBUTE_DIRECTORY)) { bRet = TRUE; }
    return bRet;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

ISFILE() Unicode version:

BOOL ISFILE(IN WCHAR* Filename) {
    BOOL bRet = FALSE;
    DWORD ftyp = GetFileAttributes(Filename);
    if ((ftyp != INVALID_FILE_ATTRIBUTES) && ((ftyp & FILE_ATTRIBUTE_DIRECTORY) == 0)) { bRet = TRUE; }
    return bRet;
}


Note: like the original PowerBASIC function:
Filename is an unambiguous file name, which may not contain an asterisk (*) or query (?). If it contains one or more of those characters, the function always returns false (0).

If you want to use wildcard characters, then use this:
BOOL FileExist (IN wstring sFileSpec) {
    WIN32_FIND_DATA fd = { 0 };
    LPCTSTR lpFileSpec = sFileSpec.c_str();
    BOOL bRet = FALSE;
    if (sFileSpec.length()) {
       HANDLE hFind;
       hFind = FindFirstFile(sFileSpec.c_str(), &fd);
       if (hFind != INVALID_HANDLE_VALUE) {
          FindClose(hFind);
          bRet = TRUE;
       }
    }
    return bRet;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

LEFT$() Unicode version:

wstring LEFT$(IN wstring sBuf, IN long nLeft) {
    wstring sResult = $NULL;
    LONG_PTR nLength = max(min((long)sBuf.length(), nLeft), 0);
    if (nLength) {
        sResult = sBuf.substr(0, nLength);
    }
    return sResult;
}



RIGHT$() Unicode version:

wstring RIGHT$(IN wstring sBuf, IN long nRight) {
    wstring sResult = $NULL;
    LONG_PTR nLength = sBuf.length();
    if (nLength) { sResult = sBuf.substr(nLength - nRight, nRight); }
    return sResult;
}



MID$() Unicode version:

wstring MID$(IN wstring sBuf, IN long nStart, IN long nMid = 0) { // dllexport
    wstring sResult = $NULL;
    long nLength = (long) sBuf.length();
    if (nMid == 0) { nMid = nLength - nStart + 1; }
    if (nLength) { sResult = sBuf.substr(nStart - 1, nMid); }
    return sResult;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

INSTR() Unicode version:

long INSTR(IN long nIndex, IN wstring sMain, IN wstring sSearch) {
    long nRet = -1; // Not found
    long nLength = (long) (sMain.length());
    if (nLength && (sSearch.length())) {
        if (nIndex < 0) {
            nRet = (long) (sMain.rfind(sSearch, nLength + nIndex + 1)); }
        else {
            nRet = (long) (sMain.find(sSearch, nIndex));
        }
    }
    return nRet + 1;
}

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com