+
+// replace \r \n to \0
+void strcleanrn(char *buf, size_t len) {
+ strcreplace(buf, len, '\n', '\0');
+ strcreplace(buf, len, '\r', '\0');
+}
+
+// replace char in buffer
+void strcreplace(char *buf, size_t len, char from, char to) {
+ for (size_t i = 0; i < len; i++) {
+ if (buf[i] == from)
+ buf[i] = to;
+ }
+}
+
+char *strmcopy(char *buf) {
+ char * str = NULL;
+ if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) {
+ memset(str, 0, strlen(buf) + 1);
+ strcpy(str, buf);
+ }
+ return str;
+}
+
+
+// determine number of logical CPU cores (use for multithreaded functions)
+extern int num_CPUs(void)
+{
+#if defined(_WIN32)
+ #include <sysinfoapi.h>
+ SYSTEM_INFO sysinfo;
+ GetSystemInfo(&sysinfo);
+ return sysinfo.dwNumberOfProcessors;
+#elif defined(__linux__) || defined(__APPLE__)
+ #include <unistd.h>
+ return sysconf(_SC_NPROCESSORS_ONLN);
+#else
+ return 1;
+#endif
+}
+