Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 dhylands 1
/***************************************************************************
2
*
3
*  srec2bin.c
4
*
5
*  This program reads an S-Record file and generates a binary file
6
*  which contains the data.
7
*
8
*  Since the address information is lost, this is primarily only useful for
9
*  taking a peek at the raw data stream.
10
*
11
****************************************************************************/
12
 
13
/* ---- Include Files ---------------------------------------------------- */
14
 
15
#include <iostream>
16
#include <iomanip>
17
 
18
#include <string.h>
19
#include <stdlib.h>
20
#include <vector>
21
#include <fstream>
22
#include <string>
23
#include <time.h>
24
 
25
#include "srec.h"
26
#include "SRecMem.h"
27
 
28
/* ---- Public Variables ------------------------------------------------- */
29
/* ---- Private Constants and Types -------------------------------------- */
30
 
31
/* ---- Private Variables ------------------------------------------------ */
32
/* ---- Private Function Prototypes -------------------------------------- */
33
 
34
//**************************************************************************
35
 
36
class SRecord2Bin : public SRecordParser
37
{
38
public:
39
            SRecord2Bin();
40
   virtual ~SRecord2Bin();
41
 
42
   bool     OpenOutputFile( const char *fileName );
43
   bool     CloseOutputFile();
44
 
45
protected:
46
 
47
   virtual  bool  FinishSegment( unsigned addr, unsigned len );
48
   virtual  bool  Header( const SRecordHeader *sRecHdr );
49
   virtual  bool  Data( const SRecordData *sRecData );
50
   virtual  bool  StartAddress( const SRecordData *sRecData );
51
   virtual  bool  StartSegment( unsigned addr );
52
 
53
private:
54
 
55
   std::ofstream                 m_outputFile;
56
};
57
 
58
//**************************************************************************
59
 
60
SRecord2Bin::SRecord2Bin()
61
{
62
}
63
 
64
//**************************************************************************
65
 
66
SRecord2Bin::~SRecord2Bin()
67
{
68
   CloseOutputFile();
69
}
70
 
71
//**************************************************************************
72
 
73
bool SRecord2Bin::CloseOutputFile()
74
{
75
    if ( !m_outputFile.is_open() )
76
    {
77
        return true;
78
    }
79
 
80
    if ( !Flush() )
81
    {
82
        return false;
83
    }
84
 
85
    return true;
86
}
87
 
88
//**************************************************************************
89
// virtual
90
 
91
bool SRecord2Bin::Data( const SRecordData *sRecData )
92
{
93
    m_outputFile.write( reinterpret_cast< const char * >( &sRecData->m_data[0] ), sRecData->m_dataLen );
94
 
95
    return true;
96
}
97
 
98
//**************************************************************************
99
// virtual
100
 
101
bool SRecord2Bin::FinishSegment( unsigned addr, unsigned len )
102
{
103
    return true;
104
}
105
 
106
//**************************************************************************
107
// virtual
108
 
109
bool SRecord2Bin::Header( const SRecordHeader *sRecHdr )
110
{
111
    return true;
112
}
113
 
114
//**************************************************************************
115
 
116
bool SRecord2Bin::OpenOutputFile( const char *fileName )
117
{
118
    m_outputFile.open( fileName, std::ios::binary );
119
    if ( !m_outputFile )
120
    {
121
        std::cerr << "Unable to open '" << fileName << "' for writing." << std::endl;
122
        return false;
123
    }
124
    return true;
125
}
126
 
127
//**************************************************************************
128
// virtual
129
 
130
bool SRecord2Bin::StartAddress( const SRecordData *sRecData )
131
{
132
    return true;
133
}
134
 
135
//**************************************************************************
136
// virtual
137
 
138
bool SRecord2Bin::StartSegment( unsigned addr )
139
{
140
    return true;
141
}
142
 
143
/* ---- Functions -------------------------------------------------------- */
144
 
145
/***************************************************************************
146
*
147
*  main
148
*
149
****************************************************************************/
150
 
151
int main( int argc, char **argv )
152
{
153
    if ( argc != 3 )
154
    {
155
        fprintf( stderr, "Usage: srec2bin srec-file bin-file\n" );
156
        exit( 1 );
157
    }
158
 
159
    SRecord2Bin sRec2Bin;
160
 
161
    if ( !sRec2Bin.OpenOutputFile( argv[ 2 ] ))
162
    {
163
        exit( 1 );
164
    }
165
 
166
    sRec2Bin.ParseFile( argv[ 1 ]);
167
    sRec2Bin.CloseOutputFile();
168
 
169
    exit( 0 );
170
    return 0;
171
 
172
} /* main */
173