aboutsummaryrefslogtreecommitdiff
path: root/UserConfig.cpp
blob: dbfa88eca3aa6a0a41e060139be0fabc39741e69 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <sstream>
#include <memory>
#include "ExitCodes.hpp"
#include "UserConfig.hpp"
#include "Logger.hpp"
#include "help_message.hpp"

using std::ostringstream;
using std::istringstream;
using std::endl;
using std::noskipws;


#define OPERATION_SPEEDS   "speeds"
#define OPERATION_ERASE    "erase"
#define OPERATION_READ     "read"
#define OPERATION_WRITE    "write"
#define OPERATION_HELP     "help"
#define OPERATION_VERSION  "version"
#define OPERATION_IDENT    "ident"


// Common options
#define OPTION_SERIAL_PORT     "-p"
#define OPTION_VERBOSE_MODE    "-v"
#define OPTION_SERIAL_SPEED    "-s"
#define OPTION_FREQUENCY       "-f"
#define OPTION_PRINT_PROGRESS  "-g"
// Options specific for an operation
#define OPTION_B            "-b"
#define OPTION_C            "-c"
#define OPTION_E            "-e"
#define OPTION_N            "-n"


CUserConfig::CUserConfig(int argc, char **argv)
{
    mSerialPortName = DEFAULT_SERIAL_PORT_NAME;
    mSerialSpeed = "0"; // Default serial speed for given device
    mVerboseMode = false;
    mSpeeds = false;
    mHelp = false;
    mVersion = false;
    mIdent = false;
    mErase = false;
    mRead = false;
    mReadOutputFilename = "";
    mReadLength = -1;
    mWrite = false;
    mWriteInputFilename = "";
    mWriteEraseWholeMemory = false;
    mWriteCheckByRead = false;
    mMcuFrequency = 0;
    mPrintProgress = false;

    vector<char *> args;

    for (int i = 1; i < argc; ++i)
        args.push_back(argv[i]);

    parseCommandLine(args);
}

const string
CUserConfig::getHelpMessage(const string & execName) const
{
    return HelpMessage::getText(execName);
}

void
CUserConfig::parseCommandLine(vector<char *> & args)
{
    // Process options common for all operations. Create custom
    // argument vector without processed common options.
    vector<char *>::iterator it = args.begin();
    // Skip operation name from processing
    if (it != args.end())
        ++it;
    // Start at the second argument, first is the name of an
    // operation.
    while (it != args.end()) {
    	string a = *it;
        bool processed = false;
        bool with_argument = false;

    	if (!a.compare(OPTION_SERIAL_PORT)) {
    	    mSerialPortName = getArgument(it, args.end());
            processed = true;
            with_argument = true;
        } else if (!a.compare(OPTION_SERIAL_SPEED)) {
    	    mSerialSpeed = getArgument(it, args.end());
            processed = true;
            with_argument = true;
    	} else if (!a.compare(OPTION_VERBOSE_MODE)) {
    	    mVerboseMode = true;
            processed = true;
        } else if (!a.compare(OPTION_PRINT_PROGRESS)) {
    	    mPrintProgress = true;
            processed = true;
    	} else if (!a.compare(OPTION_FREQUENCY)) {
            istringstream is(getArgument(it, args.end()));
            string s = is.str();
            float f;
            is >> noskipws >> f;
            bool a = (is.fail() || (f <= 0));
            bool b = (is.peek() != EOF);
            if (a || b) {
                ostringstream os;
                os << "Argument for -f option '" << s << "' is not a positive real number";
        	CLogger::error(os.str(), EXIT_USER_CONFIG);
            }
            mMcuFrequency = f;
            processed = true;
            with_argument = true;
    	}
        
        if (processed) {
            it = args.erase(it);
            if (with_argument) // Urcite bude platne, to mame osetrene getArgument()
                it = args.erase(it);
        } else {            
            ++it;
        }
    }
    
    it = args.begin();
    if (it != args.end()) {
        it = args.begin();
        // Resolve operation name
        string a = *it;

        if (!a.compare(OPERATION_SPEEDS)) {
            mSpeeds = true;
        } else if (!a.compare(OPERATION_ERASE)) {
            mErase = true;
            parseEraseArguments(++it, args.end());
        } else if (!a.compare(OPERATION_READ)) {
            mRead = true;
            parseReadArguments(++it, args.end());
        } else if (!a.compare(OPERATION_WRITE)) {
            mWrite = true;
            parseWriteArguments(++it, args.end());
        } else if (!a.compare(OPERATION_HELP)) {
            mHelp = true;
        } else if (!a.compare(OPERATION_VERSION)) {
            mVersion = true;
        } else if (!a.compare(OPERATION_IDENT)) {
            mIdent = true;
        } else {
            CLogger::error("Unknown operation requested: " + a, EXIT_USER_CONFIG);
        }
    }
}

void
CUserConfig::parseEraseArguments(vector<char *>::const_iterator args, 
                                 vector<char *>::const_iterator end)
{
    while (args != end) {
    	string a = *args;
        if (!a.compare(OPTION_B)) {
            // Prepend comma for unified parsing
            istringstream is("," + getArgument(args, end));
            // Parse block numbers list
            bool e = false;
            for (;;) {
        	// Get comma
        	int c = is.get();
        	if (is.eof()) {
        	    break;
        	} else if (c != ',') {
        	    e = true;
        	    break;
        	}
        	// Get number
        	int n;
        	is >> n;
        	if (is.fail()) {
        	    e = true;
        	    break;
        	}
        	mEraseBlockList.push_back(n);
            }
            if (e || mEraseBlockList.size() == 0)
        	CLogger::error("Argument for -b option must be in format N[,N]...", EXIT_USER_CONFIG);
            ++args;
            ++args;
    	} else {
            string s = *args;
            CLogger::error("Unknown argument '" + s + "' for erase operation", EXIT_USER_CONFIG);
        }
    }
}

void
CUserConfig::parseReadArguments(vector<char *>::const_iterator args, 
                                vector<char *>::const_iterator end)
{
    while (args != end) {
    	string a = *args;
        if (!a.compare(OPTION_N)) {
            istringstream n(getArgument(args, end));
            string s = n.str();
            n >> noskipws >> mReadLength;
            bool a = (n.fail() || (mReadLength < 0));
            bool b = (n.peek() != EOF);
            if (a || b) {
                ostringstream os;
                os << "Argument for -n option '" << s << "' is not 0 or a positive number";
        	CLogger::error(os.str(), EXIT_USER_CONFIG);
            }
            ++args;
            ++args;
    	} else {
            // First occurence of this is the name of output file
            if (mReadOutputFilename.length() == 0) {
        	mReadOutputFilename = a;
                ++args;
            } else {
        	string s = *args;
        	CLogger::error("Unknown argument '" + s + "' for read operation", EXIT_USER_CONFIG);
            }
    	}
    }
    // Must have at least filename where to write output
    if (mReadOutputFilename.length() == 0)
        CLogger::error("Missing output filename for read operation", EXIT_USER_CONFIG);
}

void
CUserConfig::parseWriteArguments(vector<char *>::const_iterator args, 
                                 vector<char *>::const_iterator end)
{
    while (args != end) {
    	string a = *args;
        if (!a.compare(OPTION_E)) {
            mWriteEraseWholeMemory = true;
            ++args;
        } else if (!a.compare(OPTION_C)) {
            mWriteCheckByRead = true;
            ++args;
    	} else {
            // First occurence of this is the name of output file
            if (mWriteInputFilename.length() == 0) {
        	mWriteInputFilename = a;
                ++args;
            } else {
        	string s = *args;
        	CLogger::error("Unknown argument '" + s + "' for write operation", EXIT_USER_CONFIG);
            }
    	}
    }
    // Must have at least filename where to write output
    if (mWriteInputFilename.length() == 0)
        CLogger::error("Missing input filename for write operation", EXIT_USER_CONFIG);
}

string
CUserConfig::getArgument(vector<char *>::const_iterator args,
                         vector<char *>::const_iterator end)
{
    if (++args == end) {
        string s(*(--args));
        CLogger::error("Missing argument for option '" + s + "'", EXIT_USER_CONFIG);
    }
    return string(*args);
}

bool
CUserConfig::isEraseSet()
{
    return mErase;
}

list<unsigned int>
CUserConfig::getEraseBlockList()
{
    return mEraseBlockList;
}

bool
CUserConfig::isReadSet()
{
    return mRead;
}

bool
CUserConfig::isWriteSet()
{
    return mWrite;
}

bool
CUserConfig::isVerboseModeSet()
{
    return mVerboseMode;
}

bool
CUserConfig::isPrintProgressSet()
{
    return mPrintProgress;
}

bool
CUserConfig::isHelpSet()
{
    return mHelp;
}

bool
CUserConfig::isVersionSet()
{
    return mVersion;
}

bool
CUserConfig::isIdentSet()
{
    return mIdent;
}

string &
CUserConfig::getSerialPortName()
{
    return mSerialPortName;
}

string &
CUserConfig::getSerialSpeed()
{
    return mSerialSpeed;
}

string &
CUserConfig::getReadOutputFname()
{
    return mReadOutputFilename;
}

string &
CUserConfig::getWriteInputFname()
{
    return mWriteInputFilename;
}

int
CUserConfig::getReadLength()
{
    return mReadLength;
}

bool
CUserConfig::getWriteEraseWholeMemory()
{
    return mWriteEraseWholeMemory;
}

bool
CUserConfig::getWriteCheckByRead()
{
    return mWriteCheckByRead;
}

bool
CUserConfig::isSpeedsSet()
{
    return mSpeeds;
}

float
CUserConfig::getMcuFrequency()
{
    return mMcuFrequency;
}