Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <windows.h> | |
| 6 | |
| 7 #include <shlwapi.h> | |
| 8 #include <stdio.h> | |
| 9 #include <tchar.h> | |
| 10 | |
| 11 #include <algorithm> | |
| 12 #include <iostream> | |
| 13 #include <sstream> | |
| 14 #include <string> | |
| 15 | |
| 16 #pragma warning(disable : 4996) | |
| 17 | |
| 18 // Create |count| number of temp files at |kTempDirForGuid| usingGUID based | |
| 19 // method. The time cost in millisecond of creating every 500 temp files or all | |
| 20 // the files if |count| < 500 is printed to the console. | |
| 21 bool CreateFilesUsingGuid(UINT count); | |
| 22 | |
| 23 // Create |count| number of temp files at |kTempDirForGetTempFileName| using | |
| 24 // GetTempFileName() API. The time cost in millisecond of creating every 500 | |
| 25 // temp files or all the files if |count| < 500 is printed to the console. | |
| 26 bool CreateFilesUsingGetTempFileName(UINT count); | |
| 27 | |
| 28 // This method converts GUID to a string. | |
| 29 char* ConvertGuidToString(const GUID* id, char* out); | |
| 30 | |
| 31 // Check if the given directory exists and is empty. If it doesn't exist, try to | |
| 32 // create it. | |
| 33 bool IsDirectoryReadyExistedAndEmpty(char* dir_name); | |
| 34 | |
| 35 // A temporary directory where the new temp files created by GetTempFileName() | |
| 36 // are written. | |
| 37 CHAR kTempDirForGetTempFileName[] = "C:\\src\\TempDirGetTempFileName\\"; | |
| 38 | |
| 39 // A temporary directory where the new temp files created by Guid-based method | |
| 40 // are written. | |
| 41 CHAR kTempDirForGuid[] = "C:\\src\\TempDirGuid\\"; | |
|
stanisc
2017/04/14 18:45:59
If you want to make the tool reusable it shouldn't
chengx
2017/04/14 22:27:22
Thanks for the reminder. I have switched to the TE
| |
| 42 | |
| 43 // Maximum number of temp files allowed to create. This is limited by the | |
| 44 // implementation of GetTempFileName(). | |
| 45 // "This limits GetTempFileName to a maximum of 65,535 unique file names if the | |
| 46 // lpPathName and lpPrefixString parameters remain the same." | |
| 47 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).as px | |
| 48 UINT kMaxFileCreate = 65535; | |
| 49 | |
| 50 int main() { | |
| 51 UINT file_create_count; | |
| 52 std::string user_input; | |
| 53 | |
| 54 while (true) { | |
| 55 std::cout << "\nPlease enter # of files to create (maximum " | |
| 56 << kMaxFileCreate << "), or \"quit\" to end the program : "; | |
| 57 std::getline(std::cin, user_input); | |
| 58 | |
| 59 std::transform(user_input.begin(), user_input.end(), user_input.begin(), | |
| 60 ::tolower); | |
| 61 if (user_input == "quit") | |
| 62 break; | |
| 63 | |
| 64 std::cout << std::endl; | |
| 65 std::stringstream ss(user_input); | |
| 66 | |
| 67 if (ss >> file_create_count && file_create_count <= kMaxFileCreate) { | |
| 68 std::cout << "\nPlease select method to create temp file names,\n" | |
| 69 << "\"t\" for GetTempFileName \n" | |
| 70 << "\"g\" for GUID-based \n" | |
| 71 << "\"b\" for both \n" | |
| 72 << "or \"quit\" to end the program : "; | |
| 73 std::getline(std::cin, user_input); | |
| 74 | |
| 75 std::transform(user_input.begin(), user_input.end(), user_input.begin(), | |
| 76 ::tolower); | |
| 77 if (user_input == "quit") | |
| 78 break; | |
| 79 | |
| 80 if (user_input == "t" || user_input == "b") { | |
| 81 std::cout << "\nGetTempFileName :" << std::endl; | |
| 82 if (CreateFilesUsingGetTempFileName(file_create_count)) { | |
| 83 std::cout << "File creation succeeds at " | |
| 84 << kTempDirForGetTempFileName | |
| 85 << ", remember to clean all of them!" << std::endl; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 if (user_input == "g" || user_input == "b") { | |
| 90 std::cout << "\nGUID-based :" << std::endl; | |
| 91 if (CreateFilesUsingGuid(file_create_count)) { | |
| 92 std::cout << "File creation succeeds at " << kTempDirForGuid | |
| 93 << ", remember to clean all of them!" << std::endl; | |
| 94 } | |
| 95 } | |
| 96 } else { | |
| 97 std::cout << "Input number is invalid, please enter # of files to create " | |
| 98 "(maximum " | |
| 99 << kMaxFileCreate << "), or \"quit\" to end the program : "; | |
| 100 } | |
| 101 std::cout << std::endl; | |
| 102 } | |
| 103 return 0; | |
| 104 } | |
| 105 | |
| 106 bool CreateFilesUsingGuid(UINT count) { | |
| 107 if (!IsDirectoryReadyExistedAndEmpty(kTempDirForGuid)) | |
| 108 return false; | |
| 109 | |
| 110 LARGE_INTEGER starting_time, ending_time, elapsed_ms; | |
| 111 ::QueryPerformanceCounter(&starting_time); | |
| 112 LARGE_INTEGER frequency; | |
| 113 ::QueryPerformanceFrequency(&frequency); | |
| 114 | |
| 115 for (UINT i = 1; i <= count; ++i) { | |
| 116 GUID guid; | |
| 117 HRESULT hCreateGuid = ::CoCreateGuid(&guid); | |
| 118 char buffer[37]; | |
| 119 ConvertGuidToString(&guid, buffer); | |
| 120 std::string temp_name = | |
| 121 std::string(kTempDirForGuid).append(buffer).append(".tmp"); | |
| 122 | |
| 123 HANDLE file_handle = | |
| 124 ::CreateFileA(temp_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, | |
| 125 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
| 126 ::CloseHandle(file_handle); | |
| 127 | |
| 128 if (i % 500 == 0 || i == count) { | |
| 129 ::QueryPerformanceCounter(&ending_time); | |
| 130 // Convert the elapsed number of ticks to milliseconds. | |
| 131 elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) * | |
| 132 1000 / frequency.QuadPart; | |
| 133 | |
| 134 starting_time = ending_time; | |
| 135 std::cout << i << " / " << count << " --- " << elapsed_ms.QuadPart | |
| 136 << " ms" << std::endl; | |
| 137 } | |
| 138 } | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 bool CreateFilesUsingGetTempFileName(UINT count) { | |
| 143 if (!IsDirectoryReadyExistedAndEmpty(kTempDirForGetTempFileName)) | |
| 144 return false; | |
| 145 | |
| 146 CHAR temp_name[MAX_PATH]; | |
| 147 LARGE_INTEGER starting_time, ending_time, elapsed_ms; | |
| 148 ::QueryPerformanceCounter(&starting_time); | |
| 149 LARGE_INTEGER frequency; | |
| 150 ::QueryPerformanceFrequency(&frequency); | |
| 151 | |
| 152 for (UINT i = 1; i <= count; ++i) { | |
| 153 ::GetTempFileNameA(kTempDirForGetTempFileName, "", 0, temp_name); | |
| 154 if (i % 500 == 0 || i == count) { | |
| 155 ::QueryPerformanceCounter(&ending_time); | |
| 156 // Convert the elapsed number of ticks to milliseconds. | |
| 157 elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) * | |
| 158 1000 / frequency.QuadPart; | |
| 159 | |
| 160 starting_time = ending_time; | |
| 161 std::cout << i << " / " << count << " --- " << elapsed_ms.QuadPart | |
| 162 << " ms" << std::endl; | |
| 163 } | |
| 164 } | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 char* ConvertGuidToString(const GUID* id, char* out) { | |
| 169 int i; | |
| 170 char* ret = out; | |
| 171 out += sprintf(out, "%.8lX-%.4hX-%.4hX-", id->Data1, id->Data2, id->Data3); | |
| 172 for (i = 0; i < sizeof(id->Data4); ++i) { | |
| 173 out += sprintf(out, "%.2hhX", id->Data4[i]); | |
|
stanisc
2017/04/14 18:46:00
Could use cast Data4 to to avoid printing byte by
stanisc
2017/04/14 18:47:58
What I was really trying to say is: Could this try
chengx
2017/04/14 22:27:22
Em, I am not sure but I will try. I think the curr
| |
| 174 if (i == 1) | |
| 175 *(out++) = '-'; | |
| 176 } | |
| 177 return ret; | |
| 178 } | |
| 179 | |
| 180 bool IsDirectoryReadyExistedAndEmpty(char* dir_name) { | |
| 181 if (::PathFileExistsA(dir_name)) { | |
| 182 if (!::PathIsDirectoryEmptyA(dir_name)) { | |
| 183 std::cout << dir_name | |
| 184 << " directory is not empty, please remove all its content."; | |
| 185 return false; | |
| 186 } | |
| 187 return true; | |
| 188 } else if (::CreateDirectoryA(dir_name, NULL) == 0) { | |
| 189 std::cout << dir_name << "directory creation fails."; | |
| 190 return false; | |
| 191 } else { | |
| 192 return true; | |
| 193 } | |
| 194 } | |
| OLD | NEW |