|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2009-2010 John Stebbins
|
|
Packit |
5e46da |
* Copyright (C) 2011-2017 Petri Hintukainen <phintuka@users.sourceforge.net>
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is free software; you can redistribute it and/or
|
|
Packit |
5e46da |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License as published by the Free Software Foundation; either
|
|
Packit |
5e46da |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
5e46da |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5e46da |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
5e46da |
* Lesser General Public License for more details.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License along with this library. If not, see
|
|
Packit |
5e46da |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if HAVE_CONFIG_H
|
|
Packit |
5e46da |
#include "config.h"
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include "strutl.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include "macro.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include <stdio.h>
|
|
Packit |
5e46da |
#include <stdarg.h>
|
|
Packit |
5e46da |
#include <stdlib.h>
|
|
Packit |
5e46da |
#include <ctype.h>
|
|
Packit |
5e46da |
#include <string.h>
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
char *str_dup(const char *str)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
char *dup = NULL;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (str) {
|
|
Packit |
5e46da |
size_t size = strlen(str) + 1;
|
|
Packit |
5e46da |
dup = malloc(size);
|
|
Packit |
5e46da |
if (dup) {
|
|
Packit |
5e46da |
memcpy(dup, str, size);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return dup;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
char *str_printf(const char *fmt, ...)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
/* Guess we need no more than 100 bytes. */
|
|
Packit |
5e46da |
va_list ap;
|
|
Packit |
5e46da |
int len;
|
|
Packit |
5e46da |
int size = 100;
|
|
Packit |
5e46da |
char *tmp, *str = NULL;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
while (1) {
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
tmp = realloc(str, size);
|
|
Packit |
5e46da |
if (tmp == NULL) {
|
|
Packit |
5e46da |
X_FREE(str);
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
str = tmp;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/* Try to print in the allocated space. */
|
|
Packit |
5e46da |
va_start(ap, fmt);
|
|
Packit |
5e46da |
len = vsnprintf(str, size, fmt, ap);
|
|
Packit |
5e46da |
va_end(ap);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/* If that worked, return the string. */
|
|
Packit |
5e46da |
if (len > -1 && len < size) {
|
|
Packit |
5e46da |
return str;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/* Else try again with more space. */
|
|
Packit |
5e46da |
if (len > -1) /* glibc 2.1 */
|
|
Packit |
5e46da |
size = len+1; /* precisely what is needed */
|
|
Packit |
5e46da |
else /* glibc 2.0 */
|
|
Packit |
5e46da |
size *= 2; /* twice the old size */
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
uint32_t str_to_uint32(const char *s, int n)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
uint32_t val = 0;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (n > 4)
|
|
Packit |
5e46da |
n = 4;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (!s || !*s) {
|
|
Packit |
5e46da |
return (INT64_C(1) << (8*n)) - 1; /* default: all bits one */
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
while (n--) {
|
|
Packit |
5e46da |
val = (val << 8) | *s;
|
|
Packit |
5e46da |
if (*s) {
|
|
Packit |
5e46da |
s++;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return val;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
void str_tolower(char *s)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
while (*s) {
|
|
Packit |
5e46da |
*s = tolower(*s);
|
|
Packit |
5e46da |
s++;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
char *str_print_hex(char *out, const uint8_t *buf, int count)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
int zz;
|
|
Packit |
5e46da |
for (zz = 0; zz < count; zz++) {
|
|
Packit |
5e46da |
sprintf(out + (zz * 2), "%02x", buf[zz]);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return out;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
const char *str_strcasestr(const char *haystack, const char *needle)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
const char *result = NULL;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
char *h = str_dup(haystack);
|
|
Packit |
5e46da |
char *n = str_dup(needle);
|
|
Packit |
5e46da |
if (h && n) {
|
|
Packit |
5e46da |
str_tolower(h);
|
|
Packit |
5e46da |
str_tolower(n);
|
|
Packit |
5e46da |
result = strstr(h, n);
|
|
Packit |
5e46da |
if (result) {
|
|
Packit |
5e46da |
result = haystack + (result - h);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
X_FREE(h);
|
|
Packit |
5e46da |
X_FREE(n);
|
|
Packit |
5e46da |
return result;
|
|
Packit |
5e46da |
}
|