-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.c
127 lines (110 loc) · 2.39 KB
/
util.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
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wchar.h>
#include "util.h"
char *
ellipsize(const char *s, size_t l, bool right)
{
size_t n = strlen(s);
if (n <= l)
return strdup(s);
char *o = calloc(l + 3 + 1, sizeof(char));
if (!o)
return NULL;
if (!right){
strcat(o, "...");
strncat(o, s + n - l, l);
} else{
strncat(o, s + n - l, l);
strcat(o, "...");
}
return o;
}
wchar_t *
stows(const char *s, size_t n)
{
char *mbs = calloc(n + 1, sizeof(char));
if (!mbs)
return NULL;
strncpy(mbs, s, n);
size_t wcn = mbstowcs(NULL, mbs, 0);
wchar_t *wcs = calloc(wcn + 1, sizeof(wchar_t));
if (!wcs)
return free(mbs), NULL;
size_t wcr = mbstowcs(wcs, mbs, wcn);
if (wcr == (size_t)-1)
return free(mbs), free(wcs), NULL;
free(mbs);
return wcs;
}
char *
wstos(const wchar_t *s, size_t n)
{
wchar_t *wcs = calloc(n + 1, sizeof(wchar_t));
if (!wcs)
return NULL;
wcsncpy(wcs, s, n);
size_t mbn = wcstombs(NULL, wcs, 0);
char *mbs = calloc(mbn + 1, sizeof(char));
if (!mbs)
return free(wcs), NULL;
size_t mbr = wcstombs(mbs, wcs, mbn);
if (mbr == (size_t)-1)
return free(mbs), free(wcs), NULL;
free(wcs);
return mbs;
}
bool
readfile(const char *fn, bool (*cb)(const wchar_t *, size_t, void *), void *p)
{
struct stat s = {0};
if (stat(fn, &s) != 0)
return false;
if ((s.st_mode & S_IFMT) == S_IFDIR)
return (errno = EISDIR), false;
FILE *f = fopen(fn, "r");
if (!f)
return false;
char *l = NULL;
size_t ln = 0;
ssize_t n = 0;
bool rc = true;
while ((n = getline(&l, &ln, f)) != -1 && rc){
if (n && l[n - 1] == '\n')
n--;
if (n){
wchar_t *c = stows(l, n);
rc = c && cb(c, wcslen(c), p);
free(c);
} else
rc = cb(L"", 0, p);
}
free(l);
fclose(f);
return rc;
}
wchar_t *
dupstr(const wchar_t *s, size_t n)
{
wchar_t *r = calloc(n + 1, sizeof(wchar_t));
if (!r)
return r;
wmemcpy(r, s, n);
return r;
}
const char *
trimleft(const char *s)
{
if (!s)
return NULL;
while (*s && isspace(*s))
s++;
return s;
}