-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_basename.c
More file actions
51 lines (46 loc) · 1.06 KB
/
node_basename.c
File metadata and controls
51 lines (46 loc) · 1.06 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
#ifndef _AMALGAMATE_
#include "script.h"
#include "log.h"
#include "xstr.h"
#include <stdlib.h>
#include <string.h>
#endif
static const char* _basename_value(struct node *n) {
struct node_foreach *fe = node_find_parent_foreach(n);
if (fe) {
free(n->impl);
n->impl = 0;
}
if (n->impl) {
return n->impl;
}
const char *val = 0;
const char *replace_ext = 0;
if (!n->child || !(val = node_value(n->child))) {
node_fatal(AK_ERROR_SCRIPT_SYNTAX, n, "Argument is not set");
}
if (n->child->next) {
replace_ext = node_value(n->child);
}
struct xstr *xstr = xstr_create_empty();
const char *dp = strrchr(val, '.');
if (dp) {
xstr_cat2(xstr, val, (dp - val));
} else {
xstr_cat(xstr, val);
}
if (replace_ext) {
xstr_cat(xstr, replace_ext);
}
n->impl = xstr_destroy_keep_ptr(xstr);
return n->impl;
}
static void _basename_dispose(struct node *n) {
free(n->impl);
}
int node_basename_setup(struct node *n) {
n->flags |= NODE_FLG_NO_CWD;
n->value_get = _basename_value;
n->dispose = _basename_dispose;
return 0;
}