C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 10:57 PM
ChokolAwt's Avatar
ChokolAwt ChokolAwt is offline
The bad and the ugly...
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: Oz... No??? Neverland then?
Posts: 142 ChokolAwt Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 6 h 21 m 2 sec
Reputation Power: 0
Send a message via ICQ to ChokolAwt Send a message via AIM to ChokolAwt
State machine parser

Hello all,

For my final homework assignment our instructor has us writing a simple byte parser. the attached file (which needs to be renamed *.bin) is the input. For some reason, my parser seems to be grabbing something at the end of the file and passing it to my parse() method thus outputting junk at the end.

The input has a standard format of
Code:
Start->Type->Size(low)->Size(high)->Payload->Checksum


Theres 4 different Types for the input file (ASCII, Integer, Float and Double) but I'm just working on ASCII right now.

my parser class:
Code:
#include "parser.h"

parser::parser()
{
	this->State = start;
	i = 0;
	j = 0;
}

parser::~parser()
{

}

bool parser::parse(unsigned char byte)
{
	switch(this->State)
		{
			case start:
				if (byte = 170)
					{
						Chk = 170;
						State = type;
						break;
					}
				
				
			case type:
				Type = byte;
				Chk += byte;
				State = size_low;
				break;
					
			case size_low:
				Size = byte;
				Chk += byte;
				State = size_high;
				break;	
				
			case size_high:
				Size += (byte << 8);
				Chk += byte;
				State = payload;
				break;
				
			case payload:
				if (i == Size)
					{
						State = checksum;
						break;
					}
					
				Data[i] = byte;
				Chk += byte;
				i++;
				break;
				
			case checksum:
				if (byte = Chk) 
					{
						State = start;
						return true;
					}
				else 
					{
						State = start;
						break;
					}
		}
	return false;
}


void parser::process(void)
{
	switch(Type)
		{
			case 0:
				char *pa;
				pa = (char*)(Data);
					for(j; j < Size; j++)
						{
							std::cout << *pa << std::endl;
							*pa++;
						}
					
			case 1:
				int *pb;
				pb = reinterpret_cast<int *>(Data);
				for(j; j < Size; j++)
						{
							std::cout << *pb;
							*pb++;
						}
						
			case 2:
				float *pc;
				pc = reinterpret_cast<float *>(Data);
				std::cout << *pc;

			case 3:
				double *pd;
				pd = reinterpret_cast<double *>(Data);
				for (j; j < Size; j++)
					{
						std::cout << *pd;
						*pd++;
					}	
		}
}


my header file:
Code:
#include <iostream>
#include <string>

// Definition for Magic numbers
#define MAX_SIZE 1024

// ParseState are the different states in your parser 
enum ParseState{start, type, size_low, size_high, payload, checksum};

// The different types of payload
enum TypeID{asciiType, integerType, floatType, doubleType};

// Parser class definition
class parser{

	private:
		
		ParseState State;				// Holds the current state of the parser
		int i;
		int j;
		unsigned int Size; 				// Holds the size of the payload
		unsigned char Type;				// Holds the type of the payload
		unsigned char Chk;				// Holds the running checksum
		unsigned char Data[MAX_SIZE];	// Holds the payload data
		
		
	public:
		// Constructor
		parser();
		
		// Destructor
		~parser();
		
		// Parse function, 
		// Argument is a byte out of the input file
		// Return value is true when packet has been completely parsed and checksum is correct
		bool parse(unsigned char);
		
		// Process payload function
		void process(void);
		
};


and my main:
Code:
#include <fstream>
#include "parser.h"

using namespace std;


int main (int argc, char *argv[])
{
	ifstream filename;
	parser funk;
	unsigned char byte;
	
	//if user calls program from cmd prompt incorrectly
		if((argc < 2)) 
			{
				cout << "This program will parse a .bin file and print payload\n";
				cout << "\n";
				cout << "Usage: hw8 [input filename].bin\n";
				exit(0);
			}
	
	filename.open(argv[1], ios::binary); //open file in binary mode
		if ( !filename )
			{
				cout << "File does not exist.\n";
				exit(0);
			}
	
	while (!filename.eof())
		{
			byte = filename.get();
			//cout << "byte :" << hex << (int)byte << endl;
			if (funk.parse(byte) == true)
				funk.process();
		}
	filename.close();
}




When you run it at command line with
Code:
hw8 testASCII.bin

The output is
Code:
Hello World!1.14314e+027


The only thing I can think is that my while loop in my main is passing something to parse() that it shouldn't but I haven't been able to figure out why. Am I not using reinterpret_cast correctly?
Attached Files
File Type: txt testASCII.txt (17 Bytes, 22 views)
__________________
"Life is not a journey with the intent on arriving at the finish line in a pretty and well preserved body. But rather to skid in broadside, totally worn out, thoroughly used up and loudly proclaiming, "Wow! What a ride!" -Anonymous
Halo! || Diablo 2 LOD Modding || OLGA's BACK!

Reply With Quote
  #2  
Old December 7th, 2012, 11:34 PM
ChokolAwt's Avatar
ChokolAwt ChokolAwt is offline
The bad and the ugly...
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: Oz... No??? Neverland then?
Posts: 142 ChokolAwt Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 6 h 21 m 2 sec
Reputation Power: 0
Send a message via ICQ to ChokolAwt Send a message via AIM to ChokolAwt
headway!

Okay, so if i modify my while loop in main to print out byte each time through
Code:
while (!filename.eof())
		{
			byte = filename.get();
			
			cout << "byte :" << hex << (int)byte << endl;
			if (funk.parse(byte) == true)
				funk.process();
		}


The output is
Code:
 
byte :aa
byte :0
byte :c
byte :0
byte :48
byte :65
byte :6c
byte :6c
byte :6f
byte :20
byte :77
byte :6f
byte :72
byte :6c
byte :64
byte :21
byte :13
byte :ff
c
Hello world!1.14314e+027

Reply With Quote
  #3  
Old December 8th, 2012, 01:45 AM
ChokolAwt's Avatar
ChokolAwt ChokolAwt is offline
The bad and the ugly...
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: Oz... No??? Neverland then?
Posts: 142 ChokolAwt Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 6 h 21 m 2 sec
Reputation Power: 0
Send a message via ICQ to ChokolAwt Send a message via AIM to ChokolAwt
figured it out!

Reply With Quote
  #4  
Old December 8th, 2012, 05:47 AM
shane002 shane002 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 shane002 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 22 m 40 sec
Reputation Power: 0
State machine parser

This is in contrast to the parsing-theory origins of the term finite-state machine where the machine is described as consuming characters or tokens.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > State machine parser

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap