How do you write a codec for Rebol 3? - rebol

I wrote a JSON encoder/decoder for Rebol 2. I'm rewriting it for Rebol 3 and would prefer to make it available in Rebol 3 as a codec:
load %data.json
save %data.json object
decode 'json to-binary {["some","json"]}
How should I go about this?
At the time of asking, documentation on this subject is scarce.

The simple answer is that you can't. As for why, there are several answers to that.
The current codec model is undocumented. Only Carl knows how to write codecs at the moment. Though someone might be able to look at the source and figure it out, noone has yet.
The current codec functions aren't really flexible enough to make what you want here. You could make a decoder, but not an encoder because encode only takes images, binaries or strings. Maybe just having a decoder is enough for you though.
Only native codecs are supported by the current model. You can't write codecs in Rebol code, and for a parser that means that you can't use parse. Do you have a native code version of your parser?
The current codec model is a placeholder for the codec model we hope to eventually have. This is why it hasn't been that well documented. We eventually hope to support incremental en/decoding, codecs that can read directly from files or other ports, and Rebol codecs. However, we haven't really decided on the final model yet, or even started the discussion of how it will be designed, beyond initial criticisms.
I wish there was a better answer to your question right now.

Codecs were imo in highly non-finished state. I proposed some kind of streamed mechanism was needed, IIRC Carl understood the need, but the solution was never imo outlined. Ditto for tasks - current "implementation" is far from what Carl envisioned for R3 tasking ...

There is a tool, read this: http://rebol2.blogspot.it/2012/12/json.html
and this:
http://www.rebol.com/article/0522.html
However json is a complex way to handle data, rebol block is perfect in my humble opinion.

I just noticed that system/catalog/codecs is removed from Rebol 3; so I suppose that the feature was removed after 31-Mar-2009.
>> ? system/catalog
SYSTEM/CATALOG is an object of value:
datatypes block! length: 56
actions block! length: 56
natives block! length: 159
errors object! [Throw Note Syntax Script Math Access Command...
reflectors block! length: 6
boot-flags block! length: 18

Related

What format does PrintDocument.getData() return?

I'm looking into what it takes to develop a PrintService on android. After reading some on-line docs I'm not quite clear on the format of data returned by PrintDocument.getData() method. I'd expect that in the case of PrintDocumentInfo.CONTENT_TYPE_PHOTO the returned data will be an image (I'm not quite sure about this). However, what can I expect when content type is CONTENT_TYPE_DOCUMENT?
There is a sample of PrintDocumentInfo that uses a builder to build a pdf file. Is this always the case? That is, is content of CONTENT_TYPE_DOCUMENT always in pdf format?
I'd appreciate any suggestions and/or pointers to relevant on-line docs.
Thanks.
It is always PDF for CONTENT_TYPE_DOCUMENT.

Asking sample code for ISO 8583 verifone vx520

I want to know the sample code for sending message to server and get back response to verifone vx520 terminal using ISO 8583.
As noted in a comment on your question, this is not a code sharing site, so such an open-ended question is a bit difficult to answer, but perhaps I can get you started on the right foot.
First of all, let me start by suggesting that if you have control over the terminal code and the server that it will be talking to, I suggest you NOT use ISO8583. Yes, it's an industry standard and yes, it communicates data efficiently, BUT it is much more difficult to use than, say, VISA-1 or XML, or JSON etc. That means you have more opportunities for bugs to creep into your code. It also means that if something goes wrong, it takes a lot more effort to try and figure out what happened and try and fix it. I have used all these protocols and others besides and I'll tell you that ISO8583 is one of my least favorite to work with.
Assuming you do not have a choice and you must use ISO8583 then it's worth noting that ISO8583 is nothing but a specification on how to assemble data packets in order to communicate. There is nothing special about the Vx520 terminal (or any other VeriFone terminal) that would change how you would implement it verses how you might do so on any other C++ platform EXCEPT that VeriFone DOES provide you with a library for working with this spec that you are free to use or ignore as you see fit.
You don't need to use this library at all. You can roll your own and be just fine. You can find more information on the specification itself at Wikipedia, Code Project, and several other places (just ask your favorite search engine). Note that when I did my 8583 project, this library was not available to me. Perhaps I wouldn't have hated this protocol so much if I had had access to it... who knows?
If you are still reading this, then I'll assume that ISO8583 is a requirement (or you are a glutton for punishment) and that you are interested in trying out this engine that VeriFone has provided.
The first thing you will need to do (and hopefully, you have already done it) is to install ACT as part of the development suite (I also suggest you head over to DevNet and get the latest version of ACT before you get started...). Once installed, the library header can be found at %evoact%\include\iso8583.h. Documentation on how to use it can be found at %evoact%\docs. In particular, see chapter 6 of DOC00310_Verix_eVo_ACT_Programmers_Guide.pdf.
Obviously, trying to include a whole chapter's worth of information here would be out of scope, but to give you a high-level idea of how the engine works, allow me to share a couple excerpts:
This engine is designed to be table driven. A single routine is used
for the assembly and disassembly of ISO 8583 packets. The assembly and
disassembly of ISO 8583 packets is driven by the following structures:
Maps One or more collections of 64 bits that drive packet assembly and
indicate what is in a message.
Field table Defines all the fields used
by the application.
Convert table Defines data-conversion routines.
Variant tables Optional tables used to define variant fields.
The process_8583() routine is used for the assembly and disassembly of ISO
8583 packets.
An example of using process_8583() is given elsewhere as follows:
#include "appl8583.h"
int packet_sz;
void assemble_packet ()
{
packet_sz = process_8583 (0, field_table, test_map, buffer, sizeof( buffer));
printf ("\ fOUTPUT SIZE %d", packet_sz);
}
void disassemble_packet ()
{
packet_sz = process_8583 (1, field_table, test_map, buffer, packet_sz);
printf ("\ fINPUT NOT PROCESSED %d", packet_sz);
}
To incorporate this engine into an application, modify the APPL8583.C
and APPL8583.H files so that each has all the application variables
required in the bit map and set up the map properly. Compile
APPL8583.C and link it with your application and the ISO 8583 library.
Use the following procedures to transmit or receive an ISO 8583 packet
using the ISO 8583 Interface Engine:
To transmit an ISO 8583 packet
1 Set data values in the application variables for those to transmit.
2 Call the prot8583_main() routine. This constructs the complete
message and returns the number of bytes in the constructed message.
3 Call write() to transmit the message.
To receive a message
1 Call read() to receive the message.
2 Call the process_8583() routine. This results in all fields being
deposited into the application variables.
3 Use the values in the application variables.

How can I add some detection or correction code in a 48bits message?

I'm developing a OCR tool reeds a set of symbols which represent 2 bits each.
For example:
{ ---> 00
} ----> 01
Always I have a 48 bits code so I have 24 symbols.
I need to implement a detector or correction of errors in Objective-c (I'm developing an application for Mac).
My only requirement is that the code is as small as possible. I have a 48-bit (which represents 24 Sibol) and do not want to add many more to my design.
I have tried to implement this code:
http://code.google.com/p/rscode-objc/
But it is outdated, I get many errors and is designed for messages of 255 bytes (and of course I do not know how to adapt). If somebody can help me to adapt it, it would be great.
If you need further details, let me know.
Thank you.
You can try these two files for a complete CRC64 implementation (from the tags you added, I thought you'd be interested in CRC):
CRC64.h:
http://pastie.org/3227564
CRC64.c:
http://pastie.org/3227559
I have finally use the Reed-Solomon code: http://code.google.com/p/rscode-objc/
But thank you for your answer H2CO3 :)

API for getting IL from byte array

There is a GetILAsByteArray method in MethodBody class which gives body of a method. I am looking for converting this byte array into more understandable IL instructions (into a List or something like that). What resources, open source code or api available are there to help me understand and convert this byte array (or do it for me)?
I found this but it does not work with generics. I am pretty much looking for guidance to convert understand these bytes in all framework versions.
CLI Documentation is also helpful for learning IL instructions but I cannot see how to use it to make these bytes make sense.
I just wrote an extension method to get a more understandable list of instructions using GetILAsByteArray. It's quite simple, the API is like:
public static IList<Instruction> GetInstructions (this MethodBase self);
You can read more about the implementation in my blog post. Or you can go take the implementation and start using it.
Have a look at the Mono.Cecil library.
It is a huge undertaking. I wrote the starts of an IL reader and it had a pretty good amount of opcodes implemented: but you will need to finish it.
http://svn.ensemble-os.org/tags/OldOCJ/CIL/
There is also MONO Cecil, which is feature-complete.

How do I create an in-memory handle in Haskell?

I want something that looks like a file handle but is really backed by an in-memory buffer to use for I/O redirects. How can I do this?
I just wrote a library which provides this, called "knob" [hackage]. You can use it to create Handles which reference/modify a ByteString:
import Data.ByteString (pack)
import Data.Knob
import System.IO
main = do
knob <- newKnob (pack [])
h <- newFileHandle knob "test.txt" WriteMode
hPutStrLn h "Hello world!"
hClose h
bytes <- Data.Knob.getContents knob
putStrLn ("Wrote bytes: " ++ show bytes)
If you can express what you want to do in terms of C or system calls you could use Haskell's Foreign Function Interface (FFI). I started to suggest using mmap, but on second thought I think mmap might be a mapping the wrong way even if you used it with the anonymous option.
You can find more information about the Haskell FFI at the haskell.org wiki.
This is actually a bug in the library design, and one that's annoyed me, too. I see two approaches to doing what you want, neither of which is terribly attractive.
Create a new typeclass, make the current handle an instance of it, write another instance to do the in-memory-data thing, and change all of your programs that need to use this facility. Possibly this is as simple as importing System.SIO (or whatever you want to call it) instead of System.IO. But if you use the custom I/O routines in libraries such as Data.ByteString, there's more work to be done there.
Rewrite the I/O libraries to extend them to support this. Not trivial, and a lot of work, but it wouldn't be particularly difficult work to do. However, then you've got a compatibility issue with systems that don't have this library.
This may not be possible. GHC, at least, seems to require a handle to have an OS file descriptor that is used for all read/write/seek operations.
See /libraries/base/IOBase.lhs from the GHC sources.
You may be able to get the same effect by enlisting the OS's help: create a temporary file, connect the handle to it and then memory map the file for the I/O redirects. This way, all the handle I/O would become visible in the memory mapped section.
To add a modern answer to this question, you could use createPipe from System.Process:
createPipe :: IO (Handle, Handle)
https://www.stackage.org/haddock/lts-10.3/process-1.6.1.0/System-Process.html#v:createPipe
It's not possible without modifying the compiler. This is because Handle is an abstract data type, not a typeclass.