summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Schleifer <js@heap.zone>2018-01-15 01:34:58 +0100
committerJonathan Schleifer <js@heap.zone>2018-01-15 01:34:58 +0100
commit6144fd68627b3f5ebd4587c798e686dcfb84554a (patch)
treee7faeaf30107085ace4e8cd1400207b531887f67
parent451433401f66770c3832920c3e7fcdc33891f716 (diff)
Add -[writeToURL:]
-rw-r--r--src/OFConstantString.m18
-rw-r--r--src/OFData.h7
-rw-r--r--src/OFData.m16
-rw-r--r--src/OFString.h16
-rw-r--r--src/OFString.m25
5 files changed, 82 insertions, 0 deletions
diff --git a/src/OFConstantString.m b/src/OFConstantString.m
index 1eb63dd5..dc6570fb 100644
--- a/src/OFConstantString.m
+++ b/src/OFConstantString.m
@@ -664,6 +664,7 @@ struct {
}
#endif
+#ifdef OF_HAVE_FILES
- (void)writeToFile: (OFString *)path
{
[self finishInitialization];
@@ -679,6 +680,23 @@ struct {
[self writeToFile: path
encoding: encoding];
}
+#endif
+
+- (void)writeToURL: (OFURL *)URL
+{
+ [self finishInitialization];
+
+ [self writeToURL: URL];
+}
+
+- (void)writeToURL: (OFURL *)URL
+ encoding: (of_string_encoding_t)encoding
+{
+ [self finishInitialization];
+
+ [self writeToURL: URL
+ encoding: encoding];
+}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
diff --git a/src/OFData.h b/src/OFData.h
index f514157d..e8e05ea4 100644
--- a/src/OFData.h
+++ b/src/OFData.h
@@ -308,6 +308,13 @@ enum {
*/
- (void)writeToFile: (OFString *)path;
#endif
+
+/*!
+ * @brief Writes the OFData to the specified URL.
+ *
+ * @param URL The URL to write to
+ */
+- (void)writeToURL: (OFURL *)URL;
@end
OF_ASSUME_NONNULL_END
diff --git a/src/OFData.m b/src/OFData.m
index 15467cbb..588385ff 100644
--- a/src/OFData.m
+++ b/src/OFData.m
@@ -601,6 +601,22 @@ _references_to_categories_of_OFData(void)
}
#endif
+- (void)writeToURL: (OFURL *)URL
+{
+ void *pool = objc_autoreleasePoolPush();
+ OFURLHandler *URLHandler;
+ OFStream *stream;
+
+ if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
+ @throw [OFUnsupportedProtocolException exceptionWithURL: URL];
+
+ stream = [URLHandler openItemAtURL: URL
+ mode: @"w"];
+ [stream writeData: self];
+
+ objc_autoreleasePoolPop(pool);
+}
+
- (OFXMLElement *)XMLElementBySerializing
{
void *pool;
diff --git a/src/OFString.h b/src/OFString.h
index a3d55c1b..a0524bd4 100644
--- a/src/OFString.h
+++ b/src/OFString.h
@@ -1218,6 +1218,22 @@ typedef void (^of_string_line_enumeration_block_t)(OFString *line, bool *stop);
encoding: (of_string_encoding_t)encoding;
#endif
+/*!
+ * @brief Writes the string to the specified URL using UTF-8 encoding.
+ *
+ * @param URL The URL to write to
+ */
+- (void)writeToURL: (OFURL *)URL;
+
+/*!
+ * @brief Writes the string to the specified URL using the specified encoding.
+ *
+ * @param URL The URL to write to
+ * @param encoding The encoding to use to write the string to the URL
+ */
+- (void)writeToURL: (OFURL *)URL
+ encoding: (of_string_encoding_t)encoding;
+
#ifdef OF_HAVE_BLOCKS
/*!
* Enumerates all lines in the receiver using the specified block.
diff --git a/src/OFString.m b/src/OFString.m
index 757b5610..3122341d 100644
--- a/src/OFString.m
+++ b/src/OFString.m
@@ -43,6 +43,7 @@
# import "OFFileManager.h"
#endif
#import "OFURL.h"
+#import "OFURLHandler.h"
#import "OFXMLElement.h"
#import "OFInitializationFailedException.h"
@@ -2901,6 +2902,30 @@ decomposedString(OFString *self, const char *const *const *table, size_t size)
}
#endif
+- (void)writeToURL: (OFURL *)URL
+{
+ [self writeToURL: URL
+ encoding: OF_STRING_ENCODING_UTF_8];
+}
+
+- (void)writeToURL: (OFURL *)URL
+ encoding: (of_string_encoding_t)encoding
+{
+ void *pool = objc_autoreleasePoolPush();
+ OFURLHandler *URLHandler;
+ OFStream *stream;
+
+ if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
+ @throw [OFUnsupportedProtocolException exceptionWithURL: URL];
+
+ stream = [URLHandler openItemAtURL: URL
+ mode: @"w"];
+ [stream writeString: self
+ encoding: encoding];
+
+ objc_autoreleasePoolPop(pool);
+}
+
#ifdef OF_HAVE_BLOCKS
- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block
{