1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

szip: use enums instead of constants (#8905)

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

View File

@ -2,7 +2,7 @@ import szip
import os import os
fn test_szip() { 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 assert false
return return
} }
@ -11,4 +11,3 @@ fn test_szip() {
os.rm('test_compile.zip') or { } os.rm('test_compile.zip') or { }
} }
} }