aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoikk2022-05-06 02:50:20 +0200
committerSoikk2022-05-06 02:50:20 +0200
commitc8aa0e39e6a46ec5dc5bb0cb1a84821a8ea8b825 (patch)
tree2595b200e0783c15b9e94df902df215735c44a1b
parent7d227b89f4361057d571e9d34ce9fcd9ad68a182 (diff)
downloadsoikk-DB-c8aa0e39e6a46ec5dc5bb0cb1a84821a8ea8b825.tar.xz
soikk-DB-c8aa0e39e6a46ec5dc5bb0cb1a84821a8ea8b825.tar.zst
Late night modifications
-rw-r--r--src/main.c10
-rw-r--r--src/storage.c31
2 files changed, 26 insertions, 15 deletions
diff --git a/src/main.c b/src/main.c
index c3b2a67..636d0b4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,19 +6,19 @@ int main(){
inputBuffer *in = newInputBuffer();
- row r = {"C:/xd", "perro", 1, 5};
- printf("%s %d %d\n", r.tags, r.numTags, r.lenTags);
+ row r = {"C:/xd", "", 0, 0};
+ printf("%s %d %d\n", r.tags, r.numTags, r.lenTags);7
insertTag(&r, "caca");
insertTag(&r, "mierda");
- insertTag(&r, "perra");
+ insertTag(&r, "caca ");
insertTag(&r, "tu");
printf("%s %d %d\n", r.tags, r.numTags, r.lenTags);
printf("%d\n", strInTags(r.tags, r.lenTags, ";perro", 6, ';'));
- row *r2 = newRow("test1test2test3testtesttesttesttesttettesttestestest");
+ row *r2 = newRow("asa");
- while(0){
+ while(1){
prompt();
getInput(in);
diff --git a/src/storage.c b/src/storage.c
index f934c4f..c9dc5d0 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -2,9 +2,9 @@
row *newRow(const char path[MAXPATH]){
+ printf("len: %d\n", len(path));
row *nr = malloc(sizeof(row));
- memcpy(nr->path, path, MAXPATH);
- memcpy(nr->tags, "\0", MAXTAGS);
+ memcpy(nr->path, path, len(path));
nr->numTags = 0;
nr->lenTags = 0;
@@ -13,7 +13,7 @@ row *newRow(const char path[MAXPATH]){
// 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
-static void split(const char *src, char sep, char ***arr, int *len){
+static void split(const char *src, char sep, char ***arr, uint16_t *len){
int slen = 0, ai = 0, wnum = 0, wlen = 0;
while(src[slen] != '\0'){
@@ -49,12 +49,17 @@ static void swapWords(char ***arr, int a, int b){
(*arr)[b] = tmp;
}
-static char *normalizeTag(char *tag){
+static char *normalizeTag(char *tag, uint16_t *ln){
uint16_t l = len(tag);
char *ntag = calloc(l+1, sizeof(char));
for(int i = 0; i < l; ++i){
ntag[i] = tolower(tag[i]);
+ if(i == l-1 && tag[i] == ' '){
+ ntag[i] = '\0';
+ --l;
+ }
}
+ *ln = l;
return ntag;
}
@@ -62,12 +67,17 @@ static char *normalizeTag(char *tag){
// 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);
+ uint16_t l, ltag;
+ tag = normalizeTag(tag, &ltag);
+
if(ltag == 0){
return;
}
-
- tag = normalizeTag(tag);
+ if((r->lenTags+ltag) > MAXTAGS-1){
+ fprintf(stderr, "Can't insert tag '%s': too big (%d + %d > %d - 1) (insertTag)",
+ tag, r->lenTags, ltag, MAXTAGS);
+ exit(EXIT_FAILURE);
+ }
// Dump tags into array of strings and add tag
char **arr, **tmp;
@@ -95,6 +105,7 @@ void insertTag(row *r, char *tag){
i = 0;
break;
case 0:
+ printf("'%s' == '%s'\n", arr[i-1], arr[i]);
// The tag already exists, no need to alter anything
free(arr);
return;
@@ -121,13 +132,13 @@ void insertTag(row *r, char *tag){
// 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);
+ uint16_t l, ltag;
+ tag = normalizeTag(tag, &ltag);
+
if(ltag == 0){
return;
}
- tag = normalizeTag(tag);
-
// Dump tags into array of strings
char **arr;
split(r->tags, ';', &arr, &l);