36 lines
1.1 KiB
Swift
36 lines
1.1 KiB
Swift
/*
|
|
This source file is part of the Swift.org open source project
|
|
|
|
Copyright (c) 2014 - 2017 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 Foundation
|
|
|
|
/// `CStringArray` represents a C null-terminated array of pointers to C strings.
|
|
///
|
|
/// The lifetime of the C strings will correspond to the lifetime of the `CStringArray`
|
|
/// instance so be careful about copying the buffer as it may contain dangling pointers.
|
|
public final class CStringArray {
|
|
/// The null-terminated array of C string pointers.
|
|
public let cArray: [UnsafeMutablePointer<Int8>?]
|
|
|
|
/// Creates an instance from an array of strings.
|
|
public init(_ array: [String]) {
|
|
#if os(Windows)
|
|
cArray = array.map({ $0.withCString({ _strdup($0) }) }) + [nil]
|
|
#else
|
|
cArray = array.map({ $0.withCString({ strdup($0) }) }) + [nil]
|
|
#endif
|
|
}
|
|
|
|
deinit {
|
|
for case let element? in cArray {
|
|
free(element)
|
|
}
|
|
}
|
|
}
|