Add bibutils wrapper

This commit is contained in:
Patrick Förster 2019-11-27 19:57:48 +01:00
parent f2993a0628
commit 2cd075afe4
39 changed files with 190 additions and 17 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "bibutils"
version = "0.1.0"
authors = [
"Eric Förster <efoerster@users.noreply.github.com>",
"Patrick Förster <pfoerster@users.noreply.github.com>"]
edition = "2018"
[dependencies]
bibutils-sys = { path = "../bibutils_sys" }
libc = "0.2"
tempfile = "3"
tokio = "0.2.0-alpha.6"

106
crates/bibutils/src/lib.rs Normal file
View file

@ -0,0 +1,106 @@
use bibutils_sys::*;
use std::ffi::CString;
use std::mem::MaybeUninit;
use tempfile::tempdir;
use tokio::fs;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum InputFormat {
Bibtex,
Biblatex,
Copac,
Ebi,
Endnote,
EndnoteXml,
Medline,
Mods,
Nbib,
Ris,
Word,
}
impl InputFormat {
fn read_mode(self) -> u32 {
match self {
Self::Bibtex => BIBL_BIBTEXIN,
Self::Biblatex => BIBL_BIBLATEXIN,
Self::Copac => BIBL_COPACIN,
Self::Ebi => BIBL_EBIIN,
Self::Endnote => BIBL_ENDNOTEIN,
Self::EndnoteXml => BIBL_ENDNOTEXMLIN,
Self::Medline => BIBL_MEDLINEIN,
Self::Mods => BIBL_MODSIN,
Self::Nbib => BIBL_NBIBIN,
Self::Ris => BIBL_RISIN,
Self::Word => BIBL_WORDIN,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum OutputFormat {
Adsabs,
Bibtex,
Endnote,
Isi,
Mods,
Nbib,
Ris,
Word2007,
}
impl OutputFormat {
fn write_mode(self) -> u32 {
match self {
Self::Adsabs => BIBL_ADSABSOUT,
Self::Bibtex => BIBL_BIBTEXOUT,
Self::Endnote => BIBL_ENDNOTEOUT,
Self::Isi => BIBL_ISIOUT,
Self::Mods => BIBL_MODSOUT,
Self::Nbib => BIBL_NBIBOUT,
Self::Ris => BIBL_RISOUT,
Self::Word2007 => BIBL_WORD2007OUT,
}
}
}
pub async fn convert(input: String, from: InputFormat, to: OutputFormat) -> String {
unsafe {
let program = CString::new("texlab").unwrap();
let mut context = MaybeUninit::zeroed();
bibl_init(context.as_mut_ptr());
let mut params = MaybeUninit::zeroed();
bibl_initparams(
params.as_mut_ptr(),
from.read_mode() as i32,
to.write_mode() as i32,
program.as_ptr(),
);
let dir = tempdir().expect("failed to create a temporary directory");
let input_path = dir.path().join("input");
let input_path_ffi = CString::new(input_path.to_str().unwrap()).unwrap();
fs::write(input_path, input).await.unwrap();
let input_mode_ffi = CString::new("r").unwrap();
let input_file = fopen(input_path_ffi.as_ptr(), input_mode_ffi.as_ptr());
bibl_read(
context.as_mut_ptr(),
input_file,
input_path_ffi.as_ptr(),
params.as_mut_ptr(),
);
fclose(input_file);
let output_path = dir.path().join("output");
let output_path_ffi = CString::new(output_path.to_str().unwrap()).unwrap();
let output_mode_ffi = CString::new("w").unwrap();
let output_file = fopen(output_path_ffi.as_ptr(), output_mode_ffi.as_ptr());
bibl_write(context.as_mut_ptr(), output_file, params.as_mut_ptr());
fclose(output_file);
bibl_freeparams(params.as_mut_ptr());
bibl_free(context.as_mut_ptr());
let data = fs::read(output_path).await.unwrap();
String::from_utf8_lossy(&data).into_owned()
}
}

View file

@ -22,6 +22,7 @@
#include "type.h"
#include "url.h"
#include "bibformats.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int adsout_initparams()

View file

@ -15,6 +15,7 @@
#include "charsets.h"
#include "str_conv.h"
#include "is_ws.h"
#include "msvc_fix.h"
/* illegal modes to pass in, but use internally for consistency */
#define BIBL_INTERNALIN (BIBL_LASTIN+1)

View file

@ -22,6 +22,7 @@
#include "reftypes.h"
#include "bibformats.h"
#include "generic.h"
#include "msvc_fix.h"
extern variants biblatex_all[];
extern int biblatex_nall;

View file

@ -22,6 +22,7 @@
#include "reftypes.h"
#include "bibformats.h"
#include "generic.h"
#include "msvc_fix.h"
static slist find = { 0, 0, 0, NULL };
static slist replace = { 0, 0, 0, NULL };

View file

@ -21,6 +21,7 @@
#include "type.h"
#include "url.h"
#include "bibformats.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int bibtexout_initparams()

View file

@ -7,6 +7,7 @@
*/
#include <string.h>
#include "bu_auth.h"
#include "msvc_fix.h"
const char *bu_genre[] = {
"academic journal",

View file

@ -10,6 +10,7 @@
#include <stdlib.h>
#include <string.h>
#include "charsets.h"
#include "msvc_fix.h"
#define ARRAYSIZE( a ) ( sizeof(a) / sizeof(a[0]) )
#define ARRAYSTART( a ) ( &(a[0]) )

View file

@ -18,6 +18,7 @@
#include "reftypes.h"
#include "bibformats.h"
#include "generic.h"
#include "msvc_fix.h"
extern variants end_all[];
extern int end_nall;

View file

@ -20,6 +20,7 @@
#include "type.h"
#include "url.h"
#include "bibformats.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int endout_initparams()

View file

@ -10,6 +10,7 @@
#include <string.h>
#include <ctype.h>
#include "entities.h"
#include "msvc_fix.h"
/* HTML 4.0 entities */

View file

@ -11,6 +11,7 @@
#include <stdint.h>
#include <string.h>
#include "fields.h"
#include "msvc_fix.h"
fields*
fields_new( void )

View file

@ -3,6 +3,7 @@
*/
#include <string.h>
#include "iso639_1.h"
#include "msvc_fix.h"
typedef struct {
char *code;

View file

@ -3,6 +3,7 @@
*/
#include <string.h>
#include "iso639_2.h"
#include "msvc_fix.h"
typedef struct {
char *code1;

View file

@ -3,6 +3,7 @@
*/
#include <string.h>
#include "iso639_3.h"
#include "msvc_fix.h"
typedef struct {
char *code;

View file

@ -67,7 +67,6 @@ pub const LEVEL_ANY: i32 = -1;
pub const LEVEL_MAIN: u32 = 0;
pub const LEVEL_HOST: u32 = 1;
pub const LEVEL_SERIES: u32 = 2;
pub const __GNUC_VA_LIST: u32 = 1;
pub const VPLIST_MEMERR: i32 = -1;
pub const VPLIST_OK: u32 = 0;
pub const FIELDS_CAN_DUP: u32 = 0;
@ -1504,7 +1503,6 @@ extern "C" {
extern "C" {
pub fn str_is_empty(s: *mut str) -> ::std::os::raw::c_int;
}
pub type __gnuc_va_list = __builtin_va_list;
pub type vplist_index = ::std::os::raw::c_int;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
@ -2200,7 +2198,21 @@ extern "C" {
_Alignment: usize,
) -> *mut ::std::os::raw::c_void;
}
pub type max_align_t = f64;
extern "C" {
pub fn _errno() -> *mut ::std::os::raw::c_int;
}
extern "C" {
pub fn _set_errno(_Value: ::std::os::raw::c_int) -> errno_t;
}
extern "C" {
pub fn _get_errno(_Value: *mut ::std::os::raw::c_int) -> errno_t;
}
extern "C" {
pub fn __threadid() -> ::std::os::raw::c_ulong;
}
extern "C" {
pub fn __threadhandle() -> usize;
}
pub type _CoreCrtSecureSearchSortCompareFunction = ::std::option::Option<
unsafe extern "C" fn(
arg1: *mut ::std::os::raw::c_void,
@ -2696,15 +2708,6 @@ extern "C" {
extern "C" {
pub fn _set_error_mode(_Mode: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn _errno() -> *mut ::std::os::raw::c_int;
}
extern "C" {
pub fn _set_errno(_Value: ::std::os::raw::c_int) -> errno_t;
}
extern "C" {
pub fn _get_errno(_Value: *mut ::std::os::raw::c_int) -> errno_t;
}
extern "C" {
pub fn __doserrno() -> *mut ::std::os::raw::c_ulong;
}
@ -5239,7 +5242,7 @@ extern "C" {
p: *mut param,
readmode: ::std::os::raw::c_int,
writemode: ::std::os::raw::c_int,
progname: *mut ::std::os::raw::c_char,
progname: *const ::std::os::raw::c_char,
) -> ::std::os::raw::c_int;
}
extern "C" {
@ -5273,7 +5276,7 @@ extern "C" {
pub fn bibl_read(
b: *mut bibl,
fp: *mut FILE,
filename: *mut ::std::os::raw::c_char,
filename: *const ::std::os::raw::c_char,
p: *mut param,
) -> ::std::os::raw::c_int;
}

View file

@ -8,6 +8,7 @@
*/
#include "marc_auth.h"
#include <string.h>
#include "msvc_fix.h"
static const char *marc_genre[] = {
"abstract or summary",

View file

@ -27,6 +27,7 @@
#include "iso639_3.h"
#include "bibutils.h"
#include "bibformats.h"
#include "msvc_fix.h"
static int modsin_readf( FILE *fp, char *buf, int bufsize, int *bufpos, str *line, str *reference, int *fcharset );
static int modsin_processf( fields *medin, const char *data, const char *filename, long nref, param *p );

View file

@ -21,6 +21,7 @@
#include "bu_auth.h"
#include "marc_auth.h"
#include "bibformats.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int modsout_initparams()

View file

@ -0,0 +1,7 @@
// Patch: Fix build on MSVC
#if defined(_WIN32) || defined(_WIN64)
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif

View file

@ -19,6 +19,7 @@
#include "slist.h"
#include "intlist.h"
#include "name.h"
#include "msvc_fix.h"
/* name_build_withcomma()
*

View file

@ -19,6 +19,7 @@
#include "title.h"
#include "bibutils.h"
#include "bibformats.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int nbibout_initparams()

View file

@ -9,6 +9,7 @@
#include <string.h>
#include "url.h"
#include "notes.h"
#include "msvc_fix.h"
/*
* notes are mostly directly copies; however, lots of formats hide

View file

@ -11,6 +11,7 @@
#include "is_ws.h"
#include "fields.h"
#include "reftypes.h"
#include "msvc_fix.h"
int
get_reftype( const char *p, long refnum, char *progname, variants *all, int nall, char *tag, int *is_default, int chattiness )

View file

@ -21,6 +21,7 @@
#include "reftypes.h"
#include "bibformats.h"
#include "generic.h"
#include "msvc_fix.h"
extern variants ris_all[];
extern int ris_nall;

View file

@ -17,6 +17,7 @@
#include "title.h"
#include "url.h"
#include "utf8.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int risout_initparams()

View file

@ -8,6 +8,7 @@
*/
#include <string.h>
#include "serialno.h"
#include "msvc_fix.h"
int
addsn( fields *info, char *buf, int level )

View file

@ -19,6 +19,7 @@
#include <limits.h>
#include "is_ws.h"
#include "str.h"
#include "msvc_fix.h"
/* Do not use asserts in STR_NOASSERT defined */
#ifdef STR_NOASSERT

View file

@ -135,4 +135,3 @@ int str_is_empty( str *s );
*/
#endif

View file

@ -15,6 +15,7 @@
#include "fields.h"
#include "title.h"
#include "is_ws.h"
#include "msvc_fix.h"
int
title_process( fields *info, const char *tag, const char *value, int level, unsigned char nosplittitle )

View file

@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include "type.h"
#include "msvc_fix.h"
static int
is_genre_element( fields *in, int n )

View file

@ -20,6 +20,7 @@
#include <ctype.h>
#include "bibutils.h"
#include "url.h"
#include "msvc_fix.h"
static void
construct_url( char *prefix, str *id, str *id_url, char sep )

View file

@ -15,6 +15,7 @@
#include "fields.h"
#include "utf8.h"
#include "bibformats.h"
#include "msvc_fix.h"
/*****************************************************
PUBLIC: int wordout_initparams()

View file

@ -0,0 +1,3 @@
// Wrapper file for bindgen
#include "bibl.h"
#include "bibutils.h"

View file

@ -12,6 +12,7 @@
#include "is_ws.h"
#include "strsearch.h"
#include "xml.h"
#include "msvc_fix.h"
char *xml_pns = NULL;

View file

@ -14,6 +14,7 @@
#include "str_conv.h"
#include "xml.h"
#include "xml_encoding.h"
#include "msvc_fix.h"
static int
xml_getencodingr( xml *node )