From: Soikk <76824648+Soikk@users.noreply.github.com> Date: Tue, 9 Aug 2022 00:04:02 +0000 (+0200) Subject: Improved database name system. X-Git-Url: https://git.xolatile.top/?a=commitdiff_plain;h=32dd785f881cf3ba6c1c6806cd3af3cbf56437c4;p=soikk-DB.git Improved database name system. --- diff --git a/CHANGELOG b/CHANGELOG index 2f43bc5..6c5c7ba 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +1660003394 (Tue Aug 09 2022 02:03:14 GMT+0200 (Central European Summer Time)) +Improved database name system. + 1659988523 (Mon Aug 08 2022 21:55:23 GMT+0200 (Central European Summer Time)) Minor changes. diff --git a/include/str.h b/include/str.h index 999f1e3..7f51996 100644 --- a/include/str.h +++ b/include/str.h @@ -12,6 +12,10 @@ char *normalizeStr(const char *str, uint32_t *ln); char *normalizeStrLimit(const char *str, uint32_t *l, uint32_t limit); +char *trimStr(const char *str, uint32_t *l); + +char *trimStrLimit(const char *str, uint32_t *l, uint32_t limit); + ssize_t strInTags(const char *tags, int n, const char *ndl, int m, char sep); #endif diff --git a/src/database.c b/src/database.c index 5b03b4b..461e02d 100644 --- a/src/database.c +++ b/src/database.c @@ -3,7 +3,9 @@ database *newDatabase(char *name){ database *db = malloc(sizeof(database)); - memcpy(db->name, name, len(name)+1); + uint32_t l; + name = trimStrLimit(name, &l, 31); + memcpy(db->name, name, l+1); db->lfiles = newLtable(0); db->ltags = newLtable(0); db->cfiles = newCtable(0); @@ -277,7 +279,7 @@ void printDatabase(database *db){ void debugAVLtree(node *n){ if(n != NULL){ - printf("\t\t+%" PRIu64 " -> %" PRIu64 "\n", n->h, n->i); + printf("\t\t+ %" PRIu64 " -> %" PRIu64 "\n", n->h, n->i); debugAVLtree(n->left); debugAVLtree(n->right); } @@ -288,11 +290,11 @@ void debugDatabase(database *db){ printf("Name: %s\n", db->name); printf("\t-lfiles: %d\n", db->lfiles->size); for(uint64_t i = 0; i < db->lfiles->size; ++i){ - printf("\t\t+[%" PRIu64 "] %s (%" PRIu64 ")\n", i, db->lfiles->table[i], db->cfiles->table[i]); + printf("\t\t+ %s (%" PRIu64 ")\n", db->lfiles->table[i], db->cfiles->table[i]); } printf("\t-ltags: %d\n", db->ltags->size); for(uint64_t i = 0; i < db->ltags->size; ++i){ - printf("\t\t+[%" PRIu64 "] %s (%" PRIu64 ")\n", i, db->ltags->table[i], db->ctags->table[i]); + printf("\t\t+ %s (%" PRIu64 ")\n", db->ltags->table[i], db->ctags->table[i]); } printf("\t-hfiles: %d\n", db->lfiles->size); debugAVLtree(db->hfiles); @@ -300,7 +302,7 @@ void debugDatabase(database *db){ debugAVLtree(db->htags); printf("\t-map: %d\n", db->map->size); for(uint64_t i = 0; i < db->map->size; ++i){ - printf("\t\t+[%" PRIu64 "] %" PRIu64 ":%" PRIu64 "\n", i, db->map->table[i].file, db->map->table[i].tag); + printf("\t\t+ %" PRIu64 ":%" PRIu64 "\n", db->map->table[i].file, db->map->table[i].tag); } printf("\n"); } diff --git a/src/main.c b/src/main.c index b6a21bc..b24dcb8 100644 --- a/src/main.c +++ b/src/main.c @@ -4,11 +4,9 @@ int main(){ - inputBuffer *in = newInputBuffer(); database *db = newDatabase("miDB"); - - + addFileTag(db, "vaca.png", "naturalezas"); addFileTags(db, "terry-davis.jpg", 3, "holyC", "programmer", "very cool"); addFileTag(db, "vaca.png", "lovely"); @@ -30,13 +28,6 @@ int main(){ } - addTagFiles(db, "elemento", 2, "vaca.png", "terry-davis.jpg"); - - printDatabase(db); - - debugDatabase(db); - - while(0){ diff --git a/src/str.c b/src/str.c index d896868..ce43b8f 100644 --- a/src/str.c +++ b/src/str.c @@ -44,6 +44,36 @@ char *normalizeStrLimit(const char *str, uint32_t *l, uint32_t limit){ return nstr; } +// Trims trailing spaces +char *trimStr(const char *str, uint32_t *l){ + *l = len(str); + uint32_t trw = 0; + while(isspace(str[--(*l)])) + ++trw; + char *nstr = calloc(++(*l)+1, sizeof(char)); + for(int i = 0; i < *l; ++i) + nstr[i] = str[i]; + return nstr; +} + +// Same as trimStr but with a limit (str[limit] will be equal to '\0') +// If limit is 0, it will return NULL +// WARNING: It allocates limit+1 characters +char *trimStrLimit(const char *str, uint32_t *l, uint32_t limit){ + if(limit == 0){ + return NULL; + } + *l = len(str); + *l = (*l > limit) ? limit : *l; + uint32_t trw = 0; + while(isspace(str[--(*l)])) + ++trw; + char *nstr = calloc(++(*l)+1, sizeof(char)); + for(int i = 0; i < *l; ++i) + nstr[i] = str[i]; + return nstr; +} + // Auxiliary function for creating a lookup table of the haystack // table[i] will be the number of shifts right until the next // separator when checking position i