summaryrefslogtreecommitdiff
path: root/swift/src/Ice/Util.swift
blob: b9ef5cd3e5330dc48c4044c8ca55332043b82b2e (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

import Dispatch
import IceObjc

/// Converts a string to an encoding version.
///
/// - parameter _: `String` - The string to convert.
///
/// - returns: `Ice.EncodingVersion` - The converted encoding version.</returns>
func stringToEncodingVersion(_ s: String) throws -> EncodingVersion {
    let (major, minor) = try stringToMajorMinor(s)
    return EncodingVersion(major: major, minor: minor)
}

func stringToMajorMinor(_ s: String) throws -> (UInt8, UInt8) {
    let components = s.components(separatedBy: ".")
    guard components.count == 2 else {
        throw VersionParseException(str: "malformed value `\(s)'")
    }

    guard let major = UInt8(components[0] as String), let minor = UInt8(components[1]) else {
        throw VersionParseException(str: "invalid version value `\(s)'")
    }

    return (major, minor)
}

func createSentCallback(sentOn: DispatchQueue?,
                        sentFlags: DispatchWorkItemFlags?,
                        sent: ((Bool) -> Void)?) -> ((Bool) -> Void)? {
    guard let s = sent, let q = sentOn else {
        //
        // This is either a nil sent callback or a sent callback that is dispatch
        // directly without a queue
        //
        return sent
    }

    //
    // Create a closure to dispatch the sent callback in the specified queue
    //
    if let flags = sentFlags {
        return { sentSynchronously in
            q.async(flags: flags) {
                s(sentSynchronously)
            }
        }
    } else {
        return { sentSynchronously in
            q.async {
                s(sentSynchronously)
            }
        }
    }
}

func escapeString(string: String, special: String, communicator: Communicator) throws -> String {
    guard factoriesRegistered else {
        fatalError("Unable to initialie Ice")
    }
    return try autoreleasepool {
        try ICEUtil.escapeString(string: string,
                                 special: special,
                                 communicator: (communicator as! CommunicatorI).handle)
    }
}

func checkSupportedEncoding(_ v: EncodingVersion) throws {
    let c = currentEncoding
    if v.major != c.major || v.minor > c.minor {
        throw UnsupportedEncodingException(reason: "", bad: v, supported: c)
    }
}