Mercurial > hg > dmlib
view src/dmstring.c @ 1423:065dedf5890e
Fix view64 window title updating.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 08 Nov 2017 02:37:01 +0200 |
parents | e06abfde6c39 |
children | 47fe47f01fea |
line wrap: on
line source
#include "dmlib.h" #include <stdarg.h> /* Returns the filename and path without the last filename extension, * E.g. everything before the last '.', if any. */ char *dm_basefilename(const char *filename) { char *tmp, *fext; if (filename == NULL || (tmp = dm_strdup(filename)) == NULL) return NULL; if ((fext = strrchr(tmp, '.')) != NULL) { char *fpath = strrchr(tmp, DM_DIR_SEPARATOR); if (fpath == NULL || (fpath != NULL && fext > fpath)) *fext = 0; } return tmp; } /* Replace filename extension based on format pattern. * Usage: res = dm_strdup_fext(orig_filename, "foo_%s.cmp"); */ char *dm_strdup_fext(const char *filename, const char *fmt) { char *result, *tmp; if ((tmp = dm_basefilename(filename)) == NULL) return NULL; result = dm_strdup_printf(fmt, tmp); dmFree(tmp); return result; } /* Check if end of the given string str matches needle * case-insensitively, return pointer to start of the match, * if found, NULL otherwise. */ char *dm_strrcasecmp(char *str, const char *needle) { if (str == NULL || needle == NULL) return NULL; const size_t slen = strlen(str), nlen = strlen(needle); if (slen < nlen) return NULL; if (strcasecmp(str - nlen - 1, needle) == 0) return str - nlen - 1; else return NULL; } /* Implementation of strdup() with a NULL check */ char *dm_strdup(const char *s) { char *res; if (s == NULL) return NULL; if ((res = dmMalloc(strlen(s) + 1)) == NULL) return NULL; strcpy(res, s); return res; } /* Implementation of strndup() with NULL check */ char *dm_strndup(const char *s, const size_t n) { char *res; if (s == NULL) return NULL; size_t len = strlen(s); if (len > n) len = n; if ((res = dmMalloc(len + 1)) == NULL) return NULL; memcpy(res, s, len); res[len] = 0; return res; } /* Simulate a sprintf() that allocates memory */ char *dm_strdup_vprintf(const char *fmt, va_list args) { int size = 64; char *buf, *tmp; if ((buf = dmMalloc(size)) == NULL) return NULL; while (1) { int n; va_list ap; va_copy(ap, args); n = vsnprintf(buf, size, fmt, ap); va_end(ap); if (n > -1 && n < size) return buf; if (n > -1) size = n + 1; else size *= 2; if ((tmp = dmRealloc(buf, size)) == NULL) { dmFree(buf); return NULL; } else buf = tmp; } } char *dm_strdup_printf(const char *fmt, ...) { char *res; va_list ap; va_start(ap, fmt); res = dm_strdup_vprintf(fmt, ap); va_end(ap); return res; }