changeset 4:50215911acb3

Add a strsplit() function and stop to build probes into a separate library
author Louis Opter <louis@dotcloud.com>
date Sat, 01 Jan 2011 16:01:19 +0100
parents 2cb8a6cbe468
children 8d8c49b066d4
files CMakeLists.txt _lxcstats.h probes/CMakeLists.txt tests/CMakeLists.txt tests/strsplit.c utils.c
diffstat 6 files changed, 84 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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
--- 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)
--- 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})
--- /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 <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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);
+}
--- 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 <sys/stat.h>
 
 #include <assert.h>
+#include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -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);
+}