about summary refs log tree commit diff stats
path: root/src/common.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-08-23 01:08:06 +0100
committerJames Booth <boothj5@gmail.com>2012-08-23 01:08:06 +0100
commit0fe70ce7d381ceb2b38cbb5ac9a99cbad95dec91 (patch)
treeb7fcc14c626baaef350a59855ecb039b98252400 /src/common.c
parent8b7975bdf3a01c57bcea554a5dbc32b5274ce636 (diff)
downloadprofani-tty-0fe70ce7d381ceb2b38cbb5ac9a99cbad95dec91.tar.gz
Merge common and util
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
index a7e6d8e0..064e032c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -25,6 +25,9 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <ctype.h>
 
 #include <glib.h>
 
@@ -57,3 +60,72 @@ create_dir(char *name)
         if (errno == ENOENT)
             e = mkdir(name, S_IRWXU);
 }
+
+void
+get_time(char *thetime)
+{
+    time_t rawtime;
+    struct tm *timeinfo;
+
+    time(&rawtime);
+    timeinfo = localtime(&rawtime);
+
+    strftime(thetime, 80, "%H:%M", timeinfo);
+}
+
+char *
+str_replace(const char *string, const char *substr, 
+    const char *replacement) 
+{
+    char *tok = NULL;
+    char *newstr = NULL;
+    char *oldstr = NULL;
+    char *head = NULL;
+ 
+    if (string == NULL)
+        return NULL;
+
+    if ( substr == NULL || 
+         replacement == NULL || 
+         (strcmp(substr, "") == 0)) 
+        return strdup (string);
+
+    newstr = strdup (string);
+    head = newstr;
+
+    while ( (tok = strstr ( head, substr ))) {
+        oldstr = newstr;
+        newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + 
+            strlen ( replacement ) + 1 );
+        
+        if ( newstr == NULL ) { 
+            free (oldstr);
+            return NULL;
+        }
+        
+        memcpy ( newstr, oldstr, tok - oldstr );
+        memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
+        memcpy ( newstr + (tok - oldstr) + strlen( replacement ), 
+            tok + strlen ( substr ), 
+            strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
+        memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + 
+            strlen ( replacement ) , 0, 1 );
+        
+        head = newstr + (tok - oldstr) + strlen( replacement );
+        free (oldstr);
+    }
+  
+    return newstr;
+}
+
+int
+str_contains(char str[], int size, char ch)
+{
+    int i;
+    for (i = 0; i < size; i++) {
+        if (str[i] == ch)
+            return 1;
+    }
+
+    return 0;
+}