1 | // |
2 | // Copyright (c) Microsoft. All rights reserved. |
3 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
4 | // |
5 | |
6 | #include "standardpch.h" |
7 | #include "verbfracture.h" |
8 | #include "simpletimer.h" |
9 | #include "methodcontext.h" |
10 | #include "methodcontextiterator.h" |
11 | #include "errorhandling.h" |
12 | #include "logging.h" |
13 | |
14 | int verbFracture::DoWork( |
15 | const char* nameOfInput, const char* nameOfOutput, int indexCount, const int* indexes, bool stripCR) |
16 | { |
17 | int rangeSize = indexes[0]; |
18 | |
19 | LogVerbose("Reading from '%s' copying %d MethodContexts files into each output file of '%s'" , nameOfInput, |
20 | rangeSize, nameOfOutput); |
21 | |
22 | MethodContextIterator mci(true); |
23 | if (!mci.Initialize(nameOfInput)) |
24 | return -1; |
25 | |
26 | int fileCount = 0; |
27 | char fileName[512]; |
28 | |
29 | HANDLE hFileOut = INVALID_HANDLE_VALUE; |
30 | while (mci.MoveNext()) |
31 | { |
32 | MethodContext* mc = mci.Current(); |
33 | |
34 | if ((hFileOut == INVALID_HANDLE_VALUE) || (((mci.MethodContextNumber() - 1) % rangeSize) == 0)) |
35 | { |
36 | if (hFileOut != INVALID_HANDLE_VALUE) |
37 | { |
38 | if (!CloseHandle(hFileOut)) |
39 | { |
40 | LogError("1st CloseHandle failed. GetLastError()=%u" , GetLastError()); |
41 | return -1; |
42 | } |
43 | hFileOut = INVALID_HANDLE_VALUE; |
44 | } |
45 | sprintf_s(fileName, 512, "%s-%0*d.mch" , nameOfOutput, 5, fileCount++); |
46 | hFileOut = CreateFileA(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, |
47 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); |
48 | if (hFileOut == INVALID_HANDLE_VALUE) |
49 | { |
50 | LogError("Failed to open output file '%s'. GetLastError()=%u" , fileName, GetLastError()); |
51 | return -1; |
52 | } |
53 | } |
54 | if (stripCR) |
55 | { |
56 | delete mc->cr; |
57 | mc->cr = new CompileResult(); |
58 | } |
59 | mc->saveToFile(hFileOut); |
60 | } |
61 | |
62 | if (hFileOut != INVALID_HANDLE_VALUE) |
63 | { |
64 | if (!CloseHandle(hFileOut)) |
65 | { |
66 | LogError("2nd CloseHandle failed. GetLastError()=%u" , GetLastError()); |
67 | return -1; |
68 | } |
69 | } |
70 | |
71 | LogInfo("Output fileCount %d" , fileCount); |
72 | |
73 | if (!mci.Destroy()) |
74 | return -1; |
75 | |
76 | return 0; |
77 | } |
78 | |