@@ -14,8 +14,12 @@ FileSugar is a lightweight Swift package that provides an elegant API for file s
14
14
15
15
## Features
16
16
17
- - Open, save, delete, and create files
18
- - Simple API for working with files
17
+ - Open, save, delete, copy, move, and create files and directories
18
+ - Stream reading and writing support for handling large files efficiently
19
+ - Asynchronous file operations with ` async ` /` await `
20
+ - File path utilities for normalizing and expanding paths
21
+ - XML parsing capabilities on macOS
22
+ - Simple and consistent API for file system interactions
19
23
20
24
## Installation
21
25
@@ -27,14 +31,70 @@ You can install FileSugar using Swift Package Manager. Simply add the following
27
31
### Usage
28
32
Here are some examples of how to use FileSugar:
29
33
```swift
30
- // Write to a file
31
- FileModifier.write (" ~/Desktop/temp.txt" .tildePath , " test " )
34
+ // Write text to a file
35
+ let success = FileModifier.write (" ~/Desktop/temp.txt" .tildePath , content : " Hello, World! " )
32
36
33
- // Check if a file exists
34
- FileAsserter.exists (" ~/Desktop/temp.txt" .tildePath ) // Output: true
37
+ // Write data to a file
38
+ let data = Data ([0x00 , 0x01 , 0x02 ])
39
+ let dataWritten = try FileModifier.write (path : " ~/Desktop/data.bin" .tildePath , data : data)
35
40
36
- // Read the contents of a file
37
- FileParser.content (" ~/Desktop/temp.txt" .tildePath ) // Output: test
41
+ // Read text content from a file
42
+ if let content = FileParser.content (filePath : " ~/Desktop/temp.txt" .tildePath ) {
43
+ print (content)
44
+ }
45
+
46
+ // Read data from a file
47
+ if let data = FileParser.data (filePath : " ~/Desktop/data.bin" .tildePath ) {
48
+ // Process data
49
+ }
50
+
51
+ // Asynchronously read content from a file
52
+ Task {
53
+ do {
54
+ let content = try await FileParser.readContentAsync (url : URL (fileURLWithPath : " ~/Desktop/temp.txt" .tildePath ))
55
+ print (content)
56
+ } catch {
57
+ print (" Error reading file: \( error ) " )
58
+ }
59
+ }
60
+
61
+ // Asynchronously write content to a file
62
+ Task {
63
+ do {
64
+ try await FileModifier.writeContentAsync (url : URL (fileURLWithPath : " ~/Desktop/temp.txt" .tildePath ), content : " Async write" )
65
+ } catch {
66
+ print (" Error writing file: \( error ) " )
67
+ }
68
+ }
69
+
70
+ // Move a file
71
+ let moved = FileModifier.move (" ~/Desktop/source.txt" .tildePath , toURL : " ~/Desktop/destination.txt" .tildePath )
72
+
73
+ // Copy a file
74
+ let copied = FileModifier.copy (" ~/Desktop/source.txt" .tildePath , toURL : " ~/Desktop/copy.txt" .tildePath )
75
+
76
+ // Delete a file
77
+ let deleted = FileModifier.delete (" ~/Desktop/temp.txt" .tildePath )
78
+
79
+ // Create a directory
80
+ let dirCreated = FileModifier.createDir (path : " ~/Desktop/NewFolder" .tildePath )
81
+
82
+ // Write data to a file at a specific index
83
+ let data = " Partial" .data (using : .utf8 )!
84
+ try FileStreamWriter.write (filePath : " ~/Desktop/stream.txt" .tildePath , data : data, index : 5 )
85
+
86
+ // Read data from a file starting at a specific index
87
+ let readData = try FileStreamReader.read (filePath : " ~/Desktop/stream.txt" .tildePath , startIndex : 5 , endIndex : 10 )
88
+
89
+ // Normalize a file path
90
+ if let normalizedPath = FilePathModifier.normalize (" ~/Desktop/../Documents/file.txt" ) {
91
+ print (normalizedPath) // Outputs the absolute path
92
+ }
93
+
94
+ // Expand a relative file path
95
+ if let expandedPath = FilePathModifier.expand (" file.txt" , baseURL : " ~/Desktop" ) {
96
+ print (expandedPath)
97
+ }
38
98
```
39
99
40
100
### Contributing
@@ -47,3 +107,8 @@ FileSugar is available under the MIT license. See the LICENSE file for more info
47
107
- Add test file in spm resource .bundle
48
108
- Move FileStreamer into it's own repo again?
49
109
- Add more examples to readme?
110
+
111
+ ## Platforms
112
+
113
+ - iOS 17 or later
114
+ - macOS 14 or later
0 commit comments