-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.c
More file actions
164 lines (136 loc) · 3.59 KB
/
notes.c
File metadata and controls
164 lines (136 loc) · 3.59 KB
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
#include "notes.h"
#include "utils.h"
#include "dirutils.h"
// create a new note
void new(char *category, char *name)
{
// Category can't be null.
// Has to have an argument to create a folder.
if (category == NULL)
{
printf("Usage: note new <category> <name>\n");
return;
}
create_category(category);
// If name is null define a default name.
if (name == NULL)
{
name = "new_note";
}
char *filename = pathlloc(category, name);
if ((path_validation(filename)) == 0)
{
printf("File already exists!\n");
open_editor(filename);
free(filename);
return;
}
FILE *fptr = fopen(filename, "w");
if (fptr == NULL)
{
printf("ERROR: File creation was not possible\n");
return;
}
fclose(fptr);
open_editor(filename);
free(filename);
}
// opens a note to edit
void edit(char* category, char *name)
{
if (category == NULL)
{
open_editor(notes_path);
return;
}
char *filename = pathlloc(category, name);
if ((path_validation(filename)) != 0)
{
printf("ERROR: File or directory not found!\n");
free(filename);
return;
}
open_editor(filename);
return;
}
void renameDir(char *category_name, char *name, char *newname)
{
// Edita o nome de um carta
// talvez seja interessante deixar editar a categoria e as tags
if (category_name == NULL || name == NULL)
{
printf("Usage: notes rename <category> <old name> <new name>\n.");
return;
}
if (newname == NULL)
{
// get the desired folder name // get the folder names
char *filepath_name = pathlloc(name, NULL);
if (path_validation(filepath_name) == 0)
{
printf("This directory's name is already being used.\n");
return;
}
// to pathlloc not put .md
// we null the second argument
// get the older name
char *filepath = pathlloc(category_name, NULL);
// and renamed it
if (rename(filepath, filepath_name) != 0)
{
printf("Was not possible rename directory.\n");
return;
}
printf("'%s' directory changed to '%s'\n", category_name, name);
free(filepath);
free(filepath_name);
return;
}
// same thing, but here name its going to have
// an second argument in pathlloc to put .md in filenames
char *filepath = pathlloc(category_name, name);
char *filepath_name = pathlloc(category_name, newname);
if (rename(filepath, filepath_name) != 0)
{
printf("Was not possible rename directory.\n");
return;
}
printf("'%s' filenames changed to '%s'\n", name, newname);
free(filepath);
free(filepath_name);
return;
}
void removeNote(char* category, char* name)
{
if (category == NULL)
{
printf("Usage: notes remove <category> <name>");
return;
}
char *dirpath = pathlloc(category, name);
if ((path_validation(dirpath)) != 0)
{
free(dirpath);
printf("Directory don't exists.\n");
return;
}
if ((confirm_removal(category, name)) != 0)
{
free(dirpath);
return;
}
handle_path(dirpath, category, name);
free(dirpath);
return;
}
void backlink (char* keyword)
{
if (keyword == NULL)
{
printf("usage: notes backlink <keyword>");
return;
}
printf("Searching for '%s' in '%s' ... \n", keyword, notes_path);
searchInDir(notes_path, keyword, 2, 0);
return;
}