summaryrefslogtreecommitdiffstats
path: root/Tools/MIDLWrapper/MIDLWrapper.cpp
blob: 8cb077acb4e3ff895c7d486bd376839d43e398bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// MIDLWrapper.cpp : Just calls the built-in midl.exe with the given arguments.

#define WIN32_LEAN_AND_MEAN    // Exclude rarely-used stuff from Windows headers
#include <process.h>
#include <stdio.h>
#include <string>
#include <windows.h>

using namespace std;

int wmain(int argc, wchar_t* argv[], wchar_t* envp[])
{
#ifndef NDEBUG
    fwprintf(stderr, L"######### im in ur IDE, compiling ur c0des ########\n");
#endif

    int pathIndex = -1;
    for (int i = 0; envp[i]; ++i)
        if (!wcsncmp(envp[i], L"PATH=", 5)) {
            pathIndex = i;
            break;
        }

    if (pathIndex == -1) {
        fwprintf(stderr, L"Couldn't find PATH environment variable!\n");
        return -1;
    }

    wchar_t* vcbin = wcsstr(envp[pathIndex], L"Tools\\vcbin");
    if (!vcbin) {
        fwprintf(stderr, L"Couldn't find Tools\\vcbin in PATH!\n");
        return -1;
    }

    wchar_t saved = *vcbin;
    *vcbin = 0;
    
    wchar_t* afterLeadingSemiColon = wcsrchr(envp[pathIndex], ';');
    if (!afterLeadingSemiColon)
        afterLeadingSemiColon = envp[pathIndex] + 5; // +5 for the length of "PATH="
    else
        afterLeadingSemiColon++;

    *vcbin = saved;

    size_t pathLength = wcslen(envp[pathIndex]);

    wchar_t* trailingSemiColon = wcschr(vcbin, ';');
    if (!trailingSemiColon)
        trailingSemiColon = envp[pathIndex] + pathLength;

    int vcbinLength = trailingSemiColon - afterLeadingSemiColon;

    size_t newPathLength = pathLength - vcbinLength;

    wchar_t* newPath = new wchar_t[newPathLength + 1];

    // Copy everything before the vcbin path...
    wchar_t* d = newPath;
    wchar_t* s = envp[pathIndex];
    while (s < afterLeadingSemiColon)
        *d++ = *s++;

    // Copy everything after the vcbin path...
    s = trailingSemiColon;
    while (*d++ = *s++);

    envp[pathIndex] = newPath;

#ifndef NDEBUG
    fwprintf(stderr, L"New path: %s\n", envp[pathIndex]);
#endif

    wchar_t** newArgv = new wchar_t*[argc + 1];
    for (int i = 0; i < argc; ++i) {
        size_t length = wcslen(argv[i]);
        newArgv[i] = new wchar_t[length + 3];
        *newArgv[i] = '\"';
        wcscpy_s(newArgv[i] + 1, length + 2, argv[i]);
        *(newArgv[i] + 1 + length) = '\"';
        *(newArgv[i] + 2 + length) = 0;
    }
    newArgv[argc] = 0;

    return _wspawnvpe(_P_WAIT, L"midl", newArgv, envp);
}