GithubHelp home page GithubHelp logo

mvpcstruct's Introduction

C struct handling for Swift

Class for packing and unpacking C structs in Swift, modeled after the struct module in Python.

Sample Code

import MVPCStruct

// Receiver expects a message with a header like this:
//  typedef struct {
//      uint16_t version;  // Message format version, currently 0x0100.
//      uint16_t reserved; // Reserved for future use.
//      uint32_t length;   // Length of data in bytes.
//      uint8_t data[];    // Binary encoded plist.
//  } __attribute__((packed)) mma_msg_t;
func sendMessageHeader(msgData: NSData) -> Bool {
    var error: NSError?
    
    let version = 0x0100
    let reserved = 0
    
    let packer = CStruct(format: "=HHI")
    if let packedHeader = packer.pack([version, reserved, msgData.length], error: &error) {
        return 8 == send(socket_fd, packedHeader.bytes, packedHeader.length)
    } else {
        return false
    }
}

Tasks

Creating CStruct Objects

- initWithFormat:

init(format: String)

Unpacking data

- unpack:format:error:

func unpack(data: NSData, format: String, error: NSErrorPointer) -> AnyObject[]?

- unpack:error:

func unpack(data: NSData, error: NSErrorPointer) -> AnyObject[]?

Packing values

- pack:format:error:

func pack(values: AnyObject[], format: String, error: NSErrorPointer) -> NSData?

- pack:error:

func pack(values: AnyObject[], error: NSErrorPointer) -> NSData? {

Format strings

Control characters

BYTE ORDER SIZE ALIGNMENT
@ native native native
= native standard none
< little-endian standard none
> big-endian standard none
! network (BE) standard none

Format characters

FORMAT C TYPE SWIFT TYPE SIZE
x pad byte no value
c char String of length 1 1
b signed char Int 1
B unsigned char UInt 1
? _Bool Bool 1
h short Int 2
H unsigned short UInt 2
i int Int 4
I unsigned int UInt 4
l long Int 4
L unsigned long UInt 4
q long long Int 8
Q unsigned long long UInt 8
f float Float 4
d double Double 8
s char[] String
p char[] String
P void * UInt 4/8

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.