]> git.xolatile.top Git - soikk-DB.git/commitdiff
Quality of life changes
author= <=>
Thu, 5 May 2022 13:23:47 +0000 (15:23 +0200)
committer= <=>
Thu, 5 May 2022 13:23:47 +0000 (15:23 +0200)
TODO [new file with mode: 0644]
db.h
parser.c
parser.h
storage.c
storage.h
strings.c [new file with mode: 0644]
strings.h [new file with mode: 0644]

diff --git a/TODO b/TODO
new file mode 100644 (file)
index 0000000..87d9437
--- /dev/null
+++ b/TODO
@@ -0,0 +1 @@
+Try to improve 'sameStr' by maybe not having to check s2[i2]
diff --git a/db.h b/db.h
index 116637fb75a2c6776947c00d64227a4c0309b113..a7db8f3a6b119b9ada32b79ea29bd3e9946ff56a 100644 (file)
--- a/db.h
+++ b/db.h
@@ -14,7 +14,7 @@
 #include "repl.h"
 #include "parser.h"
 #include "storage.h"
+#include "strings.h"
 
 
-
-#endif
\ No newline at end of file
+#endif
index 1c127cd1f111e8f27e446f37e05220fc0c65e82d..a680d2a7a134dd0517ef03850d679072dd34ca99 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1,19 +1,6 @@
 #include "db.h"
 
 
-uint16_t len(const char *s){
-       uint16_t l = -1;
-       while(s[++l]);
-       return l;
-}
-
-bool sameStr(const char* s1, const char *s2){
-       uint16_t i1 = 0, i2 = 0;
-       while(s1[i1] && s2[i2] && s1[i1] == s2[i2])
-               ++i1, ++i2;
-       return !s1[i1] && !s2[i2];
-}
-
 int handleMetaCommand(inputBuffer *in){
        if(sameStr(in->buffer, ".exit")){
                freeInputBuffer(in);
@@ -30,4 +17,4 @@ int handleInput(inputBuffer *in){
        }else{
                printf("normal command\n");
        }
-}
\ No newline at end of file
+}
index 7f1854b4c701c0e2ed8fcdaa4c2abbf855744272..2614615d6bcafbb309c5d9573e39eea47c177ebf 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -10,12 +10,8 @@ typedef enum {
 } metaCommandStatus;
 
 
-uint16_t len(const char *s);
-
-bool sameStr(const char* s1, const char *s2);
-
 int handleMetaCommand(inputBuffer *in);
 
 int handleInput(inputBuffer *in);
 
-#endif
\ No newline at end of file
+#endif
index 5e7809b262f47e327824a3138c6591f6eb009e59..2a5420bcf4af172ca318b29b4986815a2d037bcb 100644 (file)
--- a/storage.c
+++ b/storage.c
@@ -6,6 +6,7 @@ row *newRow(const char path[MAXPATH]){
        memcpy(nr->path, path, MAXPATH);
        memcpy(nr->tags, "\0", MAXTAGS);
        nr->numTags = 0;
+       nr->lenTags = 0;
 
        return nr;
 }
@@ -113,6 +114,7 @@ void insertTag(row *r, char *tag){
        }
        r->tags[tagnum] = '\0';
        r->numTags = l;
+       r->lenTags = tagnum;
 }
 
 // Remove a tag from the tags array in the row r
@@ -153,4 +155,5 @@ void removeTag(row *r, char *tag){
        }
        r->tags[tagnum] = '\0';
        r->numTags = l;
-}
\ No newline at end of file
+       r->lenTags = tagnum;
+}
index b37bdb86dfc4f2673cb97bc19d30abaca5aace6e..9689a56a1e883d5fd5674181717cd832ec0ffc78 100644 (file)
--- a/storage.h
+++ b/storage.h
@@ -10,7 +10,9 @@
 typedef struct{
        char path[MAXPATH];
        char tags[MAXTAGS];
-       int numTags;
+       uint16_t numTags;
+       uint16_t lentags;
+       
 } row;
 
 
@@ -26,4 +28,4 @@ void insertTag(row *r, char *tag);
 
 void removeTag(row *r, char *tag);
 
-#endif
\ No newline at end of file
+#endif
diff --git a/strings.c b/strings.c
new file mode 100644 (file)
index 0000000..30e00e7
--- /dev/null
+++ b/strings.c
@@ -0,0 +1,55 @@
+#include "db.h"
+
+
+uint16_t len(const char *s){
+       uint16_t l = -1;
+       while(s[++l]);
+       return l;
+}
+
+bool sameStr(const char *s1, const char *s2){
+       uint16_t i1 = 0, i2 = 0;
+       while(s1[i1] && s2[i2] && s1[i1] == s2[i2])
+               ++i1, ++i2;
+       return !s1[i1] && !s2[i2];
+}
+
+
+static int *table(char *y, int n, char sep){
+       int *tb = calloc(n, sizeof(int));
+       if(tb == NULL){
+               fprintf(stderr, "Error callocating array (table)");
+               exit(EXIT_FAILURE);
+       }
+
+       int lSep = n-1;
+       for(int i = n-1; i >= 0; --i){
+               if(y[i] == sep){
+                       tb[i] = 1;
+                       lSep = i;
+               }else if(y[i] != '\0'){
+                       tb[i] = lSep-i;
+               }
+       }
+       return tb;
+}
+
+
+ssize_t strInTags(const char *tags, int n, const char *ndl, int m, char sep){
+       int *tb = table(tags, n, sep);
+
+       for(int i = 0; i < n; ){
+               int j = 0;
+               while(j < m && tags[i+j] == ndl[j]){
+                       ++j;
+               }
+               if(j == m){
+                       return i;
+               }
+               if(tags[i+j] != ndl[j]){
+                       i += tb[i];
+               }
+       }
+       return -1;
+}
+       
diff --git a/strings.h b/strings.h
new file mode 100644 (file)
index 0000000..db3c3e1
--- /dev/null
+++ b/strings.h
@@ -0,0 +1,14 @@
+#ifndef STRINGS_H
+#define STRINGS_H
+
+#include "db.h"
+
+
+uint16_t len(const char *s);
+
+bool sameStr(const char *s1, const char *s2);
+
+ssize_t strInTags(const char *tags, int n, const char *ndl, int m, char sep); 
+
+
+#endif