7469c4a1c99317b8b8f9d97c7f7af183d89ae098
[swift-upb.git] / compat / util.cpp
1 /*
2  * Written by Arno Bakker
3  * see LICENSE.txt for license information
4  */
5
6 #include "util.h"
7 #include <vector>
8
9 #ifdef _WIN32
10 #include <windows.h>
11 #include <Tchar.h>
12 #endif
13
14 namespace swift
15 {
16
17 std::string gettmpdir(void)
18 {
19 #ifdef _WIN32
20   DWORD result = ::GetTempPath(0, _T(""));
21   if (result == 0)
22         throw std::runtime_error("Could not get system temp path");
23
24   std::vector<TCHAR> tempPath(result + 1);
25   result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
26   if((result == 0) || (result >= tempPath.size()))
27         throw std::runtime_error("Could not get system temp path");
28
29   return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
30 #else
31           return std::string("/tmp/");
32 #endif
33 }
34
35
36     
37     
38 }; // namespace
39