mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-08-12 10:20:46 +02:00
the code now list all the directories
This commit is contained in:
@@ -16,8 +16,8 @@ some way to specify the browser
|
|||||||
|
|
||||||
|
|
||||||
things to do:
|
things to do:
|
||||||
1. port vivify.vim to nixos
|
1. port vivify.vim to nixos (got problems see `https://discourse.nixos.org/t/help-with-adding-a-vim-plugin-in-nixpkgs/76682`)
|
||||||
|
2. i'm gonna work on the TUI for now.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"directory": "/home/tomasr/Documents/Notes/"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,5 +3,7 @@
|
|||||||
pkgs.mkShell {
|
pkgs.mkShell {
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
pkgs.vivify
|
pkgs.vivify
|
||||||
|
pkgs.cjson
|
||||||
|
pkgs.pkg-config
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <cjson/cJSON.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Read config file
|
||||||
|
FILE *f = fopen("./config.json", "r");
|
||||||
|
if (!f) {
|
||||||
|
fprintf(stderr, "Could not open config.json\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long size = ftell(f);
|
||||||
|
rewind(f);
|
||||||
|
|
||||||
|
char *data = malloc(size + 1);
|
||||||
|
size_t read_bytes = fread(data, 1, size, f);
|
||||||
|
if (read_bytes != size) {
|
||||||
|
fprintf(stderr, "Failed to read file\n");
|
||||||
|
free(data);
|
||||||
|
fclose(f);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
data[size] = 0;
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
// Parse JSON
|
||||||
|
cJSON *json = cJSON_Parse(data);
|
||||||
|
if (!json) {
|
||||||
|
fprintf(stderr, "JSON parse error\n");
|
||||||
|
free(data);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get "directory"
|
||||||
|
cJSON *dirJsonFormat = cJSON_GetObjectItem(json, "directory");
|
||||||
|
|
||||||
|
char *notesDirectoryString = NULL;
|
||||||
|
if (dirJsonFormat && cJSON_IsString(dirJsonFormat) && dirJsonFormat->valuestring != NULL) {
|
||||||
|
notesDirectoryString = strdup(dirJsonFormat->valuestring); // independent string
|
||||||
|
} else {
|
||||||
|
notesDirectoryString = "~/Documents/Notes/"; // default
|
||||||
|
}
|
||||||
|
|
||||||
|
// (TODO LATER) it might be a good idea to check if these directories exist
|
||||||
|
// (TODO LATER) expand ~ as it does not work with opendir()
|
||||||
|
printf("Opening %s\n", notesDirectoryString);
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------
|
||||||
|
// https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||||
|
struct dirent *notesDirectoryEntry;
|
||||||
|
DIR *notesDirectory = opendir(notesDirectoryString);
|
||||||
|
if (notesDirectory == NULL) // opendir returns NULL if couldn't open directory
|
||||||
|
{
|
||||||
|
printf("Could not open current directory" );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
||||||
|
// for readdir()
|
||||||
|
while ((notesDirectoryEntry = readdir(notesDirectory)) != NULL)
|
||||||
|
printf("%s\n", notesDirectoryEntry->d_name);
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// list all the directories (except hidden ones). They are like obsidian vaults
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
if (dirJsonFormat && notesDirectoryString != dirJsonFormat->valuestring) free(notesDirectoryString); // only free strdup
|
||||||
|
closedir(notesDirectory);
|
||||||
|
cJSON_Delete(json);
|
||||||
|
free(data);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user