Skip to content

Custom read/write functions#13

Open
gearheart wants to merge 3 commits intoTritonDataCenter:masterfrom
gearheart:master
Open

Custom read/write functions#13
gearheart wants to merge 3 commits intoTritonDataCenter:masterfrom
gearheart:master

Conversation

@gearheart
Copy link

Hi

I needed to implement a custom data type for my project, with custom read and write functions, but I couldn't find a way to add it to parser, so I created this small patch.

Basically it allows to do things like this:

parser.typdedef('customType', {read: ..., write: ...});

@rmustacc
Copy link
Contributor

This is making several different changes in one go and I need to understand the motivation a little better. I'm hesitant to commit to the interface for reading and writing types. Can you help explain the use cases for the two main changes that you're proposing, an example or two can help me understand the benefit of adding it.

  1. Returning the data size from writeData

  2. Allowing custom readers and writers for types

@gearheart
Copy link
Author

I'm implementing server which talks to existing flash application.

General format of messages used there is: length of message first and then bytes of message itself, so I've modified writeData to return size.

Also I needed new data type - string with variable length - length frist, then char array, which I couldn't do with existing structs. So I've written custom read and write functions.

@gearheart gearheart closed this Jul 26, 2011
@gearheart gearheart reopened this Jul 26, 2011
@rmustacc
Copy link
Contributor

I've been thinking on this a little bit and in this case I would probably end up using the lower level API to read the variable length amount. In fact if it is only ever the message length and the message, the low level functions are probably better.

I don't want to commit to a specific format for custom readers and writers yet. Though there needs to more generally be a better way to do this, I'm not sure that this is the best way yet.

@terinjokes
Copy link
Contributor

He should be able to do something like the following, right?

var parser = new mod_ctype.Parser({ endian: 'big' }),
    buffer = new Buffer(9);
parser.writeData([
  { 'len': { type: 'uint32_t' } },
  { 'message': { type: 'char[len]' } }
], buffer, 0, [ 5, new Buffer('hello') ]);

If I understand him correctly, it seems like the functionality is now available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants