]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Add new GString functions
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Mon, 29 Aug 2016 01:24:12 +0000 (21:24 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Mon, 29 Aug 2016 01:24:12 +0000 (21:24 -0400)
src/GLibFacade.c
src/GLibFacade.h

index f282033815e6acb7600437243944c69be718d352..572dab11163db61f06f49a19cb604ee1a7fb80e4 100644 (file)
@@ -2,12 +2,32 @@
  *     GLibFacade.c
  *     MultiMarkdown
  *     
- *             https://github.com/fletcher/MultiMarkdown-4/
- *
  *     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.
- *     Copyright 2011 __MyCompanyName__. All rights reserved.
+ *
+ *     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"
@@ -57,7 +77,7 @@ int asprintf( char **sptr, char *fmt, ... )
 #define kStringBufferStartingSize 1024
 #define kStringBufferGrowthMultiplier 2
 
-GString* g_string_new(char *startingString)
+GString* g_string_new(const char *startingString)
 {
        GString* newString = malloc(sizeof(GString));
 
@@ -81,6 +101,9 @@ GString* g_string_new(char *startingString)
 
 char* g_string_free(GString* ripString, bool freeCharacterData)
 {      
+       if (ripString == NULL)
+               return NULL;
+       
        char* returnedString = ripString->str;
        if (freeCharacterData)
        {
@@ -108,7 +131,16 @@ static void ensureStringBufferCanHold(GString* baseString, size_t newStringSize)
                        newBufferSize *= kStringBufferGrowthMultiplier;
                }
                
-               baseString->str = realloc(baseString->str, newBufferSize);
+        char *temp;
+        temp = realloc(baseString->str, newBufferSize);
+        
+        if (temp == NULL) {
+            /* realloc failed */
+            fprintf(stderr, "error reallocating memory\n");
+
+            exit(1);
+        }
+               baseString->str = temp;
                baseString->currentStringBufferSize = newBufferSize;
        }
 }
@@ -178,6 +210,74 @@ void g_string_prepend(GString* baseString, char* prependedString)
        }
 }
 
+void g_string_insert(GString* baseString, size_t pos, char * insertedString)
+{
+       if ((insertedString != NULL) && (strlen(insertedString) > 0))
+       {
+               if (pos > baseString->currentStringLength)
+                       pos = baseString->currentStringLength;
+               
+               size_t insertedStringLength = strlen(insertedString);
+               size_t newStringLength = baseString->currentStringLength + insertedStringLength;
+               ensureStringBufferCanHold(baseString, newStringLength);
+               
+               /* Shift following string to 'right' */
+               memmove(baseString->str + pos + insertedStringLength, baseString->str + pos, baseString->currentStringLength - pos);
+               strncpy(baseString->str + pos, insertedString, insertedStringLength);
+               baseString->currentStringLength = newStringLength;
+               baseString->str[baseString->currentStringLength] = '\0';
+       }
+}
+
+void g_string_insert_c(GString* baseString, size_t pos, char insertedCharacter)
+{      
+       if (pos > baseString->currentStringLength)
+               pos = baseString->currentStringLength;
+       
+       size_t newSizeNeeded = baseString->currentStringLength + 1;
+       ensureStringBufferCanHold(baseString, newSizeNeeded);
+       
+       /* Shift following string to 'right' */
+       memmove(baseString->str + pos + 1, baseString->str + pos, baseString->currentStringLength - pos);
+       
+       baseString->str[pos] = insertedCharacter;
+       baseString->currentStringLength++;      
+       baseString->str[baseString->currentStringLength] = '\0';
+}
+
+
+void g_string_insert_printf(GString* baseString, size_t pos, char* format, ...)
+{
+       va_list args;
+       va_start(args, format);
+       
+       char* formattedString = NULL;
+       vasprintf(&formattedString, format, args);
+       if (formattedString != NULL)
+       {
+               g_string_insert(baseString, pos, formattedString);
+               free(formattedString);
+       }
+       va_end(args);
+}
+
+void g_string_erase(GString* baseString, size_t pos, size_t len)
+{
+       if ((pos > baseString->currentStringLength) || (len <= 0))
+               return;
+       
+       if ((pos + len) >= baseString->currentStringLength) 
+               len = -1;
+       
+       if (len == -1) {
+               baseString->currentStringLength = pos;
+       } else {
+               memmove(baseString->str + pos, baseString->str + pos + len, baseString->currentStringLength - pos - len);
+               baseString->currentStringLength -= len;
+       }
+       baseString->str[baseString->currentStringLength] = '\0';
+}
+
 /* GSList */
 
 void g_slist_free(GSList* ripList)
@@ -219,3 +319,4 @@ GSList* g_slist_prepend(GSList* targetElement, void* newElementData)
        newElement->next = targetElement;
        return newElement;
 }
+
index f8b2dd236e0de7fa78d1b088909c69142394eec4..5a0cabf57a1ffb9dc79f1602aa2e38ea8e11678f 100644 (file)
@@ -2,13 +2,32 @@
  *     GLibFacade.h
  *     MultiMarkdown
  *     
- *             https://github.com/fletcher/MultiMarkdown-4/
- *
  *     Created by Daniel Jalkut on 7/26/11.
- *     Copyright 2011 __MyCompanyName__. All rights reserved.
+ *     Modified by Fletcher T. Penney 9/15/11 - 5/6/16.
+ *             Changes Copyright 2011-2016
+ *     Modified by Dan Lowe on 1/3/12.
  *
- *     Modified by Fletcher T. Penney
- *     Changes Copyright 2011-2016
+ *     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__
@@ -26,13 +45,14 @@ 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
 
-/** @brief Minimal replacement for GLib's GString class.
- *  
- *
- * WE implement minimal mirror implementations of GLib's GString and GSList 
+/* 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 
@@ -46,11 +66,11 @@ typedef struct
 
        /* Where in the str buffer will we add new characters */
        /* or append new strings? */
-       int currentStringBufferSize;
-       int currentStringLength;
+       unsigned long currentStringBufferSize;
+       unsigned long currentStringLength;
 } GString;
 
-GString* g_string_new(char *startingString);
+GString* g_string_new(const char *startingString);
 char* g_string_free(GString* ripString, bool freeCharacterData);
 
 void g_string_append_c(GString* baseString, char appendedCharacter);
@@ -61,6 +81,12 @@ 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