diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1bf02986..22cee925 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,46 +1,49 @@ -# Copyright (c) 2022 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -# Include tools -add_subdirectory(dawntools) - -# Change what we are building -add_subdirectory(${DAWN_BUILDING}) - -# Check the game project includes the target name -if(NOT DEFINED DAWN_TARGET_NAME) - message(FATAL_ERROR "You need to define a DAWN_TARGET_NAME") -endif() - -# Add in base library -add_subdirectory(dawn) - -# Compile entry targets -if(DAWN_TARGET_WIN32) - add_subdirectory(dawnwin32) -elseif(DAWN_TARGET_LINUX64) - add_subdirectory(dawnlinux64) -else() - message(FATAL_ERROR "You need to define an entry target") -endif() - -# Host Libraries -target_link_libraries(${DAWN_TARGET_NAME} - PUBLIC - ${DAWN_BUILD_HOST_LIBS} -) - -# Compile support targets -if(DAWN_TARGET_GLFW) - add_subdirectory(dawnglfw) - add_subdirectory(dawnopengl) -endif() - -if(DAWN_TARGET_SDL2) - add_subdirectory(dawnsdl2) - add_subdirectory(dawnopengl) -endif() - +# Copyright (c) 2022 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Include shared libs +add_subdirectory(dawnshared) + +# Include tools +add_subdirectory(dawntools) + +# Change what we are building +add_subdirectory(${DAWN_BUILDING}) + +# Check the game project includes the target name +if(NOT DEFINED DAWN_TARGET_NAME) + message(FATAL_ERROR "You need to define a DAWN_TARGET_NAME") +endif() + +# Add in base library +add_subdirectory(dawn) + +# Compile entry targets +if(DAWN_TARGET_WIN32) + add_subdirectory(dawnwin32) +elseif(DAWN_TARGET_LINUX64) + add_subdirectory(dawnlinux64) +else() + message(FATAL_ERROR "You need to define an entry target") +endif() + +# Host Libraries +target_link_libraries(${DAWN_TARGET_NAME} + PUBLIC + ${DAWN_BUILD_HOST_LIBS} +) + +# Compile support targets +if(DAWN_TARGET_GLFW) + add_subdirectory(dawnglfw) + add_subdirectory(dawnopengl) +endif() + +if(DAWN_TARGET_SDL2) + add_subdirectory(dawnsdl2) + add_subdirectory(dawnopengl) +endif() + add_subdirectory(dawnopenal) \ No newline at end of file diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt index 6f93f36c..6630eb9d 100644 --- a/src/dawn/CMakeLists.txt +++ b/src/dawn/CMakeLists.txt @@ -13,6 +13,7 @@ target_link_libraries(${DAWN_TARGET_NAME} # Includes target_include_directories(${DAWN_TARGET_NAME} PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawn/dawnlibs.hpp b/src/dawn/dawnlibs.hpp index 8e7d9e42..1248afc9 100644 --- a/src/dawn/dawnlibs.hpp +++ b/src/dawn/dawnlibs.hpp @@ -6,36 +6,9 @@ */ #pragma once +#include "dawnsharedlibs.hpp" -// Static Libs -extern "C" { - // Standard Libs - #include - #include - #include - #include - #include - #include - #include - #include - - typedef bool bool_t; - typedef float float_t; -} - -#include -#include -#include -#include -#include #include -#include -#include -#include -// #include -// #include -// #include -// #include #include #include diff --git a/src/dawnshared/CMakeLists.txt b/src/dawnshared/CMakeLists.txt new file mode 100644 index 00000000..203c8ff9 --- /dev/null +++ b/src/dawnshared/CMakeLists.txt @@ -0,0 +1,13 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Includes +set( + DAWN_SHARED_INCLUDES + ${CMAKE_CURRENT_LIST_DIR} + + CACHE INTERNAL + ${DAWN_CACHE_TARGET} +) \ No newline at end of file diff --git a/src/dawnshared/dawnsharedlibs.hpp b/src/dawnshared/dawnsharedlibs.hpp new file mode 100644 index 00000000..c98a271b --- /dev/null +++ b/src/dawnshared/dawnsharedlibs.hpp @@ -0,0 +1,31 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once + +// Static Libs +extern "C" { + // Standard Libs + #include + #include + #include + #include + #include + #include + #include + #include + + typedef bool bool_t; + typedef float float_t; +} + +#include +#include +#include +#include +#include +#include +#include +#include \ No newline at end of file diff --git a/src/dawntools/audio/audiogen/CMakeLists.txt b/src/dawntools/audio/audiogen/CMakeLists.txt index ce2e048a..058a7e09 100644 --- a/src/dawntools/audio/audiogen/CMakeLists.txt +++ b/src/dawntools/audio/audiogen/CMakeLists.txt @@ -1,23 +1,24 @@ -# Copyright (c) 2021 Dominic Msters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -# Texture Build Tool -project(audiogen VERSION 1.0) -add_executable(audiogen) -target_sources(audiogen - PRIVATE - main.cpp - ../../utils/file.c -) -target_include_directories(audiogen - PUBLIC - ${CMAKE_CURRENT_LIST_DIR}/../../ - ${CMAKE_CURRENT_LIST_DIR} -) -target_link_libraries(audiogen - PUBLIC - ${DAWN_BUILD_HOST_LIBS} - AudioFile +# Copyright (c) 2021 Dominic Msters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Texture Build Tool +project(audiogen VERSION 1.0) +add_executable(audiogen) +target_sources(audiogen + PRIVATE + main.cpp + ../../utils/file.cpp +) +target_include_directories(audiogen + PUBLIC + ${DAWN_SHARED_INCLUDES} + ${CMAKE_CURRENT_LIST_DIR}/../../ + ${CMAKE_CURRENT_LIST_DIR} +) +target_link_libraries(audiogen + PUBLIC + ${DAWN_BUILD_HOST_LIBS} + AudioFile ) \ No newline at end of file diff --git a/src/dawntools/audio/audiogen/main.cpp b/src/dawntools/audio/audiogen/main.cpp index 3ffe8dde..f8e77fbc 100644 --- a/src/dawntools/audio/audiogen/main.cpp +++ b/src/dawntools/audio/audiogen/main.cpp @@ -1,78 +1,76 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -extern "C" { - #include "../../utils/common.h" - #include "../../utils/file.h" -} -#include "AudioFile.h" - -int main(int argc, char *argv[]) { - FILE *fileOut; - char buffer[FILENAME_MAX]; - size_t bufferLength = 0; - - if(argc != 3) { - printf("Invalid number of arguments\n"); - return 1; - } - - fileNormalizeSlashes(argv[1]); - fileNormalizeSlashes(argv[2]); - std::string strFileIn = std::string(argv[1]); - std::string strFileOut = std::string(argv[2]); - - buffer[0] = '\0'; - sprintf(buffer, "%s.audio", strFileOut.c_str()); - fileOut = fopen(buffer, "rb"); - if(fileOut != NULL) { - fclose(fileOut); - return 0; - } - - // Load input file - AudioFile audioFile; - if(!audioFile.load(strFileIn)) { - printf("Failed to load audio file.\n"); - return 1; - } - - // Open Output File - fileMkdirp(buffer); - fileOut = fopen(buffer, "wb"); - if(fileOut == NULL) { - printf("Failed to create output file\n"); - return 1; - } - - // Write header - buffer[0] = '\0'; - sprintf(buffer, "%i|%i|%i|%i|", - audioFile.getNumChannels(), - audioFile.getSampleRate(), - audioFile.getNumSamplesPerChannel(), - audioFile.getNumSamplesPerChannel() * audioFile.getNumChannels()*( - sizeof(int16_t) / sizeof(uint8_t) - ) - ); - bufferLength = strlen(buffer); - fwrite(buffer, sizeof(char), bufferLength, fileOut); - - // Convert Data to 16 bit audio - for (int32_t i = 0; i < audioFile.getNumSamplesPerChannel(); i++) { - for(int32_t y = 0; y < audioFile.getNumChannels(); y++) { - double sample = audioFile.samples[y][i]; - sample = sample < -1 ? -1 : sample > 1 ? 1 : sample; - auto q = static_cast (sample * 32767.); - - buffer[0] = (q >> 8) & 0xFF; - buffer[1] = q & 0xFF; - fwrite(buffer, sizeof(uint8_t), 2, fileOut); - } - } - - fclose(fileOut); - return 0; +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "../../utils/common.hpp" +#include "../../utils/file.hpp" +#include "AudioFile.h" + +int main(int argc, char *argv[]) { + FILE *fileOut; + char buffer[FILENAME_MAX]; + size_t bufferLength = 0; + + if(argc != 3) { + printf("Invalid number of arguments\n"); + return 1; + } + + fileNormalizeSlashes(argv[1]); + fileNormalizeSlashes(argv[2]); + std::string strFileIn = std::string(argv[1]); + std::string strFileOut = std::string(argv[2]); + + buffer[0] = '\0'; + sprintf(buffer, "%s.audio", strFileOut.c_str()); + fileOut = fopen(buffer, "rb"); + if(fileOut != NULL) { + fclose(fileOut); + return 0; + } + + // Load input file + AudioFile audioFile; + if(!audioFile.load(strFileIn)) { + printf("Failed to load audio file.\n"); + return 1; + } + + // Open Output File + fileMkdirp(buffer); + fileOut = fopen(buffer, "wb"); + if(fileOut == NULL) { + printf("Failed to create output file\n"); + return 1; + } + + // Write header + buffer[0] = '\0'; + sprintf(buffer, "%i|%i|%i|%i|", + audioFile.getNumChannels(), + audioFile.getSampleRate(), + audioFile.getNumSamplesPerChannel(), + audioFile.getNumSamplesPerChannel() * audioFile.getNumChannels()*( + sizeof(int16_t) / sizeof(uint8_t) + ) + ); + bufferLength = strlen(buffer); + fwrite(buffer, sizeof(char), bufferLength, fileOut); + + // Convert Data to 16 bit audio + for (int32_t i = 0; i < audioFile.getNumSamplesPerChannel(); i++) { + for(int32_t y = 0; y < audioFile.getNumChannels(); y++) { + double sample = audioFile.samples[y][i]; + sample = sample < -1 ? -1 : sample > 1 ? 1 : sample; + auto q = static_cast (sample * 32767.); + + buffer[0] = (q >> 8) & 0xFF; + buffer[1] = q & 0xFF; + fwrite(buffer, sizeof(uint8_t), 2, fileOut); + } + } + + fclose(fileOut); + return 0; } \ No newline at end of file diff --git a/src/dawntools/display/texturegen/CMakeLists.txt b/src/dawntools/display/texturegen/CMakeLists.txt index 29dd69af..9b984bee 100644 --- a/src/dawntools/display/texturegen/CMakeLists.txt +++ b/src/dawntools/display/texturegen/CMakeLists.txt @@ -9,11 +9,12 @@ add_executable(texturegen) target_sources(texturegen PRIVATE main.cpp - ../../utils/file.c - ../../utils/image.c + ../../utils/file.cpp + ../../utils/image.cpp ) target_include_directories(texturegen PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawntools/display/texturegen/main.cpp b/src/dawntools/display/texturegen/main.cpp index 79c93d4c..8089b09b 100644 --- a/src/dawntools/display/texturegen/main.cpp +++ b/src/dawntools/display/texturegen/main.cpp @@ -5,11 +5,9 @@ * https://opensource.org/licenses/MIT */ -extern "C" { - #include "../../utils/common.h" - #include "../../utils/file.h" - #include "../../utils/image.h" -} +#include "../../utils/common.hpp" +#include "../../utils/file.hpp" +#include "../../utils/image.hpp" int main(int argc, char *argv[]) { FILE *file; diff --git a/src/dawntools/display/tilesetgen/CMakeLists.txt b/src/dawntools/display/tilesetgen/CMakeLists.txt index 7ffac01d..68cdbdd6 100644 --- a/src/dawntools/display/tilesetgen/CMakeLists.txt +++ b/src/dawntools/display/tilesetgen/CMakeLists.txt @@ -9,11 +9,12 @@ add_executable(tilesetgen) target_sources(tilesetgen PRIVATE main.cpp - ../../utils/file.c - ../../utils/image.c + ../../utils/file.cpp + ../../utils/image.cpp ) target_include_directories(tilesetgen PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawntools/display/tilesetgen/main.cpp b/src/dawntools/display/tilesetgen/main.cpp index 26b23249..86fb00c6 100644 --- a/src/dawntools/display/tilesetgen/main.cpp +++ b/src/dawntools/display/tilesetgen/main.cpp @@ -5,11 +5,9 @@ * https://opensource.org/licenses/MIT */ -extern "C" { - #include "../../utils/common.h" - #include "../../utils/file.h" - #include "../../utils/image.h" -} +#include "../../utils/common.hpp" +#include "../../utils/file.hpp" +#include "../../utils/image.hpp" int main(int argc, char *argv[]) { char *in; diff --git a/src/dawntools/display/truetypegen/CMakeLists.txt b/src/dawntools/display/truetypegen/CMakeLists.txt index f446f893..cd2c7d92 100644 --- a/src/dawntools/display/truetypegen/CMakeLists.txt +++ b/src/dawntools/display/truetypegen/CMakeLists.txt @@ -9,11 +9,12 @@ add_executable(truetypegen) target_sources(truetypegen PRIVATE main.cpp - ../../utils/file.c - ../../utils/image.c + ../../utils/file.cpp + ../../utils/image.cpp ) target_include_directories(truetypegen PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawntools/display/truetypegen/main.cpp b/src/dawntools/display/truetypegen/main.cpp index f1688b39..f22d85c3 100644 --- a/src/dawntools/display/truetypegen/main.cpp +++ b/src/dawntools/display/truetypegen/main.cpp @@ -5,15 +5,13 @@ * https://opensource.org/licenses/MIT */ -extern "C" { - #include "../../utils/common.h" - #include "../../utils/file.h" - #include "../../utils/image.h" - #ifndef STB_TRUETYPE_IMPLEMENTATION - #define STB_TRUETYPE_IMPLEMENTATION - #include - #endif -} +#include "../../utils/common.hpp" +#include "../../utils/file.hpp" +#include "../../utils/image.hpp" +#ifndef STB_TRUETYPE_IMPLEMENTATION + #define STB_TRUETYPE_IMPLEMENTATION + #include +#endif #define TRUETYPE_FIRST_CHAR 32 #define TRUETYPE_NUM_CHARS 96 diff --git a/src/dawntools/display/uigen/CMakeLists.txt b/src/dawntools/display/uigen/CMakeLists.txt index 70ed4992..d2504dc0 100644 --- a/src/dawntools/display/uigen/CMakeLists.txt +++ b/src/dawntools/display/uigen/CMakeLists.txt @@ -9,11 +9,12 @@ add_executable(uigen) target_sources(uigen PRIVATE main.cpp - ../../utils/file.c - ../../utils/xml.c + ../../utils/file.cpp + ../../utils/xml.cpp ) target_include_directories(uigen PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawntools/display/uigen/main.cpp b/src/dawntools/display/uigen/main.cpp index 3f6321c3..77959320 100644 --- a/src/dawntools/display/uigen/main.cpp +++ b/src/dawntools/display/uigen/main.cpp @@ -5,11 +5,8 @@ * https://opensource.org/licenses/MIT */ -extern "C" { - #include "../../utils/xml.h" - #include "../../utils/file.h" - #include -} +#include "../../utils/xml.hpp" +#include "../../utils/file.hpp" #include #include diff --git a/src/dawntools/locale/languagegen/CMakeLists.txt b/src/dawntools/locale/languagegen/CMakeLists.txt index 423852ae..253d344c 100644 --- a/src/dawntools/locale/languagegen/CMakeLists.txt +++ b/src/dawntools/locale/languagegen/CMakeLists.txt @@ -9,12 +9,13 @@ add_executable(languagegen) target_sources(languagegen PRIVATE main.cpp - ../../utils/file.c - ../../utils/csv.c - ../../utils/xml.c + ../../utils/file.cpp + ../../utils/csv.cpp + ../../utils/xml.cpp ) target_include_directories(languagegen PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawntools/locale/languagegen/main.cpp b/src/dawntools/locale/languagegen/main.cpp index 2669bf5c..1e41f95e 100644 --- a/src/dawntools/locale/languagegen/main.cpp +++ b/src/dawntools/locale/languagegen/main.cpp @@ -5,14 +5,10 @@ * https://opensource.org/licenses/MIT */ -extern "C" { - #include "../../utils/common.h" - #include "../../utils/file.h" - #include "../../utils/csv.h" - #include "../../utils/xml.h" - #include -} - +#include "../../utils/common.hpp" +#include "../../utils/file.hpp" +#include "../../utils/csv.hpp" +#include "../../utils/xml.hpp" #include #include #include diff --git a/src/dawntools/utils/common.h b/src/dawntools/utils/common.hpp similarity index 85% rename from src/dawntools/utils/common.h rename to src/dawntools/utils/common.hpp index 6614029e..3d5af9d3 100644 --- a/src/dawntools/utils/common.h +++ b/src/dawntools/utils/common.hpp @@ -1,14 +1,16 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include -#include -#include -#include -#include +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dawnsharedlibs.hpp" + +#include +#include +#include +#include +#include #include \ No newline at end of file diff --git a/src/dawntools/utils/csv.c b/src/dawntools/utils/csv.cpp similarity index 88% rename from src/dawntools/utils/csv.c rename to src/dawntools/utils/csv.cpp index a7a616a1..f71262f0 100644 --- a/src/dawntools/utils/csv.c +++ b/src/dawntools/utils/csv.cpp @@ -1,120 +1,120 @@ -/** - * Copyright (c) 2022 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "csv.h" - -char * csvGetCell(csv_t *csv, int32_t row, int32_t cell) { - return csv->rows[(row * CSV_COLUMN_COUNT_MAX) + cell]; -} - -void csvParse(char *string, csv_t *csv) { - char c; - size_t i, j, length; - csvparsestate_t state; - int32_t rowCellCount; - - length = strlen(string); - csv->buffer = malloc(sizeof(char) * (length+1) * CSV_COLUMN_COUNT_MAX * CSV_ROW_COUNT_MAX); - csv->cellCounts = malloc(sizeof(int32_t) * CSV_ROW_COUNT_MAX); - csv->rows = malloc(sizeof(char*) * CSV_ROW_COUNT_MAX * CSV_COLUMN_COUNT_MAX); - - i = 0; - j = 0; - rowCellCount = 0; - csv->rowCount = 0; - state = CSV_PARSE_STATE_FIND_CELL; - while(i < length) { - c = string[i++]; - - // What are we doing - switch(state) { - case CSV_PARSE_STATE_FIND_CELL: - if(c == '"') { - state = CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES; - csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j; - rowCellCount++; - continue; - } else if(c == '\r' || c == '\n') { - // Newline (todo: is this a blank line?) - state = CSV_PARSE_STATE_LINE_END; - continue; - } else if(c == ',') { - csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j; - csv->buffer[j++] = '\0'; - rowCellCount++; - continue; - } else { - state = CSV_PARSE_STATE_PARSE_CELL; - csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j; - csv->buffer[j++] = c; - rowCellCount++; - continue; - } - - case CSV_PARSE_STATE_PARSE_CELL: - if(c == '\r' || c == '\n') { - state = CSV_PARSE_STATE_LINE_END; - csv->buffer[j++] = '\0'; - continue; - } else if(c == ',') { - state = CSV_PARSE_STATE_FIND_CELL; - csv->buffer[j++] = '\0'; - continue; - } - csv->buffer[j++] = c; - continue; - - case CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES: - if((c == '\\' && string[i] == '"') || (c == '"' && string[i] == '"')) { - // Handle escaped quotes. I normally see [\"] but excel does [""] in - // most cases - csv->buffer[j++] = '"'; - i++; - continue; - } else if(c == '"') { - // Handle end of quoted string - state = CSV_PARSE_STATE_FIND_CELL; - csv->buffer[j++] = '\0'; - // Because we tend to do [",] at the end of a quoted cell, we do this - // to prevent [,,] cases being treated the same - if(string[i] == ',') i++; - continue; - } - - // Normal character. - csv->buffer[j++] = c; - continue; - - case CSV_PARSE_STATE_LINE_END: - // Skip blanks - if(c == '\r' || c == '\n') continue; - csv->cellCounts[csv->rowCount] = rowCellCount; - csv->rowCount++; - rowCellCount = 0; - state = CSV_PARSE_STATE_FIND_CELL; - i--; - continue; - - default: - printf("Error occured during parse operation."); - free(NULL); - } - } - - csv->buffer[j++] = '\0'; - - if(rowCellCount != 0) { - csv->cellCounts[csv->rowCount] = rowCellCount; - csv->rowCount++; - } -} - -void csvDispose(csv_t *csv) { - free(csv->buffer); - free(csv->cellCounts); - free(csv->rows); +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "csv.hpp" + +char * csvGetCell(csv_t *csv, int32_t row, int32_t cell) { + return csv->rows[(row * CSV_COLUMN_COUNT_MAX) + cell]; +} + +void csvParse(char *string, csv_t *csv) { + char c; + size_t i, j, length; + csvparsestate_t state; + int32_t rowCellCount; + + length = strlen(string); + csv->buffer = (char *)malloc(sizeof(char) * (length+1) * CSV_COLUMN_COUNT_MAX * CSV_ROW_COUNT_MAX); + csv->cellCounts = (int32_t *)malloc(sizeof(int32_t) * CSV_ROW_COUNT_MAX); + csv->rows = (char**)malloc(sizeof(char*) * CSV_ROW_COUNT_MAX * CSV_COLUMN_COUNT_MAX); + + i = 0; + j = 0; + rowCellCount = 0; + csv->rowCount = 0; + state = CSV_PARSE_STATE_FIND_CELL; + while(i < length) { + c = string[i++]; + + // What are we doing + switch(state) { + case CSV_PARSE_STATE_FIND_CELL: + if(c == '"') { + state = CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES; + csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j; + rowCellCount++; + continue; + } else if(c == '\r' || c == '\n') { + // Newline (todo: is this a blank line?) + state = CSV_PARSE_STATE_LINE_END; + continue; + } else if(c == ',') { + csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j; + csv->buffer[j++] = '\0'; + rowCellCount++; + continue; + } else { + state = CSV_PARSE_STATE_PARSE_CELL; + csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j; + csv->buffer[j++] = c; + rowCellCount++; + continue; + } + + case CSV_PARSE_STATE_PARSE_CELL: + if(c == '\r' || c == '\n') { + state = CSV_PARSE_STATE_LINE_END; + csv->buffer[j++] = '\0'; + continue; + } else if(c == ',') { + state = CSV_PARSE_STATE_FIND_CELL; + csv->buffer[j++] = '\0'; + continue; + } + csv->buffer[j++] = c; + continue; + + case CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES: + if((c == '\\' && string[i] == '"') || (c == '"' && string[i] == '"')) { + // Handle escaped quotes. I normally see [\"] but excel does [""] in + // most cases + csv->buffer[j++] = '"'; + i++; + continue; + } else if(c == '"') { + // Handle end of quoted string + state = CSV_PARSE_STATE_FIND_CELL; + csv->buffer[j++] = '\0'; + // Because we tend to do [",] at the end of a quoted cell, we do this + // to prevent [,,] cases being treated the same + if(string[i] == ',') i++; + continue; + } + + // Normal character. + csv->buffer[j++] = c; + continue; + + case CSV_PARSE_STATE_LINE_END: + // Skip blanks + if(c == '\r' || c == '\n') continue; + csv->cellCounts[csv->rowCount] = rowCellCount; + csv->rowCount++; + rowCellCount = 0; + state = CSV_PARSE_STATE_FIND_CELL; + i--; + continue; + + default: + printf("Error occured during parse operation."); + free(NULL); + } + } + + csv->buffer[j++] = '\0'; + + if(rowCellCount != 0) { + csv->cellCounts[csv->rowCount] = rowCellCount; + csv->rowCount++; + } +} + +void csvDispose(csv_t *csv) { + free(csv->buffer); + free(csv->cellCounts); + free(csv->rows); } \ No newline at end of file diff --git a/src/dawntools/utils/csv.h b/src/dawntools/utils/csv.hpp similarity index 92% rename from src/dawntools/utils/csv.h rename to src/dawntools/utils/csv.hpp index 8ccd561b..fa3b844d 100644 --- a/src/dawntools/utils/csv.h +++ b/src/dawntools/utils/csv.hpp @@ -1,33 +1,33 @@ -/** - * Copyright (c) 2022 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "common.h" -#include "string.h" - -#define CSV_ROW_COUNT_MAX 128 -#define CSV_COLUMN_COUNT_MAX 16 - -typedef enum { - CSV_PARSE_STATE_FIND_CELL,//0 - CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES, - CSV_PARSE_STATE_PARSE_CELL,//2 - CSV_PARSE_STATE_LINE_END -} csvparsestate_t; - -typedef struct { - char *buffer; - char **rows; - int32_t rowCount; - int32_t *cellCounts; -} csv_t; - -char * csvGetCell(csv_t *csv, int32_t row, int32_t cell); - -void csvParse(char *string, csv_t *csv); - +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "common.hpp" +#include "string.h" + +#define CSV_ROW_COUNT_MAX 128 +#define CSV_COLUMN_COUNT_MAX 16 + +typedef enum { + CSV_PARSE_STATE_FIND_CELL,//0 + CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES, + CSV_PARSE_STATE_PARSE_CELL,//2 + CSV_PARSE_STATE_LINE_END +} csvparsestate_t; + +typedef struct { + char *buffer; + char **rows; + int32_t rowCount; + int32_t *cellCounts; +} csv_t; + +char * csvGetCell(csv_t *csv, int32_t row, int32_t cell); + +void csvParse(char *string, csv_t *csv); + void csvDispose(csv_t *csv); \ No newline at end of file diff --git a/src/dawntools/utils/file.c b/src/dawntools/utils/file.cpp similarity index 94% rename from src/dawntools/utils/file.c rename to src/dawntools/utils/file.cpp index 3cb40623..984a0cea 100644 --- a/src/dawntools/utils/file.c +++ b/src/dawntools/utils/file.cpp @@ -1,195 +1,195 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "file.h" - -void fileNormalizeSlashes(char *string) { - char c; - int i = 0; - - while(c = string[i++]) { - if(c != '\\' && c != '/') continue; - string[i-1] = FILE_PATH_SEP; - } -} - -void fileMkdirp(char *path) { - char buffer[FILENAME_MAX]; - char c; - int i = 0; - bool inFile; - bool hasMore; - - inFile = false; - hasMore = false; - while(c = path[i]) { - if((c == '\\' || c == '/') && i > 0) { - buffer[i] = '\0'; - fileMkdir(buffer, 0755); - inFile = false; - hasMore = false; - buffer[i] = FILE_PATH_SEP; - i++; - continue; - } - - if(c == '.') inFile = true; - hasMore = true; - buffer[i] = c; - i++; - } - - if(!inFile && hasMore) { - buffer[i] = '\0'; - fileMkdir(buffer, 0755); - } -} - -size_t assetReadString(FILE *file, char *buffer) { - size_t length; - fseek(file, 0, SEEK_END);// Seek to the end - length = ftell(file);// Get our current position (the end) - fseek(file, 0, SEEK_SET);// Reset the seek - if(buffer == NULL) return length; - return fread(buffer, 1, length, file);// Read all the bytes -} - -int32_t readAhead( - char *bufferIn, int32_t start, - char *bufferOut, - char *needles, int32_t needleCount -) { - int32_t i = start, k = 0, j; - char c; - bool needleFound = false; - - if(bufferIn[i] == '\0') return 0; - - while((c = bufferIn[i++]) != '\0') { - for(j = 0; j < needleCount; j++) { - if(c != needles[j]) continue; - needleFound = true; - } - if(needleFound) break; - if(bufferOut != NULL) bufferOut[k] = c; - k++; - } - - if(bufferOut != NULL) bufferOut[k] = '\0'; - return k; -} - -int32_t skipAhead( - char *bufferIn, int32_t start, - char *needles, int32_t needleCount -) { - char c; - int32_t j, k = 0, i = start; - bool needleFound; - - while((c = bufferIn[i++]) != '\0') { - needleFound = false; - for(j = 0; j < needleCount; j++) { - if(c != needles[j]) continue; - needleFound = true; - break; - } - if(!needleFound) break; - k++; - } - return k; -} - -void fileGetDirectory(char *file, char* buffer) { - char *c, *p; - int32_t i; - p = strrchr(file, FILE_PATH_SEP); - c = file; - i = 0; - do { - buffer[i++] = *c; - } while(++c < p); - buffer[i] = '\0'; -} - -bool fileListChildren( - char *directory, - char *buffer, - int32_t *count, - uint8_t *types, - char **children -) { - #if defined(_MSC_VER) - WIN32_FIND_DATA fdFile; - HANDLE hFind = NULL; - char sPath[2048]; - int32_t i; - - // Append wildcard - sprintf(sPath, "%s\\*.*", directory); - - // Scan first - if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) { - printf("Path not found: [%s]\n", directory); - return false; - } - - // Iterate - i = 0; - do { - if( - strcmp(fdFile.cFileName, ".") == 0 || - strcmp(fdFile.cFileName, "..") == 0 - ) continue; - - // Get Full path. - sprintf(sPath, "%s\\%s", directory, fdFile.cFileName); - - //Is the entity a File or Folder? - if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - types[i] = FILE_CHILD_TYPE_DIR; - } else { - types[i] = FILE_CHILD_TYPE_FILE; - } - - children[i] = buffer + (i * FILE_CHILD_NAME_MAX); - strcpy(children[i], fdFile.cFileName); - i++; - } while(FindNextFile(hFind, &fdFile)); - - *count = i; - return true; - #else - struct dirent *de; - DIR *dr; - int32_t i; - - // Open Dir - dr = opendir(directory); - if(dr == NULL) { - printf("Could not open directory"); - return false; - } - - - // Iterate - i = 0; - while ((de = readdir(dr)) != NULL) { - // File or folder? - if(de->d_type != DT_REG) continue; - - // Copy into child buffer - children[i] = buffer + (i * FILE_CHILD_NAME_MAX); - strcpy(children[i], de->d_name); - i++; - } - if(closedir(dr)) return false; - - *count = i; - return true; - #endif +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "file.hpp" + +void fileNormalizeSlashes(char *string) { + char c; + int i = 0; + + while(c = string[i++]) { + if(c != '\\' && c != '/') continue; + string[i-1] = FILE_PATH_SEP; + } +} + +void fileMkdirp(char *path) { + char buffer[FILENAME_MAX]; + char c; + int i = 0; + bool inFile; + bool hasMore; + + inFile = false; + hasMore = false; + while(c = path[i]) { + if((c == '\\' || c == '/') && i > 0) { + buffer[i] = '\0'; + fileMkdir(buffer, 0755); + inFile = false; + hasMore = false; + buffer[i] = FILE_PATH_SEP; + i++; + continue; + } + + if(c == '.') inFile = true; + hasMore = true; + buffer[i] = c; + i++; + } + + if(!inFile && hasMore) { + buffer[i] = '\0'; + fileMkdir(buffer, 0755); + } +} + +size_t assetReadString(FILE *file, char *buffer) { + size_t length; + fseek(file, 0, SEEK_END);// Seek to the end + length = ftell(file);// Get our current position (the end) + fseek(file, 0, SEEK_SET);// Reset the seek + if(buffer == NULL) return length; + return fread(buffer, 1, length, file);// Read all the bytes +} + +int32_t readAhead( + char *bufferIn, int32_t start, + char *bufferOut, + char *needles, int32_t needleCount +) { + int32_t i = start, k = 0, j; + char c; + bool needleFound = false; + + if(bufferIn[i] == '\0') return 0; + + while((c = bufferIn[i++]) != '\0') { + for(j = 0; j < needleCount; j++) { + if(c != needles[j]) continue; + needleFound = true; + } + if(needleFound) break; + if(bufferOut != NULL) bufferOut[k] = c; + k++; + } + + if(bufferOut != NULL) bufferOut[k] = '\0'; + return k; +} + +int32_t skipAhead( + char *bufferIn, int32_t start, + char *needles, int32_t needleCount +) { + char c; + int32_t j, k = 0, i = start; + bool needleFound; + + while((c = bufferIn[i++]) != '\0') { + needleFound = false; + for(j = 0; j < needleCount; j++) { + if(c != needles[j]) continue; + needleFound = true; + break; + } + if(!needleFound) break; + k++; + } + return k; +} + +void fileGetDirectory(char *file, char* buffer) { + char *c, *p; + int32_t i; + p = strrchr(file, FILE_PATH_SEP); + c = file; + i = 0; + do { + buffer[i++] = *c; + } while(++c < p); + buffer[i] = '\0'; +} + +bool fileListChildren( + char *directory, + char *buffer, + int32_t *count, + uint8_t *types, + char **children +) { + #if defined(_MSC_VER) + WIN32_FIND_DATA fdFile; + HANDLE hFind = NULL; + char sPath[2048]; + int32_t i; + + // Append wildcard + sprintf(sPath, "%s\\*.*", directory); + + // Scan first + if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) { + printf("Path not found: [%s]\n", directory); + return false; + } + + // Iterate + i = 0; + do { + if( + strcmp(fdFile.cFileName, ".") == 0 || + strcmp(fdFile.cFileName, "..") == 0 + ) continue; + + // Get Full path. + sprintf(sPath, "%s\\%s", directory, fdFile.cFileName); + + //Is the entity a File or Folder? + if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + types[i] = FILE_CHILD_TYPE_DIR; + } else { + types[i] = FILE_CHILD_TYPE_FILE; + } + + children[i] = buffer + (i * FILE_CHILD_NAME_MAX); + strcpy(children[i], fdFile.cFileName); + i++; + } while(FindNextFile(hFind, &fdFile)); + + *count = i; + return true; + #else + struct dirent *de; + DIR *dr; + int32_t i; + + // Open Dir + dr = opendir(directory); + if(dr == NULL) { + printf("Could not open directory"); + return false; + } + + + // Iterate + i = 0; + while ((de = readdir(dr)) != NULL) { + // File or folder? + if(de->d_type != DT_REG) continue; + + // Copy into child buffer + children[i] = buffer + (i * FILE_CHILD_NAME_MAX); + strcpy(children[i], de->d_name); + i++; + } + if(closedir(dr)) return false; + + *count = i; + return true; + #endif } \ No newline at end of file diff --git a/src/dawntools/utils/file.h b/src/dawntools/utils/file.hpp similarity index 95% rename from src/dawntools/utils/file.h rename to src/dawntools/utils/file.hpp index 3a6b0f66..2b761be6 100644 --- a/src/dawntools/utils/file.h +++ b/src/dawntools/utils/file.hpp @@ -1,75 +1,75 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "common.h" - -#define FILE_CHILD_TYPE_DIR 0x00 -#define FILE_CHILD_TYPE_FILE 0x01 -#define FILE_CHILD_NAME_MAX 512 -#define FILE_CHILD_COUNT_MAX 64 - -#if defined(_MSC_VER) - #include - #include - #define getcwd _getcwd - #define FILE_PATH_SEP '\\' - #define fileMkdir(path, perms) _mkdir(path) -#elif defined(__GNUC__) - #include - #include - #include - #define FILE_PATH_SEP '/' - #define fileMkdir(path, perms) mkdir(path, perms) -#endif - -void fileNormalizeSlashes(char *string); - -void fileMkdirp(char *path); - -size_t assetReadString(FILE *file, char *buffer); - -void fileGetDirectory(char *file, char* buffer); - -bool fileListChildren( - char *directory, - char *buffer, - int32_t *count, - uint8_t *types, - char **children -); - -/** - * Reads ahead to the first instance of the given character you provide. - * - * @param bufferIn Buffer to scan. - * @param start Start position within the buffer to scan from (inclusive). - * @param bufferOut Where to write the temporary data that was read ahead. - * @param needles Array of characters to scan for. - * @param needleCount How many elements are within the needles array. - * @return The count of characters skipped. - */ -int32_t readAhead( - char *bufferIn, int32_t start, - char *bufferOut, - char *needles, int32_t needleCount -); - - -/** - * Skips any characters found in the needles. - * - * @param bufferIn Buffer of chars to read. - * @param start Start of the buffer. - * @param needles Needles you are trying to skip. - * @param needleCount Count of needles in the needles array. - * @return The count of chars to skip ahead. - */ -int32_t skipAhead( - char *bufferIn, int32_t start, - char *needles, int32_t needleCount +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "common.hpp" + +#define FILE_CHILD_TYPE_DIR 0x00 +#define FILE_CHILD_TYPE_FILE 0x01 +#define FILE_CHILD_NAME_MAX 512 +#define FILE_CHILD_COUNT_MAX 64 + +#if defined(_MSC_VER) + #include + #include + #define getcwd _getcwd + #define FILE_PATH_SEP '\\' + #define fileMkdir(path, perms) _mkdir(path) +#elif defined(__GNUC__) + #include + #include + #include + #define FILE_PATH_SEP '/' + #define fileMkdir(path, perms) mkdir(path, perms) +#endif + +void fileNormalizeSlashes(char *string); + +void fileMkdirp(char *path); + +size_t assetReadString(FILE *file, char *buffer); + +void fileGetDirectory(char *file, char* buffer); + +bool fileListChildren( + char *directory, + char *buffer, + int32_t *count, + uint8_t *types, + char **children +); + +/** + * Reads ahead to the first instance of the given character you provide. + * + * @param bufferIn Buffer to scan. + * @param start Start position within the buffer to scan from (inclusive). + * @param bufferOut Where to write the temporary data that was read ahead. + * @param needles Array of characters to scan for. + * @param needleCount How many elements are within the needles array. + * @return The count of characters skipped. + */ +int32_t readAhead( + char *bufferIn, int32_t start, + char *bufferOut, + char *needles, int32_t needleCount +); + + +/** + * Skips any characters found in the needles. + * + * @param bufferIn Buffer of chars to read. + * @param start Start of the buffer. + * @param needles Needles you are trying to skip. + * @param needleCount Count of needles in the needles array. + * @return The count of chars to skip ahead. + */ +int32_t skipAhead( + char *bufferIn, int32_t start, + char *needles, int32_t needleCount ); \ No newline at end of file diff --git a/src/dawntools/utils/image.c b/src/dawntools/utils/image.cpp similarity index 95% rename from src/dawntools/utils/image.c rename to src/dawntools/utils/image.cpp index 2a350cc1..f4a4a50e 100644 --- a/src/dawntools/utils/image.c +++ b/src/dawntools/utils/image.cpp @@ -1,55 +1,55 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "image.h" - -#ifndef STB_IMAGE_IMPLEMENTATION - #define STB_IMAGE_IMPLEMENTATION - #include -#endif - -#ifndef STB_IMAGE_RESIZE_IMPLEMENTATION - #define STB_IMAGE_RESIZE_IMPLEMENTATION - #include -#endif - -#ifndef STB_IMAGE_WRITE_IMPLEMENTATION - #define STB_IMAGE_WRITE_IMPLEMENTATION - #include -#endif - -void imageCopy( - uint8_t *source, int32_t sourceWidth, int32_t sourceHeight, - uint8_t *dest, int32_t destWidth, int32_t destHeight, - int32_t cropX, int32_t cropY, int32_t cropWidth, int32_t cropHeight, - int32_t pasteX, int32_t pasteY, - int32_t channels -) { - int32_t x, y, c; - int32_t absX, absY; - int32_t sourceIndex, targetIndex; - - if(cropX == -1) cropX = 0; - if(cropY == -1) cropY = 0; - if(cropWidth == -1) cropWidth = sourceWidth; - if(cropHeight == -1) cropHeight = sourceHeight; - if(pasteX == -1) pasteX = 0; - if(pasteY == -1) pasteY = 0; - - for(x = cropX; x < cropX + cropWidth; x++) { - for(y = cropY; y < cropY + cropHeight; y++) { - absX = x - cropX + pasteX; - absY = y - cropY + pasteY; - if(absX >= destWidth || absY >= destHeight || absX < 0 || absY < 0)continue; - targetIndex = absY * destWidth + absX; - sourceIndex = y * sourceWidth + x; - for(c = 0; c < channels; c++) { - dest[(targetIndex*channels) + c] = source[(sourceIndex*channels) + c]; - } - } - } +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "image.hpp" + +#ifndef STB_IMAGE_IMPLEMENTATION + #define STB_IMAGE_IMPLEMENTATION + #include +#endif + +#ifndef STB_IMAGE_RESIZE_IMPLEMENTATION + #define STB_IMAGE_RESIZE_IMPLEMENTATION + #include +#endif + +#ifndef STB_IMAGE_WRITE_IMPLEMENTATION + #define STB_IMAGE_WRITE_IMPLEMENTATION + #include +#endif + +void imageCopy( + uint8_t *source, int32_t sourceWidth, int32_t sourceHeight, + uint8_t *dest, int32_t destWidth, int32_t destHeight, + int32_t cropX, int32_t cropY, int32_t cropWidth, int32_t cropHeight, + int32_t pasteX, int32_t pasteY, + int32_t channels +) { + int32_t x, y, c; + int32_t absX, absY; + int32_t sourceIndex, targetIndex; + + if(cropX == -1) cropX = 0; + if(cropY == -1) cropY = 0; + if(cropWidth == -1) cropWidth = sourceWidth; + if(cropHeight == -1) cropHeight = sourceHeight; + if(pasteX == -1) pasteX = 0; + if(pasteY == -1) pasteY = 0; + + for(x = cropX; x < cropX + cropWidth; x++) { + for(y = cropY; y < cropY + cropHeight; y++) { + absX = x - cropX + pasteX; + absY = y - cropY + pasteY; + if(absX >= destWidth || absY >= destHeight || absX < 0 || absY < 0)continue; + targetIndex = absY * destWidth + absX; + sourceIndex = y * sourceWidth + x; + for(c = 0; c < channels; c++) { + dest[(targetIndex*channels) + c] = source[(sourceIndex*channels) + c]; + } + } + } } \ No newline at end of file diff --git a/src/dawntools/utils/image.h b/src/dawntools/utils/image.hpp similarity index 88% rename from src/dawntools/utils/image.h rename to src/dawntools/utils/image.hpp index 040d5189..99264a94 100644 --- a/src/dawntools/utils/image.h +++ b/src/dawntools/utils/image.hpp @@ -1,22 +1,22 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "common.h" -#include "file.h" - -#include -#include -#include - -void imageCopy( - uint8_t *source, int32_t sourceWidth, int32_t sourceHeight, - uint8_t *dest, int32_t destWidth, int32_t destHeight, - int32_t cropX, int32_t cropY, int32_t cropWidth, int32_t cropHeight, - int32_t pasteX, int32_t pasteY, - int32_t channels +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "common.hpp" +#include "file.hpp" + +#include +#include +#include + +void imageCopy( + uint8_t *source, int32_t sourceWidth, int32_t sourceHeight, + uint8_t *dest, int32_t destWidth, int32_t destHeight, + int32_t cropX, int32_t cropY, int32_t cropWidth, int32_t cropHeight, + int32_t pasteX, int32_t pasteY, + int32_t channels ); \ No newline at end of file diff --git a/src/dawntools/utils/string.h b/src/dawntools/utils/string.hpp similarity index 90% rename from src/dawntools/utils/string.h rename to src/dawntools/utils/string.hpp index a6811ae7..d60e4eb8 100644 --- a/src/dawntools/utils/string.h +++ b/src/dawntools/utils/string.hpp @@ -1,30 +1,30 @@ -/** - * Copyright (c) 2023 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "common.h" - -static inline void stringRemoveAll(char *string, char remove) { - size_t len = strlen(string); - size_t i, j; - - i = 0; - while(i < len) { - char c = string[i]; - if(c != remove) { - i++; - continue; - } - - j = i + 1; - while(j < len) { - string[j-1] = string[j]; - j++; - } - len--; - } +/** + * Copyright (c) 2023 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "common.hpp" + +static inline void stringRemoveAll(char *string, char remove) { + size_t len = strlen(string); + size_t i, j; + + i = 0; + while(i < len) { + char c = string[i]; + if(c != remove) { + i++; + continue; + } + + j = i + 1; + while(j < len) { + string[j-1] = string[j]; + j++; + } + len--; + } } \ No newline at end of file diff --git a/src/dawntools/utils/xml.c b/src/dawntools/utils/xml.cpp similarity index 89% rename from src/dawntools/utils/xml.c rename to src/dawntools/utils/xml.cpp index 6031551d..d6d78233 100644 --- a/src/dawntools/utils/xml.c +++ b/src/dawntools/utils/xml.cpp @@ -5,7 +5,7 @@ * https://opensource.org/licenses/MIT */ -#include "xml.h" +#include "xml.hpp" int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) { char c; @@ -13,13 +13,13 @@ int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) { uint8_t doing = XML_DOING_NOTHING; uint8_t doingBeforeComment; bool insideTag = false; - char* buffer = malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); + char* buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); int32_t bufferLength = 0; xml->value = NULL; xml->attributeCount = 0; - xml->children = malloc(sizeof(xml_t) * XML_CHILD_COUNT_MAX); + xml->children = (xml_t *)malloc(sizeof(xml_t) * XML_CHILD_COUNT_MAX); xml->childrenCount = 0; while(c = data[i++]) { @@ -54,7 +54,7 @@ int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) { if(xmlIsWhitespace(c) || c == '>' || c == '/') { buffer[bufferLength] = '\0'; xml->node = buffer; - buffer = malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); + buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); bufferLength = 0; if(c == '/') { level--; @@ -88,7 +88,7 @@ int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) { buffer[bufferLength] = '\0'; xml->attributeNames[xml->attributeCount++] = buffer; xml->attributeDatas[xml->attributeCount] = NULL; - buffer = malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); + buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); bufferLength = 0; } continue; @@ -116,7 +116,7 @@ int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) { doing = XML_LOOKING_FOR_ATTRIBUTE; buffer[bufferLength] = '\0'; xml->attributeDatas[xml->attributeCount - 1] = buffer; - buffer = malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); + buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); bufferLength = 0; continue; } @@ -132,7 +132,7 @@ int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) { buffer[bufferLength] = '\0'; bufferLength = 0; xml->value = buffer; - buffer = malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); + buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX); continue; } diff --git a/src/dawntools/utils/xml.h b/src/dawntools/utils/xml.hpp similarity index 93% rename from src/dawntools/utils/xml.h rename to src/dawntools/utils/xml.hpp index b05cb307..28b9a80c 100644 --- a/src/dawntools/utils/xml.h +++ b/src/dawntools/utils/xml.hpp @@ -6,8 +6,8 @@ */ #pragma once -#include "common.h" -#include "file.h" +#include "common.hpp" +#include "file.hpp" #define XML_DOING_NOTHING 0x00 #define XML_PARSING_TAG_NAME 0x01 diff --git a/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt b/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt index 945f6059..53f96ddf 100644 --- a/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt +++ b/src/dawntools/visualnovel/vnscenegen/CMakeLists.txt @@ -9,11 +9,12 @@ add_executable(vnscenegen) target_sources(vnscenegen PRIVATE main.cpp - ../../utils/file.c - ../../utils/xml.c + ../../utils/file.cpp + ../../utils/xml.cpp ) target_include_directories(vnscenegen PUBLIC + ${DAWN_SHARED_INCLUDES} ${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/src/dawntools/visualnovel/vnscenegen/main.cpp b/src/dawntools/visualnovel/vnscenegen/main.cpp index 26435b24..66e06a69 100644 --- a/src/dawntools/visualnovel/vnscenegen/main.cpp +++ b/src/dawntools/visualnovel/vnscenegen/main.cpp @@ -3,11 +3,8 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -extern "C" { - #include "../../utils/file.h" - #include "../../utils/xml.h" - #include -} +#include "../../utils/file.hpp" +#include "../../utils/xml.hpp" #include #include