roo_e/shared/util.c
2022-06-24 18:41:27 -05:00

182 lines
3.4 KiB
C

#include "array.h"
#include "util.h"
char __scratch[SCRATCH_SIZE];
void utilBitsPrint(uint8_t byte) {
int i = 0;
for (i = 7; 0 <= i; i--) {
printf("%c", (byte & (1 << i)) ? '1' : '0');
}
}
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 utilDie(const char *why, ...) {
va_list args;
char msg[2048];
va_start(args, why);
vsprintf(msg, why, args);
va_end(args);
logWrite("DIE: %s", msg);
exit(1);
}
uint8_t utilFileExists(char *filename) {
FILE *f = fopen(filename, "rb");
if (f) {
fclose(f);
return SUCCESS;
}
return FAIL;
}
char *utilFileExtensionChange(char *appName, char *extension) {
char *c = NULL;
char *newName = NULL;
int16_t x = strlen(appName);
uint16_t len = 2 + strlen(extension); // 2 = dot in extension and 0 terminator.
// Find last portion of filename.
while (x > 0) {
if (appName[x] == '/' || appName[x] == '\\') { x++; break; }
x--;
len++;
}
x--;
// We use this + length of new extension for new string length.
newName = (char *)malloc(len);
if (newName) {
if (strlen(appName) - x < len) {
// Replace extension
strncpy(newName, &appName[x + 1], len - 1);
c = strstr(newName, ".");
if (c) *c = 0;
strncat(newName, ".", len - 1);
strncat(newName, extension, len - 1);
}
}
return newName;
}
uint8_t utilFromFileReadByte(FILE *f, uint8_t *result) {
unsigned char c;
// Get next byte.
c = fgetc(f);
// End of file?
if (feof(f)) return FAIL;
// Add to result.
*result = c;
return SUCCESS;
}
uint8_t utilFromFileReadString(FILE *f, char **result) {
unsigned char c;
uint16_t x = 0;
// Something already in 'result'?
DEL(*result);
while (1) {
// Get next byte.
c = fgetc(f);
// End of file?
if (feof(f)) return FAIL;
// Add to result.
__scratch[x++] = c;
__scratch[x] = 0;
// End of string?
if (c == 0) {
*result = strdup(__scratch);
return SUCCESS;
}
}
}
void utilStringToLower(char *string) {
uint16_t i;
for (i=0; i<strlen(string); i++) string[i] = tolower(string[i]);
}
char **utilWrapText(char *text, uint16_t width) {
char **lines = NULL;
char *head = text;
char *buffer = NULL;
int32_t bufferPos;
int32_t pos;
int32_t lastSpace;
int32_t isLf;
buffer = (char *)malloc(width + 2);
if (buffer) {
pos = lastSpace = bufferPos = 0;
while (head[pos] != 0) {
isLf = (head[pos] == '\n');
if (isLf || pos == width) {
if (isLf || lastSpace == 0) lastSpace = pos; // just cut it
while (*head!=0 && lastSpace-- > 0) {
buffer[bufferPos++] = *head++;
buffer[bufferPos] = 0;
}
arrput(lines, strdup(buffer));
if (isLf) head++; // jump the line feed
while (*head!=0 && *head==' ') head++; // clear the leading space
lastSpace = pos = bufferPos = 0;
} else {
if (head[pos] == ' ') lastSpace = pos;
pos++;
}
}
arrput(lines, strdup(head));
free(buffer);
}
return lines;
}