News:

Download Pelles C here: http://www.pellesc.se

Main Menu

ComCpp2C converter example

Started by TimoVJL, May 21, 2018, 09:31:20 PM

Previous topic - Next topic

TimoVJL

ComCpp2C converter converts -> and some keywords from cpp-file for COM to C.

-> to ->lpVtbl->
nullptr to NULL
__uuidof() to &IID_
reinterpret_cast<void**> to (void**)

A bit less modifications by hand ;)

Hopefully someone share us a better program for same purboses.

EDIT 2018-05-23 minor fix ->
#define BUFSIZE 4096
char zero;
char buf1[BUFSIZE];    // read buffer
char buf2[BUFSIZE];    // write buffer

// -> nullptr __uuidof() reinterpret_cast<void**>
// const_cast<UINT> static_cast<DWORD>
char *pnptr, *puuid, *pcast, *plptr;    // globals

char *check_line(char *buf1)
{    // start values
    pnptr = strstr(buf1, "nullptr");
    puuid = strstr(buf1, "__uuidof(");
    pcast = strstr(buf1, "_cast<");
    plptr = strstr(buf1, "->");

    if (!(pnptr || puuid || pcast || plptr)) {
        return buf1;
    }
    char *p1 = buf1;
    char *p2 = buf2;
    char *p3 = buf1 + BUFSIZE;
    char *p4;

    while (1) {
        int len;
        if (pnptr && pnptr < p3) p3 = pnptr;
        if (puuid && puuid < p3) p3 = puuid;
        if (pcast && pcast < p3) p3 = pcast;
        if (plptr && plptr < p3) p3 = plptr;

        len = p3 - p1;
        if (len>0)
            memcpy(p2, p1, len);
        else strcpy(p2, p1);    // copy rest of line
        p1 += len;
        p2 += len;
        if (p3 == pnptr) {    // nullptr
            p1 += 7;    // source buffer pos
            strcpy(p2, "NULL");
            p2 += 4;    // target buffer pos
            pnptr = strstr(p3 + 7, "nullptr");
            p3 = buf1 + BUFSIZE;    // force check
            continue;
        }
        if (p3 == puuid) {    // __uuidof(
            p1 += 9;    // source buffer pos
            while (*p1 == ' ') p1++;    // delims off
            strcpy(p2, "&IID_");
            p2 += 5;    // target buffer pos
            p4 = p1;
            while (*p4 && *p4 != ')') p4++;
            len = p4 - p1;
            strncpy(p2, p1, len);
            p1 += len + 1;    // past ')'
            p2 += len;
            puuid = strstr(p3 + 9, "__uuidof(");
            p3 = buf1 + BUFSIZE;    // force check
            continue;
        }
        if (p3 == pcast) {    // _cast<
            char *p5 = p1;
            while (*p5 && *p5-- > ' ') p2--;
            p2++;
            p1 += 5;    // source buffer pos
            *p1 = '(';
            p4 = p1;
            while (*p4 && *p4 != '>') p4++;
            *p4 = ')';
            len = p4 - p1;
            strncpy(p2, p1, len);
            p1 += len;    // source buffer pos
            p2 += len;    // target buffer pos
            pcast = strstr(p3 + 6, "_cast<");
            p3 = buf1 + BUFSIZE;    // force check
            continue;
        }
        if (p3 == plptr) {    // ->
            p4 = p1 - 1;
            while (*p4 && *p4 > ' ') p4--;    // find start of pointer name
            p4++;
            int len1 = p1 - p4;    // pointer name len
            char *p5 = p1 + 2;
            while (*p5 && *p5 != '(' && *p5 != ' ') p5++;
            if (*p5 == '(') {    // just pointer
                p5++;
                len = p5 - p1;
                strcpy(p2, "->lpVtbl");
                p2 += 8;
                strncpy(p2, p1, len);
                p1 += len;    // source buffer pos
                p2 += len;    // target buffer pos
                strncpy(p2, p4, len1);
                p2 += len1;
                if (*p5 != ')') {
                    *p2 = ',';
                    p2++;
                }
            }
            plptr = strstr(p3 + 2, "->");
            p3 = buf1 + BUFSIZE;    // force check
            continue;
        }
        break;
    }
    return buf2;
}
May the source be with you