f256/tools/shared/util.c
2024-04-28 19:55:01 -05:00

250 lines
5.3 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include "util.h"
#ifdef _WIN32
#define ourMkDir(p,m) mkdir(p)
#else
#define ourMkDir mkdir
#endif
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;
}
void utilFixPathSeparators(char **path, bool slash) {
int32_t i = 0;
int32_t j = 0;
char *work = *path;
char *temp = NULL;
// Flip path separators to whatever our OS wants & remove repeated separators.
while (work[i] != 0) {
// Correct separator
if (work[i] == '\\' || work[i] == '/') {
// Was the prior character a seprator?
if (j == 0) {
work[j++] = utilGetPathSeparator();
} else {
if (work[j - 1] != utilGetPathSeparator()) {
// No, accept it.
work[j++] = utilGetPathSeparator();
}
}
} else {
work[j++] = work[i];
}
i++;
}
work[j] = 0;
if (slash) {
// Does this string end with a path separator?
if (work[strlen(work) - 1] != utilGetPathSeparator()) {
// No - append one.
temp = strdup(work);
free(work);
work = malloc(sizeof(char) * (strlen(temp) + 2));
strcpy(work, temp);
work[strlen(temp)] = utilGetPathSeparator();
work[strlen(temp) + 1] = 0;
free(temp);
}
}
*path = work;
}
char *utilGetLastPathComponent(char *pathname) {
static char *start;
int32_t x;
start = pathname;
// Scan through name and find the last path separator
for (x=0; x<(int32_t)strlen(pathname); x++) {
if (pathname[x] == '\\' || pathname[x] == '/') {
start = &pathname[x + 1];
}
}
return strdup(start);
}
char utilGetPathSeparator(void) {
#ifdef _WIN32
return '\\';
#else
return '/';
#endif
}
char *utilGetUpToLastPathComponent(char *pathname) {
static char *copy = NULL;
bool dumb = false; // Using (copy == NULL) below didn't work after optimizations, so enter the dummy.
int32_t x;
x = (int32_t)(strlen(pathname) - strlen(utilGetLastPathComponent(pathname))) - 1;
if (x < 0) x = 0;
if (dumb) {
free(copy);
copy = NULL;
} else {
dumb = true;
}
copy = strdup(pathname);
copy[x] = 0;
utilFixPathSeparators(&copy, true);
return copy;
}
bool utilMkDirP(const char *dir, const mode_t mode) {
char tmp[UTIL_PATH_MAX];
char *p = NULL;
struct stat sb;
size_t len;
// Make copy of dir.
len = strnlen(dir, UTIL_PATH_MAX);
if (len == 0 || len == UTIL_PATH_MAX) {
return -1;
}
memcpy(tmp, dir, len);
tmp[len] = '\0';
// Remove trailing slash.
if (tmp[len - 1] == utilGetPathSeparator()) {
tmp[len - 1] = '\0';
}
// Does it already exist?
if (stat(tmp, &sb) == 0) {
if (S_ISDIR (sb.st_mode)) {
return true;
}
}
// Recursive mkdir.
for (p = tmp + 1; *p; p++) {
if (*p == utilGetPathSeparator()) {
*p = 0;
if (stat(tmp, &sb) != 0) {
// Does not exist - create it.
if (ourMkDir(tmp, mode) < 0) {
return false;
}
} else {
if (!S_ISDIR(sb.st_mode)) {
// Not a directory
return false;
}
}
*p = utilGetPathSeparator();
}
}
// Check path
if (stat(tmp, &sb) != 0) {
// Does not exist - create it.
if (ourMkDir(tmp, mode) < 0) {
return false;
}
} else {
if (!S_ISDIR(sb.st_mode)) {
// Not a directory
return false;
}
}
return true;
}
char *utilReplaceExtension(char *org, char *new_ext) {
char *ext;
char *tmp = strdup(org);
size_t newSize;
char *newName;
ext = strrchr(tmp , '.');
if (ext) *ext = 0;
newSize = strlen(tmp) + strlen(new_ext) + 1;
newName = malloc(newSize);
sprintf(newName, "%s%s", tmp, new_ext);
free(tmp);
return newName;
}
int utilStricmp(char *a, char *b) {
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a) {
return d;
}
}
}