# HG changeset patch # User Louis Opter # Date 1293894079 -3600 # Node ID 50215911acb37c4c304fc84613f14ed0c496553b # Parent 2cb8a6cbe468afdfab2966b1d6b7fb195bd2a6e2 Add a strsplit() function and stop to build probes into a separate library diff -r 2cb8a6cbe468 -r 50215911acb3 CMakeLists.txt --- a/CMakeLists.txt Fri Dec 31 20:04:25 2010 +0100 +++ b/CMakeLists.txt Sat Jan 01 16:01:19 2011 +0100 @@ -30,6 +30,11 @@ # Build rules ################################################################## +SET(PROBES_SRC + probes/cpuacct.c + probes/probes.c + ) + SET(SRC compat/strlcpy.c compat/strlcat.c @@ -37,14 +42,13 @@ container.c open.c utils.c + ${PROBES_SRC} ) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) ADD_LIBRARY(lxcstats SHARED ${SRC}) -TARGET_LINK_LIBRARIES(lxcstats probes) - # Install/Uninstall rules ###################################################### INSTALL(TARGETS lxcstats LIBRARY DESTINATION lib) diff -r 2cb8a6cbe468 -r 50215911acb3 _lxcstats.h --- a/_lxcstats.h Fri Dec 31 20:04:25 2010 +0100 +++ b/_lxcstats.h Sat Jan 01 16:01:19 2011 +0100 @@ -52,4 +52,16 @@ */ ssize_t _lxcst_read_file(const char *path, char **content); +/** + * Split a string around spaces (defined by isspace()). + * + * @param [in] str The string to split, spaces are erased with '\0'. + * @param [out] fields An array of string, each entry will point to a word of + * the string. + * @param [in] nfields Number of entries into the fields array. + * + * @return The number of fields found. + */ +int _lxcst_strsplit(char *str, char **fields, size_t nfields); + #endif diff -r 2cb8a6cbe468 -r 50215911acb3 probes/CMakeLists.txt --- a/probes/CMakeLists.txt Fri Dec 31 20:04:25 2010 +0100 +++ b/probes/CMakeLists.txt Sat Jan 01 16:01:19 2011 +0100 @@ -1,9 +1,1 @@ -SET(PROBES_SRC - cpuacct.c - probes.c - ) - -ADD_LIBRARY(probes STATIC ${PROBES_SRC}) -SET_TARGET_PROPERTIES(probes PROPERTIES COMPILE_FLAGS "-fPIC") - ADD_SUBDIRECTORY(tests) diff -r 2cb8a6cbe468 -r 50215911acb3 tests/CMakeLists.txt --- a/tests/CMakeLists.txt Fri Dec 31 20:04:25 2010 +0100 +++ b/tests/CMakeLists.txt Sat Jan 01 16:01:19 2011 +0100 @@ -15,6 +15,7 @@ read_file read_big_file span_containers + strsplit ) FOREACH(I ${TESTS}) diff -r 2cb8a6cbe468 -r 50215911acb3 tests/strsplit.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/strsplit.c Sat Jan 01 16:01:19 2011 +0100 @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#include "lxcstats.h" +#include "_lxcstats.h" + +void +check(const char *field, const char *ref) +{ + printf("comparing: [%s] with reference: [%s].\n", field, ref); + if (strcmp(field, ref)) + exit(EXIT_FAILURE); +} + +int +main(void) +{ + char usage[] = "system: 4242\nuser: 4242"; + char test[] = " \tword1 word2 \f\tword3\n"; + char *fields[4]; + + if (_lxcst_strsplit(usage, fields, sizeof(fields) / sizeof(fields[0])) != 4) + errx(EXIT_FAILURE, "invalid number of fields."); + check(fields[0], "system:"); + check(fields[1], "4242"); + check(fields[2], "user:"); + check(fields[3], "4242"); + + if (_lxcst_strsplit(test, fields, sizeof(fields) / sizeof(fields[0])) != 3) + errx(EXIT_FAILURE, "invalid number of fields."); + check(fields[0], "word1"); + check(fields[1], "word2"); + check(fields[2], "word3"); + + return (EXIT_SUCCESS); +} diff -r 2cb8a6cbe468 -r 50215911acb3 utils.c --- a/utils.c Fri Dec 31 20:04:25 2010 +0100 +++ b/utils.c Sat Jan 01 16:01:19 2011 +0100 @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -98,3 +99,29 @@ free(*content); return (ret); } + +int +_lxcst_strsplit(char *str, char **fields, size_t nfields) +{ + char prev; + int i; + + prev = ' '; + i = 0; + while (*str && nfields) { + if (!isspace(*str)) { + if (isspace(prev)) { /* new word */ + fields[i] = str; + ++i; + --nfields; + } + prev = *str; + } else { + prev = *str; + *str = '\0'; + } + str++; + } + + return (i); +}