[go: up one dir, main page]

File: file.cc

package info (click to toggle)
srecord 1.55-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,728 kB
  • ctags: 2,401
  • sloc: cpp: 23,402; sh: 6,520; makefile: 3,116; awk: 208; vhdl: 15
file content (316 lines) | stat: -rw-r--r-- 6,166 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// srecord - manipulate eprom load files
// Copyright (C) 1998-2010 Peter Miller
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see
// <http://www.gnu.org/licenses/>.
//

#include <cerrno>
#include <cstdio>
#include <cstring>
#include <iostream>

#include <srecord/input/file.h>

bool srecord::input_file::ignore_checksums_default = false;


srecord::input_file::input_file() :
    file_name("standard input"),
    line_number(1),
    prev_was_newline(false),
    vfp(stdin),
    checksum(0),
    is_text(0),
    ignore_checksums(ignore_checksums_default)
{
}


const char *
srecord::input_file::mode()
    const
{
    return "r";
}


srecord::input_file::input_file(const std::string &a_file_name) :
    file_name(a_file_name),
    line_number(1),
    prev_was_newline(false),
    vfp(0),
    checksum(0),
    is_text(0),
    ignore_checksums(ignore_checksums_default)
{
    if (file_name == "-")
    {
        file_name = "standard input";
        vfp = stdin;
    }
    else
    {
        //
        // The call to fopen is deferred until the constructor has
        // completed.  This is so that the virtual mode() method
        // is available (it isn't in the base class constructor).
        //
    }
}


void *
srecord::input_file::get_fp()
{
    if (!vfp)
    {
        //
        // The call to fopen is deferred until the constructor has
        // completed.  This is so that the virtual mode() method
        // is available (it isn't in the base class constructor).
        //
        const char *the_mode = mode();
        vfp = fopen(file_name.c_str(), the_mode);
        if (!vfp)
                fatal_error_errno("open");
        is_text = !strchr(the_mode, 'b');
        if (!is_text)
            line_number = 0;
    }
    return vfp;
}


srecord::input_file::~input_file()
{
    FILE *fp = (FILE *)get_fp();
    if (fp != stdin && fclose(fp))
        fatal_error_errno("close");
}


std::string
srecord::input_file::filename()
    const
{
    return file_name;
}


std::string
srecord::input_file::filename_and_line()
    const
{
    if (!vfp)
        return file_name;
    char buffer[20];
    if (is_text)
        sprintf(buffer, ": %d", line_number);
    else
        sprintf(buffer, ": 0x%04X", line_number);
    return (file_name + buffer);
}


int
srecord::input_file::get_char()
{
    FILE *fp = (FILE *)get_fp();
    if (prev_was_newline)
        ++line_number;
    int c = getc(fp);
    if (c == EOF)
    {
        if (ferror(fp))
            fatal_error_errno("read");

        //
        // If this is a text file, but the last character wasn't
        // a newline, insert one.
        //
        c = ((is_text && !prev_was_newline) ? '\n' : -1);
    }
    else if (c == '\r' && is_text)
    {
        //
        // If this is a text file, turn CRLF into LF.
        // Leave all other sequences containing CR alone.
        //
        c = getc(fp);
        if (c == EOF)
        {
            if (ferror(fp))
                fatal_error_errno("read");
            c = '\r';
        }
        else if (c != '\n')
        {
            ungetc(c, fp);
            c = '\r';
        }
    }
    if (!is_text && c >= 0)
        ++line_number;
    prev_was_newline = (is_text && c == '\n');
    return c;
}


void
srecord::input_file::get_char_undo(int c)
{
    if (c >= 0)
    {
        FILE *fp = (FILE *)get_fp();
        prev_was_newline = false;
        if (!is_text)
            --line_number;
        ungetc(c, fp);
    }
}


int
srecord::input_file::peek_char()
{
    FILE *fp = (FILE *)get_fp();
    int c = getc(fp);
    if (c == EOF)
    {
        if (ferror(fp))
            fatal_error_errno("read");
        c = -1;
    }
    else
        ungetc(c, fp);
    return c;
}


int
srecord::input_file::get_nibble_value(int c)
{
    switch (c)
    {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        return (c - '0');

    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        return (c - 'a' + 10);

    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        return (c - 'A' + 10);
    }
    return -1;
}


int
srecord::input_file::get_nibble()
{
    int c = get_char();
    int n = get_nibble_value(c);
    if (n < 0)
        fatal_error("hexadecimal digit expected");
    return n;
}


int
srecord::input_file::get_byte()
{
    int c1 = get_nibble();
    int c2 = get_nibble();
    int n = ((c1 << 4) | c2);
    checksum_add(n);
    return n;
}


int
srecord::input_file::get_word()
{
    int b1 = get_byte();
    int b2 = get_byte();
    return ((b1 << 8) | b2);
}


unsigned long
srecord::input_file::get_3bytes()
{
    unsigned long b1 = get_byte();
    unsigned long b2 = get_byte();
    unsigned long b3 = get_byte();
    return ((((b1 << 8) | b2) << 8) | b3);
}


unsigned long
srecord::input_file::get_4bytes()
{
    unsigned long b1 = get_byte();
    unsigned long b2 = get_byte();
    unsigned long b3 = get_byte();
    unsigned long b4 = get_byte();
    return ((((((b1 << 8) | b2) << 8) | b3) << 8) | b4);
}


int
srecord::input_file::checksum_get()
    const
{
    return (checksum & 0xFF);
}


int
srecord::input_file::checksum_get16()
    const
{
    return (checksum & 0xFFFF);
}


void
srecord::input_file::checksum_reset()
{
    checksum = 0;
}


void
srecord::input_file::checksum_add(unsigned char n)
{
    checksum += n;
}


void
srecord::input_file::seek_to_end()
{
    FILE *fp = (FILE *)get_fp();
    fseek(fp, 0L, SEEK_END);
}


void
srecord::input_file::disable_checksum_validation()
{
    ignore_checksums = true;
}