mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-08-11 18:18:36 +02:00
Separated notes.c from utils.c
This commit is contained in:
@@ -14,7 +14,7 @@ endif
|
||||
|
||||
# Sources and target
|
||||
SRC_DIR := src
|
||||
SRC := $(SRC_DIR)/main.c $(SRC_DIR)/ui.c $(SRC_DIR)/utils.c
|
||||
SRC := $(SRC_DIR)/main.c $(SRC_DIR)/ui.c $(SRC_DIR)/utils.c $(SRC_DIR)/notes.c
|
||||
TARGET := notewrapper
|
||||
|
||||
# Phony targets
|
||||
|
||||
+3
-1
@@ -1,5 +1,6 @@
|
||||
#include "ui.h"
|
||||
#include "utils.h"
|
||||
#include "notes.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -110,6 +111,7 @@ int main(int argc, char *argv[]) {
|
||||
// flags and arguments overwrite the config
|
||||
// (TODO LATER) maybe add a way to combine flags (such which rm -fr?)
|
||||
// (TODO LATER) add a version flag
|
||||
// (TODO LATER) separate -n for journals from notes
|
||||
char *bypassVaultSelection = HASH_MACRO; // (TODO LATER) find a better idea. If later it detects other string than that 256 string. It will bypass the selection
|
||||
char *bypassNoteSelection = HASH_MACRO;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
@@ -244,7 +246,7 @@ note_selection:
|
||||
goto note_creation;
|
||||
}
|
||||
}
|
||||
noteSelected = ncursesSelect(filesArray, "Select note (Use arrows or WASD, Enter to select):", filesCount, extraNotesOptions, shouldDebug);
|
||||
noteSelected = ncursesSelect(filesArray, "Select note or journal (Use arrows or WASD, Enter to select):", filesCount, extraNotesOptions, shouldDebug);
|
||||
// now that we won't use filesArray in this iteration of the loop, we should free it and all its elements. (As this is memory in the heap and not the stack and thus is our responsability to manage)
|
||||
for (int i = 0; i < filesCount; i++) {
|
||||
if (noteSelected != filesArray[i]) { // we must prevent noteSelected to be freed. It will cause a lot of problems
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
#include "notes.h"
|
||||
|
||||
char** getVaultsFromDirectory(char *dirString, size_t *count, int shouldDebug) {
|
||||
// (TODO LATER) it might be a good idea to check if these directories exist
|
||||
// (TODO LATER) expand ~ as it does not work with opendir()
|
||||
// this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes
|
||||
debug("Opening %s", dirString);
|
||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||
struct dirent *vaultsDirectoryEntry;
|
||||
DIR *vaultsDirectory = opendir(dirString);
|
||||
error(vaultsDirectory==NULL, "program", "Could not open directory %s", dirString);
|
||||
char **dirsArray = NULL; // will contain all the dirs/vaults
|
||||
size_t dirsCount = 0; // we need to count how many dirs there is to always readjust how many memory we alloc
|
||||
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||
// for readdir()
|
||||
debug("┌------------------------------\n Detected files and dirs from the directory:");
|
||||
while ((vaultsDirectoryEntry = readdir(vaultsDirectory)) != NULL) {
|
||||
altDebug("%s\n", vaultsDirectoryEntry->d_name);
|
||||
if (vaultsDirectoryEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
|
||||
char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght
|
||||
snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s", dirString, vaultsDirectoryEntry->d_name); // sets the full absolute path to fullPathEntry
|
||||
|
||||
struct stat metadataPathEntry;
|
||||
if (stat(fullPathEntry, &metadataPathEntry) == 0 && S_ISDIR(metadataPathEntry.st_mode)) { // if this entry is a directory
|
||||
dirsArray = realloc(dirsArray, (dirsCount + 1)*sizeof(char*)); // resize dirsArray so that
|
||||
dirsArray[dirsCount] = strdup(vaultsDirectoryEntry->d_name); // copy the dir name into dirsArray
|
||||
dirsCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
altDebug("└------------------------------\n");
|
||||
// (TODO LATER) Alphabetically sort them
|
||||
|
||||
// free's some used memory
|
||||
closedir(vaultsDirectory);
|
||||
*count = dirsCount;
|
||||
return dirsArray;
|
||||
}
|
||||
|
||||
|
||||
char** getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug) {
|
||||
// this function is inputed a path to a vault (which was selected before) and outpus all the suitable notes (so not the hidden ones)
|
||||
// (TODO LATER) Check how it handles non .md files
|
||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||
debug("Searching %s for notes", vault);
|
||||
struct dirent *vaultEntry; // (TODO LATER) change name of these variables. notesDirectory is dumb as it is the directory of vaults
|
||||
char tempPath[PATH_MAX];
|
||||
snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry
|
||||
DIR *vaultDirectory = opendir(tempPath);
|
||||
error(vaultDirectory==NULL, "program", "Could not open directory %s", tempPath);
|
||||
char **notesArray = NULL; // will contain all the notes
|
||||
size_t notesCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc
|
||||
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||
// for readdir()
|
||||
debug("┌------------------------------\nDetected Files and dirs from the vault:");
|
||||
while ((vaultEntry = readdir(vaultDirectory)) != NULL) {
|
||||
|
||||
altDebug("%s ", vaultEntry->d_name);
|
||||
// for the regex code https://stackoverflow.com/a/1085120
|
||||
regex_t regex;
|
||||
int regexReturn;
|
||||
// compiles the regex
|
||||
regexReturn = regcomp(®ex, journalRegex, 0);
|
||||
if (vaultEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
|
||||
char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght
|
||||
snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s/%s", pathToVault, vault, vaultEntry->d_name); // sets the full absolute path to fullPathEntry
|
||||
// check if it matches the regex. If it does not match regexReturn != 0.
|
||||
regexReturn = regexec(®ex, vaultEntry->d_name, 0, NULL, 0);
|
||||
struct stat metadataPathEntry;
|
||||
if (stat(fullPathEntry, &metadataPathEntry) == 0 && regexReturn && !S_ISDIR(metadataPathEntry.st_mode)) { // if this entry is a file
|
||||
notesArray = realloc(notesArray, (notesCount + 1)*sizeof(char*)); // resize notesArray so that
|
||||
notesArray[notesCount] = strdup(vaultEntry->d_name); // copy the dir name into notesArray
|
||||
notesCount++;
|
||||
altDebug("did not match with the regex. It is a note.\n");
|
||||
} else if (!regexReturn) {altDebug("matched with the regex. It is a journal.\n");}
|
||||
} else {altDebug("was ignored\n");}
|
||||
}
|
||||
altDebug("└ ------------------------------\n");
|
||||
// (TODO LATER) Alphabetically sort them
|
||||
// free's some used memory
|
||||
closedir(vaultDirectory);
|
||||
*count = notesCount; // passes the number of files
|
||||
return notesArray;
|
||||
}
|
||||
|
||||
|
||||
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug) {
|
||||
debug("Searching %s for journals", vault);
|
||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||
struct dirent *vaultEntry;
|
||||
char tempPath[PATH_MAX];
|
||||
snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry
|
||||
DIR *vaultDirectory = opendir(tempPath);
|
||||
error(vaultDirectory==NULL, "program", "Could not open directory %s", tempPath);
|
||||
char **journalsArray = NULL; // will contain all the notes
|
||||
size_t journalsCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc
|
||||
|
||||
// https://stackoverflow.com/a/1085120 for regex code
|
||||
regex_t regex;
|
||||
int regexReturn;
|
||||
// compiles the regex
|
||||
regexReturn = regcomp(®ex, journalRegex, 0);
|
||||
error(regexReturn, "program", "Regex could not compile. Perhaps there is an error with the regex string");
|
||||
debug("Regex compiled succesfully");
|
||||
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||
// for readdir()
|
||||
debug("┌------------------------------------------\nDetected files and dirs from the vault");
|
||||
while ((vaultEntry = readdir(vaultDirectory)) != NULL) { // we iterate over every entry from the dir. So files and dirs (. and .. included)
|
||||
altDebug("%s ", vaultEntry->d_name);
|
||||
if (vaultEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
|
||||
regexReturn = regexec(®ex, vaultEntry->d_name, 0, NULL, 0);
|
||||
if (!regexReturn) { // if the regex matches
|
||||
altDebug("matched with the regex. It is a journal.\n");
|
||||
journalsArray = realloc(journalsArray, (journalsCount + 1)*sizeof(char*)); // resize notesArray so that
|
||||
journalsArray[journalsCount] = strdup(vaultEntry->d_name); // copy the dir name into notesArray
|
||||
journalsCount++;
|
||||
} else {
|
||||
altDebug("did not matched with the regex. It is a note.\n");
|
||||
}
|
||||
} else {
|
||||
altDebug(" was ignored\n");
|
||||
}
|
||||
}
|
||||
altDebug("└ ------------------------------\n");
|
||||
// free's some used memory
|
||||
closedir(vaultDirectory);
|
||||
*count = journalsCount; // passes the number of files
|
||||
return journalsArray;
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
#ifndef NOTES_H
|
||||
#define NOTES_H
|
||||
#include "utils.h"
|
||||
#include "ui.h"
|
||||
char **getVaultsFromDirectory(char *dirString, size_t *count, int debug);
|
||||
// this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes
|
||||
char **getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int debug);
|
||||
// this function is inputed a path to a vault (which was selected before) and outpus all the suitable notes (so not the hidden ones)
|
||||
// journalRegex is the regex code for the journals. If a note matches this code, it is treated as a journal and it is not outputed from this function.
|
||||
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int debug);
|
||||
// gets the journals from the vault
|
||||
// to distinguish journals from note we use regex.
|
||||
// Note: we must handle them separatly as journals can either be a single file (which will treat specially) or a directory
|
||||
#endif
|
||||
@@ -47,6 +47,7 @@ input_screen:
|
||||
}
|
||||
|
||||
char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, char *bypass, int shouldDebug) {
|
||||
// (TODO LATER) Add code to create journal
|
||||
// (TODO LATER) Add check. If the user creates a note with a name that already exists. it erases the old one
|
||||
// input from user for the name
|
||||
char fileName[256];
|
||||
|
||||
-128
@@ -116,134 +116,6 @@ int rmrf(char *path) {
|
||||
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
|
||||
}
|
||||
|
||||
char** getVaultsFromDirectory(char *dirString, size_t *count, int shouldDebug) {
|
||||
// (TODO LATER) it might be a good idea to check if these directories exist
|
||||
// (TODO LATER) expand ~ as it does not work with opendir()
|
||||
// this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes
|
||||
debug("Opening %s", dirString);
|
||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||
struct dirent *vaultsDirectoryEntry;
|
||||
DIR *vaultsDirectory = opendir(dirString);
|
||||
error(vaultsDirectory==NULL, "program", "Could not open directory %s", dirString);
|
||||
char **dirsArray = NULL; // will contain all the dirs/vaults
|
||||
size_t dirsCount = 0; // we need to count how many dirs there is to always readjust how many memory we alloc
|
||||
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||
// for readdir()
|
||||
if (shouldDebug) {printf("┌------------------------------\n\e[0;32m[DEBUG]\e[0m Files and dirs from the directory\n");}
|
||||
debug("┌------------------------------\n Detected files and dirs from the directory:");
|
||||
while ((vaultsDirectoryEntry = readdir(vaultsDirectory)) != NULL) {
|
||||
altDebug("%s\n", vaultsDirectoryEntry->d_name);
|
||||
if (vaultsDirectoryEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
|
||||
char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght
|
||||
snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s", dirString, vaultsDirectoryEntry->d_name); // sets the full absolute path to fullPathEntry
|
||||
|
||||
struct stat metadataPathEntry;
|
||||
if (stat(fullPathEntry, &metadataPathEntry) == 0 && S_ISDIR(metadataPathEntry.st_mode)) { // if this entry is a directory
|
||||
dirsArray = realloc(dirsArray, (dirsCount + 1)*sizeof(char*)); // resize dirsArray so that
|
||||
dirsArray[dirsCount] = strdup(vaultsDirectoryEntry->d_name); // copy the dir name into dirsArray
|
||||
dirsCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
altDebug("└------------------------------\n");
|
||||
// (TODO LATER) Alphabetically sort them
|
||||
|
||||
// free's some used memory
|
||||
closedir(vaultsDirectory);
|
||||
*count = dirsCount;
|
||||
return dirsArray;
|
||||
}
|
||||
|
||||
|
||||
char** getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug) {
|
||||
// this function is inputed a path to a vault (which was selected before) and outpus all the suitable notes (so not the hidden ones)
|
||||
// (TODO LATER) Check how it handles non .md files
|
||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||
debug("Searching %s for notes", vault);
|
||||
struct dirent *vaultEntry; // (TODO LATER) change name of these variables. notesDirectory is dumb as it is the directory of vaults
|
||||
char tempPath[PATH_MAX];
|
||||
snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry
|
||||
DIR *vaultDirectory = opendir(tempPath);
|
||||
error(vaultDirectory==NULL, "program", "Could not open directory %s", tempPath);
|
||||
char **notesArray = NULL; // will contain all the notes
|
||||
size_t notesCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc
|
||||
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||
// for readdir()
|
||||
debug("┌------------------------------\nDetected Files and dirs from the vault:");
|
||||
while ((vaultEntry = readdir(vaultDirectory)) != NULL) {
|
||||
|
||||
altDebug("%s ", vaultEntry->d_name);
|
||||
// for the regex code https://stackoverflow.com/a/1085120
|
||||
regex_t regex;
|
||||
int regexReturn;
|
||||
// compiles the regex
|
||||
regexReturn = regcomp(®ex, journalRegex, 0);
|
||||
if (vaultEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
|
||||
char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght
|
||||
snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s/%s", pathToVault, vault, vaultEntry->d_name); // sets the full absolute path to fullPathEntry
|
||||
// check if it matches the regex. If it does not match regexReturn != 0.
|
||||
regexReturn = regexec(®ex, vaultEntry->d_name, 0, NULL, 0);
|
||||
struct stat metadataPathEntry;
|
||||
if (stat(fullPathEntry, &metadataPathEntry) == 0 && regexReturn && !S_ISDIR(metadataPathEntry.st_mode)) { // if this entry is a file
|
||||
notesArray = realloc(notesArray, (notesCount + 1)*sizeof(char*)); // resize notesArray so that
|
||||
notesArray[notesCount] = strdup(vaultEntry->d_name); // copy the dir name into notesArray
|
||||
notesCount++;
|
||||
altDebug("did not match with the regex. It is a note.\n");
|
||||
} else if (!regexReturn) {altDebug("matched with the regex. It is a journal.\n");}
|
||||
} else {altDebug("was ignored\n");}
|
||||
}
|
||||
altDebug("└ ------------------------------\n");
|
||||
// (TODO LATER) Alphabetically sort them
|
||||
// free's some used memory
|
||||
closedir(vaultDirectory);
|
||||
*count = notesCount; // passes the number of files
|
||||
return notesArray;
|
||||
}
|
||||
|
||||
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug) {
|
||||
debug("Searching %s for journals", vault);
|
||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||
struct dirent *vaultEntry;
|
||||
char tempPath[PATH_MAX];
|
||||
snprintf(tempPath, sizeof(tempPath), "%s/%s", pathToVault, vault); // sets the full absolute path to fullPathEntry
|
||||
DIR *vaultDirectory = opendir(tempPath);
|
||||
error(vaultDirectory==NULL, "program", "Could not open directory %s", tempPath);
|
||||
char **journalsArray = NULL; // will contain all the notes
|
||||
size_t journalsCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc
|
||||
|
||||
// https://stackoverflow.com/a/1085120 for regex code
|
||||
regex_t regex;
|
||||
int regexReturn;
|
||||
// compiles the regex
|
||||
regexReturn = regcomp(®ex, journalRegex, 0);
|
||||
error(regexReturn, "program", "Regex could not compile. Perhaps there is an error with the regex string");
|
||||
debug("Regex compiled succesfully");
|
||||
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||
// for readdir()
|
||||
debug("┌------------------------------------------\nDetected files and dirs from the vault");
|
||||
while ((vaultEntry = readdir(vaultDirectory)) != NULL) { // we iterate over every entry from the dir. So files and dirs (. and .. included)
|
||||
altDebug("%s ", vaultEntry->d_name);
|
||||
if (vaultEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
|
||||
regexReturn = regexec(®ex, vaultEntry->d_name, 0, NULL, 0);
|
||||
if (!regexReturn) { // if the regex matches
|
||||
altDebug("matched with the regex. It is a journal.\n");
|
||||
journalsArray = realloc(journalsArray, (journalsCount + 1)*sizeof(char*)); // resize notesArray so that
|
||||
journalsArray[journalsCount] = strdup(vaultEntry->d_name); // copy the dir name into notesArray
|
||||
journalsCount++;
|
||||
} else {
|
||||
altDebug("did not matched with the regex. It is a note.\n");
|
||||
}
|
||||
} else {
|
||||
altDebug(" was ignored\n");
|
||||
}
|
||||
}
|
||||
altDebug("└ ------------------------------\n");
|
||||
// free's some used memory
|
||||
closedir(vaultDirectory);
|
||||
*count = journalsCount; // passes the number of files
|
||||
return journalsArray;
|
||||
}
|
||||
|
||||
|
||||
int openEditor(char *path, char *editor, int render, int endOfFile, int shouldDebug) {
|
||||
// (TODO LATER) Bug app breaks if browser was not already launched before vivify
|
||||
|
||||
@@ -49,15 +49,6 @@ void sanitize(char *string);
|
||||
int rmrf(char *path);
|
||||
//from https://stackoverflow.com/a/5467788
|
||||
//deletes an entire directory. Use with parsimony and carefullness
|
||||
char **getVaultsFromDirectory(char *dirString, size_t *count, int debug);
|
||||
// this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes
|
||||
char **getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int debug);
|
||||
// this function is inputed a path to a vault (which was selected before) and outpus all the suitable notes (so not the hidden ones)
|
||||
// journalRegex is the regex code for the journals. If a note matches this code, it is treated as a journal and it is not outputed from this function.
|
||||
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int debug);
|
||||
// gets the journals from the vault
|
||||
// to distinguish journals from note we use regex.
|
||||
// Note: we must handle them separatly as journals can either be a single file (which will treat specially) or a directory
|
||||
int openEditor(char *path, char *editor, int render, int endOfFile, int debug);
|
||||
// Inputs are the path to the file, the editor to open and some rendering option
|
||||
// render: if we render the .md file with Vivify
|
||||
|
||||
Reference in New Issue
Block a user