This repository was archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
123 lines (105 loc) · 2.55 KB
/
utils.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
// General standalone utilities not directly related to main functions.
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h> // for getenv
#include <string.h>
#include "main.h"
#include "utils.h"
struct utils_date utils_full_date(struct tm tm)
{
struct utils_date date_ = {
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour + 1,
tm.tm_min + 1,
tm.tm_sec + 1,
};
return date_;
}
char *utils_timestamp(struct tm date)
{
static char date_str[26]; // For example: 2020-05-23T16:01:58+03:00
strftime(date_str, 26, "%Y-%m-%dT%H:%M:%S%z", &date);
return date_str;
}
// make sure a directory exists
int utils_ensure_dir(const char *path)
{
struct stat st = {0};
if (stat(path, &st) == -1) {
return mkdir(path, 0700);
}
return 0;
}
// make directories recursively (must be able to modify path but is changed back)
// It ignores the last item unless it ends with a separator as well.
// "path/to/something" will make directories for "path" and "to", but not
// for "something", but "path/to/something/" will.
// It took way too long to write this function
int utils_mkdir(char *path)
{
int err = 0;
char *s;
if ((s = strstr(path, SEPARATOR)) != NULL) {
while (s != NULL) {
*s = '\0';
err -= utils_ensure_dir(path);
*s = SEPARATOR[0]; // change it back
s = strstr(s + 1, SEPARATOR);
if (s == NULL)
return 0;
}
} else {
return utils_ensure_dir(path);
}
return 0;
}
// a simple hashing function. Source: http://www.cse.yorku.ca/~oz/hash.html
unsigned long utils_djb2_hash(char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + (size_t)c;
return hash;
}
// gets the default text editor based on environment variables, or else vi
char *utils_default_editor()
{
char *editor;
if ((editor = getenv("VISUAL")) != NULL) return editor;
if ((editor = getenv("EDITOR")) != NULL) return editor;
static char e[] = "vi";
return e;
}
// Only gets basic yaml information in the form of `key: value`.
char *utils_mini_yaml(FILE *fp, const char *key, char value[512]) {
fseek(fp, 0, SEEK_SET);
if (fp == NULL)
return NULL;
char line[512];
size_t key_len = strlen(key);
while (fgets(line, 512, fp)) {
if (strncmp(line, key, key_len) == 0) {
size_t i = 0;
while (line[i] != ':' && line[i] != '\n')
i++;
i++;
if (line[i] == ' ')
i++;
if (line[i] == '\n')
continue;
size_t x = 0;
while (line[i] != '\n' && x < sizeof(line) - 1) {
value[x] = line[i];
i++;
x++;
}
value[x] = '\0';
return value;
}
}
return NULL;
}