• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // system_utils: Defines common utility functions
8 
9 #include "util/test_utils.h"
10 
11 #include <cstring>
12 #include <fstream>
13 
14 namespace angle
15 {
CreateTemporaryFile(char * tempFileNameOut,uint32_t maxFileNameLen)16 bool CreateTemporaryFile(char *tempFileNameOut, uint32_t maxFileNameLen)
17 {
18     constexpr uint32_t kMaxPath = 1000u;
19     char tempPath[kMaxPath];
20 
21     if (!GetTempDir(tempPath, kMaxPath))
22         return false;
23 
24     return CreateTemporaryFileInDir(tempPath, tempFileNameOut, maxFileNameLen);
25 }
26 
GetFileSize(const char * filePath,uint32_t * sizeOut)27 bool GetFileSize(const char *filePath, uint32_t *sizeOut)
28 {
29     std::ifstream stream(filePath);
30     if (!stream)
31     {
32         return false;
33     }
34 
35     stream.seekg(0, std::ios::end);
36     *sizeOut = static_cast<uint32_t>(stream.tellg());
37     return true;
38 }
39 
ReadEntireFileToString(const char * filePath,char * contentsOut,uint32_t maxLen)40 bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen)
41 {
42     std::ifstream stream(filePath);
43     if (!stream)
44     {
45         return false;
46     }
47 
48     std::string contents;
49 
50     stream.seekg(0, std::ios::end);
51     contents.reserve(static_cast<unsigned int>(stream.tellg()));
52     stream.seekg(0, std::ios::beg);
53 
54     contents.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
55 
56     strncpy(contentsOut, contents.c_str(), maxLen);
57     return true;
58 }
59 
60 // static
61 Process::~Process() = default;
62 
ProcessHandle()63 ProcessHandle::ProcessHandle() : mProcess(nullptr) {}
64 
ProcessHandle(Process * process)65 ProcessHandle::ProcessHandle(Process *process) : mProcess(process) {}
66 
ProcessHandle(const std::vector<const char * > & args,ProcessOutputCapture captureOutput)67 ProcessHandle::ProcessHandle(const std::vector<const char *> &args,
68                              ProcessOutputCapture captureOutput)
69     : mProcess(LaunchProcess(args, captureOutput))
70 {}
71 
~ProcessHandle()72 ProcessHandle::~ProcessHandle()
73 {
74     reset();
75 }
76 
ProcessHandle(ProcessHandle && other)77 ProcessHandle::ProcessHandle(ProcessHandle &&other) : mProcess(other.mProcess)
78 {
79     other.mProcess = nullptr;
80 }
81 
operator =(ProcessHandle && rhs)82 ProcessHandle &ProcessHandle::operator=(ProcessHandle &&rhs)
83 {
84     std::swap(mProcess, rhs.mProcess);
85     return *this;
86 }
87 
reset()88 void ProcessHandle::reset()
89 {
90     if (mProcess)
91     {
92         delete mProcess;
93         mProcess = nullptr;
94     }
95 }
96 }  // namespace angle
97