From 98dbfff09a1a0c2e41bad79565cdd0c642763316 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Fri, 2 Dec 2016 16:10:43 -0500 Subject: [PATCH] CHANGED: Refactor GLibFacade to d_string; Use Xcode specific build directories --- CMakeLists.txt | 12 +-- Makefile | 20 +++- README.md | 4 +- src/GLibFacade.h | 102 ------------------- src/{GLibFacade.c => d_string.c} | 162 ++++++++++++++----------------- src/d_string.h | 100 +++++++++++++++++++ src/main.c | 14 +-- 7 files changed, 205 insertions(+), 209 deletions(-) delete mode 100644 src/GLibFacade.h rename src/{GLibFacade.c => d_string.c} (65%) create mode 100644 src/d_string.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d0ed63..9e63c00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,10 +8,10 @@ cmake_minimum_required (VERSION 2.6) set (My_Project_Title "C-Template") set (My_Project_Description "Boilerplate c project with cmake support, CuTest unit testing, and more.") set (My_Project_Author "Fletcher T. Penney") -set (My_Project_Revised_Date "2016-08-24") +set (My_Project_Revised_Date "2016-12-02") set (My_Project_Version_Major 1) set (My_Project_Version_Minor 0) -set (My_Project_Version_Patch 3) +set (My_Project_Version_Patch 4) set (My_Project_Version "${My_Project_Version_Major}.${My_Project_Version_Minor}.${My_Project_Version_Patch}") @@ -169,11 +169,11 @@ set(public_header_files # Utility source files will not be included in doxygen set(src_utility_files -# src/GLibFacade.c +# src/d_string.c ) set(header_utility_files -# src/GLibFacade.h +# src/d_string.h ${PROJECT_BINARY_DIR}/version.h ) @@ -304,8 +304,8 @@ endif (WIN32) # if (NOT DEFINED TEST) # add_executable(main # src/main.c -# src/GLibFacade.c -# src/GLibFacade.h +# src/d_string.c +# src/d_string.h # ${header_files} # ) # diff --git a/Makefile b/Makefile index bcc835e..13802fc 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ BUILD_DIR = build +XCODE_BUILD_DIR = build-xcode +XCODE_DEBUG_BUILD_DIR = build-xcode-debug # The release target will perform additional optimization .PHONY : release @@ -32,10 +34,15 @@ analyze: $(BUILD_DIR) # xcodebuild -configuration Debug # xcodebuild -configuration Release .PHONY : xcode -xcode: $(BUILD_DIR) - cd $(BUILD_DIR); \ +xcode: $(XCOD_BUILD_DIR) + cd $(XCOD_BUILD_DIR); \ cmake -G Xcode .. +.PHONY : xcode-debug +xcode-debug: $(XCODE_DEBUG_BUILD_DIR) + cd $(XCODE_DEBUG_BUILD_DIR); \ + cmake -G Xcode -DTEST=1 .. + # Cross-compile for Windows using MinGW on *nix .PHONY : windows windows: $(BUILD_DIR) @@ -81,6 +88,15 @@ $(BUILD_DIR): CHANGELOG -mkdir $(BUILD_DIR) 2>/dev/null -cd $(BUILD_DIR); rm -rf * +# Build xcode directories if they don't exist +$(XCODE_BUILD_DIR): + -mkdir $(XCODE_BUILD_DIR) 2>/dev/null + -cd $(XCODE_BUILD_DIR); rm -rf * + +$(XCODE_DEBUG_BUILD_DIR): + -mkdir $(XCODE_DEBUG_BUILD_DIR) 2>/dev/null + -cd $(XCODE_DEBUG_BUILD_DIR); rm -rf * + # Generate a list of changes since last commit to 'master' branch .PHONY : CHANGELOG CHANGELOG: diff --git a/README.md b/README.md index 648068d..eeb13da 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ | ---------- | ------------------------- | | Title: | C-Template | | Author: | Fletcher T. Penney | -| Date: | 2016-08-24 | +| Date: | 2016-12-02 | | Copyright: | Copyright © 2015-2016 Fletcher T. Penney. | -| Version: | 1.0.3 | +| Version: | 1.0.4 | ## Introduction ## diff --git a/src/GLibFacade.h b/src/GLibFacade.h deleted file mode 100644 index 5a0cabf..0000000 --- a/src/GLibFacade.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * GLibFacade.h - * MultiMarkdown - * - * Created by Daniel Jalkut on 7/26/11. - * Modified by Fletcher T. Penney 9/15/11 - 5/6/16. - * Changes Copyright 2011-2016 - * Modified by Dan Lowe on 1/3/12. - * - * License for original code by Daniel Jalkut: - * - * Copyright 2011 Daniel Jalkut. All rights reserved. - * - * 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. - */ - -#ifndef __MARKDOWN_GLIB_FACADE__ -#define __MARKDOWN_GLIB_FACADE__ - -/* peg_markdown uses the link symbol for its own purposes */ -#define link MARKDOWN_LINK_IGNORED -#include -#undef link - -#include -#include - -typedef int gboolean; -typedef char gchar; - -/* This style of bool is used in shared source code */ -#if !defined(FALSE) -#define FALSE false -#endif -#if !defined(TRUE) -#define TRUE true -#endif - -/* WE implement minimal mirror implementations of GLib's GString and GSList - * sufficient to cover the functionality required by MultiMarkdown. - * - * NOTE: THese are 100% clean, from-scratch implementations using only the - * GLib function prototype as guide for behavior. - */ - -typedef struct -{ - /* Current UTF8 byte stream this string represents */ - char* str; - - /* Where in the str buffer will we add new characters */ - /* or append new strings? */ - unsigned long currentStringBufferSize; - unsigned long currentStringLength; -} GString; - -GString* g_string_new(const char *startingString); -char* g_string_free(GString* ripString, bool freeCharacterData); - -void g_string_append_c(GString* baseString, char appendedCharacter); -void g_string_append_c_array(GString *baseString, char * appendedChars, size_t bytes); -void g_string_append(GString* baseString, char *appendedString); - -void g_string_prepend(GString* baseString, char* prependedString); - -void g_string_append_printf(GString* baseString, char* format, ...); - -void g_string_insert(GString* baseString, size_t pos, char * insertedString); -void g_string_insert_c(GString* baseString, size_t pos, char insertedCharacter); -void g_string_insert_printf(GString* baseString, size_t pos, char* format, ...); - -void g_string_erase(GString* baseString, size_t pos, size_t len); - -/* Just implement a very simple singly linked list. */ - -typedef struct _GSList -{ - void* data; - struct _GSList* next; -} GSList; - -void g_slist_free(GSList* ripList); -GSList* g_slist_prepend(GSList* targetElement, void* newElementData); -GSList* g_slist_reverse(GSList* theList); - -#endif diff --git a/src/GLibFacade.c b/src/d_string.c similarity index 65% rename from src/GLibFacade.c rename to src/d_string.c index 572dab1..93d8654 100644 --- a/src/GLibFacade.c +++ b/src/d_string.c @@ -1,42 +1,67 @@ +/** + + Smart String -- Library to abstract smart typing features from MMD Composer + + @file d_string.c + + @brief Dynamic string -- refactoring of old GLibFacade + + + @author Daniel Jalkut, modified by Fletcher T. Penney and Dan Lowe + @bug + +**/ + /* - * GLibFacade.c - * MultiMarkdown - * - * Created by Daniel Jalkut on 7/26/11. - * Modified by Fletcher T. Penney 9/15/11 - 5/6/16. - * Changes Copyright 2011-2016 - * Modified by Dan Lowe on 1/3/12. - * - * License for original code by Daniel Jalkut: - * - * Copyright 2011 Daniel Jalkut. All rights reserved. - * - * 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 "GLibFacade.h" + Copyright © 2011 Daniel Jalkut. + Modifications by Fletcher T. Penney, Copyright © 2011-2016 Fletcher T. Penney. + Modifications by Dan Lowe, Copyright © 2011 Dan Lowe. + + + The `c-template` project is released under the MIT License. + + GLibFacade.c and GLibFacade.h are from the MultiMarkdown v4 project: + + https://github.com/fletcher/MultiMarkdown-4/ + + MMD 4 is released under both the MIT License and GPL. + + + CuTest is released under the zlib/libpng license. See CuTest.c for the text + of the license. + + + ## The MIT License ## + + 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 #include #include #include +#include "d_string.h" + + /* * The following section came from: * @@ -72,14 +97,14 @@ int asprintf( char **sptr, char *fmt, ... ) #endif -/* GString */ +/* DString */ #define kStringBufferStartingSize 1024 #define kStringBufferGrowthMultiplier 2 -GString* g_string_new(const char *startingString) +DString* d_string_new(const char *startingString) { - GString* newString = malloc(sizeof(GString)); + DString* newString = malloc(sizeof(DString)); if (startingString == NULL) startingString = ""; @@ -99,7 +124,7 @@ GString* g_string_new(const char *startingString) return newString; } -char* g_string_free(GString* ripString, bool freeCharacterData) +char* d_string_free(DString* ripString, bool freeCharacterData) { if (ripString == NULL) return NULL; @@ -119,7 +144,7 @@ char* g_string_free(GString* ripString, bool freeCharacterData) return returnedString; } -static void ensureStringBufferCanHold(GString* baseString, size_t newStringSize) +static void ensureStringBufferCanHold(DString* baseString, size_t newStringSize) { size_t newBufferSizeNeeded = newStringSize + 1; if (newBufferSizeNeeded > baseString->currentStringBufferSize) @@ -145,7 +170,7 @@ static void ensureStringBufferCanHold(GString* baseString, size_t newStringSize) } } -void g_string_append(GString* baseString, char* appendedString) +void d_string_append(DString* baseString, char* appendedString) { if ((appendedString != NULL) && (strlen(appendedString) > 0)) { @@ -159,7 +184,7 @@ void g_string_append(GString* baseString, char* appendedString) } } -void g_string_append_c(GString* baseString, char appendedCharacter) +void d_string_append_c(DString* baseString, char appendedCharacter) { size_t newSizeNeeded = baseString->currentStringLength + 1; ensureStringBufferCanHold(baseString, newSizeNeeded); @@ -169,7 +194,7 @@ void g_string_append_c(GString* baseString, char appendedCharacter) baseString->str[baseString->currentStringLength] = '\0'; } -void g_string_append_c_array(GString *baseString, char * appendedChars, size_t bytes) +void d_string_append_c_array(DString *baseString, const char * appendedChars, size_t bytes) { size_t newSizeNeeded = baseString->currentStringLength + bytes; ensureStringBufferCanHold(baseString, newSizeNeeded); @@ -180,7 +205,7 @@ void g_string_append_c_array(GString *baseString, char * appendedChars, size_t b baseString->str[baseString->currentStringLength] = '\0'; } -void g_string_append_printf(GString* baseString, char* format, ...) +void d_string_append_printf(DString* baseString, char* format, ...) { va_list args; va_start(args, format); @@ -189,13 +214,13 @@ void g_string_append_printf(GString* baseString, char* format, ...) vasprintf(&formattedString, format, args); if (formattedString != NULL) { - g_string_append(baseString, formattedString); + d_string_append(baseString, formattedString); free(formattedString); } va_end(args); } -void g_string_prepend(GString* baseString, char* prependedString) +void d_string_prepend(DString* baseString, char* prependedString) { if ((prependedString != NULL) && (strlen(prependedString) > 0)) { @@ -210,7 +235,7 @@ void g_string_prepend(GString* baseString, char* prependedString) } } -void g_string_insert(GString* baseString, size_t pos, char * insertedString) +void d_string_insert(DString* baseString, size_t pos, const char * insertedString) { if ((insertedString != NULL) && (strlen(insertedString) > 0)) { @@ -229,7 +254,7 @@ void g_string_insert(GString* baseString, size_t pos, char * insertedString) } } -void g_string_insert_c(GString* baseString, size_t pos, char insertedCharacter) +void d_string_insert_c(DString* baseString, size_t pos, char insertedCharacter) { if (pos > baseString->currentStringLength) pos = baseString->currentStringLength; @@ -246,7 +271,7 @@ void g_string_insert_c(GString* baseString, size_t pos, char insertedCharacter) } -void g_string_insert_printf(GString* baseString, size_t pos, char* format, ...) +void d_string_insert_printf(DString* baseString, size_t pos, char* format, ...) { va_list args; va_start(args, format); @@ -255,13 +280,13 @@ void g_string_insert_printf(GString* baseString, size_t pos, char* format, ...) vasprintf(&formattedString, format, args); if (formattedString != NULL) { - g_string_insert(baseString, pos, formattedString); + d_string_insert(baseString, pos, formattedString); free(formattedString); } va_end(args); } -void g_string_erase(GString* baseString, size_t pos, size_t len) +void d_string_erase(DString* baseString, size_t pos, size_t len) { if ((pos > baseString->currentStringLength) || (len <= 0)) return; @@ -277,46 +302,3 @@ void g_string_erase(GString* baseString, size_t pos, size_t len) } baseString->str[baseString->currentStringLength] = '\0'; } - -/* GSList */ - -void g_slist_free(GSList* ripList) -{ - GSList* thisListItem = ripList; - while (thisListItem != NULL) - { - GSList* nextItem = thisListItem->next; - - /* I guess we don't release the data? Non-retained memory management is hard... let's figure it out later. */ - free(thisListItem); - - thisListItem = nextItem; - } -} - -/* Currently only used for markdown_output.c endnotes printing */ -GSList* g_slist_reverse(GSList* theList) -{ - GSList* lastNodeSeen = NULL; - - /* Iterate the list items, tacking them on to our new reversed List as we find them */ - GSList* listWalker = theList; - while (listWalker != NULL) - { - GSList* nextNode = listWalker->next; - listWalker->next = lastNodeSeen; - lastNodeSeen = listWalker; - listWalker = nextNode; - } - - return lastNodeSeen; -} - -GSList* g_slist_prepend(GSList* targetElement, void* newElementData) -{ - GSList* newElement = malloc(sizeof(GSList)); - newElement->data = newElementData; - newElement->next = targetElement; - return newElement; -} - diff --git a/src/d_string.h b/src/d_string.h new file mode 100644 index 0000000..79dfb67 --- /dev/null +++ b/src/d_string.h @@ -0,0 +1,100 @@ +/** + + Smart String -- Library to abstract smart typing features from MMD Composer + + @file d_string.h + + @brief Dynamic string -- refactoring of old GLibFacade + + + @author Daniel Jalkut, modified by Fletcher T. Penney and Dan Lowe + @bug + +**/ + +/* + + Copyright © 2011 Daniel Jalkut. + Modifications by Fletcher T. Penney, Copyright © 2011-2016 Fletcher T. Penney. + Modifications by Dan Lowe, Copyright © 2011 Dan Lowe. + + + The `c-template` project is released under the MIT License. + + GLibFacade.c and GLibFacade.h are from the MultiMarkdown v4 project: + + https://github.com/fletcher/MultiMarkdown-4/ + + MMD 4 is released under both the MIT License and GPL. + + + CuTest is released under the zlib/libpng license. See CuTest.c for the text + of the license. + + + ## The MIT License ## + + 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. + +*/ + + +#ifndef D_STRING_SMART_STRING_H +#define D_STRING_SMART_STRING_H + +#include + +/* WE implement minimal mirror implementations of GLib's GString + * sufficient to cover the functionality required by MultiMarkdown. + * + * NOTE: THese are 100% clean, from-scratch implementations using only the + * GLib function prototype as guide for behavior. + */ + +typedef struct +{ + /* Current UTF8 byte stream this string represents */ + char* str; + + /* Where in the str buffer will we add new characters */ + /* or append new strings? */ + unsigned long currentStringBufferSize; + unsigned long currentStringLength; +} DString; + +DString* d_string_new(const char *startingString); + +char* d_string_free(DString* ripString, bool freeCharacterData); + +void d_string_append_c(DString* baseString, char appendedCharacter); +void d_string_append_c_array(DString *baseString, const char * appendedChars, size_t bytes); +void d_string_append(DString* baseString, char *appendedString); + +void d_string_prepend(DString* baseString, char* prependedString); + +void d_string_append_printf(DString* baseString, char* format, ...); + +void d_string_insert(DString* baseString, size_t pos, const char * insertedString); +void d_string_insert_c(DString* baseString, size_t pos, char insertedCharacter); +void d_string_insert_printf(DString* baseString, size_t pos, char* format, ...); + +void d_string_erase(DString* baseString, size_t pos, size_t len); + + +#endif diff --git a/src/main.c b/src/main.c index 270d19b..7d8ab53 100644 --- a/src/main.c +++ b/src/main.c @@ -24,21 +24,21 @@ #include #include -#include "GLibFacade.h" +#include "d_string.h" #define kBUFFERSIZE 4096 // How many bytes to read at a time -GString * stdin_buffer() { +DString * stdin_buffer() { /* Read from stdin and return a GString * `buffer` will need to be freed elsewhere */ char chunk[kBUFFERSIZE]; size_t bytes; - GString * buffer = g_string_new(""); + DString * buffer = d_string_new(""); while ((bytes = fread(chunk, 1, kBUFFERSIZE, stdin)) > 0) { - g_string_append_c_array(buffer, chunk, bytes); + d_string_append_c_array(buffer, chunk, bytes); } fclose(stdin); @@ -46,7 +46,7 @@ GString * stdin_buffer() { return buffer; } -GString * scan_file(char * fname) { +DString * scan_file(char * fname) { /* Read from stdin and return a GString * `buffer` will need to be freed elsewhere */ @@ -59,10 +59,10 @@ GString * scan_file(char * fname) { return NULL; } - GString * buffer = g_string_new(""); + DString * buffer = d_string_new(""); while ((bytes = fread(chunk, 1, kBUFFERSIZE, file)) > 0) { - g_string_append_c_array(buffer, chunk, bytes); + d_string_append_c_array(buffer, chunk, bytes); } fclose(file); -- 2.40.0