webengine/osswebengine/WebKitTools/MIDLWrapper/MIDLWrapper.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 // MIDLWrapper.cpp : Just calls the built-in midl.exe with the given arguments.
       
    19 
       
    20 #define WIN32_LEAN_AND_MEAN    // Exclude rarely-used stuff from Windows headers
       
    21 #include <process.h>
       
    22 #include <stdio.h>
       
    23 #include <string>
       
    24 #include <windows.h>
       
    25 
       
    26 using namespace std;
       
    27 
       
    28 int wmain(int argc, wchar_t* argv[], wchar_t* envp[])
       
    29 {
       
    30 #ifndef NDEBUG
       
    31     fwprintf(stderr, L"######### im in ur IDE, compiling ur c0des ########\n");
       
    32 #endif
       
    33 
       
    34     int pathIndex = -1;
       
    35     for (int i = 0; envp[i]; ++i)
       
    36         if (!wcsncmp(envp[i], L"PATH=", 5)) {
       
    37             pathIndex = i;
       
    38             break;
       
    39         }
       
    40 
       
    41     if (pathIndex == -1) {
       
    42         fwprintf(stderr, L"Couldn't find PATH environment variable!\n");
       
    43         return -1;
       
    44     }
       
    45 
       
    46     wchar_t* vcbin = wcsstr(envp[pathIndex], L"WebKitTools\\vcbin");
       
    47     if (!vcbin) {
       
    48         fwprintf(stderr, L"Couldn't find WebKitTools\\vcbin in PATH!\n");
       
    49         return -1;
       
    50     }
       
    51 
       
    52     wchar_t saved = *vcbin;
       
    53     *vcbin = 0;
       
    54     
       
    55     wchar_t* afterLeadingSemiColon = wcsrchr(envp[pathIndex], ';');
       
    56     if (!afterLeadingSemiColon)
       
    57         afterLeadingSemiColon = envp[pathIndex] + 5; // +5 for the length of "PATH="
       
    58     else
       
    59         afterLeadingSemiColon++;
       
    60 
       
    61     *vcbin = saved;
       
    62 
       
    63     size_t pathLength = wcslen(envp[pathIndex]);
       
    64 
       
    65     wchar_t* trailingSemiColon = wcschr(vcbin, ';');
       
    66     if (!trailingSemiColon)
       
    67         trailingSemiColon = envp[pathIndex] + pathLength;
       
    68 
       
    69     int vcbinLength = trailingSemiColon - afterLeadingSemiColon;
       
    70 
       
    71     size_t newPathLength = pathLength - vcbinLength;
       
    72 
       
    73     wchar_t* newPath = new wchar_t[newPathLength + 1];
       
    74 
       
    75     // Copy everything before the vcbin path...
       
    76     wchar_t* d = newPath;
       
    77     wchar_t* s = envp[pathIndex];
       
    78     while (s < afterLeadingSemiColon)
       
    79         *d++ = *s++;
       
    80 
       
    81     // Copy everything after the vcbin path...
       
    82     s = trailingSemiColon;
       
    83     while (*d++ = *s++);
       
    84 
       
    85     envp[pathIndex] = newPath;
       
    86 
       
    87 #ifndef NDEBUG
       
    88     fwprintf(stderr, L"New path: %s\n", envp[pathIndex]);
       
    89 #endif
       
    90 
       
    91     wchar_t** newArgv = new wchar_t*[argc + 1];
       
    92     for (int i = 0; i < argc; ++i) {
       
    93         size_t length = wcslen(argv[i]);
       
    94         newArgv[i] = new wchar_t[length + 3];
       
    95         *newArgv[i] = '\"';
       
    96         wcscpy_s(newArgv[i] + 1, length + 2, argv[i]);
       
    97         *(newArgv[i] + 1 + length) = '\"';
       
    98         *(newArgv[i] + 2 + length) = 0;
       
    99     }
       
   100     newArgv[argc] = 0;
       
   101 
       
   102     return _wspawnvpe(_P_WAIT, L"midl", newArgv, envp);
       
   103 }