Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ parser

C++ parser

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrea Di Domenico
    wrote on last edited by
    #1

    Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.

    C CPalliniC R A 4 Replies Last reply
    0
    • A Andrea Di Domenico

      Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The word message is overused in software, so that it doesn't mean anything at all outside its context. I guess you are talking about a couple of bytes which come from a certain source (e.g. a serial port). AFAIK there's no library which could help you for that. However, this shouldn't be too complex to implement yourself. But it depends also what you have to do exactly: how does this parsing works ? Can you give a concrete example (with a concrete message and concrete rules) ?

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      A 1 Reply Last reply
      0
      • C Cedric Moonen

        The word message is overused in software, so that it doesn't mean anything at all outside its context. I guess you are talking about a couple of bytes which come from a certain source (e.g. a serial port). AFAIK there's no library which could help you for that. However, this shouldn't be too complex to implement yourself. But it depends also what you have to do exactly: how does this parsing works ? Can you give a concrete example (with a concrete message and concrete rules) ?

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        A Offline
        A Offline
        Andrea Di Domenico
        wrote on last edited by
        #3

        thanks for your answer. A message is represented as Int16 type. The user, at run-time, can specify e.g (rule): bit1 :on/off field bit2 - bit5: address bit 6 - 11: sub address and so on... another rule could be: bit1: T/R field bit2 - bit10 telemetry and so on... When i write code don't know the rules. When the rules have been specified, the user select a particular rule and when arrive a message, the specified fields are filled according to the selected rule. Thank You.

        C 1 Reply Last reply
        0
        • A Andrea Di Domenico

          Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          You may embed a scripting language in your application, like (the excellent) Lua. Then, users can formulate the decoding rules by implementing functions (or simple data structures) using the hosted language. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • A Andrea Di Domenico

            thanks for your answer. A message is represented as Int16 type. The user, at run-time, can specify e.g (rule): bit1 :on/off field bit2 - bit5: address bit 6 - 11: sub address and so on... another rule could be: bit1: T/R field bit2 - bit10 telemetry and so on... When i write code don't know the rules. When the rules have been specified, the user select a particular rule and when arrive a message, the specified fields are filled according to the selected rule. Thank You.

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Andrea Di Domenico wrote:

            the specified fields are filled according to the selected rule.

            I still don't get what those fields are. Are they simple variables ? Any particular type ? As Carlo suggested, you might take a look at a scripting language, although this is perhaps a bit overkill for your case (that's what I'm trying to figure by asking you so many questions).

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            1 Reply Last reply
            0
            • A Andrea Di Domenico

              Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.

              R Offline
              R Offline
              Rick York
              wrote on last edited by
              #6

              I am not sure how experienced you are in C++ but one thing you will find is that the address operator (&) does not work with bits of a byte. This will be a very important factor since you are configuring which bits to peel apart. One thing I have done to help with this is I made functions to unpack the bits of a byte into an array of bytes and to also do the converse - pack the LSB of an array of bytes into one byte. Here is what the unpacker looks like :

              void BitUnpack( uchar byteArray[], uchar bits )
              {
              for( uint n = 0; n < 8; ++n )
              {
              uchar mask = 1 << n;
              byteArray[n] = ( bits & mask ) ? 1 : 0;
              }
              }

              This will give you an array of bytes where each one corresponds to the value of the bit at that location. This means that byteArray[2] will be 1 if the 3rd bit is set and it will be 0 if not. If you unpack all of the bits of your message into array of bytes it will be easy to then repack, say, bits 3-7 into one value and so on. Note that the function above unpacks just one byte. If you call this repeatedly you can unpack all of the bytes you receive into arrays for processing later. BTW - you may want to tweak the packer function to have it pack arbitrary numbers of bits to make things easier for you later. Here's the packer I use :

              void BitPack( uchar byteValue, uchar byteArray[] )
              {
              for( uint n = 0; n < 8; ++n )
              {
              if( byteArray[n] )
              {
              uchar mask = 1 << n;
              byteValue |= mask;
              }
              }
              }

              If you always work exclusively with 16-bit values then change the uchars to ushorts and the 8s to 16s and you should be ready to go. I should mention that I define the uchar, ushort, uint, and ulong types for myself but you don't have to use those since VC++ already defines the capitalized versions of these types. What this all comes down to is sorting out the bits and then reassembling them later. I hope these functions give you some ideas on how to approach this.

              modified on Tuesday, October 26, 2010 6:13 PM

              A 1 Reply Last reply
              0
              • A Andrea Di Domenico

                Hello, I would to develop an application in c++ to decode some messages. The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified. e.g. I have a message formed by 16 bit and at most I can have 32 messages in the same frame. The user can add the rules to decode the frame. Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user. How can i implement this application? is there some tool that simplifies the development? Thank You, Andrea.

                A Offline
                A Offline
                Alain Rist
                wrote on last edited by
                #7

                Hi Andrea, Have a look at std::valarray<bool>[^] and the associated std::slice[^]. Not much more out there to help you. cheers, AR

                When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                1 Reply Last reply
                0
                • R Rick York

                  I am not sure how experienced you are in C++ but one thing you will find is that the address operator (&) does not work with bits of a byte. This will be a very important factor since you are configuring which bits to peel apart. One thing I have done to help with this is I made functions to unpack the bits of a byte into an array of bytes and to also do the converse - pack the LSB of an array of bytes into one byte. Here is what the unpacker looks like :

                  void BitUnpack( uchar byteArray[], uchar bits )
                  {
                  for( uint n = 0; n < 8; ++n )
                  {
                  uchar mask = 1 << n;
                  byteArray[n] = ( bits & mask ) ? 1 : 0;
                  }
                  }

                  This will give you an array of bytes where each one corresponds to the value of the bit at that location. This means that byteArray[2] will be 1 if the 3rd bit is set and it will be 0 if not. If you unpack all of the bits of your message into array of bytes it will be easy to then repack, say, bits 3-7 into one value and so on. Note that the function above unpacks just one byte. If you call this repeatedly you can unpack all of the bytes you receive into arrays for processing later. BTW - you may want to tweak the packer function to have it pack arbitrary numbers of bits to make things easier for you later. Here's the packer I use :

                  void BitPack( uchar byteValue, uchar byteArray[] )
                  {
                  for( uint n = 0; n < 8; ++n )
                  {
                  if( byteArray[n] )
                  {
                  uchar mask = 1 << n;
                  byteValue |= mask;
                  }
                  }
                  }

                  If you always work exclusively with 16-bit values then change the uchars to ushorts and the 8s to 16s and you should be ready to go. I should mention that I define the uchar, ushort, uint, and ulong types for myself but you don't have to use those since VC++ already defines the capitalized versions of these types. What this all comes down to is sorting out the bits and then reassembling them later. I hope these functions give you some ideas on how to approach this.

                  modified on Tuesday, October 26, 2010 6:13 PM

                  A Offline
                  A Offline
                  Alain Rist
                  wrote on last edited by
                  #8

                  Congrats! You invented std::bitset[^] :)

                  When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                  R 1 Reply Last reply
                  0
                  • A Alain Rist

                    Congrats! You invented std::bitset[^] :)

                    When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                    R Offline
                    R Offline
                    Rick York
                    wrote on last edited by
                    #9

                    I doubt it. Those two functions were first written more than twenty years ago. :rolleyes:

                    A 1 Reply Last reply
                    0
                    • R Rick York

                      I doubt it. Those two functions were first written more than twenty years ago. :rolleyes:

                      A Offline
                      A Offline
                      Alain Rist
                      wrote on last edited by
                      #10

                      Hi Rick,

                      Rick York wrote:

                      Those two functions were first written more than twenty years ago

                      Dont' be ashamed to be a precursor :) C++ improved a lot from this time, and is improving again with C++0x. cheers, AR

                      When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                      A 1 Reply Last reply
                      0
                      • A Alain Rist

                        Hi Rick,

                        Rick York wrote:

                        Those two functions were first written more than twenty years ago

                        Dont' be ashamed to be a precursor :) C++ improved a lot from this time, and is improving again with C++0x. cheers, AR

                        When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                        A Offline
                        A Offline
                        Andrea Di Domenico
                        wrote on last edited by
                        #11

                        Thank you fro the answer

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups