Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hrbrmstr committed Aug 12, 2016
0 parents commit 08527c4
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .Rbuildignore
@@ -0,0 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^README\.*md$
^README\.*html$
^NOTES\.*Rmd$
^NOTES\.*html$
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Rproj
src/*.o
src/*.so
src/*.dll
17 changes: 17 additions & 0 deletions .travis.yml
@@ -0,0 +1,17 @@
language: r
warnings_are_errors: true
sudo: required

apt_packages:
- binutils
- libproj-dev
- gdal-bin

env:
global:
- CRAN: http://cran.rstudio.com

notifications:
email:
on_success: change
on_failure: change
20 changes: 20 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,20 @@
Package: filemagic
Type: Package
Title: filemagic title goes here
Version: 0.1.0
Date: 2016-08-12
Author: Bob Rudis (@hrbrmstr)
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
Depends:
R (>= 3.0.0)
Imports:
purrr,
Rcpp
LinkingTo: Rcpp
RoxygenNote: 5.0.1
6 changes: 6 additions & 0 deletions NAMESPACE
@@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(get_mimetype)
import(purrr)
importFrom(Rcpp,sourceCpp)
useDynLib(filemagic)
2 changes: 2 additions & 0 deletions NEWS.md
@@ -0,0 +1,2 @@
0.1.0
* Initial release
10 changes: 10 additions & 0 deletions R/RcppExports.R
@@ -0,0 +1,10 @@
# This file was generated by Rcpp::compileAttributes
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Return file info
#'
#' @export
get_mimetype <- function(path) {
.Call('filemagic_get_mimetype', PACKAGE = 'filemagic', path)
}

9 changes: 9 additions & 0 deletions R/filemagic-package.R
@@ -0,0 +1,9 @@
#' A package to ...
#'
#' @name filemagic
#' @docType package
#' @author Bob Rudis (@@hrbrmstr)
#' @import purrr
#' @useDynLib filemagic
#' @importFrom Rcpp sourceCpp
NULL
41 changes: 41 additions & 0 deletions README.Rmd
@@ -0,0 +1,41 @@
---
output: rmarkdown::github_document
---

`filemagic` : ...

The following functions are implemented:

The following data sets are included:

### Installation

```{r eval=FALSE}
devtools::install_github("hrbrmstr/filemagic")
```

```{r echo=FALSE, message=FALSE, warning=FALSE, error=FALSE}
options(width=120)
```

### Usage

```{r}
library(filemagic)
# current verison
packageVersion("filemagic")
```

### Test Results

```{r}
library(filemagic)
library(testthat)
date()
test_dir("tests/")
```

21 changes: 21 additions & 0 deletions filemagic.Rproj
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --resave-data
PackageRoxygenize: rd,collate,namespace
14 changes: 14 additions & 0 deletions man/filemagic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/get_mimetype.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/.gitignore
@@ -0,0 +1,3 @@
*.o
*.so
*.dll
3 changes: 3 additions & 0 deletions src/Makevars
@@ -0,0 +1,3 @@
CPPFLAGS =
CXXFLAGS = -std=gnu99 -Wall -Wextra -O2 -g -fstack-protector
PKG_LIBS = -lmagic
18 changes: 18 additions & 0 deletions src/RcppExports.cpp
@@ -0,0 +1,18 @@
// This file was generated by Rcpp::compileAttributes
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <Rcpp.h>

using namespace Rcpp;

// get_mimetype
DataFrame get_mimetype(CharacterVector path);
RcppExport SEXP filemagic_get_mimetype(SEXP pathSEXP) {
BEGIN_RCPP
Rcpp::RObject __result;
Rcpp::RNGScope __rngScope;
Rcpp::traits::input_parameter< CharacterVector >::type path(pathSEXP);
__result = Rcpp::wrap(get_mimetype(path));
return __result;
END_RCPP
}
51 changes: 51 additions & 0 deletions src/filemagic.cpp
@@ -0,0 +1,51 @@
#include <Rcpp.h>
using namespace Rcpp;

#include "magic.h"
#include "limits.h"

//' Return file info
//'
//' @export
// [[Rcpp::export]]
DataFrame get_mimetype(CharacterVector path) {

unsigned int input_size = path.size();
StringVector mime_type(input_size);

for (unsigned int i=0; i<input_size; i++) {

if ((i % 10000) == 0) Rcpp::checkUserInterrupt();

std::string path_str = as<std::string>(path[i]);
std::string fullPath(R_ExpandFileName(path_str.c_str()));

int flags = MAGIC_MIME_TYPE;
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;
}

}

}

return DataFrame::create(_["file"] = path,
_["mime_type"] = mime_type,
_["stringsAsFactors"] = false);

}
126 changes: 126 additions & 0 deletions src/magic.h
@@ -0,0 +1,126 @@
/*
* Copyright (c) Christos Zoulas 2003.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _MAGIC_H
#define _MAGIC_H

#include <sys/types.h>

#define MAGIC_NONE 0x0000000 /* No flags */
#define MAGIC_DEBUG 0x0000001 /* Turn on debugging */
#define MAGIC_SYMLINK 0x0000002 /* Follow symlinks */
#define MAGIC_COMPRESS 0x0000004 /* Check inside compressed files */
#define MAGIC_DEVICES 0x0000008 /* Look at the contents of devices */
#define MAGIC_MIME_TYPE 0x0000010 /* Return the MIME type */
#define MAGIC_CONTINUE 0x0000020 /* Return all matches */
#define MAGIC_CHECK 0x0000040 /* Print warnings to stderr */
#define MAGIC_PRESERVE_ATIME 0x0000080 /* Restore access time on exit */
#define MAGIC_RAW 0x0000100 /* Don't convert unprintable chars */
#define MAGIC_ERROR 0x0000200 /* Handle ENOENT etc as real errors */
#define MAGIC_MIME_ENCODING 0x0000400 /* Return the MIME encoding */
#define MAGIC_MIME (MAGIC_MIME_TYPE|MAGIC_MIME_ENCODING)
#define MAGIC_APPLE 0x0000800 /* Return the Apple creator/type */
#define MAGIC_EXTENSION 0x1000000 /* Return a /-separated list of
* extensions */
#define MAGIC_COMPRESS_TRANSP 0x2000000 /* Check inside compressed files
* but not report compression */
#define MAGIC_NODESC (MAGIC_EXTENSION|MAGIC_MIME|MAGIC_APPLE)

#define MAGIC_NO_CHECK_COMPRESS 0x0001000 /* Don't check for compressed files */
#define MAGIC_NO_CHECK_TAR 0x0002000 /* Don't check for tar files */
#define MAGIC_NO_CHECK_SOFT 0x0004000 /* Don't check magic entries */
#define MAGIC_NO_CHECK_APPTYPE 0x0008000 /* Don't check application type */
#define MAGIC_NO_CHECK_ELF 0x0010000 /* Don't check for elf details */
#define MAGIC_NO_CHECK_TEXT 0x0020000 /* Don't check for text files */
#define MAGIC_NO_CHECK_CDF 0x0040000 /* Don't check for cdf files */
#define MAGIC_NO_CHECK_TOKENS 0x0100000 /* Don't check tokens */
#define MAGIC_NO_CHECK_ENCODING 0x0200000 /* Don't check text encodings */

/* No built-in tests; only consult the magic file */
#define MAGIC_NO_CHECK_BUILTIN ( \
MAGIC_NO_CHECK_COMPRESS | \
MAGIC_NO_CHECK_TAR | \
/* MAGIC_NO_CHECK_SOFT | */ \
MAGIC_NO_CHECK_APPTYPE | \
MAGIC_NO_CHECK_ELF | \
MAGIC_NO_CHECK_TEXT | \
MAGIC_NO_CHECK_CDF | \
MAGIC_NO_CHECK_TOKENS | \
MAGIC_NO_CHECK_ENCODING | \
0 \
)

/* Defined for backwards compatibility (renamed) */
#define MAGIC_NO_CHECK_ASCII MAGIC_NO_CHECK_TEXT

/* Defined for backwards compatibility; do nothing */
#define MAGIC_NO_CHECK_FORTRAN 0x000000 /* Don't check ascii/fortran */
#define MAGIC_NO_CHECK_TROFF 0x000000 /* Don't check ascii/troff */

#define MAGIC_VERSION 528 /* This implementation */


#ifdef __cplusplus
extern "C" {
#endif

typedef struct magic_set *magic_t;
magic_t magic_open(int);
void magic_close(magic_t);

const char *magic_getpath(const char *, int);
const char *magic_file(magic_t, const char *);
const char *magic_descriptor(magic_t, int);
const char *magic_buffer(magic_t, const void *, size_t);

const char *magic_error(magic_t);
int magic_setflags(magic_t, int);

int magic_version(void);
int magic_load(magic_t, const char *);
int magic_load_buffers(magic_t, void **, size_t *, size_t);

int magic_compile(magic_t, const char *);
int magic_check(magic_t, const char *);
int magic_list(magic_t, const char *);
int magic_errno(magic_t);

#define MAGIC_PARAM_INDIR_MAX 0
#define MAGIC_PARAM_NAME_MAX 1
#define MAGIC_PARAM_ELF_PHNUM_MAX 2
#define MAGIC_PARAM_ELF_SHNUM_MAX 3
#define MAGIC_PARAM_ELF_NOTES_MAX 4
#define MAGIC_PARAM_REGEX_MAX 5
#define MAGIC_PARAM_BYTES_MAX 6

int magic_setparam(magic_t, int, const void *);
int magic_getparam(magic_t, int, void *);

#ifdef __cplusplus
};
#endif

#endif /* _MAGIC_H */
2 changes: 2 additions & 0 deletions tests/test-all.R
@@ -0,0 +1,2 @@
library(testthat)
test_check("filemagic")
6 changes: 6 additions & 0 deletions tests/testthat/test-filemagic.R
@@ -0,0 +1,6 @@
context("basic functionality")
test_that("we can do something", {

#expect_that(some_function(), is_a("data.frame"))

})

0 comments on commit 08527c4

Please sign in to comment.