pull/5/head
weetmuts 2017-08-31 11:19:57 +02:00
rodzic 2ef26b06e7
commit 7afde17d27
2 zmienionych plików z 128 dodań i 0 usunięć

81
cmdline.cc 100644
Wyświetl plik

@ -0,0 +1,81 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include"cmdline.h"
#include"util.h"
using namespace std;
CommandLine *parseCommandLine(int argc, char **argv) {
CommandLine * c = new CommandLine;
int i=1;
if (argc < 2) {
c->need_help = true;
return c;
}
while (argv[i] && argv[i][0] == '-') {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
c->need_help = true;
return c;
}
if (!strcmp(argv[i], "--verbose")) {
c->verbose = true;
i++;
continue;
}
if (!strcmp(argv[i], "--robot")) {
c->robot = true;
i++;
continue;
}
if (!strcmp(argv[i], "--")) {
i++;
break;
}
error("Unknown option \"%s\"\n", argv[i]);
}
c->usb_device = argv[i];
i++;
if (!c->usb_device) error("You must supply the usb device to which the wmbus dongle is connected.\n");
verbose("Using usb device: %s\n", c->usb_device);
if ((argc-i) % 3 != 0) {
error("For each meter you must supply a: name,id and key.\n");
}
int num_meters = (argc-i)/3;
verbose("Number of meters: %d\n", num_meters);
for (int m=0; m<num_meters; ++m) {
char *name = argv[m*3+i+0];
char *id = argv[m*3+i+1];
char *key = argv[m*3+i+2];
if (!isValidId(id)) error("Not a valid meter id \"%s\"\n", id);
if (!isValidKey(key)) error("Not a valid meter key \"%s\"\n", key);
c->meters.push_back(MeterInfo(name,id,key));
}
return c;
}

47
cmdline.h 100644
Wyświetl plik

@ -0,0 +1,47 @@
// Copyright (c) 2017 Fredrik Öhrström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include<string.h>
#include<vector>
using namespace std;
struct MeterInfo {
char *name;
char *id;
char *key;
MeterInfo(char *n, char *i, char *k) {
name = n;
id = i;
key = k;
}
};
struct CommandLine {
bool need_help;
bool verbose;
bool robot;
char *usb_device;
vector<MeterInfo> meters;
};
CommandLine *parseCommandLine(int argc, char **argv);