Debian Codegraph @

R/utilities.R

raw
1 #' @rdname curl_options
2 #' @export
3 curl_symbols <- function(filter = ""){
4 m <- grep(filter, curl_symbol_data$name, ignore.case = TRUE)
5 curl_symbol_data[m,]
6 }
7
8 #' @useDynLib curl R_curl_version
9 #' @export
10 #' @rdname curl_options
11 #' @examples
12 #' # Curl/ssl version info
13 #' curl_version()
14 curl_version <- function(){
15 .Call(R_curl_version);
16 }
17
18 #' Parse date/time
19 #'
20 #' Can be used to parse dates appearing in http response headers such
21 #' as `Expires` or `Last-Modified`. Automatically recognizes
22 #' most common formats. If the format is known, [strptime()]
23 #' might be easier.
24 #'
25 #' @param datestring a string consisting of a timestamp
26 #' @useDynLib curl R_curl_getdate
27 #' @export
28 #' @examples
29 #' # Parse dates in many formats
30 #' parse_date("Sunday, 06-Nov-94 08:49:37 GMT")
31 #' parse_date("06 Nov 1994 08:49:37")
32 #' parse_date("20040911 +0200")
33 parse_date <- function(datestring){
34 out <- .Call(R_curl_getdate, datestring);
35 class(out) <- c("POSIXct", "POSIXt")
36 out
37 }
38
39
40 #' @useDynLib curl R_split_string
41 split_string <- function(x, split = ":"){
42 .Call(R_split_string, x, split)
43 }
44
45 trimws <- function(x) {
46 sub("\\s+$", "", sub("^\\s+", "", x))
47 }
48
49 is_string <- function(x){
50 is.character(x) && length(x) && nchar(x)
51 }
52
53 # Callback for typed libcurl errors
54 raise_libcurl_error <- function(errnum, message, errbuf = NULL, source_url = NULL, error_cb = NULL){
55 error_code <- libcurl_error_codes[errnum]
56 if(is.na(error_code))
57 error_code <- NULL #future proof new error codes
58 if(is_string(source_url)){
59 host <- try_parse_url(source_url)$host
60 if(is_string(host))
61 message <- sprintf('%s [%s]', message, host)
62 }
63 if(is_string(errbuf)){
64 message <- sprintf('%s:\n%s', message, errbuf)
65 }
66 if(is.function(error_cb)){
67 if(length(formals(error_cb)) > 0){
68 error_cb(structure(message, class = c(error_code, "curl_error", "character")))
69 } else {
70 error_cb()
71 }
72 } else {
73 cl <- sys.call(-1)
74 e <- structure(
75 class = c(error_code, "curl_error", "error", "condition"),
76 list(message = message, call = cl)
77 )
78 stop(e)
79 }
80 }