misc.c

changeset 0
86b7f6f9c5c0
child 1
6e704fc09528
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc.c	Tue Feb 15 18:57:52 2005 +0100
@@ -0,0 +1,149 @@
+/*
+ * libtu/misc.c
+ *
+ * Copyright (c) Tuomo Valkonen 1999-2000.
+ * 
+ * This file is distributed under the terms of the "Artistic License".
+ * See the included file LICENSE for details.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <malloc.h>
+
+#include "include/misc.h"
+
+
+void *malloczero(int size)
+{
+	void *p=malloc(size);
+	
+	if(p!=NULL)
+		memset(p, 0, size);
+	
+	return p;
+}
+
+
+void *remalloczero(void *ptr, int oldsize, int newsize)
+{
+	void *p=NULL;
+	
+	if(newsize!=0){
+		p=malloc(newsize);
+	
+		if(p==NULL)
+			return NULL;
+	
+		memset(p, 0, newsize);
+	
+		if(ptr!=NULL && oldsize<newsize)
+			memcpy(p, ptr, oldsize);
+	}
+	
+	if(ptr!=NULL)
+		free(ptr);
+
+	return p;
+}
+
+
+char *scopy(const char *p)
+{
+	char*pn;
+	size_t l=strlen(p);
+	
+	pn=(char*)malloc(l+1);
+	
+	if(pn==NULL)
+		return NULL;
+	
+	memcpy(pn, p, l+1);
+	
+	return pn;
+}
+	
+	
+char *scat(const char *p1, const char *p2)
+{
+	size_t l1, l2;
+	char*pn;
+	
+	l1=strlen(p1);
+	l2=strlen(p2);
+	
+	pn=(char*)malloc(l1+l2+1);
+	
+	if(pn==NULL)
+		return NULL;
+	
+	memcpy(pn, p1, l1);
+	memcpy(pn+l1, p2, l2+1);
+	
+	return pn;
+}
+
+
+char *scat3(const char *p1, const char *p2, const char *p3)
+{
+	size_t l1, l2, l3;
+	char *pn;
+	
+	l1=strlen(p1);
+	l2=strlen(p2);
+	l3=strlen(p3);
+	
+	pn=(char*)malloc(l1+l2+l3+1);
+	
+	if(pn==NULL)
+		return NULL;
+	
+	memcpy(pn, p1, l1);
+	memcpy(pn+l1, p2, l2);
+	memcpy(pn+l1+l2, p3, l3+1);
+	
+	return pn;
+}
+
+
+/* */
+
+
+const char *simple_basename(const char *name)
+{
+	const char *p;
+	
+	p=name+strlen(name)-1;
+	
+	/* Skip any trailing slashes */
+	while(*p=='/'){
+		/* root? */
+		if(p==name)
+			return name;
+		p--;
+	}
+	
+	while(p!=name){
+		if(*p=='/')
+			return p+1;
+		p--;
+	}
+	
+	return name;
+}
+
+
+/* */
+
+
+bool readf(FILE *f, void *buf, size_t n)
+{
+	return fread(buf, 1, n, f)!=1;
+}
+
+
+bool writef(FILE *f, const void *buf, size_t n)
+{
+	return fwrite(buf, 1, n, f)!=1;
+}

mercurial