forked from fletcher/peg-multimarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLibFacade.c
209 lines (172 loc) · 5.51 KB
/
GLibFacade.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* 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(__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);
}
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;
}