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

mvpcstruct's People

Stargazers

Serhiy Butz avatar clyde avatar  avatar Jay Mayu avatar Eric Boxer avatar Riku Arakawa avatar Mikal avatar Chris Ballinger avatar Dale Price avatar  avatar tomisacat avatar  avatar Rick Windham avatar  avatar Eric Zhang avatar percy avatar Philippe Jayet avatar Nicolas Seriot avatar Devran Cosmo Uenal avatar Konstantinos Kontos avatar Hank Bao avatar Giles avatar José L Salazar Espitia avatar Markus W avatar Ryan Maloney avatar Omeed avatar Yume avatar

Watchers

James Cloos avatar  avatar

mvpcstruct's Issues

Suggested improvements

I would like to see 2 features in this project:

  1. Packing and unpacking of bit fields.
    The bit fields would allow a value to occupy 1 or more bits. See https://en.wikipedia.org/wiki/C_syntax#Bit_fields
  2. Packing and unpacking of packed BCD encoded values for integer types.
    A value of 1234 would be stored as 0x1234

I will be happy to try to add these features to this project.

Fails in Swift 2

Just tried the swift code and it looks like it is failing in XCode 7

License?

Is this project under any kind of open source license? If it is, it would be appropriate to specify it in a LICENSE file or in the README, otherwise it's technically illegal for anyone to copy, fork or use this code without explicit permission for each case.

Thanks,

/ Markus

upgrade to latest Swift

Will you upgrade this awesome repo to latest Swift? Try it myself and drown by the pointer :<

Update to Xcode 6 - Beta 6

There's been some syntax changes (among other things) to Swift since this was first written. In particular regarding array declarations and for loop ranges. I made the updates but I'm having issues building the project (may or may not be related to beta 4), otherwise I'd send a PR.

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.