RetroShare/supportlibs/pegmarkdown/GLibFacade.c

210 lines
5.5 KiB
C
Raw Normal View History

/*
* GLibFacade.c
* MultiMarkdown
*
* Created by Daniel Jalkut on 7/26/11.
* Modified by Fletcher T. Penney on 9/15/11.
* Modified by Dan Lowe on 1/3/12.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*/
#include "GLibFacade.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
/*
* The following section came from:
*
* http://lists-archives.org/mingw-users/12649-asprintf-missing-vsnprintf-
* behaving-differently-and-_vsncprintf-undefined.html
*
* and
*
* http://groups.google.com/group/jansson-users/browse_thread/thread/
* 76a88d63d9519978/041a7d0570de2d48?lnk=raot
*/
/* Solaris and Windows do not provide vasprintf() or asprintf(). */
#if (defined(__WIN32) && !defined(__MINGW64_VERSION_MAJOR)) || (defined(__SVR4) && defined(__sun))
int vasprintf( char **sptr, char *fmt, va_list argv )
{
int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
if( (wanted > 0) && ((*sptr = malloc( 1 + wanted )) != NULL) )
return vsprintf( *sptr, fmt, argv );
return wanted;
}
int asprintf( char **sptr, char *fmt, ... )
{
int retval;
va_list argv;
va_start( argv, fmt );
retval = vasprintf( sptr, fmt, argv );
va_end( argv );
return retval;
}
#endif
/* GString */
#define kStringBufferStartingSize 1024
#define kStringBufferGrowthMultiplier 2
GString* g_string_new(char *startingString)
{
GString* newString = malloc(sizeof(GString));
if (startingString == NULL) startingString = "";
size_t startingBufferSize = kStringBufferStartingSize;
size_t startingStringSize = strlen(startingString);
while (startingBufferSize < (startingStringSize + 1))
{
startingBufferSize *= kStringBufferGrowthMultiplier;
}
newString->str = malloc(startingBufferSize);
newString->currentStringBufferSize = startingBufferSize;
strncpy(newString->str, startingString, startingStringSize);
newString->str[startingStringSize] = '\0';
newString->currentStringLength = startingStringSize;
return newString;
}
char* g_string_free(GString* ripString, bool freeCharacterData)
{
char* returnedString = ripString->str;
if (freeCharacterData)
{
if (ripString->str != NULL)
{
free(ripString->str);
}
returnedString = NULL;
}
free(ripString);
return returnedString;
}
static void ensureStringBufferCanHold(GString* baseString, size_t newStringSize)
{
size_t newBufferSizeNeeded = newStringSize + 1;
if (newBufferSizeNeeded > baseString->currentStringBufferSize)
{
size_t newBufferSize = baseString->currentStringBufferSize;
while (newBufferSizeNeeded > newBufferSize)
{
newBufferSize *= kStringBufferGrowthMultiplier;
}
baseString->str = realloc(baseString->str, newBufferSize);
baseString->currentStringBufferSize = newBufferSize;
}
}
void g_string_append(GString* baseString, char* appendedString)
{
if ((appendedString != NULL) && (strlen(appendedString) > 0))
{
size_t appendedStringLength = strlen(appendedString);
size_t newStringLength = baseString->currentStringLength + appendedStringLength;
ensureStringBufferCanHold(baseString, newStringLength);
/* We already know where the current string ends, so pass that as the starting address for strncat */
strncat(baseString->str + baseString->currentStringLength, appendedString, appendedStringLength);
baseString->currentStringLength = newStringLength;
}
}
void g_string_append_c(GString* baseString, char appendedCharacter)
{
size_t newSizeNeeded = baseString->currentStringLength + 1;
ensureStringBufferCanHold(baseString, newSizeNeeded);
baseString->str[baseString->currentStringLength] = appendedCharacter;
baseString->currentStringLength++;
baseString->str[baseString->currentStringLength] = '\0';
}
void g_string_append_printf(GString* baseString, char* format, ...)
{
va_list args;
va_start(args, format);
char* formattedString = NULL;
vasprintf(&formattedString, format, args);
if (formattedString != NULL)
{
g_string_append(baseString, formattedString);
free(formattedString);
}
Fix error reported by CppChecker: /libretroshare/src/gxs/rsgenexchange.cc:2850: erreur : Cppcheck: Dereferencing 'grp' after it is deallocated / released /libretroshare/src/gxstunnel/p3gxstunnel.cc:1004: erreur : Cppcheck: Invalid usage of output stream: '<< std::cerr'. /libretroshare/src/util/contentvalue.cc:185: erreur : Cppcheck: Memory leak: dest /openpgpsdk/src/openpgpsdk/reader_armoured.c:575: erreur : Cppcheck: Common realloc mistake: 'buf' nulled but not freed upon failure /retroshare-gui/src/gui/FileTransfer/DLListDelegate.cpp:103: erreur : Cppcheck: Array 'byteUnits[4]' accessed at index 4, which is out of bounds. /retroshare-gui/src/gui/FileTransfer/DLListDelegate.cpp:122: erreur : Cppcheck: Array 'byteUnits[4]' accessed at index 4, which is out of bounds. /retroshare-gui/src/gui/FileTransfer/DLListDelegate.cpp:141: erreur : Cppcheck: Array 'byteUnits[4]' accessed at index 4, which is out of bounds. /retroshare-gui/src/gui/FileTransfer/ULListDelegate.cpp:92: erreur : Cppcheck: Array 'byteUnits[4]' accessed at index 4, which is out of bounds. /retroshare-gui/src/gui/FileTransfer/ULListDelegate.cpp:111: erreur : Cppcheck: Array 'byteUnits[4]' accessed at index 4, which is out of bounds. /retroshare-gui/src/gui/People/PeopleDialog.cpp:774: erreur : Cppcheck: Uninitialized variable: layout /retroshare-gui/src/gui/People/PeopleDialog.cpp:864: erreur : Cppcheck: Uninitialized variable: layout /retroshare-gui/src/gui/common/AvatarWidget.cpp:225: erreur : Cppcheck: Uninitialized variable: status /retroshare-nogui/src/rpc/proto/rpcprotostream.cc:805: erreur : Cppcheck: Memory leak: buffer /supportlibs/pegmarkdown/GLibFacade.c:150: erreur : Cppcheck: va_list 'args' was opened but not closed by va_end(). /openpgpsdk/src/openpgpsdk/reader_armoured.c:575: erreur : Cppcheck: Common realloc mistake: 'buf' nulled but not freed upon failure /tests/unittests/libretroshare/serialiser/rsturtleitem_test.cc:213: erreur : Cppcheck: Return value of allocation function 'init_item' is not stored.
2016-03-20 07:10:56 -04:00
va_end(args);
}
void g_string_prepend(GString* baseString, char* prependedString)
{
if ((prependedString != NULL) && (strlen(prependedString) > 0))
{
size_t prependedStringLength = strlen(prependedString);
size_t newStringLength = baseString->currentStringLength + prependedStringLength;
ensureStringBufferCanHold(baseString, newStringLength);
memmove(baseString->str + prependedStringLength, baseString->str, baseString->currentStringLength);
strncpy(baseString->str, prependedString, prependedStringLength);
baseString->currentStringLength = newStringLength;
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;
}