Commits

paulc committed 81c1a98

Add listJoin, listMap, listApply

Comments (0)

Files changed (5)

 readfile
 encrypt
 int64
+split
 
 OBJ = blowfish.o sds.o zmalloc.o sdsutils.o slre.o sha256.o lzf_c.o lzf_d.o adlist.o
 LIB = libsdsutil.a
-PROGS = int64 re readfile encrypt
+PROGS = int64 re readfile encrypt split
 
 all : $(PROGS)
 
-split: split.o $(OBJ)
-	$(CC) -o split $(LDFLAGS) $(DEBUG) split.o $(OBJ)
-
 # Deps (from 'make dep')
 adlist.o: adlist.c adlist.h zmalloc.h
 blowfish.o: blowfish.c blowfish.h
-encrypt.o: encrypt.c sds.h sdsutils.h slre.h
-int64.o: int64.c sds.h sdsutils.h slre.h
+encrypt.o: encrypt.c sds.h sdsutils.h adlist.h blowfish.h lzf.h sha256.h \
+  slre.h zmalloc.h
+int64.o: int64.c sds.h sdsutils.h adlist.h blowfish.h lzf.h sha256.h \
+  slre.h zmalloc.h
 lzf_c.o: lzf_c.c lzfP.h
 lzf_d.o: lzf_d.c lzfP.h
-re.o: re.c sds.h sdsutils.h slre.h
-readfile.o: readfile.c adlist.h sds.h sdsutils.h slre.h
+re.o: re.c sdsutils.h adlist.h blowfish.h lzf.h sds.h sha256.h slre.h \
+  zmalloc.h
+readfile.o: readfile.c adlist.h sds.h sdsutils.h blowfish.h lzf.h \
+  sha256.h slre.h zmalloc.h
 sds.o: sds.c sds.h zmalloc.h
-sdsutils.o: sdsutils.c sdsutils.h sds.h slre.h adlist.h blowfish.h lzf.h \
-  sha256.h zmalloc.h
+sdsutils.o: sdsutils.c sdsutils.h adlist.h blowfish.h lzf.h sds.h \
+  sha256.h slre.h zmalloc.h
 sha256.o: sha256.c sha256.h
 slre.o: slre.c slre.h
+split.o: split.c sdsutils.h adlist.h blowfish.h lzf.h sds.h sha256.h \
+  slre.h zmalloc.h
 zmalloc.o: zmalloc.c config.h
 
 # Targets
+readfile : readfile.o $(OBJ)
+	$(CC) -o readfile $(LDFLAGS) $(DEBUG) readfile.o $(OBJ)
+
 int64 : int64.o $(OBJ)
 	$(CC) -o int64 $(LDFLAGS) $(DEBUG) int64.o $(OBJ)
 
 re : re.o $(OBJ)
 	$(CC) -o re $(LDFLAGS) $(DEBUG) re.o $(OBJ)
 
-readfile : readfile.o $(OBJ)
-	$(CC) -o readfile $(LDFLAGS) $(DEBUG) readfile.o $(OBJ)
-
 encrypt: encrypt.o $(OBJ)
 	$(CC) -o encrypt $(LDFLAGS) $(DEBUG) encrypt.o $(OBJ)
 
+split: split.o $(OBJ)
+	$(CC) -o split $(LDFLAGS) $(DEBUG) split.o $(OBJ)
+
 # Lib
 $(LIB) : $(OBJ)
 	$(AR) rcs $(LIB) $(OBJ)
     return NULL;
 }
 
-/* FIX: Delim at start/end of line or null value doesnt work */
 list *sdssplit(sds s,sds delim) {
     int len = sdslen(s);
     int dlen = sdslen(delim);
     list *result = listCreate();
     listSetFreeMethod(result,_sdsfree);
     for (int i=0;i<len;i++) {
-        if (i - start > dlen) {
-            if (memcmp(delim,s+i-dlen,dlen) == 0) {
-                listAddNodeTail(result,sdsnewlen(s+start,i-dlen-start));
-                start = i;
+        if (i + 1 - start >= dlen) {
+            if (memcmp(delim,s+i-dlen+1,dlen) == 0) {
+                result = listAddNodeTail(result,sdsnewlen(s+start,i-dlen-start+1));
+                start = i+1;
             }
         }
     }
-    listAddNodeTail(result,sdsnewlen(s+start,len-start));
+    result = listAddNodeTail(result,sdsnewlen(s+start,len-start));
     return result;
 }
 
+sds listJoin(list *l,sds delim) {
+    listIter *iter = listGetIterator(l,AL_START_HEAD);
+    listNode *node;
+    int i = 0;
+    sds result = sdsempty();
+    while ((node = listNext(iter)) != NULL) {
+        if (i++ > 0) {
+            result = sdscatlen(result,delim,sdslen(delim));
+        }
+        result = sdscatlen(result,(sds)listNodeValue(node),sdslen((sds)listNodeValue(node)));
+    } 
+    listReleaseIterator(iter);
+    return result;
+}
+
+list *listMap(list *l,void *(*f)(listNode *node),void (*free)(void *ptr)) {
+    list *result = listCreate();
+    listSetFreeMethod(result,free);
+    listIter *iter = listGetIterator(l,AL_START_HEAD);
+    listNode *node;
+    while ((node = listNext(iter)) != NULL) {
+        result = listAddNodeTail(result,f(node));
+    } 
+    listReleaseIterator(iter);
+    return result;
+}
+
+list *listMapWithState(list *l,void *(*f)(listNode *node,void *state),
+                        void (*free)(void *ptr),void *state) {
+    list *result = listCreate();
+    listSetFreeMethod(result,free);
+    listIter *iter = listGetIterator(l,AL_START_HEAD);
+    listNode *node;
+    while ((node = listNext(iter)) != NULL) {
+        result = listAddNodeTail(result,f(node,state));
+    } 
+    listReleaseIterator(iter);
+    return result;
+}
+
+void listApply(list *l,void *(*f)(listNode *node)) {
+    listIter *iter = listGetIterator(l,AL_START_HEAD);
+    listNode *node;
+    while ((node = listNext(iter)) != NULL) {
+        f(node);
+    } 
+    listReleaseIterator(iter);
+}
+
+
+
 sds sdsexec(char *cmd);
 sds sdspipe(char *cmd,sds input);
 list *sdssplit(sds s,sds delim);
+sds listJoin(list *l,sds delim);
+list *listMap(list *l,void *(*f)(listNode *node),void (*free)(void *ptr));
+list *listMapWithState(list *l,void *(*f)(listNode *node,void *state),
+                               void (*free)(void *ptr),void *state);
+void listApply(list *l,void *(*f)(listNode *node));
+
 
 #endif /* _SDSUTILS_H */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sdsutils.h"
+
+void _sdsfree(void *ptr) {
+    printf("--> Free: %p %s\n",ptr, (char *)ptr);
+    sdsfree((sds) ptr);
+}
+
+void *hexify(listNode *node,void *state) {
+    sds in = (sds)listNodeValue(node);
+    sds result = sdshex(in);
+    *(int *)state += 1;
+    printf("Map: %s -> %s\n", (char *)in, (char *)result);
+    return result;
+}
+
+int main(int argc, char** argv) {
+	if (argc != 3) {
+		printf("Usage: ./re <split delim> <join delim>\n");
+		exit(1);
+	}
+    sds arg = sdsnew(argv[1]);
+    sds split_delim = sdsunrepr(arg);
+    sdsfree(arg);
+
+    arg = sdsnew(argv[2]);
+    sds join_delim = sdsunrepr(arg);
+    sdsfree(arg);
+
+
+	while (!feof(stdin)) {
+		sds line = sdsreadline(stdin,"Line: ");
+        list *matches = sdssplit(line,split_delim);
+        sdsfree(line);
+
+        listIter *iter = listGetIterator(matches,AL_START_HEAD);
+        listNode *node;
+
+        while ((node = listNext(iter)) != NULL) {
+            sdsprintrepr(stdout,">>",(sds)listNodeValue(node),"<<\n");
+        } 
+
+        listReleaseIterator(iter);
+
+        int count=0;
+        list *map = listMapWithState(matches,hexify,_sdsfree,(void *)&count);
+        sds join = listJoin(map,join_delim);
+        sdsprintrepr(stdout,"Map >",join,"< \n");
+        printf("Count=%d\n",count);
+        listRelease(map);
+        sdsfree(join);
+
+        listRelease(matches);
+	}
+    sdsfree(split_delim);
+    sdsfree(join_delim);
+    exit(0);
+}