File: zlib.lsp

package info (click to toggle)
newlisp 10.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,460 kB
  • ctags: 4,357
  • sloc: ansic: 33,202; lisp: 7,369; java: 7,012; sh: 647; makefile: 273
file content (81 lines) | stat: -rw-r--r-- 2,560 bytes parent folder | download | duplicates (2)
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
;; @module zlib.lsp
;; @description Functions for compression/decompression with zlib
;; @version 1.1 - comments redone for automatic documentation
;; @version 1.2 - new library detection routine
;; @version 1.3 - added lib for opnBSD and tested for 64-bit newLISP
;; @version 1.4 - doc changes
;; @version 1.5 - replaced <tt>write-buffer</tt> with <tt>write</tt>
;; @version 1.6 - removed broken <tt>squeeze</tt> and <tt>unsqueeze</tt> functions.
;; @version 1.7 - added library for CentOS 6 Linux
;; @version 1.71 - added library for Ubuntu Linux 12.04 and 13.04
;; @version 1.72 - swap else-less 'if' for 'when'
;; @version 1.73 changes references to /usr/ to /usr/local/
;; @version 1.74 library path additions
;; @author L.M 2006-2016
;; <h3>Functions for compression/decompression with zlib</h3> 
;; For this module a platform sepcific library
;; from @link http://www.zlib.net/ www.zib.net is needed.
;;
;; The module offers two compression/decompression support as
;; GZ compatible file compression and decompression.
;; 
;; Before using the module it must be loaded:
;; <pre>
;; (load "/usr/local/share/newlisp/modules/zlib.lsp")
;; ; or shorter
;; (module "zlib.lsp")
;; </pre>

(when (< (sys-info -2) 10110)
	(constant (global 'write) write-buffer))

(context 'zlib)

(set 'library "libz.so.1")

(import library "compress")
(import library "uncompress")
(import library "gzopen")
(import library "gzread")
(import library "gzclose")
(import library "gzwrite")

;; @syntax (zlib:gz-read-file <str-file-name>)
;; @return A string buffer with the original contents.
;;
;; Uncompresses the GZ compressed file in <str-file-name>.
;; @example
;; (set 'buff (zlib:gz-read-file "myfile.gz"))

(define (gz-read-file file-name)
    (let (  (fno (gzopen file-name "rb"))
            (buff (dup "\000" 0x1000))
            (result ""))
        (when (!= fno 0)
            (while (> (set 'bytes (gzread fno buff 0x1000)) 0)
            (write result buff bytes))
            (gzclose fno)
            result)
))

;; @syntax (zlib:gz-write-file <str-file-name> <str-buffer>)
;; @return The number of bytes in <str-buffer>.
;;
;; Does a GZ compatible comrpression of a buffer in <str-buffer> and
;; writes it to the file in <str-file-name>.
;; @example
;; (zlib:gz-write-file "myfile.gz" buff) 

(define (gz-write-file file-name buff)
    (let (  (fno (gzopen file-name "wb"))
            (result nil))
        (when (!= fno 0)	
            (set 'result (gzwrite fno buff (length buff)))
            (gzclose fno)
            result)
))
	
(context MAIN) 

; eof