Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/teliosdev/packed_struct
Cleans up the string mess when packing items.
https://github.com/teliosdev/packed_struct
Last synced: 1 day ago
JSON representation
Cleans up the string mess when packing items.
- Host: GitHub
- URL: https://github.com/teliosdev/packed_struct
- Owner: teliosdev
- License: mit
- Created: 2013-06-26T13:32:44.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-08-15T18:52:13.000Z (over 10 years ago)
- Last Synced: 2024-10-31T15:08:05.732Z (15 days ago)
- Language: Ruby
- Homepage: http://rdoc.info/github/redjazz96/packed_struct
- Size: 192 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PackedStruct [![Build Status](https://travis-ci.org/medcat/packed_struct.png?branch=master)](https://travis-ci.org/redjazz96/packed_struct)
`PackedStruct` is a way to define packing strings (see [`Array#pack`](http://ruby-doc.org/core-2.0/Array.html#method-i-pack)).
It was created after @charliesome suggested [a format](https://gist.github.com/medcat/6dda0554f62e4f77253a) for defining these strings, but never finished it.The basic way of defining a packed struct is such:
```Ruby
class RconPacket
include PackedStruct
struct_layout :packet do
little_endian signed size[32] # defaults to a number of size 32.
# also the same as: `little_endian signed long size`
little_endian signed id[32]
little_endian signed type[32]
string body[size]
null
end
end
```This can be accessed as:
```Ruby
RconPacket.structs[:packet].pack(size: 11, id: 1, type: 0, body: "hello world")
# => "\v\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00hello world\x00"
```You can also unpack strings.
```Ruby
RconPacket.structs[:packet].unpack("\v\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00hello world\x00")
# => {:size => 11, :id => 1, :type => 0, :body => "hello world"}
```From sockets, too. Anything that responds to `#read`.
```Ruby
file = File.open("/path/to/some/file", "r")RconPacket.structs[:packet].unpack_from_socket(file)
# => ...
```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/redjazz96/packed_struct/trend.png)](https://bitdeli.com/free "Bitdeli Badge")