]> git.xolatile.top Git - soikk-DB.git/commitdiff
Add or remove tags
authorSoikk <76824648+Soikk@users.noreply.github.com>
Fri, 29 Apr 2022 01:28:34 +0000 (03:28 +0200)
committerSoikk <76824648+Soikk@users.noreply.github.com>
Fri, 29 Apr 2022 01:28:34 +0000 (03:28 +0200)
main.c
repl.c
storage.c
storage.h
strnatcmp.c

diff --git a/main.c b/main.c
index 8eb334a5524657220896ad097f6c9dacded6761b..b6ef2441692b89da458a6e02187be7dc2abe723a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,30 +1,32 @@
 #include "db.h"
 
 
-
-
-
 int main(){
 
        inputBuffer *in = newInputBuffer();
 
-       row r = {"C:/xd", "pepe;soyjak;"};
-       printf("%s\n", r.tags);
-       insertTag(&r, "WOJAK");
+
+       row r = {"C:/xd", "perro", 1};
        printf("%s\n", r.tags);
+       insertTag(&r, "caca");
+       insertTag(&r, "mierda");
+       insertTag(&r, "tu");
+       insertTag(&r, "tu");
+       printf("%s %d\n", r.tags, r.numTags);
+       removeTag(&r, "");
+       printf("%s %d\n", r.tags, r.numTags);
+
+       row *r2 = newRow("test1test2test3testtesttesttesttesttettesttestestest");
 
-       while(0){
+       while(1){
 
                prompt();
                getInput(in);
                
-               char **arr;
-               int len;
-               split(in->buffer, ';', &arr, &len);
-               for(int i = 0; i < len; ++i)
-                       printf("%s\t", arr[i]);
-               printf("\n");
+               insertTag(r2, in->buffer);
+               printf("Tags of row '%s': %s\n", r2->path, r2->tags);
                
+
                /*switch(handleInput(in)){
                        case META_COMMAND_SUCCESS:
                                printf("we done it nigger\n");
diff --git a/repl.c b/repl.c
index d5418314377eb6566045bc997fe525ca15d1061b..c63ad447635b475fd11eadddc5e5a64aecfef8f2 100644 (file)
--- a/repl.c
+++ b/repl.c
@@ -5,6 +5,7 @@ inputBuffer *newInputBuffer(void){
        inputBuffer *in = malloc(sizeof(inputBuffer));
        in->buffer = NULL;
        in->inputSize = 0;
+       
        return in;
 }
 
index 043eeae07b1f99685faf9a0acf03fe6fdcbb93a4..5e7809b262f47e327824a3138c6591f6eb009e59 100644 (file)
--- a/storage.c
+++ b/storage.c
@@ -1,56 +1,44 @@
 #include "db.h"
 
 
+row *newRow(const char path[MAXPATH]){
+       row *nr = malloc(sizeof(row));
+       memcpy(nr->path, path, MAXPATH);
+       memcpy(nr->tags, "\0", MAXTAGS);
+       nr->numTags = 0;
 
+       return nr;
+}
 
 // Splits src into words based on a separator character (sep) and stores them in arr, and the length in len. Inspired by https://github.com/joshdk/tag/blob/master/src/dsv.c's split
 void split(const char *src, char sep, char ***arr, int *len){
-       int i = 0, asize = 1, ai = 0, wsize = 1, wi = 0;
-       char c = 0;
-
-       (*arr) = malloc(asize*sizeof(char*));
-       (*arr)[ai] = malloc(wsize*sizeof(char));
-
-       while((c = src[i]) != '\0'){
-               // If there's a new word (new array index) reallocate the array and allocate space for it
-               if(wi == 0){
-                       char **tmp;
-                       if((tmp = realloc((*arr), (asize+ai)*sizeof(char*))) != NULL){
-                               *arr = tmp;
-                               tmp = NULL;
-                       }else{
-                               fprintf(stderr, "Error reallocating array (split)");
-                               exit(EXIT_FAILURE);
-                       }
-                       (*arr)[ai] = malloc(wsize*sizeof(char));
-               }
-               // Allocate space for a new character in a word
-               char *tmp;
-               if((tmp = realloc((*arr)[ai], (wsize+wi)*sizeof(char))) != NULL){
-                       strcpy(tmp, (*arr)[ai]);
-                       (*arr)[ai] = tmp;
-                       tmp = NULL;
-               }else{
-                       fprintf(stderr, "Error reallocating word (split)");
-                       exit(EXIT_FAILURE);
+       int slen = 0, ai = 0, wnum = 0, wlen = 0;
+
+       while(src[slen] != '\0'){
+               if(src[slen] == sep){
+                       ++wnum;
                }
-               // If the character is a separator, terminate the string and increment the array index; if not, assign the character and increment the word index
-               if(c == sep){
-                       (*arr)[ai][wi] = '\0';
-                       wi = 0;
+               ++slen;
+       }
+       if(slen != 0 && src[slen-1] != sep){
+               ++wnum;
+       }
+       ++slen;
+
+       *arr = calloc((wnum+1), sizeof(char*));
+       for(int i = 0; i < slen; ++i){
+               if(src[i] == sep || src[i] == '\0'){
+                       (*arr)[ai] = calloc(wlen+1, sizeof(char));
+                       for(int j = i-wlen, k = 0; j < i; ++j, ++k){
+                               (*arr)[ai][k] = src[j];
+                       }
                        ++ai;
+                       wlen = 0;
                }else{
-                       (*arr)[ai][wi] = c;
-                       ++wi;
+                       ++wlen;
                }
-               ++i;
-       }
-       if(src[i-1] != sep){
-               (*arr)[ai][wi] = '\0';
-               ++ai;
        }
-       (*arr)[ai] = NULL;
-       *len = ai;
+       *len = wnum;
 }
 
 void swapWords(char ***arr, int a, int b){
@@ -70,14 +58,19 @@ char *normalizeTag(char *tag){
 
 // Adds a tag in the tags array in the row r, sorted by natural string comparison with strnatcmp
 // We assume that when adding a tag all other tags are already sorted
+// Nothing is done if the tag is already in the tags
 void insertTag(row *r, char *tag){
+       int l, ltag = len(tag);
+       if(ltag == 0){
+               return;
+       }
 
        tag = normalizeTag(tag);
 
        // Dump tags into array of strings and add tag
        char **arr, **tmp;
-       int l;
        split(r->tags, ';', &arr, &l);
+       
        if((tmp = realloc(arr, (l+1)*sizeof(char*))) != NULL){
                arr = tmp;
                tmp = NULL;
@@ -108,15 +101,56 @@ void insertTag(row *r, char *tag){
        ++l; // Succesfully added new tag
 
        // Insert tags back into tags member of row structure with the ';' separator in between them
-       int g = 0;
+       int tagnum = 0;
+       for(int i = 0; i < l; ++i){
+               int j = 0;
+               while(arr[i][j] != '\0'){
+                       r->tags[tagnum] = arr[i][j];
+                       ++j;
+                       ++tagnum;
+               }
+               r->tags[tagnum++] = ';';
+       }
+       r->tags[tagnum] = '\0';
+       r->numTags = l;
+}
+
+// Remove a tag from the tags array in the row r
+// Nothing is done if the tag isnt in the tags
+void removeTag(row *r, char *tag){
+       int l, ltag = len(tag);
+       if(ltag == 0){
+               return;
+       }
+
+       tag = normalizeTag(tag);
+
+       // Dump tags into array of strings
+       char **arr;
+       split(r->tags, ';', &arr, &l);
+
+       // Search for tag and remove it
+       for(int i = 0; i <= l; ++i){
+               if(sameStr(arr[i], tag)){
+                       for(int j = i; j < l; ++j){
+                               arr[j] = arr[j+1];
+                       }
+                       --l;
+                       break;
+               }
+       }
+
+       // Insert tags back into tags member of row structure with the ';' separator in between them
+       int tagnum = 0;
        for(int i = 0; i < l; ++i){
                int j = 0;
                while(arr[i][j] != '\0'){
-                       r->tags[g] = arr[i][j];
+                       r->tags[tagnum] = arr[i][j];
                        ++j;
-                       ++g;
+                       ++tagnum;
                }
-               r->tags[g++] = ';';
+               r->tags[tagnum++] = ';';
        }
-       r->tags[g] = '\0';
+       r->tags[tagnum] = '\0';
+       r->numTags = l;
 }
\ No newline at end of file
index 20cb329445b6f77c46dfae057b3b53729b8efb86..b37bdb86dfc4f2673cb97bc19d30abaca5aace6e 100644 (file)
--- a/storage.h
+++ b/storage.h
 typedef struct{
        char path[MAXPATH];
        char tags[MAXTAGS];
+       int numTags;
 } row;
 
 
+row *newRow(const char *path);
+
 void split(const char *src, char sep, char ***arr, int *len);
 
 void swapWords(char ***arr, int a, int b);
@@ -21,4 +24,6 @@ char *normalizeTag(char *tag);
 
 void insertTag(row *r, char *tag);
 
+void removeTag(row *r, char *tag);
+
 #endif
\ No newline at end of file
index 21370c3042e8e5d06a78bc61a48b89072b360a6b..46b794681c9b2bf7ba017ff2264e17c68d94e5e7 100644 (file)
@@ -134,7 +134,16 @@ static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
 
          /* process run of digits */
          if (nat_isdigit(ca)  &&  nat_isdigit(cb)) {
-              fractional = (ca == '0' || cb == '0');
+               /*
+                       Modified by Soikk for the purpose of DB
+                       To revert to original version, remove this comment,
+                       the uncommented code inside this if statement and
+                       uncomment the remaining piece of code inside this
+                       if statement
+               */
+                  if ((result = compare_right(a+ai, b+bi)) != 0)
+                   return result;
+              /*fractional = (ca == '0' || cb == '0');
 
               if (fractional) {
                    if ((result = compare_left(a+ai, b+bi)) != 0)
@@ -142,7 +151,7 @@ static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
               } else {
                    if ((result = compare_right(a+ai, b+bi)) != 0)
                         return result;
-              }
+              }*/
          }
 
          if (!ca && !cb) {