paulc avatar paulc committed ae7ea7d

Convert sdsmatch to return adlist, initial version of sdssplit (buggy)

Comments (0)

Files changed (4)

 
 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
 re.o: re.c sds.h sdsutils.h slre.h
 readfile.o: readfile.c adlist.h sds.h sdsutils.h slre.h
 sds.o: sds.c sds.h zmalloc.h
-sdsutils.o: sdsutils.c sdsutils.h sds.h slre.h blowfish.h lzf.h sha256.h zmalloc.h
+sdsutils.o: sdsutils.c sdsutils.h sds.h slre.h adlist.h blowfish.h lzf.h \
+  sha256.h zmalloc.h
 sha256.o: sha256.c sha256.h
 slre.o: slre.c slre.h
 zmalloc.o: zmalloc.c config.h
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "sds.h"
 #include "sdsutils.h"
 
 int main(int argc, char** argv) {
 	}
 	while (!feof(stdin)) {
 		sds line = sdsreadline(stdin,">> ");
-        int count = 0;
-        sds *matches = sdsmatch(line,argv[1],&count);
+        list *matches = sdsmatch(line,argv[1]);
+
         if (matches == NULL) {
             printf("Problem compiling regex: %s\n",argv[1]);
             exit(1);
         }
-        for (int i=0; i<count; i++) {
-            printf("Match %d: ",i);
-            sdsprintrepr(stdout,"",matches[i],"\n");
-        }
-        sdsfreematchres(matches,count);
+
+        listIter *iter = listGetIterator(matches,AL_START_HEAD);
+        listNode *node;
+
+        while ((node = listNext(iter)) != NULL) {
+            sdsprintrepr(stdout,"Match: ",(sds)listNodeValue(node),"\n");
+        } 
+
+        listReleaseIterator(iter);
+        listRelease(matches);
         sdsfree(line);
 	}
     exit(0);
 
-#include "sdsutils.h"
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "blowfish.h"
-#include "lzf.h"
-#include "sds.h"
-#include "sha256.h"
-#include "slre.h"
-#include "zmalloc.h"
+
+#include "sdsutils.h"
 
 int char_count(char *s, char c) {
     int count = 0;
     return line;
 }
 
-sds *sdsmatchre(sds s,struct slre *slre,int ncap,int *count) {
-    int slots = 5, elements = 0;
-    sds *matches = zmalloc(sizeof(sds)*slots);
+static void _sdsfree(void *p) {
+    sdsfree((sds) p);
+}
+
+list *sdsmatchre(sds s,struct slre *slre,int ncap) {
 	struct cap *cap = zcalloc(sizeof(struct cap) * ncap);
+    list *result = listCreate();
+    listSetFreeMethod(result,_sdsfree);
     if (slre_match(slre,s,sdslen(s),cap)) {
         for (int i=0; i < ncap; i++) {
             if (cap[i].len > 0) {
-                if (slots < elements+1) {
-                    slots *= 2;
-                    sds *newmatches = zrealloc(matches,sizeof(sds)*slots);
-                    matches = newmatches;
-                }
-                matches[elements++] = sdsnewlen(cap[i].ptr,cap[i].len);
+                listAddNodeTail(result,sdsnewlen(cap[i].ptr,cap[i].len));
             }
         }
     }
     zfree(cap);
-    *count = elements;
-    return matches;
+    return result;
 }
 
-sds *sdsmatch(sds s,char *re,int *count) {
+list *sdsmatch(sds s,char *re) {
 	struct slre slre;
 	if (slre_compile(&slre,re) != 1) {
         return NULL;
 	}
     int ncap = char_count(re,'(')+1;
-    return sdsmatchre(s,&slre,ncap,count);
-}
-
-void sdsfreematchres(sds* matches,int count) {
-    if (!matches) return;
-    while(count--)
-        sdsfree(matches[count]);
-    zfree(matches);
+    return sdsmatchre(s,&slre,ncap);
 }
 
 sds sdshex(sds s) {
     return data;
 }
 
+/* FIX: Check for leaks */
 sds sdspipe(char *cmd,sds input) {
     int p_in[2],p_out[2];
     pid_t pid;
     }
     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);
+    int start = 0;
+    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;
+            }
+        }
+    }
+    listAddNodeTail(result,sdsnewlen(s+start,len-start));
+    return result;
+}
+
 #ifndef _SDSUTILS_H
 #define _SDSUTILS_H
 
+#include "adlist.h"
+#include "blowfish.h"
+#include "lzf.h"
 #include "sds.h"
+#include "sha256.h"
 #include "slre.h"
+#include "zmalloc.h"
 
 #include <stdio.h>
 #include <stdint.h>
 sds sdsreadfile(FILE *fp);
 sds sdsreaddelim(FILE *fp,void *delim,int len);
 sds sdsreadline(FILE *fp,const char *prompt);
-sds *sdsmatchre(sds s,struct slre *slre,int ncap,int *count);
-sds *sdsmatch(sds s,char *re,int *count);
+list *sdsmatchre(sds s,struct slre *slre,int ncap);
+list *sdsmatch(sds s,char *re);
 sds sdssha256(sds s);
 sds sdscompress(sds s);
 sds sdsdecompress(sds s);
 sds sdsencrypt(sds s,sds key,sds iv);
 sds sdsdecrypt(sds z,sds key);
-void sdsfreematchres(sds* matches,int count);
 sds sdshex(sds s);
 sds sdsunhex(sds s);
 sds sdsrepr(sds s);
 void sdsprinthex(FILE *fp,char *prefix,sds s,char *suffix);
 sds sdsexec(char *cmd);
 sds sdspipe(char *cmd,sds input);
+list *sdssplit(sds s,sds delim);
 
 #endif /* _SDSUTILS_H */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.