[lxc-devel] [patch 1/7] extend parsing interface with strtok-like utils
Andrian Nord
nightnord at gmail.com
Mon Dec 7 10:13:56 UTC 2009
strtok is not very good, so this is lxc-way implementation. Required for
futher parsing interfaces.
Signed-off-by: Andrian Nord <NightNord at gmail.com>
diff --git a/src/lxc/parse.c b/src/lxc/parse.c
index b3d1194..4721d01 100644
--- a/src/lxc/parse.c
+++ b/src/lxc/parse.c
@@ -113,6 +113,52 @@ int lxc_char_right_gc(char *buffer, size_t len)
return 0;
}
+int lxc_char_next_space(char *buffer, size_t len)
+{
+ int i;
+
+ for (i = 0; i < len; i++) {
+ if (buffer[i] == ' ' ||
+ buffer[i] == '\t' ||
+ buffer[i] == '\n' ||
+ buffer[i] == '\0')
+ return i;
+ }
+
+ return len;
+}
+
+char *lxc_next_token(char *buffer, int *anchor, size_t len)
+{
+ char *search_at, *token;
+ int token_length = 0;
+
+ if (*anchor >= len)
+ return NULL;
+
+ *anchor += lxc_char_left_gc(buffer + *anchor, len - *anchor);
+ search_at = buffer + *anchor;
+
+ token_length = lxc_char_next_space(search_at, len - *anchor);
+
+ if (!token_length)
+ return NULL;
+
+ *anchor += token_length;
+
+ token = malloc(token_length + 1);
+
+ if (!token) {
+ SYSERROR("failed to allocate memory");
+ return NULL;
+ }
+
+ strncpy(token, search_at, token_length);
+ *(token + token_length) = '\0';
+
+ return token;
+}
+
int lxc_is_line_empty(char *line)
{
int i;
diff --git a/src/lxc/parse.h b/src/lxc/parse.h
index a1cccea..5f502c2 100644
--- a/src/lxc/parse.h
+++ b/src/lxc/parse.h
@@ -38,6 +38,10 @@ extern int lxc_char_left_gc(char *buffer, size_t len);
extern int lxc_char_right_gc(char *buffer, size_t len);
+extern int lxc_char_next_space(char *buffer, size_t len);
+
+extern char *lxc_next_token(char *buffer, int *anchor, size_t len);
+
extern int lxc_is_line_empty(char *line);
#endif
More information about the lxc-devel
mailing list