summaryrefslogtreecommitdiff
path: root/src/ToolBox/superpmi/mcs/verbfracture.cpp
blob: bcc6d04e1d76d72bf1c9c15f7fb562e119dd1af9 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#include "standardpch.h"
#include "verbfracture.h"
#include "simpletimer.h"
#include "methodcontext.h"
#include "methodcontextiterator.h"
#include "errorhandling.h"
#include "logging.h"

int verbFracture::DoWork(const char *nameOfInput, const char *nameOfOutput, int indexCount, const int *indexes, bool stripCR)
{
    int rangeSize = indexes[0];

    LogVerbose("Reading from '%s' copying %d MethodContexts files into each output file of '%s'", nameOfInput, rangeSize, nameOfOutput);

    MethodContextIterator mci(true);
    if (!mci.Initialize(nameOfInput))
        return -1;

    int fileCount = 0;
    char fileName[512];

    HANDLE hFileOut = INVALID_HANDLE_VALUE;
    while (mci.MoveNext())
    {
        MethodContext* mc = mci.Current();

        if ((hFileOut == INVALID_HANDLE_VALUE) || (((mci.MethodContextNumber() - 1) % rangeSize) == 0))
        {
            if (hFileOut != INVALID_HANDLE_VALUE)
            {
                if (!CloseHandle(hFileOut))
                {
                    LogError("1st CloseHandle failed. GetLastError()=%u", GetLastError());
                    return -1;
                }
                hFileOut = INVALID_HANDLE_VALUE;
            }
            sprintf_s(fileName, 512, "%s-%0*d.mch", nameOfOutput, 5, fileCount++);
            hFileOut = CreateFileA(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL);
            if (hFileOut == INVALID_HANDLE_VALUE)
            {
                LogError("Failed to open output file '%s'. GetLastError()=%u", fileName, GetLastError());
                return -1;
            }
        }
        if (stripCR)
        {
            delete mc->cr;
            mc->cr = new CompileResult();
        }
        mc->saveToFile(hFileOut);
    }

    if (hFileOut != INVALID_HANDLE_VALUE)
    {
        if (!CloseHandle(hFileOut))
        {
            LogError("2nd CloseHandle failed. GetLastError()=%u", GetLastError());
            return -1;
        }
    }

    LogInfo("Output fileCount %d", fileCount);

    if (!mci.Destroy())
        return -1;

    return 0;
}