Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=xbdmenu.git;a=commitdiff;h=f13f3cfaa5de694a9e24e482606047de68b3434a

commit f13f3cfaa5de694a9e24e482606047de68b3434a
Author: James Buren <r...@frugalware.org>
Date:   Mon Jun 8 16:11:16 2009 -0500

util.c
* initial draft

diff --git a/util.c b/util.c
new file mode 100644
index 0000000..28b2500
--- /dev/null
+++ b/util.c
@@ -0,0 +1,109 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include "util.h"
+
+/* Formated print to stderr */
+int eprintf(const char *fmt,...) {
+       va_list val; int ret;
+
+       va_start(val,fmt);
+
+       ret = vfprintf(stderr,fmt,val);
+
+       va_end(val);
+
+       return ret;
+}
+
+/* Combine two strings into a malloc'ed buffer */
+char *strcmb(const char *str1,const char *str2) {
+       char *str;
+
+       /* Check for invalid arguments */
+       if( !str1 || !str2 ) {
+               return NULL;
+       }
+
+       str = malloc( (strlen(str1) + strlen(str2) + 1) * sizeof(char) );
+
+       /* Check if str was allocated successfully */
+       if( !str ) {
+               return NULL;
+       }
+
+       sprintf(str,"%s%s",str1,str2);
+
+       return str;
+}
+
+/* Strip trailing and leading whitespace from a C string */
+char *strstrip(char *str) {
+       int i, ol, lw, tw, nl;
+
+       /* Check for invalid arguments */
+       if( !str ) {
+               return NULL;
+       }
+
+       ol = strlen(str);
+
+       for( lw = 0 ; isspace( str[lw] ) ; ++lw ) {
+               ;
+       }
+
+       /* Check if there is only whitespace */
+       if( lw == ol ) {
+               str[0] = '\0';
+               return str;
+       }
+
+       for( tw = 0 ; isspace( str[ol-1-tw] ) ; ++tw ) {
+               ;
+       }
+
+       /* Check if there is no whitespace */
+       if( !(lw+tw) ) {
+               return str;
+       }
+
+       nl = ol-lw-tw;
+
+       /* Check if leading whitespace is present */
+       if( lw ) {
+               for( i = 0 ; i < nl ; ++i ) {
+                       str[i] = str[i+lw];
+               }
+       }
+
+       str[nl] = '\0';
+
+       return str;
+}
+
+/* Fill dest with n characters from end of src, converting to lowercase */
+char *strend(char *dest,const char *src,int n) {
+       int l, i;
+
+       /* Check for invalid arguments */
+       if( !dest || !src || n <= 0 ) {
+               return NULL;
+       }
+
+       l = strlen(src);
+
+       /* Check if src has enough characters to copy from */
+       if( l < n ) {
+               return NULL;
+       }
+
+       for( i = 0 ; i < n ; ++i ) {
+               dest[n-1-i] = tolower( src[l-1-i] );
+       }
+
+       dest[n] = '\0';
+
+       return dest;
+}
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to