NSString+FileKit.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // NSString+FileKit.swift
  3. // FileKit
  4. //
  5. // The MIT License (MIT)
  6. //
  7. // Copyright (c) 2015-2017 Nikolai Vazquez
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. import Foundation
  28. extension NSString {
  29. /// Returns an String object initialized by copying the characters from
  30. /// the raw value of a given path.
  31. public convenience init(path: Path) {
  32. self.init(string: path.rawValue)
  33. }
  34. }
  35. extension NSString: Writable {
  36. /// Writes the string to a path atomically.
  37. ///
  38. /// - Parameter path: The path being written to.
  39. ///
  40. public func write(to path: Path) throws {
  41. try write(to: path, atomically: true)
  42. }
  43. /// Writes the string to a path with `NSUTF8StringEncoding` encoding.
  44. ///
  45. /// - Parameter path: The path being written to.
  46. /// - Parameter useAuxiliaryFile: If `true`, the data is written to an
  47. /// auxiliary file that is then renamed to the
  48. /// file. If `false`, the data is written to
  49. /// the file directly.
  50. ///
  51. public func write(to path: Path, atomically useAuxiliaryFile: Bool) throws {
  52. do {
  53. try self.write(toFile: path._safeRawValue,
  54. atomically: useAuxiliaryFile,
  55. encoding: String.Encoding.utf8.rawValue)
  56. } catch {
  57. throw FileKitError.writeToFileFail(path: path, error: error)
  58. }
  59. }
  60. }
  61. /*
  62. extension NSString: Readable {
  63. /// Creates a string from a path.
  64. public class func read(from path: Path) throws -> Self {
  65. let possibleContents = try? NSString(
  66. contentsOfFile: path._safeRawValue,
  67. encoding: String.Encoding.utf8.rawValue)
  68. guard let contents = possibleContents else {
  69. throw FileKitError.readFromFileFail(path: path)
  70. }
  71. return contents
  72. }
  73. }
  74. */