Tue, 15 Feb 2005 18:57:52 +0100
Tailorization of trunk
Import of the upstream sources from the repository
http://tao.uab.es/ion/svn/libtu/trunk
as of revision 2
| 0 | 1 | /* |
| 2 | * libtu/misc.c | |
| 3 | * | |
| 4 | * Copyright (c) Tuomo Valkonen 1999-2000. | |
| 5 | * | |
| 6 | * This file is distributed under the terms of the "Artistic License". | |
| 7 | * See the included file LICENSE for details. | |
| 8 | */ | |
| 9 | ||
| 10 | #include <stdlib.h> | |
| 11 | #include <stdio.h> | |
| 12 | #include <string.h> | |
| 13 | #include <malloc.h> | |
| 14 | ||
| 15 | #include "include/misc.h" | |
| 16 | ||
| 17 | ||
| 18 | void *malloczero(int size) | |
| 19 | { | |
| 20 | void *p=malloc(size); | |
| 21 | ||
| 22 | if(p!=NULL) | |
| 23 | memset(p, 0, size); | |
| 24 | ||
| 25 | return p; | |
| 26 | } | |
| 27 | ||
| 28 | ||
| 29 | void *remalloczero(void *ptr, int oldsize, int newsize) | |
| 30 | { | |
| 31 | void *p=NULL; | |
| 32 | ||
| 33 | if(newsize!=0){ | |
| 34 | p=malloc(newsize); | |
| 35 | ||
| 36 | if(p==NULL) | |
| 37 | return NULL; | |
| 38 | ||
| 39 | memset(p, 0, newsize); | |
| 40 | ||
| 41 | if(ptr!=NULL && oldsize<newsize) | |
| 42 | memcpy(p, ptr, oldsize); | |
| 43 | } | |
| 44 | ||
| 45 | if(ptr!=NULL) | |
| 46 | free(ptr); | |
| 47 | ||
| 48 | return p; | |
| 49 | } | |
| 50 | ||
| 51 | ||
| 52 | char *scopy(const char *p) | |
| 53 | { | |
| 54 | char*pn; | |
| 55 | size_t l=strlen(p); | |
| 56 | ||
| 57 | pn=(char*)malloc(l+1); | |
| 58 | ||
| 59 | if(pn==NULL) | |
| 60 | return NULL; | |
| 61 | ||
| 62 | memcpy(pn, p, l+1); | |
| 63 | ||
| 64 | return pn; | |
| 65 | } | |
| 66 | ||
| 67 | ||
| 68 | char *scat(const char *p1, const char *p2) | |
| 69 | { | |
| 70 | size_t l1, l2; | |
| 71 | char*pn; | |
| 72 | ||
| 73 | l1=strlen(p1); | |
| 74 | l2=strlen(p2); | |
| 75 | ||
| 76 | pn=(char*)malloc(l1+l2+1); | |
| 77 | ||
| 78 | if(pn==NULL) | |
| 79 | return NULL; | |
| 80 | ||
| 81 | memcpy(pn, p1, l1); | |
| 82 | memcpy(pn+l1, p2, l2+1); | |
| 83 | ||
| 84 | return pn; | |
| 85 | } | |
| 86 | ||
| 87 | ||
| 88 | char *scat3(const char *p1, const char *p2, const char *p3) | |
| 89 | { | |
| 90 | size_t l1, l2, l3; | |
| 91 | char *pn; | |
| 92 | ||
| 93 | l1=strlen(p1); | |
| 94 | l2=strlen(p2); | |
| 95 | l3=strlen(p3); | |
| 96 | ||
| 97 | pn=(char*)malloc(l1+l2+l3+1); | |
| 98 | ||
| 99 | if(pn==NULL) | |
| 100 | return NULL; | |
| 101 | ||
| 102 | memcpy(pn, p1, l1); | |
| 103 | memcpy(pn+l1, p2, l2); | |
| 104 | memcpy(pn+l1+l2, p3, l3+1); | |
| 105 | ||
| 106 | return pn; | |
| 107 | } | |
| 108 | ||
| 109 | ||
| 110 | /* */ | |
| 111 | ||
| 112 | ||
| 113 | const char *simple_basename(const char *name) | |
| 114 | { | |
| 115 | const char *p; | |
| 116 | ||
| 117 | p=name+strlen(name)-1; | |
| 118 | ||
| 119 | /* Skip any trailing slashes */ | |
| 120 | while(*p=='/'){ | |
| 121 | /* root? */ | |
| 122 | if(p==name) | |
| 123 | return name; | |
| 124 | p--; | |
| 125 | } | |
| 126 | ||
| 127 | while(p!=name){ | |
| 128 | if(*p=='/') | |
| 129 | return p+1; | |
| 130 | p--; | |
| 131 | } | |
| 132 | ||
| 133 | return name; | |
| 134 | } | |
| 135 | ||
| 136 | ||
| 137 | /* */ | |
| 138 | ||
| 139 | ||
| 140 | bool readf(FILE *f, void *buf, size_t n) | |
| 141 | { | |
| 142 | return fread(buf, 1, n, f)!=1; | |
| 143 | } | |
| 144 | ||
| 145 | ||
| 146 | bool writef(FILE *f, const void *buf, size_t n) | |
| 147 | { | |
| 148 | return fwrite(buf, 1, n, f)!=1; | |
| 149 | } |