M Makefile => Makefile +24 -17
@@ 1,10 1,10 @@
LIB = cstring
VERSION = 0.1
-DIST = ${BIN}-${VERSION}
+DIST = ${LIB}-${VERSION}
MAN3 = ${LIB}.3
PREFIX = /usr/local
MAN_DIR = ${PREFIX}/man/man3
-HDR_DIR = ${PREFIX}/include
+INC_DIR = ${PREFIX}/include
LIB_DIR = ${PREFIX}/lib
EXT = c
@@ 14,11 14,12 @@ OBJ = ${SRC:%.${EXT}=%.o}
AR = ar
ARFLAGS = rs
CC = gcc
-CPPFLAGS += -Iinclude -DCSTRING_DBG -DVERSION=\"${VERSION}\"
-#CPPFLAGS += -Iinclude -DVERSION=\"${VERSION}\"
-CFLAGS += -Wall -std=c99 -pedantic -O3
-LDFLAGS += -Llib
-#LDLIBS +=
+INCS = -Iinclude
+# Uncomment if you want to compile the library in debug mode
+#CPPFLAGS = -DCSTRING_DBG -DVERSION=\"${VERSION}\"
+CPPFLAGS = -DVERSION=\"${VERSION}\"
+CFLAGS = -Wall -std=c99 -pedantic -O3 ${INCS} ${CPPFLAGS}
+LDFLAGS = -Llib
CP = cp -f
RM = rm -f
@@ 27,37 28,43 @@ MKDIR = mkdir -p
TAR = tar -cf
GZIP = gzip
-all: ${LIB}
+all: options ${LIB}
+
+options:
+ @echo ${LIB} build options:
+ @echo "CFLAGS = ${CFLAGS}"
+ @echo "LDFLAGS = ${LDFLAGS}"
+ @echo "CC = ${CC}"
${LIB}: ${OBJ}
${AR} ${ARFLAGS} lib${LIB}.a ${OBJ}
%.o: %.${EXT}
- ${CC} ${CPPFLAGS} ${CFLAGS} -c $< -o $@
+ ${CC} ${CFLAGS} -c $< -o $@
dist: clean
${MKDIR} ${DIST}
- ${CP} -R tests ${SRC} ${MAN3} cstring.h Makefile README.md ${DIST}
+ ${CP} -R tests ${SRC} ${MAN3} LICENSE Makefile README.md ${DIST}
${TAR} ${DIST}.tar ${DIST}
${GZIP} ${DIST}.tar
${RM_DIR} ${DIST}
install: all
- ${MKDIR} ${DESTDIR}${LIB_DIR} ${DESTDIR}${HDR_DIR} ${DESTDIR}${MAN_DIR}
- ${CP} ${LIB}.h ${DESTDIR}${HDR_DIR}
+ ${MKDIR} ${DESTDIR}${LIB_DIR} ${DESTDIR}${INC_DIR} ${DESTDIR}${MAN_DIR}
+ ${CP} ${LIB}.h ${DESTDIR}${INC_DIR}
${CP} lib${LIB}.a ${DESTDIR}${LIB_DIR}
${CP} ${MAN3} ${DESTDIR}${MAN_DIR}
sed "s/VERSION/${VERSION}/g" < ${MAN3} > ${DESTDIR}${MAN_DIR}/${MAN3}
- chmod 644 ${DESTDIR}${HDR_DIR}/${LIB}.h
+ chmod 755 ${DESTDIR}${INC_DIR}/${LIB}.h
chmod 644 ${DESTDIR}${LIB_DIR}/lib${LIB}.a
chmod 644 ${DESTDIR}${MAN_DIR}/${MAN3}
-uninstall: all
- ${RM} ${DESTDIR}${HDR_DIR}/${LIB}.h
+uninstall:
+ ${RM} ${DESTDIR}${INC_DIR}/${LIB}.h
${RM} ${DESTDIR}${LIB_DIR}/lib${LIB}.a
${RM} ${DESTDIR}${MAN_DIR}/${MAN3}
clean:
- ${RM} ${OBJ} ${LIB} lib${LIB}.a
+ ${RM} ${LIB} ${OBJ} lib${LIB}.a ${DIST}.tar.gz
-.PHONY: all clean dist install uninstall
+.PHONY: all options clean dist install uninstall
M README.md => README.md +2 -2
@@ 84,7 84,7 @@ The recommended way of initializing an empty string is by doing `cstring foo = c
## Macros
-* `CSTRING_OUT_OF_BOUNDS`: Check if `pos` is out of bounds.
+* `CSTRING_OUT_OF_BOUNDS`: Check if value is out of bounds.
* `CSTRING_ARR_LEN`: Determine an array's length. The macro must be called in the same function the array is declared.
* `CSTRING_FLAG_CHECK`: Check if flag is on.
* `CSTRING_MALLOC`: Allocate memory with error checking.
@@ 112,7 112,7 @@ int
main(int argc, char **argv)
{
cstring s = cstring_create("Foo");
- cstring_append(&s, "bar.");
+ cstring_append(&s, "bar");
printf("%s\n", s.str);
cstring_delete(&s);
M cstring.3 => cstring.3 +0 -0
M cstring.c => cstring.c +4 -4
@@ 124,6 124,7 @@ cstring_insert(cstring *cs, const char *s, size_t i)
}
#ifdef CSTRING_DBG
+// FIX IT
#define CSTRING_DBG_EXPECTED_ERASE_STR(cs, pos, len) do { \
CSTRING_DBG_LOG("%s", "CSTRING_DBG_EXPECTED_ERASE_STR: "); \
size_t _i; \
@@ 142,8 143,8 @@ void
cstring_erase(cstring *cs, size_t pos, size_t len)
{
if (!cstring_empty(cs)
- && !CSTRING_OUT_OF_BOUNDS(cs, pos)
- && !CSTRING_OUT_OF_BOUNDS(cs, len))
+ && (!CSTRING_OUT_OF_BOUNDS(cs, pos)
+ || !CSTRING_OUT_OF_BOUNDS(cs, len)))
{
#ifdef CSTRING_DBG
CSTRING_DBG_LOG("STR: %s | INDEX: %ld | LEN: %ld\n", cs->str, pos, len);
@@ 154,7 155,7 @@ cstring_erase(cstring *cs, size_t pos, size_t len)
char *tmp;
CSTRING_MALLOC(tmp, newlen + 1);
memcpy(tmp, cs->str, pos);
- memcpy(tmp + pos, cs->str + pos + len, cs->len - len + pos);
+ memcpy(tmp + pos, cs->str + pos + len, cs->len - pos - len);
CSTRING_FREE(cs); /* Useless check but keep it for consistency */
cs->len = newlen;
cs->str = tmp;
@@ 254,7 255,6 @@ cstring_swap(cstring *lhs, cstring *rhs)
*rhs = tmp;
}
-// FIX
void
cstring_sort_partial(cstring *cs,
size_t pos,
M cstring.h => cstring.h +1 -1
@@ 11,7 11,7 @@ extern "C" {
#define CSTRING_NPOS -1
#define CSTRING_INIT_EMPTY ""
-#define CSTRING_OUT_OF_BOUNDS(cs, pos) ((pos) > cs->len)
+#define CSTRING_OUT_OF_BOUNDS(cs, val) ((val) > cs->len)
#define CSTRING_ARR_LEN(arr) ((size_t)sizeof((arr)) / sizeof((arr)[0]))
#define CSTRING_FLAG_CHECK(flag, bit) (((flag) & (int)(bit)) == (int)(bit))
A tests/Makefile => tests/Makefile +38 -0
@@ 0,0 1,38 @@
+BINS = test_basic test_insert
+CC = gcc
+CFLAGS = -Wall -std=c99 -pedantic -O3
+LDFLAGS = -Llib -lcstring
+
+all: options ${BINS}
+
+options:
+ @echo "build options:"
+ @echo "CFLAGS = ${CFLAGS}"
+ @echo "LDFLAGS = ${LDFLAGS}"
+ @echo "CC = ${CC}"
+
+run:
+ @echo "---------------------------"
+ @echo "---------------------------"
+ @echo "RUNNING: test_basic"
+ @echo "---------------------------"
+ @echo "---------------------------"
+ ./test_basic
+
+ @echo "---------------------------"
+ @echo "---------------------------"
+ @echo "RUNNING: test_insert"
+ @echo "---------------------------"
+ @echo "---------------------------"
+ ./test_insert
+
+clean:
+ rm -f ${BINS} *.o
+
+test_basic: test_basic.c
+ ${CC} test_basic.c ${CFLAGS} -o test_basic ${LDFLAGS}
+
+test_insert: test_insert.c
+ ${CC} test_insert.c ${CFLAGS} -o test_insert ${LDFLAGS}
+
+.PHONY: all options run clean
R tests/test.c => tests/test_basic.c +1 -3
@@ 1,6 1,6 @@
#include "cstring.h"
-// Compilation: gcc test.c -lcstring
+// Compilation: gcc test_basic.c -lcstring
int
main(int argc, char **argv)
@@ 11,7 11,6 @@ main(int argc, char **argv)
cstring_sort_chars(&s, CSTRING_SORT_ASCENDING, NULL);
printf("cstring_sort_chars: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- // BUG
cstring_assign(&s, "New string");
printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
@@ 44,7 43,6 @@ main(int argc, char **argv)
cstring_replace_str(&s, "hell", 0, strlen("hell"));
printf("cstring_replace_str: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- // BUGS
cstring_erase(&s, 1, 4);
printf("cstring_erase: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
A tests/test_insert.c => tests/test_insert.c +11 -0
@@ 0,0 1,11 @@
+#include <stdio.h>
+#include <cstring.h>
+
+int
+main(int argc, char **argv)
+{
+ cstring s = cstring_create("HHHHHHEEEEEEEEEEEEEEEEEEEEEYYYYYYYYYYYYYY");
+ printf("%s\n", s.str);
+ cstring_delete(&s);
+ return 0;
+}