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
|
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <string>
#define LOG !(ov4::GLOBAL_DEBUG && ov4::DEBUG) ? (void)0 : ov4::LogVoidify() & std::clog
namespace ov4 {
void atomic_print(char*, bool);
void atomic_print(int x);
void atomic_debug(char*, bool);
void atomic_debug(int);
bool isnum(char*);
void reverse(char[], int);
char *itoa(int, char*, int);
constexpr bool GLOBAL_DEBUG = true;
bool DEBUG = false; // verbose mode, can be enabled on demand using something like -v
struct LogVoidify { void operator&(std::ostream&) const {} };
const int DECIMAL = 10;
const int LENGTH = 25;
void atomic_print(char *s, bool ignore_wrap = false)
{
if (ignore_wrap)
for (int i = 0; s[i] != 0; i++)
if (s[i] == '\n')
{
write(STDOUT_FILENO, s, i);
return;
}
write(STDOUT_FILENO, s, strlen(s));
}
void atomic_print(int x)
{
char msg[LENGTH];
itoa(x, msg, DECIMAL);
atomic_print(msg);
}
void reverse(char str[], int length)
{
int start = 0;
int end = length - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
end--;
start++;
}
}
void atomic_debug(char *s, bool ignore_wrap = false)
{
if (DEBUG && GLOBAL_DEBUG) atomic_print(s, ignore_wrap);
}
void atomic_debug(int x)
{
if (DEBUG && GLOBAL_DEBUG) atomic_print(x);
}
bool isnum(char *s)
{
for (int i = 0; s[i] != 0; i++)
if (!isdigit(s[i]))
return false;
return true;
}
char *itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitly, otherwise empty string is
* printed for 0 */
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled
// only with base 10. Otherwise numbers are
// considered unsigned.
if (num < 0 && base == 10) {
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0) {
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
num = num / base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}
}
|