Sortiert Intel-Hex-Dateien nach aufsteigenden Adressen
-
#include <string> #include <fstream> #include <iostream> #include <list> #include <stdio.h> #define MAX_LINE_LENGTH 256 using namespace std; class AddressString { public: AddressString(const string& line = string()); const string& GetLine() const {return m_Line;} long GetAddress() const {return m_Address;} bool operator< (const AddressString& s) const; bool operator== (const AddressString& s) const; private: string m_Line; long m_Address; bool m_IsLastString; }; typedef list<AddressString> AddressStringList; AddressString::AddressString(const string& line) : m_Line(line), m_IsLastString(false),m_Address(0) { string tmpString; try { tmpString = m_Line.substr(1, 2); int tmpVal; sscanf(tmpString.c_str(), "%x", &tmpVal); if (tmpVal == 0) { m_IsLastString = true; return; } tmpString = m_Line.substr(3, 4); sscanf(tmpString.c_str(), "%X", &m_Address); } catch(const exception& e) { cout << "Error parsing: " << m_Line << endl; return; } } inline bool AddressString::operator< (const AddressString& s) const { if (m_IsLastString) return false; return GetAddress() < s.GetAddress(); } inline bool AddressString::operator== (const AddressString& s) const { if (m_IsLastString) return false; return GetAddress() == s.GetAddress(); } int main(int argc, char* argv[]) { if (argc != 3) { cout << "Usage: ihxsort <input.hex> <output.hex>" << endl; return 1; } if (!strcmp(argv[1], argv[2])) { cout << "Please use two different file names!" << endl; return 1; } ifstream in(argv[1]); if (in.fail()) { cout << "Error, could not read file: \"" << argv[1] << "\"!" << endl; return 1; } AddressStringList l; string s; char buf[MAX_LINE_LENGTH + 1]; do { in.getline(buf, MAX_LINE_LENGTH); buf[MAX_LINE_LENGTH] = '\0'; //zur Sicherheit nullterminieren s = buf; // Leerzeichen am Ende entfernen int pos = s.length() - 1; while ((pos >= 0) && (s[pos] == ' ')) --pos; s.erase (pos + 1); if (s.length() != 0) { AddressString tmpString(buf); l.push_back(tmpString); } } while((!in.eof()) && (!in.fail())); l.sort(); AddressStringList::const_iterator i = l.begin(); ofstream out(argv[2]); while ( (!out.fail()) && (!(i == l.end())) ) { out << (*i).GetLine() << endl; i++; } if (out.fail()) cout << "Output error writing \"" << argv[2] << "\"!" << endl; else cout << "IHX file successfully sorted. Output file: \"" << argv[2] << "\"." << endl; return 0; }
-
zeig auch mal ne typische eingabedatei, damit ich sehen kann, ob dein code die aufgabe elegant löst. ich zweifle nämlich.
-
der beitrag sollte eigentlich nur ein sinnloser sein *g*
:020000040000FA :2000000000000000000000000000000000000000000000000000000000000000800044001C :2000200002043E060000000000000000000000000000000000000000000000000000000076 :200040000000000000000000000000003E0B000000003E0B000000003E0B000000003E0B7C :20006000000000003E0B000000003E0B000000003E0B000000003E0B000000003E0B000013 :2000800000003E0B000000003E0B000000003E0B000000003E0B000000003E0B00000000F3 :2000A0003E0B000000003E0B000000003E0B000000003E0B000000003E0B000000003E0B8A :2000C000000000003E0B000000003E0B000000003E0B000000003E0B0000000000000000FC :2000E00000000000000000000000000000000000000000003E0B000000003E0B000000006E :200100003E0B000000003E0B000000003E0B000000003E0B000000003E0B000000003E0B29 :20012000000000003E0B000000003E0B000000003E0B000000003E0B000000003E0B000052 :2001400000003E0B000000003E0B000000003E0B000000003E0B000000003E0B0000000032 :200160003E0B000000003E0B000000003E0B000000003E0B000000003E0B000000003E0BC9 :200180001C0000000210000000043E0B00000000060F1C000000061100000000300F00005D :2001A0000000340F00000000370F00000000380F000000003C0F000000003F0F00000000D6 :2001C000250F01000000260F000000002D0F010000002E0F000000000A10A01C02003E0124 :2001E000BA0130003E02AA0131003E02000000000A11000001000D14001800000B14210221 :2002000000000E0F060000000F0F03000000001000C810003E02410000002D1146000000AD :200220002E11040002001F0FC2000200BE0602A000003E015C000200200645000000031105 :20024000C2000200BE06201002003E01C2000200BE06001002003E0116000000200700008F :20026000DBB1090FBB0830003E02000000000B1100000200010F00010000020F2F0417001D :200280003E01410000002E1104000200140F40000200180F000000001C0F40148285CF7048 :2002A00094FFFF00000E000000804F7604000200000F000000B20F7040000000011142007F :2002C00000000211470000000411040002043E061C0000000E1100000000800000A0020004 :2002E0003E0103000000000700A002003E01050000002007002000883F7001000000020D41 :2003000000000080BE51600002003E0600A002003E0107000000200700000000DC0F0000AE :200320000000DD0F002000883F7001000000020D000080EDBE51600002003E0600A00200A6 :200340003E01080000002007000000883F7004000000020DC2000204BE0600000000000059 :2003600000000000000000008081BE51600002003E0600A002003E010800000020070000B7 :2003800000883F7004000000020DC2000204BE060000000000000000000000000000808186 :2003A000BE51600002003E0600A002003E01080000002007000000883F7004000000020D2E :2003C000C2000204BE06000000000000000000000000000080EDBE51600002003E0600A0CF :2003E00002003E01D8FFFF00000700A002003E01D6FFFF00000700A002003E01DAFFFF006A :20040000000700A002003E0107000000200700000000DC0F00000000DD0F0020008C3F7094 :2004200001000000020D000080EDBF51600002003E0600A002003E01D8FFFF00000700A02B :2004400002003E01DFFFFF00000700A002003E01E6FFFF00000700A002003E016000020068 :2004600020060000008C3F7004000000020DC2000204BE060000000000000000000000007C :20048000000080EDBF51600002003E06000000000D10000000000B11400000001F1142004E :2004A00000002E11470000000F111C0000000E1100000000800004000200DB1200000000E8 :2004C0000D11000000043E0A00000081CF7D20128281DF7D00000000000000000000000054 :2004E0000000000000000000000000000000000000000000000000000000000000000000FC :200500000000000000000000000000000000000000000000000000000000000000000000DB :200520000000000000000000000000000000000000000000000000000000000000000000BB :2005400000000000000000000000000000000000000000000000000000000000000000009B :2005600000000000000000000000000000000000000000000000000000000000000000007B :2005800000000000000000000000000000000000000000000000000000000000000000005B :2005A00000000000000000000000000000000000000000000000000000000000000000003B :2005C00000000000000000000000000000000000000000000000000000000000000000001B :2005E0000000000000000000000000000000000000000000000000000000000000000000FB :2006000000002A06400001000000000002000000000003000000000004000000000005005B :0A06200000000000060000000000CA
-
das scheint schon sortiert zu sein. kannst du ja durcheinander machen, wenn du willst