Browse Source

returns almost everything

tags/v0.3.0
Bob Rudis 6 years ago
parent
commit
ce06eee606
  1. 7
      DESCRIPTION
  2. 2
      NAMESPACE
  3. 14
      R/RcppExports.R
  4. 15
      README.Rmd
  5. 38
      README.md
  6. BIN
      inst/img/Rlogo.jpg
  7. BIN
      inst/img/Rlogo.pdf
  8. BIN
      inst/img/Rlogo.png
  9. 14
      inst/img/Rlogo.svg
  10. BIN
      inst/img/Rlogo.tiff
  11. 5
      inst/img/example.c
  12. 4
      inst/img/example.html
  13. 1
      inst/img/example.r
  14. 5
      inst/img/example.rtf
  15. 12
      man/get_mimetype.Rd
  16. 27
      man/incant.Rd
  17. 8
      src/RcppExports.cpp
  18. 76
      src/filemagic.cpp

7
DESCRIPTION

@ -3,14 +3,17 @@ Type: Package
Title: filemagic title goes here
Version: 0.1.0
Date: 2016-08-12
Author: Bob Rudis (@hrbrmstr)
Author: Bob Rudis (@hrbrmstr), Christos Zoulas [libmagic]
Maintainer: Bob Rudis <bob@rudis.net>
Description: filemagic description goes here
URL: http://github.com/hrbrmstr/filemagic
BugReports: https://github.com/hrbrmstr/filemagic/issues
License: AGPL
Suggests:
testthat
testthat,
tibble,
magrittr,
dplyr
Depends:
R (>= 3.0.0)
Imports:

2
NAMESPACE

@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand
export(get_mimetype)
export(incant)
import(purrr)
importFrom(Rcpp,sourceCpp)
useDynLib(filemagic)

14
R/RcppExports.R

@ -3,8 +3,18 @@
#' Return file info
#'
#' @param path character vector of files to use magic on
#' @return a \code{tibble} / \code{data.frame} of file magic attributes
#' @export
get_mimetype <- function(path) {
.Call('filemagic_get_mimetype', PACKAGE = 'filemagic', path)
#' @examples
#' library(magrittr)
#' library(dplyr)
#'
#' system.file("img", package="filemagic") %>%
#' list.files(full.names=TRUE) %>%
#' incant() %>%
#' glimpse()
incant <- function(path) {
.Call('filemagic_incant', PACKAGE = 'filemagic', path)
}

15
README.Rmd

@ -2,11 +2,13 @@
output: rmarkdown::github_document
---
`filemagic` : R interface to libmagic
`filemagic` : R interface to `libmagic`
So you do need to install `libmagic` to use this. This should also be pretty straightforward to get working on Windows. Assistance to do that is welcome.
The following functions are implemented:
- `get_mimetype` : returns the mimetype of the files in the input vector (as a data frame)
- `incant` : returns the mimetype of the files in the input vector (as a data frame)
The following data sets are included:
@ -24,8 +26,15 @@ options(width=120)
### Usage
```{r}
```{r message=FALSE}
library(filemagic)
library(magrittr)
library(dplyr)
system.file("img", package="filemagic") %>%
list.files(full.names=TRUE) %>%
incant() %>%
glimpse()
# current verison
packageVersion("filemagic")

38
README.md

@ -1,9 +1,11 @@
`filemagic` : R interface to libmagic
`filemagic` : R interface to `libmagic`
So you do need to install `libmagic` to use this. This should also be pretty straightforward to get working on Windows. Assistance to do that is welcome.
The following functions are implemented:
- `get_mimetype` : returns the mimetype of the files in the input vector (as a data frame)
- `incant` : returns the mimetype of the files in the input vector (as a data frame)
The following data sets are included:
@ -19,7 +21,24 @@ devtools::install_github("hrbrmstr/filemagic")
``` r
library(filemagic)
library(magrittr)
library(dplyr)
system.file("img", package="filemagic") %>%
list.files(full.names=TRUE) %>%
incant() %>%
glimpse()
```
## Observations: 10
## Variables: 5
## $ file <chr> "/Library/Frameworks/R.framework/Versions/3.3/Resources/library/filemagic/img/example_dir", "/L...
## $ mime_type <chr> "inode/directory", "text/x-c", "text/html", "text/plain", "text/rtf", "image/jpeg", "applicatio...
## $ encoding <chr> "binary", "us-ascii", "us-ascii", "us-ascii", "us-ascii", "binary", "binary", "binary", "us-asc...
## $ extensions <chr> NA, "???", "???", "???", "???", "jpeg/jpg/jpe/jfif", "???", "???", "???", "???"
## $ description <chr> "directory", "C source, ASCII text", "HTML document, ASCII text, with CRLF line terminators", "...
``` r
# current verison
packageVersion("filemagic")
```
@ -31,11 +50,24 @@ packageVersion("filemagic")
``` r
library(filemagic)
library(testthat)
```
##
## Attaching package: 'testthat'
## The following object is masked from 'package:dplyr':
##
## matches
## The following objects are masked from 'package:magrittr':
##
## equals, is_less_than, not
``` r
date()
```
## [1] "Fri Aug 12 18:16:56 2016"
## [1] "Fri Aug 12 19:41:08 2016"
``` r
test_dir("tests/")

BIN
inst/img/Rlogo.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
inst/img/Rlogo.pdf

Binary file not shown.

BIN
inst/img/Rlogo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

14
inst/img/Rlogo.svg

@ -0,0 +1,14 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="724" height="561" viewBox="0 0 724 561">
<defs>
<linearGradient id="gradientFill-1" x1="0" x2="1" y1="0" y2="1" gradientUnits="objectBoundingBox" spreadMethod="pad">
<stop offset="0" stop-color="rgb(203,206,208)" stop-opacity="1"/>
<stop offset="1" stop-color="rgb(132,131,139)" stop-opacity="1"/>
</linearGradient>
<linearGradient id="gradientFill-2" x1="0" x2="1" y1="0" y2="1" gradientUnits="objectBoundingBox" spreadMethod="pad">
<stop offset="0" stop-color="rgb(39,109,195)" stop-opacity="1"/>
<stop offset="1" stop-color="rgb(22,92,170)" stop-opacity="1"/>
</linearGradient>
</defs>
<path d="M361.453,485.937 C162.329,485.937 0.906,377.828 0.906,244.469 C0.906,111.109 162.329,3.000 361.453,3.000 C560.578,3.000 722.000,111.109 722.000,244.469 C722.000,377.828 560.578,485.937 361.453,485.937 ZM416.641,97.406 C265.289,97.406 142.594,171.314 142.594,262.484 C142.594,353.654 265.289,427.562 416.641,427.562 C567.992,427.562 679.687,377.033 679.687,262.484 C679.687,147.971 567.992,97.406 416.641,97.406 Z" fill="url(#gradientFill-1)" fill-rule="evenodd"/>
<path d="M550.000,377.000 C550.000,377.000 571.822,383.585 584.500,390.000 C588.899,392.226 596.510,396.668 602.000,402.500 C607.378,408.212 610.000,414.000 610.000,414.000 L696.000,559.000 L557.000,559.062 L492.000,437.000 C492.000,437.000 478.690,414.131 470.500,407.500 C463.668,401.969 460.755,400.000 454.000,400.000 C449.298,400.000 420.974,400.000 420.974,400.000 L421.000,558.974 L298.000,559.026 L298.000,152.938 L545.000,152.938 C545.000,152.938 657.500,154.967 657.500,262.000 C657.500,369.033 550.000,377.000 550.000,377.000 ZM496.500,241.024 L422.037,240.976 L422.000,310.026 L496.500,310.002 C496.500,310.002 531.000,309.895 531.000,274.877 C531.000,239.155 496.500,241.024 496.500,241.024 Z" fill="url(#gradientFill-2)" fill-rule="evenodd"/>
</svg>

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
inst/img/Rlogo.tiff

Binary file not shown.

5
inst/img/example.c

@ -0,0 +1,5 @@
#include <stdio.h>
void main() {
}

4
inst/img/example.html

@ -0,0 +1,4 @@
<html>
<head></head>
<body></body>
</html>

1
inst/img/example.r

@ -0,0 +1 @@
print("examle")

5
inst/img/example.rtf

@ -0,0 +1,5 @@
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
{\fonttbl}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww12540\viewh14960\viewkind1
}

12
man/get_mimetype.Rd

@ -1,12 +0,0 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{get_mimetype}
\alias{get_mimetype}
\title{Return file info}
\usage{
get_mimetype(path)
}
\description{
Return file info
}

27
man/incant.Rd

@ -0,0 +1,27 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{incant}
\alias{incant}
\title{Return file info}
\usage{
incant(path)
}
\arguments{
\item{path}{character vector of files to use magic on}
}
\value{
a \code{tibble} / \code{data.frame} of file magic attributes
}
\description{
Return file info
}
\examples{
library(magrittr)
library(dplyr)
system.file("img", package="filemagic") \%>\%
list.files(full.names=TRUE) \%>\%
incant() \%>\%
glimpse()
}

8
src/RcppExports.cpp

@ -5,14 +5,14 @@
using namespace Rcpp;
// get_mimetype
DataFrame get_mimetype(CharacterVector path);
RcppExport SEXP filemagic_get_mimetype(SEXP pathSEXP) {
// incant
DataFrame incant(CharacterVector path);
RcppExport SEXP filemagic_incant(SEXP pathSEXP) {
BEGIN_RCPP
Rcpp::RObject __result;
Rcpp::RNGScope __rngScope;
Rcpp::traits::input_parameter< CharacterVector >::type path(pathSEXP);
__result = Rcpp::wrap(get_mimetype(path));
__result = Rcpp::wrap(incant(path));
return __result;
END_RCPP
}

76
src/filemagic.cpp

@ -6,12 +6,25 @@ using namespace Rcpp;
//' Return file info
//'
//' @param path character vector of files to use magic on
//' @return a \code{tibble} / \code{data.frame} of file magic attributes
//' @export
//' @examples
//' library(magrittr)
//' library(dplyr)
//'
//' system.file("img", package="filemagic") %>%
//' list.files(full.names=TRUE) %>%
//' incant() %>%
//' glimpse()
// [[Rcpp::export]]
DataFrame get_mimetype(CharacterVector path) {
DataFrame incant(CharacterVector path) {
unsigned int input_size = path.size();
StringVector mime_type(input_size);
StringVector encoding(input_size);
StringVector extensions(input_size);
StringVector description(input_size);
for (unsigned int i=0; i<input_size; i++) {
@ -24,28 +37,73 @@ DataFrame get_mimetype(CharacterVector path) {
magic_t cookie = magic_open(flags);
if (cookie == NULL) {
mime_type[i] = NA_STRING;
} else {
magic_load(cookie, NULL);
const char *magic_result = magic_file(cookie, fullPath.c_str());
if (magic_result == NULL) {
mime_type[i] = NA_STRING;
} else {
std::string res = std::string(magic_result, strnlen(magic_result, 1024));
mime_type(i) = res;
}
}
flags = MAGIC_MIME_ENCODING;
cookie = magic_open(flags);
if (cookie == NULL) {
encoding[i] = NA_STRING;
} else {
magic_load(cookie, NULL);
const char *magic_result = magic_file(cookie, fullPath.c_str());
if (magic_result == NULL) {
encoding[i] = NA_STRING;
} else {
std::string res = std::string(magic_result, strnlen(magic_result, 1024));
encoding(i) = res;
}
}
flags = MAGIC_EXTENSION;
cookie = magic_open(flags);
if (cookie == NULL) {
extensions[i] = NA_STRING;
} else {
magic_load(cookie, NULL);
const char *magic_result = magic_file(cookie, fullPath.c_str());
if (magic_result == NULL) {
extensions[i] = NA_STRING;
} else {
std::string res = std::string(magic_result, strnlen(magic_result, 1024));
extensions(i) = res;
}
}
flags = MAGIC_NONE;
cookie = magic_open(flags);
if (cookie == NULL) {
description[i] = NA_STRING;
} else {
magic_load(cookie, NULL);
const char *magic_result = magic_file(cookie, fullPath.c_str());
if (magic_result == NULL) {
description[i] = NA_STRING;
} else {
std::string res = std::string(magic_result, strnlen(magic_result, 1024));
description(i) = res;
}
}
}
return DataFrame::create(_["file"] = path,
_["mime_type"] = mime_type,
_["stringsAsFactors"] = false);
DataFrame df = DataFrame::create(_["file"] = path,
_["mime_type"] = mime_type,
_["encoding"] = encoding,
_["extensions"] = extensions,
_["description"] = description,
_["stringsAsFactors"] = false);
df.attr("class") = CharacterVector::create("tbl_df", "tbl", "data.frame");
return(df);
}

Loading…
Cancel
Save