aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cc3
-rw-r--r--src/util.cc100
2 files changed, 101 insertions, 2 deletions
diff --git a/src/main.cc b/src/main.cc
index 95ac9b0..22395fb 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -106,8 +106,7 @@ int main(int argc, char **argv)
usage();
break;
case 'v': /* emit additional diagnostic info */
- ov4::DEBUG = true;
- verbose = 1;
+ verbose = true;
break;
case 'p': /* don't print a prompt */
emit_prompt = 0; /* handy for automatic testing */
diff --git a/src/util.cc b/src/util.cc
new file mode 100644
index 0000000..bd516e8
--- /dev/null
+++ b/src/util.cc
@@ -0,0 +1,100 @@
+#include "util.h"
+
+namespace ov4
+{
+
+// constexpr int DECIMAL = 10;
+// constexpr int LENGTH = 25;
+
+void atomic_print(char *s, bool ignore_wrap)
+{
+ 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)
+{
+ if (verbose && GLOBAL_DEBUG) atomic_print(s, ignore_wrap);
+}
+
+void atomic_debug(int x)
+{
+ if (verbose && 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;
+}
+
+}