diff --git a/src/main.c b/src/main.c index 345f897..ce4c807 100644 --- a/src/main.c +++ b/src/main.c @@ -487,7 +487,6 @@ next_arg: } } - qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically debug("┌--------------------------------\nAvailable vaults:"); if (shouldDebug) { for (int i = 0; i < vaultsCount; i++) { diff --git a/src/notes.c b/src/notes.c index 7b685ed..a016d76 100644 --- a/src/notes.c +++ b/src/notes.c @@ -3,17 +3,24 @@ char *getDirectoryFromVault(char *targetVault, char **vaultsArray, int vaultTotalNumber, int *vaultNumberPerDirectory, char **directoryArray, int directoryNumber, int shouldDebug) { debug("Searching the vault %s inside all the directories...", targetVault); - int index = 0; - int startIndex = 0; + debug("Here are how many vaults there is per directory:"); for (int i = 0; i < directoryNumber; i++) { - while (index - startIndex < vaultNumberPerDirectory[i]) { - if (strcmp(vaultsArray[index], targetVault) == 0) { // we found it - debug("%s found in %s. (This was the %dth step of a maximum of %d)", targetVault, directoryArray[i], index, vaultTotalNumber); - return directoryArray[i]; + altDebug("%d for %s\n", vaultNumberPerDirectory[i], directoryArray[i]); + } + debug("Here are all the vaults in the order they will be searched:"); + for (int i = 0; i < vaultTotalNumber; i++) { + altDebug("%s\n", vaultsArray[i]); + } + int index = 0; + + for (int i = 0; i < directoryNumber; i++) { + for (int j = 0; j < vaultNumberPerDirectory[i]; j++) { + if (strcmp(vaultsArray[index], targetVault) == 0) { + debug("%s found in %s. (index %d)", targetVault, directoryArray[i], index); + return directoryArray[i]; + } + index++; } - index++; - } - startIndex += vaultNumberPerDirectory[i]; } error(1, "program", "the vault %s was not found", targetVault); return "this makes the compiler and clangd happy. Who doesn't want GCC and clangd to be happy? Such person would be a terrible monster... One must imagine GCC and clangd happy."; @@ -112,6 +119,7 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber // to avoid having to work with a tree-dimensional array. We will use a 2d and vaultsPerDirectoryNumber will indicate the width of the directories. char **vaultsArray = NULL; int nthVault = 0; // this is only used internally to set the string into the right place in directoryStringArray. + int previousStartIndex = 0; for (int i = 0; i < directoryNumber; i++) { debug("Opening %s", directoryStringArray[i]); // originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/ @@ -146,7 +154,10 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber altDebug(" is not a vault (hidden file/dir)\n"); } } + // we sort entries for the same vault. We can't sort them all. This would break the order. + qsort(vaultsArray + previousStartIndex, nthVault - previousStartIndex, sizeof(const char *), compareString); // sorts the vaults alphabetically closedir(vaultsDirectory); + previousStartIndex = nthVault; } // we calculate once the total number of vaults to avoid recalculation every time we use it *count = 0; @@ -167,7 +178,7 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int *journalWas sanitize(dateWithExtension); debug("Sanitized date: %s\n(it might be used later for a file name if the journal is divided)", dateWithExtension); struct stat metadata; - error(stat(path, &metadata), "program", "stat() failed to get information about %s", path); + stat(path, &metadata); if (S_ISREG(metadata.st_mode)) { debug("%s is a unified journal.", path); if (!isStringInFile(path, date, shouldDebug)) { // if there is no entry for current date diff --git a/src/ui.c b/src/ui.c index caf2907..acf7c5d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -112,7 +112,7 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c snprintf(fileFullPath, PATH_MAX, "%s/%s/%s/", dirToVault, vaultFromDir, fileName); struct stat st = {0}; if (stat(fileFullPath, &st) == -1) { - error(!mkdir(fileFullPath, 0744), "program", "mkdir failed"); + error(mkdir(fileFullPath, 0744), "program", "mkdir failed"); } else { error(1, "program", "%s could not be created", fileFullPath); }