forked from pgadmin-org/pgagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cpp
232 lines (209 loc) · 4.44 KB
/
misc.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//////////////////////////////////////////////////////////////////////////
//
// pgAgent - PostgreSQL Tools
//
// Copyright (C) 2002 - 2018 The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// misc.cpp - misc functions
//
//////////////////////////////////////////////////////////////////////////
#include "pgAgent.h"
#include "connection.h"
#if !BOOST_OS_WINDOWS
#include <unistd.h>
#include <stdlib.h>
#endif
#define APPVERSION_STR PGAGENT_VERSION
// In unix.c or win32.c
void usage(const std::wstring &executable);
std::wstring getArg(int &argc, char **&argv)
{
std::wstring s;
if (argv[0][2])
s = CharToWString(argv[0] + 2);
else
{
if (argc >= 1)
{
argc--;
argv++;
s = CharToWString(argv[0]);
}
else
{
// very bad!
LogMessage(L"Invalid command line argument", LOG_ERROR);
}
}
return s;
}
void printVersion()
{
printf("PostgreSQL Scheduling Agent\n");
printf("Version: %s\n", APPVERSION_STR);
}
void setOptions(int argc, char **argv, const std::wstring &executable)
{
while (argc-- > 0)
{
if (argv[0][0] == '-')
{
switch (argv[0][1])
{
case 't':
{
int val = atoi((const char*)getArg(argc, argv).c_str());
if (val > 0)
shortWait = val;
break;
}
case 'r':
{
int val = atoi((const char*)getArg(argc, argv).c_str());
if (val >= 10)
longWait = val;
break;
}
case 'l':
{
int val = atoi((const char*)getArg(argc, argv).c_str());
if (val >= 0 && val <= 2)
minLogLevel = val;
break;
}
case 'v':
{
printVersion();
exit(0);
}
#if !BOOST_OS_WINDOWS
case 'f':
{
runInForeground = true;
break;
}
case 's':
{
logFile = getArg(argc, argv);
break;
}
#endif
default:
{
usage(executable);
exit(1);
}
}
}
else
{
if (connectString != L"")
connectString += L" ";
connectString = connectString + CharToWString(*argv);
if (**argv == '"')
connectString = connectString.substr(1, connectString.length() - 2);
}
argv++;
}
}
void WaitAWhile(const bool waitLong)
{
int count;
if (waitLong)
count = longWait;
else
count = shortWait;
while (count--)
{
#ifdef WIN32
CheckForInterrupt();
Sleep(1000);
#else
sleep(1);
#endif
}
}
std::wstring NumToStr(const long l)
{
std::wstring buf(boost::lexical_cast<std::wstring>(l));
return buf;
}
// This function is used to convert char* to std::wstring.
std::wstring CharToWString(const char* cstr)
{
std::string s = std::string(cstr);
std::wstring wsTmp(s.begin(), s.end());
return wsTmp;
}
// This function is used to convert std::wstring to char *.
char * WStringToChar(const std::wstring &wstr)
{
const wchar_t *wchar_str = wstr.c_str();
int wstr_length = wcslen(wchar_str);
char *dst = new char[wstr_length + 10];
memset(dst, 0x00, (wstr_length + 10));
wcstombs(dst, wchar_str, wstr_length);
return dst;
}
// Below function will generate random string of given character.
std::string generateRandomString(size_t length)
{
char *str = new char[length];
size_t i = 0;
int r;
str[length - 1] = '\0';
srand(time(NULL));
for(i = 0; i < length - 1; ++i)
{
for(;;)
{
// interval between 'A' and 'z'
r = rand() % 57 + 65;
if((r >= 65 && r <= 90) || (r >= 97 && r <= 122))
{
str[i] = (char)r;
break;
}
}
}
std::string result(str);
if (str != NULL)
{
delete []str;
str = NULL;
}
return result;
}
std::wstring getTemporaryDirectoryPath()
{
#if BOOST_OS_WINDOWS
std::wstring tmp_dir = L"";
wchar_t wcharPath[MAX_PATH];
if (GetTempPathW(MAX_PATH, wcharPath))
tmp_dir = wcharPath;
#else
// Read this environment variable (TMPDIR, TMP, TEMP, TEMPDIR) and if not found then use "/tmp"
std::wstring tmp_dir = L"/tmp";
const char *s = getenv("TMPDIR");
if (s != NULL)
tmp_dir = CharToWString(s);
else
{
const char *s1 = getenv("TMP");
if (s1 != NULL)
tmp_dir = CharToWString(s1);
else {
const char *s2 = getenv("TEMP");
if (s2 != NULL)
tmp_dir = CharToWString(s2);
else {
const char *s3 = getenv("TEMPDIR");
if (s3 != NULL)
tmp_dir = CharToWString(s2);
}
}
}
#endif
return tmp_dir;
}