www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RosettaCode proposal: RFC diagram converter

reply "bearophile" <bearophileHUGS lycos.com> writes:
Inspired by a talk by Eden in the StrangeLoop 2013 conference, 
I'd like to create a new small Rosettacode Task. Perhaps someone 
here is able to review this task description a little, or even to 
implement the D solution:

  - - - - - - -TASK DESCRIPTION START- - - - - - - - -

Task Title: ASCII art diagram converter


Given the RFC 1035 message diagram from Section 4.1.1 (Header 
section format) as a string:
http://www.ietf.org/rfc/rfc1035.txt

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    QDCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ANCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    NSCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ARCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+


Where:

ID is 16 bits
QR = Query (0) or Response (1)
Opcode = Four bits defining kine of query:
   0:    a standard query (QUERY)
   1:    an inverse query (IQUERY)
   2:    a server status request (STATUS)
   3-15: reserved for future use
AA = Authoritative Answer bit
TC = Truncation bit
RD = Recursion Desired bit
RA = Recursion Available bit
Z = Reserved
RCODE = Response code
QC = Question Count
ANC = Answer Count
AUC = Authority Count
ADC = Additional Count</pre>

Write a function, member function, class or template that accepts 
a similar multi-line string as input to define a data structure 
or something else able to decode or store a header with that 
specified bit structure.

If your language has macros, introspection, code generation, or 
powerful enough templates, then accept such string at 
compile-time to define the header data structure statically.

Such "Header" function or template should accept a table with 8, 
16, 32, 64 columns and any number of rows. The only allowed 
symbols to define the table are + - | (plus, minus, pipe), and 
whitespace. Lines of the input string composed just of whitespace 
should be ignored. Leading and trailing whitespace in the input 
string should be ignored, as well as  before and after each table 
row. The box for each bit of the diagram takes four chars "+--+". 
The code should perform a little of validation of the input 
string, but for brevity a full validation is not required.

  - - - - - - -TASK DESCRIPTION END- - - - - - - - -

I think such ASCII art inside the source code is easy to read, so 
in theory the D entry could be useful for more than just a 
Rosettacode Task.

The usage syntax of the D entry:


alias H1 = BitfieldsTable!"
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    QDCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ANCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    NSCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ARCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+";


Bye,
bearophile
Sep 30 2013
parent reply "qznc" <qznc web.de> writes:
On Monday, 30 September 2013 at 23:27:48 UTC, bearophile wrote:
 Inspired by a talk by Eden in the StrangeLoop 2013 conference, 
 I'd like to create a new small Rosettacode Task. Perhaps 
 someone here is able to review this task description a little, 
 or even to implement the D solution:

  - - - - - - -TASK DESCRIPTION START- - - - - - - - -

 Task Title: ASCII art diagram converter


 Given the RFC 1035 message diagram from Section 4.1.1 (Header 
 section format) as a string:
 http://www.ietf.org/rfc/rfc1035.txt

 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                      ID                       |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    QDCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    ANCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    NSCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    ARCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+


 Where:

 ID is 16 bits
 QR = Query (0) or Response (1)
 Opcode = Four bits defining kine of query:
   0:    a standard query (QUERY)
   1:    an inverse query (IQUERY)
   2:    a server status request (STATUS)
   3-15: reserved for future use
 AA = Authoritative Answer bit
 TC = Truncation bit
 RD = Recursion Desired bit
 RA = Recursion Available bit
 Z = Reserved
 RCODE = Response code
 QC = Question Count
 ANC = Answer Count
 AUC = Authority Count
 ADC = Additional Count</pre>

 Write a function, member function, class or template that 
 accepts a similar multi-line string as input to define a data 
 structure or something else able to decode or store a header 
 with that specified bit structure.

 If your language has macros, introspection, code generation, or 
 powerful enough templates, then accept such string at 
 compile-time to define the header data structure statically.

 Such "Header" function or template should accept a table with 
 8, 16, 32, 64 columns and any number of rows. The only allowed 
 symbols to define the table are + - | (plus, minus, pipe), and 
 whitespace. Lines of the input string composed just of 
 whitespace should be ignored. Leading and trailing whitespace 
 in the input string should be ignored, as well as  before and 
 after each table row. The box for each bit of the diagram takes 
 four chars "+--+". The code should perform a little of 
 validation of the input string, but for brevity a full 
 validation is not required.

  - - - - - - -TASK DESCRIPTION END- - - - - - - - -

 I think such ASCII art inside the source code is easy to read, 
 so in theory the D entry could be useful for more than just a 
 Rosettacode Task.

 The usage syntax of the D entry:


 alias H1 = BitfieldsTable!"
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                      ID                       |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    QDCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    ANCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    NSCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 |                    ARCOUNT                    |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+";


 Bye,
 bearophile
Nice idea! :) I assume 1 column corresponds to 1 bit. This should probably be mentioned explicitly in the task description.
Oct 01 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
qznc:

 Nice idea! :)

 I assume 1 column corresponds to 1 bit. This should probably be 
 mentioned explicitly in the task description.
OK. http://rosettacode.org/wiki/ASCII_art_diagram_converter Bye, bearophile
Oct 14 2013