Browse Source

ensured dir isn't empty

tags/v0.3.0
Bob Rudis 8 years ago
parent
commit
768990564f
  1. 4
      README.md
  2. 1
      inst/img/example_dir/test.txt
  3. 46
      src/wand.cpp
  4. 26
      tests/testthat/test-wand.R

4
README.md

@ -45,6 +45,8 @@ system.file("img", package="wand") %>%
## $ description <chr> "directory", "C source, ASCII text", "HTML document, ASCII text, with CRLF line terminators", "... ## $ description <chr> "directory", "C source, ASCII text", "HTML document, ASCII text, with CRLF line terminators", "...
``` r ``` r
# Use a non-system magic-file
system.file("img", package="wand") %>% system.file("img", package="wand") %>%
list.files(full.names=TRUE) %>% list.files(full.names=TRUE) %>%
incant(magic_wand_file()) %>% incant(magic_wand_file()) %>%
@ -79,7 +81,7 @@ library(testthat)
date() date()
``` ```
## [1] "Mon Aug 15 08:17:44 2016" ## [1] "Mon Aug 15 09:30:22 2016"
``` r ``` r
test_dir("tests/") test_dir("tests/")

1
inst/img/example_dir/test.txt

@ -0,0 +1 @@
Test text file since the dir can't be empty and I need to test out dir functionality.

46
src/wand.cpp

@ -12,7 +12,6 @@ using namespace Rcpp;
#ifndef WINDOWS #ifndef WINDOWS
#include "magic.h" #include "magic.h"
#include "limits.h"
#endif #endif
#ifndef WINDOWS #ifndef WINDOWS
@ -48,6 +47,9 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") {
// bits of info. I may just switch this over to a single call (all the // bits of info. I may just switch this over to a single call (all the
// availabel flags) and do string parsing before pushing to CRAN. // availabel flags) and do string parsing before pushing to CRAN.
int flags = MAGIC_MIME_TYPE;
magic_t cookie = magic_open(flags);
for (unsigned int i=0; i<input_size; i++) { for (unsigned int i=0; i<input_size; i++) {
if ((i % 10000) == 0) Rcpp::checkUserInterrupt(); if ((i % 10000) == 0) Rcpp::checkUserInterrupt();
@ -55,8 +57,6 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") {
std::string path_str = as<std::string>(path[i]); std::string path_str = as<std::string>(path[i]);
std::string fullPath(R_ExpandFileName(path_str.c_str())); std::string fullPath(R_ExpandFileName(path_str.c_str()));
int flags = MAGIC_MIME_TYPE;
magic_t cookie = magic_open(flags);
if (cookie == NULL) { if (cookie == NULL) {
mime_type[i] = NA_STRING; mime_type[i] = NA_STRING;
} else { } else {
@ -71,8 +71,18 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") {
} }
} }
flags = MAGIC_MIME_ENCODING; }
cookie = magic_open(flags);
flags = MAGIC_MIME_ENCODING;
cookie = magic_open(flags);
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()));
if (cookie == NULL) { if (cookie == NULL) {
encoding[i] = NA_STRING; encoding[i] = NA_STRING;
} else { } else {
@ -87,7 +97,17 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") {
} }
} }
if (version >= 528) { }
if (version >= 528) {
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()));
flags = MAGIC_EXTENSION; flags = MAGIC_EXTENSION;
cookie = magic_open(flags); cookie = magic_open(flags);
if (cookie == NULL) { if (cookie == NULL) {
@ -105,8 +125,17 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") {
} }
} }
flags = MAGIC_NONE; }
cookie = magic_open(flags);
flags = MAGIC_NONE;
cookie = magic_open(flags);
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()));
if (cookie == NULL) { if (cookie == NULL) {
description[i] = NA_STRING; description[i] = NA_STRING;
} else { } else {
@ -120,6 +149,7 @@ DataFrame incant_(CharacterVector path, std::string magic_db="system") {
description(i) = res; description(i) = res;
} }
} }
} }
DataFrame df; DataFrame df;

26
tests/testthat/test-wand.R

@ -1,13 +1,13 @@
context("basic functionality") context("basic functionality")
test_that("we can do something", { test_that("we can do something", {
tmp <- incant(list.files(system.file("img", package="wand"), tmp <- incant(list.files(system.file("img", package="wand"),
full.names=TRUE), full.names=TRUE),
magic_wand_file()) magic_wand_file())
tmp <- tmp$description tmp <- tmp$description
tmp <- unlist(tmp, use.names=FALSE) tmp <- unlist(tmp, use.names=FALSE)
tmp <- sort(tmp) tmp <- sort(tmp)
expect_that(substr(tmp[8], 1, 3), equals("PNG")) expect_that(any(substr(tmp, 1, 3) == "PNG"), equals(TRUE))
}) })

Loading…
Cancel
Save