@@ -40,11 +40,11 @@ #import "OFOpenItemFailedException.h" #import "OFReadFailedException.h" #import "OFSeekFailedException.h" #import "OFWriteFailedException.h" -#define BUFFER_SIZE 4096 +#define bufferSize 4096 OF_APPLICATION_DELEGATE(OFArc) static void help(OFStream *stream, bool full, int status) @@ -79,19 +79,19 @@ [OFApplication terminateWithStatus: status]; } static void -mutuallyExclusiveError(of_unichar_t shortOption1, OFString *longOption1, - of_unichar_t shortOption2, OFString *longOption2) +mutuallyExclusiveError(OFUnichar shortOption1, OFString *longOption1, + OFUnichar shortOption2, OFString *longOption2) { OFString *shortOption1Str = [OFString stringWithFormat: @"%C", shortOption1]; OFString *shortOption2Str = [OFString stringWithFormat: @"%C", shortOption2]; - [of_stderr writeLine: OF_LOCALIZED(@"2_options_mutually_exclusive", + [OFStdErr writeLine: OF_LOCALIZED(@"2_options_mutually_exclusive", @"Error: -%[shortopt1] / --%[longopt1] and " @"-%[shortopt2] / --%[longopt2] " @"are mutually exclusive!", @"shortopt1", shortOption1Str, @"longopt1", longOption1, @@ -99,15 +99,15 @@ @"longopt2", longOption2)]; [OFApplication terminateWithStatus: 1]; } static void -mutuallyExclusiveError5(of_unichar_t shortOption1, OFString *longOption1, - of_unichar_t shortOption2, OFString *longOption2, - of_unichar_t shortOption3, OFString *longOption3, - of_unichar_t shortOption4, OFString *longOption4, - of_unichar_t shortOption5, OFString *longOption5) +mutuallyExclusiveError5(OFUnichar shortOption1, OFString *longOption1, + OFUnichar shortOption2, OFString *longOption2, + OFUnichar shortOption3, OFString *longOption3, + OFUnichar shortOption4, OFString *longOption4, + OFUnichar shortOption5, OFString *longOption5) { OFString *shortOption1Str = [OFString stringWithFormat: @"%C", shortOption1]; OFString *shortOption2Str = [OFString stringWithFormat: @"%C", shortOption2]; @@ -116,11 +116,11 @@ OFString *shortOption4Str = [OFString stringWithFormat: @"%C", shortOption4]; OFString *shortOption5Str = [OFString stringWithFormat: @"%C", shortOption5]; - [of_stderr writeLine: OF_LOCALIZED(@"5_options_mutually_exclusive", + [OFStdErr writeLine: OF_LOCALIZED(@"5_options_mutually_exclusive", @"Error: -%[shortopt1] / --%[longopt1], " @"-%[shortopt2] / --%[longopt2], -%[shortopt3] / --%[longopt3], " @"-%[shortopt4] / --%[longopt4] and\n" @" -%[shortopt5] / --%[longopt5] are mutually exclusive!", @"shortopt1", shortOption1Str, @@ -137,21 +137,21 @@ } static void writingNotSupported(OFString *type) { - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"writing_not_supported", @"Writing archives of type %[type] is not (yet) supported!", @"type", type)]; } @implementation OFArc - (void)applicationDidFinishLaunching { OFString *outputDir, *encodingString, *type; - const of_options_parser_option_t options[] = { + const OFOptionsParserOption options[] = { { 'a', @"append", 0, NULL, NULL }, { 'c', @"create", 0, NULL, NULL }, { 'C', @"directory", 1, NULL, &outputDir }, { 'E', @"encoding", 1, NULL, &encodingString }, { 'f', @"force", 0, NULL, NULL }, @@ -163,12 +163,12 @@ { 't', @"type", 1, NULL, &type }, { 'v', @"verbose", 0, NULL, NULL }, { 'x', @"extract", 0, NULL, NULL }, { '\0', nil, 0, NULL, NULL } }; - of_unichar_t option, mode = '\0'; - of_string_encoding_t encoding = OF_STRING_ENCODING_AUTODETECT; + OFUnichar option, mode = '\0'; + OFStringEncoding encoding = OFStringEncodingAutodetect; OFOptionsParser *optionsParser; OFArray OF_GENERIC(OFString *) *remainingArguments, *files; id archive; #ifdef OF_HAVE_SANDBOX @@ -180,11 +180,11 @@ sandbox.allowsChangingFileAttributes = true; sandbox.allowsUserDatabaseReading = true; /* Dropped after parsing options */ sandbox.allowsUnveil = true; - [OFApplication activateSandbox: sandbox]; + [OFApplication of_activateSandbox: sandbox]; #endif #ifndef OF_AMIGAOS [OFLocale addLanguageDirectory: @LANGUAGE_DIR]; #else @@ -236,34 +236,34 @@ 'x', @"extract"); mode = option; break; case 'h': - help(of_stdout, true, 0); + help(OFStdOut, true, 0); break; case '=': - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"option_takes_no_argument", @"%[prog]: Option --%[opt] takes no argument", @"prog", [OFApplication programName], @"opt", optionsParser.lastLongOption)]; [OFApplication terminateWithStatus: 1]; break; case ':': if (optionsParser.lastLongOption != nil) - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"long_option_requires_argument", @"%[prog]: Option --%[opt] requires an " @"argument", @"prog", [OFApplication programName], @"opt", optionsParser.lastLongOption)]; else { OFString *optStr = [OFString stringWithFormat: @"%C", optionsParser.lastOption]; - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"option_requires_argument", @"%[prog]: Option -%[opt] requires an " @"argument", @"prog", [OFApplication programName], @"opt", optStr)]; @@ -271,20 +271,20 @@ [OFApplication terminateWithStatus: 1]; break; case '?': if (optionsParser.lastLongOption != nil) - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"unknown_long_option", @"%[prog]: Unknown option: --%[opt]", @"prog", [OFApplication programName], @"opt", optionsParser.lastLongOption)]; else { OFString *optStr = [OFString stringWithFormat: @"%C", optionsParser.lastOption]; - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"unknown_option", @"%[prog]: Unknown option: -%[opt]", @"prog", [OFApplication programName], @"opt", optStr)]; } @@ -294,13 +294,13 @@ } } @try { if (encodingString != nil) - encoding = of_string_parse_encoding(encodingString); + encoding = OFStringEncodingParseName(encodingString); } @catch (OFInvalidArgumentException *e) { - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"invalid_encoding", @"%[prog]: Invalid encoding: %[encoding]", @"prog", [OFApplication programName], @"encoding", encodingString)]; @@ -311,26 +311,25 @@ switch (mode) { case 'a': case 'c': if (remainingArguments.count < 1) - help(of_stderr, false, 1); + help(OFStdErr, false, 1); files = [remainingArguments objectsInRange: - of_range(1, remainingArguments.count - 1)]; + OFRangeMake(1, remainingArguments.count - 1)]; #ifdef OF_HAVE_SANDBOX if (![remainingArguments.firstObject isEqual: @"-"]) [sandbox unveilPath: remainingArguments.firstObject permissions: (mode == 'a' ? @"rwc" : @"wc")]; for (OFString *path in files) - [sandbox unveilPath: path - permissions: @"r"]; + [sandbox unveilPath: path permissions: @"r"]; sandbox.allowsUnveil = false; - [OFApplication activateSandbox: sandbox]; + [OFApplication of_activateSandbox: sandbox]; #endif archive = [self openArchiveWithPath: remainingArguments.firstObject type: type @@ -339,19 +338,19 @@ [archive addFiles: files]; break; case 'l': if (remainingArguments.count != 1) - help(of_stderr, false, 1); + help(OFStdErr, false, 1); #ifdef OF_HAVE_SANDBOX if (![remainingArguments.firstObject isEqual: @"-"]) [sandbox unveilPath: remainingArguments.firstObject permissions: @"r"]; sandbox.allowsUnveil = false; - [OFApplication activateSandbox: sandbox]; + [OFApplication of_activateSandbox: sandbox]; #endif archive = [self openArchiveWithPath: remainingArguments.firstObject type: type @@ -360,23 +359,23 @@ [archive listFiles]; break; case 'p': if (remainingArguments.count < 1) - help(of_stderr, false, 1); + help(OFStdErr, false, 1); #ifdef OF_HAVE_SANDBOX if (![remainingArguments.firstObject isEqual: @"-"]) [sandbox unveilPath: remainingArguments.firstObject permissions: @"r"]; sandbox.allowsUnveil = false; - [OFApplication activateSandbox: sandbox]; + [OFApplication of_activateSandbox: sandbox]; #endif files = [remainingArguments objectsInRange: - of_range(1, remainingArguments.count - 1)]; + OFRangeMake(1, remainingArguments.count - 1)]; archive = [self openArchiveWithPath: remainingArguments.firstObject type: type mode: mode @@ -384,34 +383,36 @@ [archive printFiles: files]; break; case 'x': if (remainingArguments.count < 1) - help(of_stderr, false, 1); + help(OFStdErr, false, 1); files = [remainingArguments objectsInRange: - of_range(1, remainingArguments.count - 1)]; + OFRangeMake(1, remainingArguments.count - 1)]; #ifdef OF_HAVE_SANDBOX if (![remainingArguments.firstObject isEqual: @"-"]) [sandbox unveilPath: remainingArguments.firstObject permissions: @"r"]; if (files.count > 0) for (OFString *path in files) - [sandbox unveilPath: path - permissions: @"wc"]; + [sandbox unveilPath: path permissions: @"wc"]; else { - OFString *path = (outputDir != nil - ? outputDir : OF_PATH_CURRENT_DIRECTORY); + OFString *path = outputDir; + + if (path == nil) + path = [[OFFileManager defaultManager] + currentDirectoryPath]; + /* We need 'r' to change the directory to it. */ - [sandbox unveilPath: path - permissions: @"rwc"]; + [sandbox unveilPath: path permissions: @"rwc"]; } sandbox.allowsUnveil = false; - [OFApplication activateSandbox: sandbox]; + [OFApplication of_activateSandbox: sandbox]; #endif archive = [self openArchiveWithPath: remainingArguments.firstObject type: type @@ -433,43 +434,43 @@ [archive extractFiles: files]; } @catch (OFCreateDirectoryFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stderr writeString: @"\r"]; - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeString: @"\r"]; + [OFStdErr writeLine: OF_LOCALIZED( @"failed_to_create_directory", @"Failed to create directory %[dir]: %[error]", @"dir", e.URL.fileSystemRepresentation, @"error", error)]; _exitStatus = 1; } @catch (OFOpenItemFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stderr writeString: @"\r"]; - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeString: @"\r"]; + [OFStdErr writeLine: OF_LOCALIZED( @"failed_to_open_file", @"Failed to open file %[file]: %[error]", @"file", e.path, @"error", error)]; _exitStatus = 1; } break; default: - help(of_stderr, true, 1); + help(OFStdErr, true, 1); break; } [OFApplication terminateWithStatus: _exitStatus]; } - (id )openArchiveWithPath: (OFString *)path type: (OFString *)type mode: (char)mode - encoding: (of_string_encoding_t)encoding + encoding: (OFStringEncoding)encoding { OFString *modeString, *fileModeString; OFStream *file = nil; id archive = nil; @@ -498,30 +499,29 @@ if ([path isEqual: @"-"]) { switch (mode) { case 'a': case 'c': - file = of_stdout; + file = OFStdOut; break; case 'l': case 'p': case 'x': - file = of_stdin; + file = OFStdIn; break; default: @throw [OFInvalidArgumentException exception]; } } else { @try { - file = [OFFile fileWithPath: path - mode: fileModeString]; + file = [OFFile fileWithPath: path mode: fileModeString]; } @catch (OFOpenItemFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stderr writeString: @"\r"]; - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeString: @"\r"]; + [OFStdErr writeLine: OF_LOCALIZED( @"failed_to_open_file", @"Failed to open file %[file]: %[error]", @"file", e.path, @"error", error)]; [OFApplication terminateWithStatus: 1]; @@ -566,11 +566,11 @@ } else if ([type isEqual: @"zip"]) archive = [ZIPArchive archiveWithStream: file mode: modeString encoding: encoding]; else { - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"unknown_archive_type", @"Unknown archive type: %[type]", @"type", type)]; goto error; } @@ -584,26 +584,26 @@ @throw e; } @catch (OFReadFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file", + [OFStdErr writeLine: OF_LOCALIZED(@"failed_to_read_file", @"Failed to read file %[file]: %[error]", @"file", path, @"error", error)]; goto error; } @catch (OFSeekFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stderr writeLine: OF_LOCALIZED(@"failed_to_seek_in_file", + [OFStdErr writeLine: OF_LOCALIZED(@"failed_to_seek_in_file", @"Failed to seek in file %[file]: %[error]", @"file", path, @"error", error)]; goto error; } @catch (OFInvalidFormatException *e) { - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"file_is_not_a_valid_archive", @"File %[file] is not a valid archive!", @"file", path)]; goto error; } @@ -633,28 +633,28 @@ ![[OFFileManager defaultManager] fileExistsAtPath: outFileName]) return true; if (_overwrite == -1) { if (_outputLevel >= 0) { - [of_stdout writeString: @" "]; - [of_stdout writeLine: + [OFStdOut writeString: @" "]; + [OFStdOut writeLine: OF_LOCALIZED(@"file_skipped", @"skipped")]; } return false; } do { - [of_stderr writeString: @"\r"]; - [of_stderr writeString: OF_LOCALIZED(@"ask_overwrite", + [OFStdErr writeString: @"\r"]; + [OFStdErr writeString: OF_LOCALIZED(@"ask_overwrite", @"Overwrite %[file]? [ynAN?]", @"file", fileName)]; - [of_stderr writeString: @" "]; + [OFStdErr writeString: @" "]; - line = [of_stdin readLine]; + line = [OFStdIn readLine]; if ([line isEqual: @"?"]) - [of_stderr writeLine: OF_LOCALIZED( + [OFStdErr writeLine: OF_LOCALIZED( @"ask_overwrite_help", @" y: yes\n" @" n: no\n" @" A: always\n" @" N: never")]; @@ -666,19 +666,19 @@ else if ([line isEqual: @"N"]) _overwrite = -1; if ([line isEqual: @"n"] || [line isEqual: @"N"]) { if (_outputLevel >= 0) - [of_stdout writeLine: OF_LOCALIZED(@"skipping_file", + [OFStdOut writeLine: OF_LOCALIZED(@"skipping_file", @"Skipping %[file]...", @"file", fileName)]; return false; } if (_outputLevel >= 0) - [of_stdout writeString: OF_LOCALIZED(@"extracting_file", + [OFStdOut writeString: OF_LOCALIZED(@"extracting_file", @"Extracting %[file]...", @"file", fileName)]; return true; } @@ -685,37 +685,35 @@ - (ssize_t)copyBlockFromStream: (OFStream *)input toStream: (OFStream *)output fileName: (OFString *)fileName { - char buffer[BUFFER_SIZE]; + char buffer[bufferSize]; size_t length; @try { - length = [input readIntoBuffer: buffer - length: BUFFER_SIZE]; + length = [input readIntoBuffer: buffer length: bufferSize]; } @catch (OFReadFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stdout writeString: @"\r"]; - [of_stderr writeLine: OF_LOCALIZED(@"failed_to_read_file", + [OFStdOut writeString: @"\r"]; + [OFStdErr writeLine: OF_LOCALIZED(@"failed_to_read_file", @"Failed to read file %[file]: %[error]", @"file", fileName, @"error", error)]; return -1; } @try { - [output writeBuffer: buffer - length: length]; + [output writeBuffer: buffer length: length]; } @catch (OFWriteFailedException *e) { OFString *error = [OFString stringWithCString: strerror(e.errNo) encoding: [OFLocale encoding]]; - [of_stdout writeString: @"\r"]; - [of_stderr writeLine: OF_LOCALIZED(@"failed_to_write_file", + [OFStdOut writeString: @"\r"]; + [OFStdErr writeLine: OF_LOCALIZED(@"failed_to_write_file", @"Failed to write file %[file]: %[error]", @"file", fileName, @"error", error)]; return -1; }