roo_e/roo_e/util.c
2025-12-19 20:11:05 -06:00

80 lines
1.8 KiB
C

/* Roo/E, the Kangaroo Punch Portable GUI Toolkit
* Copyright (C) 2026 Scott Duensing
*
* http://kangaroopunch.com
*
*
* This file is part of Roo/E.
*
* Roo/E is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Roo/E is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Roo/E. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "util.h"
#include <ctype.h>
char *utilCreateString(char *format, ...) {
va_list args;
char *string;
va_start(args, format);
string = utilCreateStringVArgs(format, args);
va_end(args);
return string;
}
__attribute__((__format__(__printf__, 1, 0)))
char *utilCreateStringVArgs(char *format, va_list args) {
va_list argsCopy;
int32_t size = 0;
char *buffer = NULL;
va_copy(argsCopy, args);
size = vsnprintf(NULL, 0, format, argsCopy) + 1;
va_end(argsCopy);
buffer = calloc(1, (size_t)size);
if (buffer) {
vsnprintf(buffer, (size_t)size, format, args);
}
return buffer;
}
bool utilEndsWith(const char *haystack, const char *needle, const bool caseSensitive) {
size_t h = strlen(haystack);
size_t n = strlen(needle);
if (h < n) return false;
while (n > 0) {
h--;
n--;
if (caseSensitive) {
if (haystack[h] != needle[n]) return false;
} else {
if ((toupper(haystack[h]) != toupper(needle[n]))) return false;
}
}
return true;
}