/
low_level_write.clj
114 lines (100 loc) · 4.29 KB
/
low_level_write.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(ns clj-xlsxio.low-level-write
(:refer-clojure :exclude [name])
(:require [clojure.java.io :as io])
(:import [com.sun.jna NativeLibrary Pointer]
[java.util Date]
[org.joda.time DateTime]
[java.time LocalDateTime ZoneOffset]
[clojure.lang Ratio]))
(try
(do
(def z (NativeLibrary/getInstance "libz.so.1"))
(def minizip (NativeLibrary/getInstance "minizip"))
(def libxlsxio-write (NativeLibrary/getInstance "xlsxio_write"))
(let [c (NativeLibrary/getInstance "c")
setlocale (.getFunction c "setlocale")]
(doall (map #(.invoke setlocale String (to-array [% "C"])) (range 20)))))
(catch Exception e
(do
(println "============================================================================
We've had a problem loading the native libraries.
This library has two dependencies:
minizip and libz (which is used by minizip)
All of them are bundled in this jar library,
however if you are having issues loading the shared objects consider
installing them on your system.
It is important to notice that all of these bundled native libraries were
compiled with GNU libc standard library. If you are on a system based on musl
(like Alpine Linux ) or another standard library you WILL
need to install those 2 dependencies on your system.
============================================================================")
(pr e))))
(defn open
^Pointer
([^String filename ^String sheetname]
(let [res
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_open") Pointer (to-array [filename sheetname]))]
(if (= res Pointer/NULL)
(throw (RuntimeException. "Error on xlsxiowrite_open, returned NULL."))
res)))
([^String filename]
(open filename nil)))
(defn close
^Long
[^Pointer handle]
(let [res
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_close") Long (to-array [handle]))]
(if-not (= res 0)
(throw (RuntimeException. (str "Error on xlsxiowrite_close, returned " res)))
res)))
(defn add-column
^Void
([^Pointer handle ^String name ^Long width]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_add_column") Void (to-array [handle name width])))
([^Pointer handle ^String name]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_add_column") Void (to-array [handle name]))))
(defn next-row
^Void
[^Pointer handle]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_next_row") Void (to-array [handle])))
(defn add-cell-string
^Void
[^Pointer handle ^String value]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_add_cell_string") Void (to-array [handle value])))
(defn add-cell-int
^Void
[^Pointer handle ^Long value]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_add_cell_int") Void (to-array [handle value])))
(defn add-cell-float
^Void
[^Pointer handle ^Double value]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_add_cell_float") Void (to-array [handle value])))
(defn add-cell-double
^Void
[^Pointer handle ^Double value]
(add-cell-float handle value))
(defn add-cell-datetime
^Void
[^Pointer handle ^Long value]
(.invoke (.getFunction libxlsxio-write "xlsxiowrite_add_cell_datetime") Void (to-array [handle value])))
(defprotocol AddCell
(add-cell-generic [generic handle]))
(extend-protocol AddCell
String (add-cell-generic [value handle]
(add-cell-string handle value))
Double (add-cell-generic [value handle]
(add-cell-float handle value))
Ratio (add-cell-generic [value handle]
(add-cell-float handle (double value)))
Long (add-cell-generic [value handle]
(add-cell-int handle value))
Date (add-cell-generic [value handle]
(add-cell-datetime handle (long (/ (.getTime value) 1000))))
DateTime (add-cell-generic [value handle]
(add-cell-datetime handle (long (/ (.getMillis value) 1000))))
LocalDateTime (add-cell-genetic [value handle]
(add-cell-datetime handle (.toEpochSecond value (ZoneOffset/ofHours 0)))))
(defn add-cell
^Void
[^Pointer handle value]
(add-cell-generic value handle))