Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting into two libraries for easier testing.
- Loading branch information
Rudi Cilibrasi
committed
Dec 15, 2014
1 parent
20d4df6
commit 2fd493f
Showing
11 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
LIBRARY_1.0 { | ||
global: | ||
newStringHolder; | ||
pushStringHolder; | ||
freeStringHolder; | ||
getStringHolderData; | ||
getStringHolderCount; | ||
|
||
local: | ||
*; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
newStringHolder | ||
pushStringHolder | ||
freeStringHolder | ||
getStringHolderData | ||
getStringHolderCount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
if HAVE_LD_VERSION_SCRIPT | ||
VERSION_LDFLAGS = -Wl,--version-script=$(top_srcdir)/lib/clutil.map | ||
else | ||
VERSION_LDFLAGS = -export-symbols $(top_srcdir)/lib/clutil.sym | ||
endif | ||
|
||
lib_LTLIBRARIES = libclutil.la | ||
libclutil_la_SOURCES = stringholder.c stringholder.h | ||
libclutil_la_CPPFLAGS = -I$(top_srcdir)/include -Wall | ||
libclutil_la_LDFLAGS = $(VERSION_LDFLAGS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include "stringholder.h" | ||
|
||
#define STARTING_STRING_SIZE 16 | ||
|
||
void newStringHolder(struct StringHolder *dh) { | ||
dh->alloc = STARTING_STRING_SIZE; | ||
dh->val = malloc(sizeof(char *)*dh->alloc); | ||
dh->count = 0; | ||
} | ||
|
||
void freeStringHolder(struct StringHolder *dh) { | ||
if (dh->val) { | ||
int i; | ||
for (i = 0; i < dh->count; ++i) { | ||
free(dh->val[i]); | ||
dh->val[i] = 0; | ||
} | ||
free(dh->val); | ||
dh->val = 0; | ||
dh->alloc = 0; | ||
} | ||
dh->count = 0; | ||
} | ||
|
||
void pushStringHolder(struct StringHolder *dh, const char *str) { | ||
if (!dh->val) { | ||
fprintf(stderr, "Cannot push onto freed StringHolder.\n"); | ||
exit(1); | ||
} | ||
if (dh->count + 1 >= dh->alloc) { | ||
int nextStep = dh->alloc * 2; | ||
char **nextVal = malloc(sizeof(char *) * nextStep); | ||
memcpy(nextVal, dh->val, sizeof(char *) * dh->count); | ||
free(dh->val); | ||
dh->val = nextVal; | ||
dh->alloc = nextStep; | ||
} | ||
dh->val[dh->count] = strdup(str); | ||
dh->count += 1; | ||
} | ||
|
||
char **getStringHolderData(struct StringHolder *dh) { | ||
return dh->val; | ||
} | ||
|
||
uint32_t getStringHolderCount(struct StringHolder *dh) { | ||
return dh->count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef __STRINGHOLDER_H | ||
#define __STRINGHOLDER_H | ||
|
||
#include <stdint.h> | ||
|
||
struct StringHolder { | ||
uint32_t count, alloc; | ||
char **val; | ||
}; | ||
|
||
void newStringHolder(struct StringHolder *dh); | ||
void pushStringHolder(struct StringHolder *dh, const char *str); | ||
void freeStringHolder(struct StringHolder *dh); | ||
char **getStringHolderData(struct StringHolder *dh); | ||
uint32_t getStringHolderCount(struct StringHolder *dh); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
|
||
if HAVE_CHECKMK | ||
|
||
bin_PROGRAMS=lctest | ||
bin_PROGRAMS=lctest lcutiltest | ||
|
||
lctest_SOURCES=basic_complete.c | ||
lctest_CPPFLAGS=-I../../libcl/include -Wall @CHECK_CFLAGS@ | ||
lctest_LDADD =../../libcl/libcomplearn.la -lm @CHECK_LIBS@ | ||
lctest_CPPFLAGS=-I../../libcl/include -Wall @CHECK_CFLAGS@ -I../../libclutil -g | ||
lctest_LDADD =../../libcl/libcomplearn.la ../../libclutil/libclutil.la -lm @CHECK_LIBS@ | ||
|
||
lcutiltest_SOURCES=basic_stringholder.c | ||
lcutiltest_CPPFLAGS=-I../../libcl/include -Wall @CHECK_CFLAGS@ -I../../libclutil -g | ||
lcutiltest_LDADD =../../libcl/libcomplearn.la ../../libclutil/libclutil.la -lm @CHECK_LIBS@ | ||
|
||
.ts.c: | ||
checkmk $< >$@ | ||
|
||
endif | ||
|
||
EXTRA_DIST=basic_complete.ts | ||
EXTRA_DIST=basic_complete.ts \ | ||
basic_stringholder.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* A complete test example */ | ||
|
||
#include <stdio.h> | ||
#include <stringholder.h> | ||
|
||
#test stringholder_test | ||
struct StringHolder sh; | ||
newStringHolder(&sh); | ||
ck_assert(0 == getStringHolderCount(&sh)); | ||
freeStringHolder(&sh); | ||
newStringHolder(&sh); | ||
ck_assert(0 == getStringHolderCount(&sh)); | ||
pushStringHolder(&sh, "cat"); | ||
ck_assert(1 == getStringHolderCount(&sh)); | ||
ck_assert(0 != getStringHolderData(&sh)); | ||
ck_assert(0 == strcmp("cat", getStringHolderData(&sh)[0])); | ||
pushStringHolder(&sh, "dog"); | ||
ck_assert(2 == getStringHolderCount(&sh)); | ||
ck_assert(0 != getStringHolderData(&sh)); | ||
ck_assert(0 == strcmp("dog", getStringHolderData(&sh)[1])); | ||
ck_assert(0 == strcmp("cat", getStringHolderData(&sh)[0])); | ||
freeStringHolder(&sh); |