-
Notifications
You must be signed in to change notification settings - Fork 2
/
epiworld-hpp.R
69 lines (48 loc) · 1.41 KB
/
epiworld-hpp.R
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
line78 <- paste(rep("/", 78), collapse = "")
N_CALLS <- 0L
MAX_CALLS <- 10L
unfolder <- function(txt, rel = "include/epiworld/") {
heads <- which(grepl("^\\s*#include \"", txt))
if (!length(heads))
return(txt)
N_CALLS <<- N_CALLS + 1L
if (N_CALLS >= MAX_CALLS) {
warning("Max calls reached.")
return(txt)
}
fns <- gsub("^[^\"]+\"|\"$", "", txt[heads])
head_start <- sprintf(
"/*%s\n%1$s//\n\n Start of -%%s-\n\n%s//\n%1$s*/\n\n",
line78,
line78
)
head_end <- sprintf(
"/*%s\n%1$s//\n\n End of -%%s-\n\n%s//\n%1$s*/\n\n",
line78,
line78
)
new_src <- txt
for (h in rev(1L:length(fns)))
{
loc <- heads[h]
fn <- trimws(paste0(rel, fns[h]))
tmp_lines <- readLines(fn, warn = FALSE)
# Extracting relative path
sub_rel <- gsub("[^/]+$", "", fns[h])
tmp_lines <- unfolder(tmp_lines, rel = paste0(rel, "/", sub_rel))
# Getting the filename
new_src <- c(
new_src[1:(loc - 1)],
sprintf(head_start, fn),
tmp_lines,
sprintf(head_end, fn),
new_src[(loc + 1):length(new_src)]
)
}
return(new_src)
}
rel <- "include/epiworld/"
# Barry core
src <- readLines(paste0(rel, "epiworld.hpp"), warn = FALSE)
src_new <- unfolder(src, rel)
writeLines(src_new, "epiworld.hpp")