-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
212 lines (185 loc) · 5.64 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* See LICENSE for license details */
/*
▒██ █ ███
█░ █
█ █
█████ ███ █ ███ ▓██▒
█ █ █ ▓▓ ▒█ ▓█ ▓
█ █ █ █ █ █░
█ █ █ █████ █
█ █ █ █ █░
█ █ █░ ▓▓ █ █ ▓█ ▓
█ █████ ▒██ ███▒ █ ▓██▒
-------------------------------------------
Author: Nicola Ferru aka NFVblog
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <git2.h>
#include "consts.h"
#include "timeGet.h"
#include "config.h"
void help();
void list_formats();
void create_file(char name[], char format[], int date_form, char title[]);
void remove_file(char cat[], char name[]);
bool verformat(char *str);
void list_directory(char *str);
void engage(char *format,char *title, char *date, FILE *f);
void openEditor(char *file);
void verDir();
bool checkFile(const char *filename);
void openFile(const char *filename, const char *cat);
void printerr(char *err, int err_code);
void search(char *str);
// Functions developed
void help ()
{
printf("┏━┛┏━ ┏━┃━┏┛┏━┛┏━┛\n");
printf("━━┃┃ ┃┃ ┃ ┃ ┏━┛━━┃\n");
printf("━━┛┛ ┛━━┛ ┛ ━━┛━━┛\n\
------------------------------------------------------------------------------\n");
printf(" \
--help [-h]: Print help with all commands\n \
--formats-list [-F]: View the list of formats\n \
--remove [-R]: remove notes or directories\n \
--category [-c]: search by category\n \
--list-directory [-l]: list of directory\n \
--tree-of-directory [-T]: Print all the contents of the Notes directory\n");
}
// file manager
void list_formats()
{
printf("Format list:\n- html \n- Org Mode\n- Markdown\n");
}
void create_file(char name[], char format[], int date_form, char title[])
{
char file[MAX_DIM_NAME_FILE];
FILE *note;
strcat(file,def_direct);
char *dateNote = getDate(date_form);
strcat(file,dateNote);
strcat(file,"/");
verDir(file);
strcat(file,name);
strcat(file,".");
strcat(file,format);
if (checkFile(file)){
printerr("The file already exists",-2);
} else {
note = fopen(file, "w");
engage(format,title,dateNote,note);
fclose(note);
printf("The note was created in the format %s with the name %s in %s\n", format,name,file);
// open text editor
openEditor(file);
}
}
bool verformat(char str[])
{
// Function that checks for supported formats
if(!strcmp(str, "org") || !strcmp(str, "md") || !strcmp(str, "html") || !strcmp(str, "txt"))
return true;
else return false;
}
void remove_file(char cat[], char name[])
{
char file[MAX_DIM_NAME_FILE];
int removed;
FILE *note;
strcat(file,def_direct);
strcat(file,cat);
strcat(file,"/");
strcat(file,name);
removed = remove(file);
if(removed == 0) {
printf("Note deleted successfully\n");
} else {
printerr("Note does not exist",-6);
}
}
void search(char *str)
{
char directory[MAX_DIM_NAME_FILE];
strcat(directory,def_direct);
strcat(directory,str);
DIR *d;
struct dirent *dir;
d = opendir(directory);
if (d) {
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}
closedir(d);
} else printerr("the directory does not exist",-4);
}
void engage(char *format, char *title, char *date, FILE *f)
{
if (!strcmp("org", format))
fprintf (f, "#+author: %s\n#+title: %s\n#+date: %s %s \n\n* %s\n",author, title,\
date,getHours_note(),title);
else if (!strcmp("md", format))
fprintf (f, "---\nauthor: %s\ntitle: %s\ndate: %s %s\n--- \n\n# %s\n",author, title,\
date,getHours_note(),title);
else if (!strcmp("html", format))
fprintf (f, "<!-- author: %s title: %s date: %s -->\n <!DOCTYPE html>\n <html lang=\"%s\">\n<head>\n<title>%s</title>\n<meta charset=\"utf-8\">\n</head>\n<body>\n \
<h1>%s<h1>\n\n</body>\n</html>",author, title, date,def_lang,title,title,title);
else if (!strcmp("txt", format))
fprintf (f, "author: %s\ntitle: %s\ndate: %s %s \n------------\n",author, title, \
date, getHours_note());
else if (!strcmp("ASCIIMathML", format))
fprintf (f, "<!-- author: %s title: %s date: %s -->\n<!DOCTYPE html>\n <html lang=\"%s\">\n<head>\n<script type=\"text/javascript\" src=\"ASCIIMathML.js\"></script>\n<title>%s</title>\n<meta charset=\"utf-8\">\n</head>\n<body>\n \
<h1>%s<h1>\n\n</body>\n</html>",author, title, date,def_lang,title,title,title);
}
void openEditor(char *file)
{
char cmd[MAX_DIM_NAME_FILE*2];
strcat(cmd,editor);
strcat(cmd," ");
strcat(cmd,file);
strcat(cmd," &");
system(cmd);
}
void verDir(char *src)
{
struct stat st = {0};
if (stat(src, &st) == -1)
mkdir(src, 0700);
}
bool checkFile(const char *filename)
{
FILE *file;
if (file = fopen(filename, "r"))
{
fclose(file);
return true;
}
return false;
}
void openFile(const char *filename, const char *cat)
{
char file[MAX_DIM_NAME_FILE];
strcat(file,def_direct);
strcat(file,cat);
strcat(file,"/");
strcat(file,filename);
//printf("%s",file); // debug instruction
if(checkFile(file))
{
openEditor(file);
}
}
// print instruction for errors
void printerr(char *err, int err_code)
{
printf("Error %d: %s\n",err_code, err);
exit(err_code);
}