/
YAML.swift
63 lines (52 loc) · 1.52 KB
/
YAML.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
This source file is part of the Swift.org open source project
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import struct Utility.Path
import struct libc.FILE
import func libc.fclose
import POSIX
class YAML {
let path: String
private let fp: UnsafeMutablePointer<FILE>
init(path: String...) throws {
self.path = Path.join(path)
fp = try fopen(self.path, mode: .Write)
}
func close() {
fclose(fp)
}
func write(anys: Any...) throws {
var anys = anys
try fputs(anys.removeFirst() as! String, fp)
if !anys.isEmpty {
try fputs(anys.map(toYAML).joinWithSeparator(""), fp)
}
try fputs("\n", fp)
}
}
private func toYAML(any: Any) -> String {
func quote(input: String) -> String {
for c in input.characters {
if c == "@" || c == " " || c == "-" {
return "\"\(input)\""
}
}
return input
}
switch any {
case let string as String where string == "":
return "\"\""
case let string as String:
return string
case let array as [String]:
return "[" + array.map(quote).joinWithSeparator(", ") + "]"
case let bool as Bool:
return bool ? "true" : "false"
default:
fatalError("Unimplemented YAML type")
}
}