00001
00044 #include "hexfile.h"
00045 #include <sstream>
00046 #include <iomanip>
00047 #include <string>
00048 #include <stdexcept>
00049 #include <vector>
00050 #include <fstream>
00051 #include <algorithm>
00052
00053
00054
00056 class ParameterParser
00057 {
00058 private:
00059 std::string m_filename;
00060 std::vector< byte > m_MACAddress;
00061
00063 std::vector< byte > ParseHexString( const std::string & hexString ) const;
00065 std::string CreateHexString( const std::vector< byte > & byteVector ) const;
00066
00067 public:
00069 ParameterParser( size_t argc, const char * * argv );
00070 ~ParameterParser() {}
00071
00072
00073
00074
00075 const std::string & GetFilename() const { return m_filename; }
00076 const std::vector< byte > & GetMACAddress() const { return m_MACAddress; }
00077
00078 std::string GetMACAddressString() const { return CreateHexString( GetMACAddress() ); }
00079 };
00080
00081
00082
00083 std::vector< byte > ParameterParser::ParseHexString( const std::string & hexString ) const
00084 {
00085
00086 if( hexString.size() % 2 != 0 ) {
00087 throw std::runtime_error( "Hex string does not have even character count" );
00088 }
00089
00090
00091
00092
00093 std::vector< byte > bytes;
00094 for( size_t idx = 0; idx < hexString.size(); idx += 2 ) {
00095 std::istringstream iss( hexString.substr( idx, 2 ) );
00096 byte data;
00097 iss >> std::hex >> data;
00098 if( !iss ) {
00099 throw std::runtime_error( "Error parsing hex string \"" + hexString + "\"" );
00100 }
00101 bytes.push_back( data );
00102 }
00103 return bytes;
00104 }
00105
00106
00107
00108 std::string ParameterParser::CreateHexString( const std::vector< byte > & byteVector ) const
00109 {
00110 std::ostringstream oss;
00111 oss << std::setfill('0') << std::hex;
00112
00113
00114 for( size_t idx = 0; idx < byteVector.size() - 1; ++idx ) {
00115 oss << std::setw(2) << byteVector.at( idx ) << ' ';
00116 }
00117
00118
00119 if( byteVector.size() > 0 ) {
00120 oss << std::setw(2) << byteVector.at( byteVector.size()-1 );
00121 }
00122
00123 return oss.str();
00124 }
00125
00126
00127
00128 ParameterParser::ParameterParser( size_t argc, const char * * argv )
00129 {
00130
00131 if( argc != 3 ) {
00132 std::cout << "Parameters:\n"
00133 " filename - Filename for resulting Intel HEX file\n"
00134 " macaddress - Hex representation of the MAC address (32 hex digits)\n"
00135 << std::endl;
00136 throw std::runtime_error( "Not exactly 2 parameters" );
00137 }
00138
00139
00140
00141
00142 std::string arg1( argv[1] );
00143 std::string arg2( argv[2] );
00144
00145
00146
00147
00148 m_filename = arg1;
00149
00150 m_MACAddress = ParseHexString( arg2 );
00151
00152
00153
00154
00155 if( m_MACAddress.size() != 16 ) {
00156 throw std::runtime_error( "MAC address has invalid length" );
00157 }
00158 }
00159
00160
00161
00163 void CreateHEXFile( const ParameterParser & parser )
00164 {
00165 HEXFile hexData;
00166 std::ofstream outFile( parser.GetFilename().c_str() );
00167
00168
00169 HEXRecord MACAddress;
00170 std::vector< byte > MACAddressReversed = parser.GetMACAddress();
00171 std::reverse( MACAddressReversed.begin(), MACAddressReversed.end() );
00172 MACAddress << MACAddressReversed;
00173
00174
00175 hexData << MACAddress;
00176
00177
00178 outFile << hexData;
00179 }
00180
00181
00182
00183 int main( size_t argc, const char * * argv )
00184 {
00185 try {
00186 const std::string endian( " (Byte order reversed in HEX file)" );
00187 ParameterParser parser( argc, argv );
00188 std::cout << "Filename : " << parser.GetFilename() << std::endl
00189 << "MAC address : " << parser.GetMACAddressString() << endian << std::endl;
00190
00191 CreateHEXFile( parser );
00192 std::cout << std::endl << parser.GetFilename() << " created!" << std::endl;
00193
00194 } catch( const std::exception & e ) {
00195 std::cout << "Error: " << e.what() << std::endl;
00196 } catch(...) {
00197 std::cout << "Unknown error" << std::endl;
00198 }
00199 }
00200