SourceForge: hexciting/hexciting: changeset 6:e24d27c62ea7
Added start of printOperation() code. Currently doesn't work properly.
authorSam Kerr <kerr.sam@gmail.com>
Wed Jul 01 23:12:36 2009 -0400 (4 months ago)
changeset 6e24d27c62ea7
parent 5944cb237e543
child 77d53795796a1
Added start of printOperation() code. Currently doesn't work properly.
Command.cpp
Command.h
CommandParser.l
CommandParser.y
file
     1.1 --- a/Command.cpp	Tue Jun 30 19:23:54 2009 -0400
     1.2 +++ b/Command.cpp	Wed Jul 01 23:12:36 2009 -0400
     1.3 @@ -1,9 +1,13 @@
     1.4  #include "Command.h"
     1.5  #include <stdio.h>
     1.6 +#include <stdlib.h>
     1.7 +
     1.8 +#include <iostream>
     1.9 +#include <fstream>
    1.10 +
    1.11  Command::Command()
    1.12  {
    1.13      filename = 0; // null
    1.14 -    data = 0; // null
    1.15      dataCount = 0;
    1.16  }
    1.17  
    1.18 @@ -14,11 +18,30 @@
    1.19  
    1.20  void Command::print()
    1.21  {
    1.22 -    printf("------\n");
    1.23 +    printf("\n------\n");
    1.24      printf("Printing command info:\n");
    1.25      printf("------\n");
    1.26  
    1.27 -    printf("Mode: %d\n", mode);
    1.28 +    printf("Mode:");
    1.29 +	switch(mode)
    1.30 +	{
    1.31 +		case PRINT:
    1.32 +			printf("Print");
    1.33 +			break;
    1.34 +		case XOR:
    1.35 +			printf("Xor");
    1.36 +			break;
    1.37 +		case OR:
    1.38 +			printf("Or");
    1.39 +			break;
    1.40 +		case AND:
    1.41 +			printf("And");
    1.42 +			break;
    1.43 +		case NOT:
    1.44 +			printf("Not");
    1.45 +			break;
    1.46 +	};
    1.47 +	printf("\n");
    1.48  
    1.49      printf("Range: ");
    1.50      if(range == true)
    1.51 @@ -37,20 +60,80 @@
    1.52          printf("Data: %#x\n", (int)data[i]);
    1.53      
    1.54      printf("------\n");
    1.55 -    /*
    1.56 -    MODE_ENUM mode;
    1.57 -    int start;
    1.58 +}
    1.59  
    1.60 -    bool range;
    1.61 -    int end;
    1.62 -
    1.63 -    char* filename;
    1.64 -
    1.65 -    int* data;
    1.66 -    int dataCount;
    1.67 -*/
    1.68 -}
    1.69  void processCommand(Command* command)
    1.70  {
    1.71  
    1.72 +	switch(command->mode)
    1.73 +	{
    1.74 +		case PRINT:
    1.75 +			command->printOperation();
    1.76 +			break;
    1.77 +		case XOR:
    1.78 +			command->xorOperation();
    1.79 +			break;
    1.80 +		case OR:
    1.81 +			command->orOperation();
    1.82 +			break;
    1.83 +		case AND:
    1.84 +			command->andOperation();
    1.85 +			break;
    1.86 +		case NOT:
    1.87 +			command->notOperation();
    1.88 +			break;
    1.89 +	};
    1.90  }
    1.91 +
    1.92 +
    1.93 +void Command::printOperation()
    1.94 +{
    1.95 +	std::ifstream ifs(filename, std::ifstream::in | std::ifstream::binary);
    1.96 +	
    1.97 +	int length;
    1.98 +
    1.99 +	
   1.100 +	// calculate how many bytes we're printing
   1.101 +	length = end - start;
   1.102 +	
   1.103 +	// TODO: Optimise this for large files (>64 kb(?))
   1.104 +
   1.105 +	char dataBuffer[length];
   1.106 +	memset(dataBuffer, 0, length);
   1.107 +
   1.108 +	// seek to the start and scan in
   1.109 +	ifs.seekg(start, std::ios::beg);
   1.110 +	ifs.read(dataBuffer, length);
   1.111 +
   1.112 +	int currentAddress = start;
   1.113 +
   1.114 +	int asciiBuffer[0x10], hexBuffer[0x10];
   1.115 +
   1.116 +	for(int i = 0; i < 0x10; i++)
   1.117 +	{
   1.118 +		asciiBuffer[i] = 0;
   1.119 +		hexBuffer[i] = 0;
   1.120 +	}
   1.121 +
   1.122 +	for(int i = 0; i < length; i++)
   1.123 +	{
   1.124 +		// we've filled our buffers, so print them out
   1.125 +		if(i % 0x10 == 0 && i != 0 || (i + 1) == length)
   1.126 +		{
   1.127 +			printf("\n%#x\t", currentAddress);
   1.128 +			currentAddress += 0x10;
   1.129 +
   1.130 +			for(int j = 0; j < 0x10; j++)
   1.131 +				printf("%x ", hexBuffer[j]);
   1.132 +			printf("    ");
   1.133 +			for(int j = 0; j < 0x10; j++)
   1.134 +				printf("%c ", asciiBuffer[j]);
   1.135 +		}
   1.136 +		hexBuffer[i%16] = asciiBuffer[i%16] = dataBuffer[i];
   1.137 +	}
   1.138 +}
   1.139 +
   1.140 +void Command::xorOperation(){}
   1.141 +void Command::orOperation(){}
   1.142 +void Command::andOperation(){}
   1.143 +void Command::notOperation(){}
     2.1 --- a/Command.h	Tue Jun 30 19:23:54 2009 -0400
     2.2 +++ b/Command.h	Wed Jul 01 23:12:36 2009 -0400
     2.3 @@ -3,6 +3,8 @@
     2.4  #ifndef COMMANDHEADER
     2.5  #define COMMANDHEADER
     2.6  
     2.7 +#include <vector>
     2.8 +
     2.9  enum MODE_ENUM {PRINT=1, XOR, OR, AND, NOT};
    2.10  
    2.11  class Command {
    2.12 @@ -10,6 +12,7 @@
    2.13  public:
    2.14  
    2.15      Command();
    2.16 +	~Command();
    2.17      
    2.18      MODE_ENUM mode;
    2.19      int start;
    2.20 @@ -19,11 +22,16 @@
    2.21  
    2.22      char* filename;
    2.23  
    2.24 -    int* data;
    2.25 +    std::vector<int> data;
    2.26      int dataCount;
    2.27  
    2.28      void print();
    2.29      
    2.30 +	void printOperation();
    2.31 +	void xorOperation();
    2.32 +	void orOperation();
    2.33 +	void andOperation();
    2.34 +	void notOperation();
    2.35  
    2.36  };
    2.37  
    2.38 @@ -32,4 +40,4 @@
    2.39  
    2.40  void processCommand(Command*);
    2.41  
    2.42 -#endif
    2.43 \ No newline at end of file
    2.44 +#endif
     3.1 --- a/CommandParser.l	Tue Jun 30 19:23:54 2009 -0400
     3.2 +++ b/CommandParser.l	Wed Jul 01 23:12:36 2009 -0400
     3.3 @@ -7,43 +7,38 @@
     3.4  %%
     3.5  
     3.6  " \t\n" {
     3.7 -	printf("Whitespace\n");
     3.8 +	return;
     3.9  }
    3.10  
    3.11  -p {
    3.12 -	printf("Print mode\n");
    3.13      return PRINTMODE; 
    3.14  }
    3.15  
    3.16  -x {
    3.17 -	printf("Xor mode\n");
    3.18      return XORMODE;
    3.19  }
    3.20  
    3.21  -n {
    3.22 -	printf("Not mode\n");
    3.23      return NOTMODE;
    3.24  }
    3.25  
    3.26  -a {
    3.27 -	printf("And mode\n");
    3.28      return ANDMODE;
    3.29  }
    3.30  
    3.31 -
    3.32 -
    3.33  -o {
    3.34 -	printf("Or mode\n");
    3.35      return ORMODE;
    3.36  }
    3.37  
    3.38  -r {
    3.39 -	printf("Range mode\n");
    3.40      return RANGEFLAG;
    3.41  }
    3.42  
    3.43 -0x[0-9a-fA-F]+ {
    3.44 -    printf("Hex number\n");
    3.45 +-l {
    3.46 +	return LENGTHFLAG;
    3.47 +}
    3.48 +
    3.49 +0[xX][0-9a-fA-F]+ {
    3.50      int retVal = sscanf(yytext, "%x",&(yylval.int_val));
    3.51      if(retVal != 1)
    3.52          return;
    3.53 @@ -51,19 +46,16 @@
    3.54  }
    3.55  
    3.56  [0-9]+ {
    3.57 -	printf("Number\n");
    3.58      yylval.int_val = atoi(yytext);
    3.59  	return NUMBER;
    3.60  }
    3.61  
    3.62  ['"].*+['"] {
    3.63 -    printf("Argument\n");
    3.64      yylval.string_val = strdup(yytext);
    3.65      return ARGUMENT;
    3.66  }
    3.67  
    3.68  [^ \t\n]+ {
    3.69 -	printf("Alpha\n");
    3.70      yylval.string_val = strdup(yytext);
    3.71  	return ARGUMENT;
    3.72  }
    3.73 @@ -72,9 +64,10 @@
    3.74  %%
    3.75  void configBuffer(char* arguments)
    3.76  {
    3.77 +#ifdef DEBUG
    3.78  	printf("Given the buffer: %s\n", arguments);
    3.79 +#endif
    3.80  	yy_delete_buffer(YY_CURRENT_BUFFER);
    3.81  
    3.82  	yy_scan_string(arguments);
    3.83 -	printf("Store the buffer: %s\n", yytext);
    3.84  }
     4.1 --- a/CommandParser.y	Tue Jun 30 19:23:54 2009 -0400
     4.2 +++ b/CommandParser.y	Wed Jul 01 23:12:36 2009 -0400
     4.3 @@ -1,7 +1,7 @@
     4.4  // this file contains the yacc/bison parsing code
     4.5  
     4.6  
     4.7 -%token PRINTMODE XORMODE ORMODE ANDMODE NOTMODE RANGEFLAG
     4.8 +%token PRINTMODE XORMODE ORMODE ANDMODE NOTMODE RANGEFLAG LENGTHFLAG
     4.9  
    4.10  %union {
    4.11      char *string_val;
    4.12 @@ -49,24 +49,13 @@
    4.13  
    4.14  data:
    4.15      NUMBER {
    4.16 -        if(theCommand->data == NULL)
    4.17 -        {
    4.18 -            // null, so allocate some space
    4.19 -            theCommand->data = (int*)malloc(sizeof(int));
    4.20 -        }
    4.21 +		theCommand->dataCount++;
    4.22 +		theCommand->data.push_back($1);
    4.23      }
    4.24      |
    4.25      data NUMBER {
    4.26 -        if(theCommand->data == NULL)
    4.27 -        {
    4.28 -            // null, so allocate some space
    4.29 -            theCommand->data = (int*)malloc(sizeof(int));
    4.30 -        }
    4.31 -        else
    4.32 -        {
    4.33 -            // we've already got some space, so increase it
    4.34 -            theCommand->data = (int*)realloc(theCommand->data, ++theCommand->dataCount);
    4.35 -        }
    4.36 +		theCommand->dataCount++;
    4.37 +		theCommand->data.push_back($2);
    4.38      }
    4.39      
    4.40  ;
    4.41 @@ -96,11 +85,17 @@
    4.42  destination:
    4.43      range
    4.44      |
    4.45 +	length
    4.46      NUMBER {
    4.47 +    }
    4.48 +;
    4.49 +
    4.50 +length:
    4.51 +	LENGTHFLAG NUMBER NUMBER {
    4.52          theCommand->range = false;
    4.53 -        theCommand->start = $1;
    4.54 -        theCommand->end = -1;
    4.55 -    }
    4.56 +        theCommand->start = $2;
    4.57 +        theCommand->end = $2+$3;
    4.58 +	}
    4.59  ;
    4.60  
    4.61  range:
    4.62 @@ -115,7 +110,7 @@
    4.63  
    4.64  void yyerror(const char * s)
    4.65  {
    4.66 -	fprintf(stderr,"%s", s);
    4.67 +	fprintf(stderr,"%s\n", s);
    4.68  }
    4.69  
    4.70  void printUsage()
    4.71 @@ -163,6 +158,7 @@
    4.72  	// give our arguments to lex for parsing
    4.73  	configBuffer(tempBuffer);
    4.74  
    4.75 +
    4.76  	// use bison parsing
    4.77  	int returnVal = yyparse();
    4.78  	if(returnVal != 0)
    4.79 @@ -170,6 +166,7 @@
    4.80  		printf("yyparse failed!\n");
    4.81  	}
    4.82  
    4.83 +
    4.84  }
    4.85  
    4.86  int main(int argc, char** argv)
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/file	Wed Jul 01 23:12:36 2009 -0400
     5.3 @@ -0,0 +1,4 @@
     5.4 +ah ha here is some text finally that i can show to you
     5.5 +huxzah
     5.6 +
     5.7 +will it work?