-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg-shortcuts.h
117 lines (111 loc) · 3.02 KB
/
msg-shortcuts.h
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
#ifndef SHORTCUT_H
#define SHORTCUT_H
#include "constants.h"
#include "file-util.h"
#include "data.h"
#include <stdbool.h>
bool get_message_from_shortcut(char* message, const char* shortcut)
{
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
chdir(get_config_path(true));
//
FILE* file = fopen("message-shortcuts.txt", "r");
if (file == NULL) return false;
char shrt[SHORTCUT_MAX], msg[COMMIT_MESSAGE_MAX];
while ( fscanf(file, "%[^~]", shrt) != EOF ) {
fscanf(file, "~%[^~]~\n", msg);
if ( strcmp(shrt, shortcut) == 0 ) {
fclose(file);
chdir(original_path);
if (message != NULL)
strcpy(message, msg);
return true;
}
}
fclose(file);
//
chdir(original_path);
return false;
}
bool add_new_shortcut(char* shortcut, char* message)
{
if ( get_message_from_shortcut(NULL, shortcut) )
return false;
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
chdir(get_config_path(true));
//
FILE* file = fopen("message-shortcuts.txt", "a");
fprintf(file, "%s~%s~\n", shortcut, message);
fclose(file);
//
chdir(original_path);
return true;
}
bool replace_shortcut(char* shortcut, char* message)
{
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
chdir(get_config_path(true));
//
FILE* file = fopen("message-shortcuts.txt", "r");
FILE* tmp = fopen("tmp", "w");
if (file == NULL) return false;
bool found = false;
char shrt[SHORTCUT_MAX], msg[COMMIT_MESSAGE_MAX];
while ( fscanf(file, "%[^~]", shrt) != EOF ) {
fprintf(tmp, "%s", shrt);
fscanf(file, "~%[^~]~\n", msg);
if ( strcmp(shrt, shortcut) == 0 ) {
fprintf(tmp, "~%s~\n", message);
found = true;
} else {
fprintf(tmp, "~%s~\n", msg);
}
}
fclose(file);
fclose(tmp);
if (found) {
remove("message-shortcuts.txt");
rename("tmp", "message-shortcuts.txt");
} else {
remove("tmp");
}
//
chdir(original_path);
return found;
}
bool remove_shortcut(char* shortcut)
{
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
chdir(get_config_path(true));
//
FILE* file = fopen("message-shortcuts.txt", "r");
FILE* tmp = fopen("tmp", "w");
if (file == NULL) return false;
bool found = false;
char shrt[SHORTCUT_MAX], msg[COMMIT_MESSAGE_MAX];
while ( fscanf(file, "%[^~]", shrt) != EOF ) {
fscanf(file, "~%[^~]~\n", msg);
if ( strcmp(shrt, shortcut) == 0 ) {
found = true;
} else {
fprintf(tmp, "%s", shrt);
fprintf(tmp, "~%s~\n", msg);
}
}
fclose(file);
fclose(tmp);
if (found) {
remove("message-shortcuts.txt");
rename("tmp", "message-shortcuts.txt");
} else {
remove("tmp");
}
//
chdir(original_path);
return found;
}
#endif