/* * Roo/E, the Kangaroo Punch Portable GUI Toolkit * Copyright (C) 2022 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 . * */ #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 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; }