From f1f4fee67f1d066dfd3611c1f098a9d3f8cd6bc9 Mon Sep 17 00:00:00 2001
From: Gianluca Bertani
Date: Sun, 18 Oct 2015 14:32:45 +0200
Subject: [PATCH] Improved interface of locateFileInZip and readDataWithBuffer
in NSError variant
---
.../Objective-Zip_Swift_Tests.swift | 42 +++++--------------
Objective-Zip Tests/ObjectiveZip_Tests.m | 9 ++--
Objective-Zip/OZZipFile+NSError.h | 24 ++++++++---
Objective-Zip/OZZipFile+Standard.h | 2 +
Objective-Zip/OZZipFile.m | 7 ++--
Objective-Zip/OZZipReadStream+NSError.h | 15 +++++--
Objective-Zip/OZZipReadStream+Standard.h | 2 +
Objective-Zip/OZZipReadStream.m | 5 ++-
Objective-Zip/OZZipWriteStream+NSError.h | 2 +
9 files changed, 59 insertions(+), 49 deletions(-)
diff --git a/Objective-Zip Tests/Objective-Zip_Swift_Tests.swift b/Objective-Zip Tests/Objective-Zip_Swift_Tests.swift
index 72217c9..a33fa73 100644
--- a/Objective-Zip Tests/Objective-Zip_Swift_Tests.swift
+++ b/Objective-Zip Tests/Objective-Zip_Swift_Tests.swift
@@ -140,12 +140,7 @@ class Objective_Zip_Swift_Tests: XCTestCase {
NSLog("Test 1: reading from first file's stream...")
let data1 = NSMutableData(length:256)!
-
- var error : NSError?
- let bytesRead1 = read1.readDataWithBuffer(data1, error:&error)
- if error != nil {
- throw error!
- }
+ let bytesRead1 = try read1.readDataWithBuffer(data1)
XCTAssertEqual(3, bytesRead1)
@@ -167,11 +162,7 @@ class Objective_Zip_Swift_Tests: XCTestCase {
NSLog("Test 1: reading from second file's stream...")
let data2 = NSMutableData(length:256)!
-
- let bytesRead2 = read2.readDataWithBuffer(data2, error:&error)
- if error != nil {
- throw error!
- }
+ let bytesRead2 = try read2.readDataWithBuffer(data2)
XCTAssertEqual(3, bytesRead2)
@@ -201,11 +192,11 @@ class Objective_Zip_Swift_Tests: XCTestCase {
}
}
+ /*
+ * Uncomment to execute this test, but be careful: takes 5 minutes and consumes 5 GB of disk space
+ *
func test02ZipAndUnzip5GB() {
- // TODO Remove to enable this test, but be careful: takes 5 minutes and consumes 5 GB of disk space
- return
-
let documentsUrl = NSURL(fileURLWithPath:NSHomeDirectory(), isDirectory:true).URLByAppendingPathComponent("Documents")
let fileUrl = documentsUrl.URLByAppendingPathComponent("huge_test.zip")
let filePath = fileUrl.path!
@@ -286,13 +277,9 @@ class Objective_Zip_Swift_Tests: XCTestCase {
NSLog("Test 2: reading from file's stream...")
for (var i = 0; i < HUGE_TEST_NUMBER_OF_BLOCKS; i++) {
- var error : NSError?
- let bytesRead = read.readDataWithBuffer(buffer, error:&error)
- if error != nil {
- throw error!
- }
+ let bytesRead = try read.readDataWithBuffer(buffer)
- XCTAssertEqual(UInt(data.length), bytesRead)
+ XCTAssertEqual(data.length, bytesRead)
let range = buffer.rangeOfData(checkData, options:NSDataSearchOptions(), range:NSMakeRange(0, buffer.length))
@@ -319,6 +306,7 @@ class Objective_Zip_Swift_Tests: XCTestCase {
XCTFail("Error caught: \(error.code) - \(error.userInfo[NSLocalizedFailureReasonErrorKey])")
}
}
+ */
func test03UnzipMacZipFile() -> () {
let documentsUrl = NSURL(fileURLWithPath:NSHomeDirectory(), isDirectory:true).URLByAppendingPathComponent("Documents")
@@ -355,12 +343,7 @@ class Objective_Zip_Swift_Tests: XCTestCase {
NSLog("Test 3: reading from file's stream...")
let buffer = NSMutableData(length:1024)!
-
- var error : NSError?
- let bytesRead = read.readDataWithBuffer(buffer, error:&error)
- if error != nil {
- throw error!
- }
+ let bytesRead = try read.readDataWithBuffer(buffer)
let fileText = NSString(bytes:buffer.bytes, length:Int(bytesRead), encoding:NSUTF8StringEncoding)
@@ -418,12 +401,7 @@ class Objective_Zip_Swift_Tests: XCTestCase {
NSLog("Test 4: reading from file's stream...")
let buffer = NSMutableData(length:1024)!
-
- var error : NSError?
- let bytesRead = read.readDataWithBuffer(buffer, error:&error)
- if error != nil {
- throw error!
- }
+ let bytesRead = try read.readDataWithBuffer(buffer)
let fileText = NSString(bytes:buffer.bytes, length:Int(bytesRead), encoding:NSUTF8StringEncoding)
diff --git a/Objective-Zip Tests/ObjectiveZip_Tests.m b/Objective-Zip Tests/ObjectiveZip_Tests.m
index df595e6..ec2164d 100644
--- a/Objective-Zip Tests/ObjectiveZip_Tests.m
+++ b/Objective-Zip Tests/ObjectiveZip_Tests.m
@@ -208,11 +208,11 @@
}
}
+/*
+ * Uncomment to execute this test, but be careful: takes 5 minutes and consumes 5 GB of disk space
+ *
- (void) test02ZipAndUnzip5GB {
-
-// TODO Remove to enable this test, but be careful: takes 5 minutes and consumes 5 GB of disk space
- return;
-
+
NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath= [documentsDir stringByAppendingPathComponent:@"huge_test.zip"];
@@ -319,6 +319,7 @@
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
}
}
+ */
- (void) test03UnzipMacZipFile {
NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
diff --git a/Objective-Zip/OZZipFile+NSError.h b/Objective-Zip/OZZipFile+NSError.h
index 945ce42..b8d8c0d 100644
--- a/Objective-Zip/OZZipFile+NSError.h
+++ b/Objective-Zip/OZZipFile+NSError.h
@@ -34,6 +34,17 @@
#import "OZZipFile.h"
+/**
+ @brief Indicates the file could not be located in the zip file.
+ */
+static const NSInteger OZLocateFileResultNotFound= -1;
+
+/**
+ @brief Indicates the file has been successfully located in the zip file.
+ */
+static const NSInteger OZLocateFileResultFound= 1;
+
+
@interface OZZipFile (NSError)
@@ -211,13 +222,16 @@
readCurrentFileInZip.
@param error If passed, may be filled with an NSError is case the file can't
be located.
- @return YES if the file has been located and selected,
- NO if the specified file name is not present in the zip file or
- the file could not be located due to an error.
+ @return OZLocateFileResultFound if the file has been located
+ and selected, OZLocateFileResultNotFound if the specified
+ file name is not present in the zip file, or 0 if the file could
+ not be located due to an error.
+
NOTE: return value convention is different in the standard (non-NSError
+ compliant) interface.
@throws OZZipException If the zip file has been opened with a mode other than
Unzip.
*/
-- (BOOL) locateFileInZip:(nonnull NSString *)fileNameInZip error:(NSError * __autoreleasing __nullable * __nullable)error;
+- (NSInteger) __attribute__((swift_error(zero_result))) locateFileInZip:(nonnull NSString *)fileNameInZip error:(NSError * __autoreleasing __nullable * __nullable)error;
/**
@brief Returns the number of files contained in the zip file.
@@ -228,7 +242,7 @@
@throws OZZipException If the zip file has been opened with a mode other
than Unzip.
*/
-- (NSUInteger) numFilesInZipWithError:(NSError * __autoreleasing __nullable * __nullable)error;
+- (NSUInteger) __attribute__((swift_error(zero_result))) numFilesInZipWithError:(NSError * __autoreleasing __nullable * __nullable)error;
/**
@brief Returns a list of OZFileInZipInfo with the information on all the files
diff --git a/Objective-Zip/OZZipFile+Standard.h b/Objective-Zip/OZZipFile+Standard.h
index 0619ca5..244abc5 100644
--- a/Objective-Zip/OZZipFile+Standard.h
+++ b/Objective-Zip/OZZipFile+Standard.h
@@ -199,6 +199,8 @@
readCurrentFileInZip.
@return YES if the file has been located and selected,
NO if the specified file name is not present in the zip file.
+
NOTE: return value convention is different in NSError compliant
+ interface.
@throws OZZipException If the file can't be located due to an error or if the
zip file has been opened with a mode other than Unzip.
*/
diff --git a/Objective-Zip/OZZipFile.m b/Objective-Zip/OZZipFile.m
index 4825652..1538597 100644
--- a/Objective-Zip/OZZipFile.m
+++ b/Objective-Zip/OZZipFile.m
@@ -444,12 +444,13 @@
} ERROR_WRAP_END_AND_RETURN(error, NO);
}
-- (BOOL) locateFileInZip:(NSString *)fileNameInZip error:(NSError * __autoreleasing *)error {
+- (NSInteger) locateFileInZip:(NSString *)fileNameInZip error:(NSError * __autoreleasing *)error {
ERROR_WRAP_BEGIN {
- return [self locateFileInZip:fileNameInZip];
+ BOOL located= [self locateFileInZip:fileNameInZip];
+ return (located ? OZLocateFileResultFound : OZLocateFileResultNotFound);
- } ERROR_WRAP_END_AND_RETURN(error, NO);
+ } ERROR_WRAP_END_AND_RETURN(error, 0);
}
- (NSUInteger) numFilesInZipWithError:(NSError * __autoreleasing *)error {
diff --git a/Objective-Zip/OZZipReadStream+NSError.h b/Objective-Zip/OZZipReadStream+NSError.h
index 8cde39d..a4e16b1 100644
--- a/Objective-Zip/OZZipReadStream+NSError.h
+++ b/Objective-Zip/OZZipReadStream+NSError.h
@@ -34,6 +34,12 @@
#import "OZZipReadStream.h"
+/**
+ @brief Indicates the end of the file has been reached.
+ */
+static const NSInteger OZReadStreamResultEndOfFile= -1;
+
+
@interface OZZipReadStream (NSError)
@@ -46,10 +52,13 @@
@param buffer The buffer where read and uncompressed data must be stored.
@param error If passed, may be filled with an NSError is case data could
not be read.
- @return The number of uncompressed bytes read, 0 if the end of
- the file has been reached or data could not be read due to an error.
+ @return The number of uncompressed bytes read, OZReadStreamResultEndOfFile
+ if the end of the file has been reached, or 0
+ if data could not be read due to an error.
+
NOTE: return value convention is different in the standard (non-NSError
+ compliant) interface.
*/
-- (NSUInteger) readDataWithBuffer:(nonnull NSMutableData *)buffer error:(NSError * __autoreleasing __nullable * __nullable)error;
+- (NSInteger) __attribute__((swift_error(zero_result))) readDataWithBuffer:(nonnull NSMutableData *)buffer error:(NSError * __autoreleasing __nullable * __nullable)error;
/**
@brief Closes the read steam.
diff --git a/Objective-Zip/OZZipReadStream+Standard.h b/Objective-Zip/OZZipReadStream+Standard.h
index 8dfa654..859c0f4 100644
--- a/Objective-Zip/OZZipReadStream+Standard.h
+++ b/Objective-Zip/OZZipReadStream+Standard.h
@@ -46,6 +46,8 @@
@param buffer The buffer where read and uncompressed data must be stored.
@return The number of uncompressed bytes read, 0 if the end of
the file has been reached.
+
NOTE: return value convention is different in NSError compliant
+ interface.
@throws OZZipException If the data could not be read due to an error.
*/
- (NSUInteger) readDataWithBuffer:(nonnull NSMutableData *)buffer;
diff --git a/Objective-Zip/OZZipReadStream.m b/Objective-Zip/OZZipReadStream.m
index 45f4baa..e1cb72b 100644
--- a/Objective-Zip/OZZipReadStream.m
+++ b/Objective-Zip/OZZipReadStream.m
@@ -93,10 +93,11 @@
#pragma mark -
#pragma mark Reading data (NSError variants)
-- (NSUInteger) readDataWithBuffer:(NSMutableData *)buffer error:(NSError * __autoreleasing *)error {
+- (NSInteger) readDataWithBuffer:(NSMutableData *)buffer error:(NSError * __autoreleasing *)error {
ERROR_WRAP_BEGIN {
- return [self readDataWithBuffer:buffer];
+ NSUInteger bytesRead= [self readDataWithBuffer:buffer];
+ return (bytesRead == 0) ? OZReadStreamResultEndOfFile : bytesRead;
} ERROR_WRAP_END_AND_RETURN(error, 0);
}
diff --git a/Objective-Zip/OZZipWriteStream+NSError.h b/Objective-Zip/OZZipWriteStream+NSError.h
index 6ef2f5c..d804dc0 100644
--- a/Objective-Zip/OZZipWriteStream+NSError.h
+++ b/Objective-Zip/OZZipWriteStream+NSError.h
@@ -47,6 +47,8 @@
@param data The data to be compressed and written.
@param error If passed, may be filled with an NSError is case data could
not be written.
+ @return YES if data has been written, NO if
+ data could not be written due to an error.
*/
- (BOOL) writeData:(nonnull NSData *)data error:(NSError * __autoreleasing __nullable * __nullable)error;