szip: use enums instead of constants (#8905)

pull/8911/head
StunxFS 2021-02-22 14:02:45 -04:00 committed by GitHub
parent 68972fcec6
commit d78c9476e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 33 deletions

View File

@ -1,5 +1,7 @@
module szip
import os
#flag -I @VROOT/thirdparty/zip
#include "zip.c"
#include "zip.h"
@ -37,40 +39,50 @@ fn C.zip_entry_fread(&Zip, byteptr) int
fn C.zip_total_entries(&Zip) int
// compression level - ref: miniz.h
const (
no_compression = 0
best_speed = 1
best_compression = 9
uber_compression = 10
default_level = 6
// CompressionLevel lists compression levels, see in "thirdparty/zip/miniz.h"
pub enum CompressionLevel {
no_compression = 0
best_speed = 1
best_compression = 9
uber_compression = 10
default_level = 6
default_compression = -1
)
}
const (
m_write = `w`
m_ronly = `r`
m_append = `a`
)
// OpenMode lists the opening modes
// .write: opens a file for reading/extracting (the file must exists).
// .read_only: creates an empty file for writing.
// .append: appends to an existing archive.
pub enum OpenMode {
write
read_only
append
}
// open opens zip archive with compression level using the given mode.<br>
// compression levels: 0-9 are the standard zlib-style levels.<br>
// modes:<br>
// - 'r': opens a file for reading/extracting (the file must exists).<br>
// - 'w': creates an empty file for writing.<br>
// - 'a': appends to an existing archive.
pub fn open(name string, level int, mode byte) ?&Zip {
mut nlevel := level
if (nlevel & 0xF) > szip.uber_compression {
nlevel = szip.default_level
[inline]
fn (om OpenMode) to_byte() byte {
return match om {
.write {
`w`
}
.read_only {
`r`
}
.append {
`a`
}
}
}
// open opens zip archive with compression level using the given mode.
// name: the name of the zip file to open
// level: can be any value of the CompressionLevel enum
// mode: can be any value of the OpenMode enum
pub fn open(name string, level CompressionLevel, mode OpenMode) ?&Zip {
if name.len == 0 {
return error('szip: name of file empty')
}
if mode !in [szip.m_write, szip.m_ronly, szip.m_append] {
return error('szip: invalid provided open mode')
}
p_zip := &Zip(C.zip_open(name.str, nlevel, mode))
p_zip := &Zip(C.zip_open(name.str, int(level), mode.to_byte()))
if isnil(p_zip) {
return error('szip: cannot open/create/append new zip archive')
}
@ -78,6 +90,7 @@ pub fn open(name string, level int, mode byte) ?&Zip {
}
// close closes the zip archive, releases resources - always finalize.
[inline]
pub fn (mut z Zip) close() {
C.zip_close(z)
}
@ -94,6 +107,7 @@ pub fn (mut zentry Zip) open_entry(name string) ? {
}
// close_entry closes a zip entry, flushes buffer and releases resources.
[inline]
pub fn (mut zentry Zip) close_entry() {
C.zip_entry_close(zentry)
}
@ -110,7 +124,7 @@ pub fn (mut zentry Zip) name() string {
if name == 0 {
return ''
}
return unsafe { tos_clone(name) }
return unsafe { name.vstring() }
}
/// index returns an index of the current zip entry.
@ -132,11 +146,13 @@ pub fn (mut zentry Zip) is_dir() ?bool {
}
// size returns an uncompressed size of the current zip entry.
[inline]
pub fn (mut zentry Zip) size() u64 {
return C.zip_entry_size(zentry)
}
// crc32 returns CRC-32 checksum of the current zip entry.
[inline]
pub fn (mut zentry Zip) crc32() u32 {
return C.zip_entry_crc32(zentry)
}
@ -176,8 +192,8 @@ pub fn (mut zentry Zip) read_entry() ?voidptr {
// extract_entry extracts the current zip entry into output file.
pub fn (mut zentry Zip) extract_entry(path string) ? {
if C.access(charptr(path.str), 0) == -1 {
return error('Cannot open file for extracting, file not exists')
if !os.is_file(path) {
return error('szip: cannot open file for extracting, "$path" not exists')
}
res := C.zip_entry_fread(zentry, path.str)
if res != 0 {
@ -190,7 +206,7 @@ extract extracts the current zip entry using a callback function (on_extract).
fn (mut zentry Zip) extract(path string) bool {
if C.access(path.str, 0) == -1 {
return false
// return error('Cannot open directory for extracting, directory not exists')
// return error('szip: cannot open directory for extracting, directory not exists')
}
res := C.zip_extract(zentry, path.str, 0, 0)
return res == 0

View File

@ -2,7 +2,7 @@ import szip
import os
fn test_szip() {
mut z := szip.open('test_compile.zip', szip.best_speed, szip.m_write) or {
mut z := szip.open('test_compile.zip', .best_speed, .write) or {
assert false
return
}
@ -11,4 +11,3 @@ fn test_szip() {
os.rm('test_compile.zip') or { }
}
}