-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_to_numer_for_kill_process.cpp
More file actions
82 lines (78 loc) · 1.26 KB
/
Copy pathstring_to_numer_for_kill_process.cpp
File metadata and controls
82 lines (78 loc) · 1.26 KB
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
#include "string_tools.h"
/* Why there are 2.
Because for some awful reason
*/
bool _cdecl StringToNumber_ForKillProcess(const char* input, int* output)
{
bool IsPositive = false;
int space_skipper = 0;
int mul = 1;
int len = 0;
if (input == nullptr)
return false;
if (output == nullptr)
return false;
// eat any spaces at the start
for (;; space_skipper++)
if ((!(IsSpace(input[space_skipper]))))
{
break;
}
if (input[space_skipper] == '-')
{
IsPositive = false;
space_skipper++;
}
else
{
// why not if (x), then if (x) - what if the person passed "+-5" or "-+5"
if (input[space_skipper] == '+')
{
IsPositive = true;
space_skipper++;
}
else
{
IsPositive = true;
}
}
for (int i = space_skipper; ; i++)
{
if (input[i] == 0)
{
// to the future: we sub 1 from i so the code below DOESN'T start at null
len = i - 1;
break;
}
else
{
if (!IsDigit(input[i]))
{
return false;
}
}
}
if (len == 0)
return false;
else
{
*output = 0;
for (int i = len; i >= space_skipper; i--)
{
if ((input[i] < '0') || (input[i] > '9'))
{
return false;
}
else
{
*output += (input[i] - '0') * mul;
mul *= 10;
}
}
if (!IsPositive)
{
*output *= -1;
}
return true;
}
}