Skip to content

Commit 99d0894

Browse files
Fix windows build for examples (#618)
Relates-To: OLPEDGE-1338 Signed-off-by: Liubov Didkivska <[email protected]>
1 parent 340a7ab commit 99d0894

File tree

3 files changed

+133
-56
lines changed

3 files changed

+133
-56
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ else()
7171
olp-cpp-sdk-dataservice-read)
7272

7373
add_executable(${OLP_SDK_DATASERVICE_EXAMPLE_TARGET}
74+
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
7475
${CMAKE_CURRENT_SOURCE_DIR}/Examples.h
75-
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
76+
${CMAKE_CURRENT_SOURCE_DIR}/Options.h)
7677

7778
target_link_libraries(${OLP_SDK_DATASERVICE_EXAMPLE_TARGET}
7879
${OLP_SDK_DATASERVICE_READ_EXAMPLE_TARGET}

examples/Options.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Copyright (C) 2020 HERE Global B.V. and its affiliate(s).
2+
* All rights reserved.
3+
*
4+
* This software and other materials contain proprietary information
5+
* controlled by HERE and are protected by applicable copyright legislation.
6+
* Any use and utilization of this software and other materials and
7+
* disclosure to any third parties is conditional upon having a separate
8+
* agreement with HERE for the access, use, utilization or disclosure of this
9+
* software. In the absence of such agreement, the use of the software is not
10+
* allowed.
11+
*/
12+
13+
#pragma once
14+
15+
#include <string>
16+
#include <vector>
17+
18+
namespace tools {
19+
/// A command line option.
20+
struct Option {
21+
std::string short_name;
22+
std::string long_name;
23+
std::string description;
24+
};
25+
26+
const Option kHelpOption{"-h", "--help", "Print the help message and exit."};
27+
28+
const Option kExampleOption{"-e", "--example",
29+
"Run example [read, write, cache]."};
30+
31+
const Option kKeyIdOption{"-i", "--key_id", "Here key ID to access OLP."};
32+
33+
const Option kKeySecretOption{"-s", "--key_secret",
34+
"Here secret key to access OLP."};
35+
36+
const Option kCatalogOption{"-c", "--catalog",
37+
"Catalog HRN (HERE Resource Name)."};
38+
39+
const Option kLayerIdOption{"-l", "--layer_id",
40+
"The layer ID inside the catalog where you want to "
41+
"publish data to(required for write example)."};
42+
43+
const Option kAllOption{"-a", "--all", "Run all examples."};
44+
45+
} // namespace tools

examples/main.cpp

Lines changed: 86 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20+
#include "Options.h"
2021
#include "ProtectedCacheExample.h"
2122
#include "ReadExample.h"
2223
#include "WriteExample.h"
2324

24-
#include <getopt.h>
25+
#include <iostream>
2526
#include <stdio.h>
2627
#include <string.h>
27-
#include <unistd.h>
28-
#include <iostream>
28+
29+
bool IsMatch(const std::string& name, const tools::Option& option) {
30+
return name == option.short_name || name == option.long_name;
31+
}
2932

3033
enum Examples : int {
3134
read_example = 0b1,
@@ -39,7 +42,7 @@ constexpr auto usage =
3942
"example [read, write, cache] \n -i [--key_id] here.access.key.id \n -s "
4043
"[--key_secret] here.access.key.secret \n"
4144
" -c [--catalog] catalog HRN (HERE Resource Name). \n"
42-
" -l [--layer_id] t layer ID inside the catalog where you want to "
45+
" -l [--layer_id] the layer ID inside the catalog where you want to "
4346
"publish data to(required for write example). \n"
4447
" -h [--help]: show usage \n For "
4548
"instructions on how to get the access key ID and access key secret, see "
@@ -48,6 +51,80 @@ constexpr auto usage =
4851
"user-guide/topics/get-credentials.html) section in the Terms and "
4952
"Permissions User Guide.";
5053

54+
int RequiredArgumentError(const tools::Option& arg) {
55+
std::cout << "option requires an argument -- '" << arg.short_name << '\''
56+
<< " [" << arg.long_name << "] " << arg.description << std::endl;
57+
return 0;
58+
}
59+
int ParseArguments(const int argc, char** argv, AccessKey& access_key,
60+
std::string& catalog, std::string& layer_id) {
61+
int examples_to_run = 0;
62+
63+
const std::vector<std::string> arguments(argv + 1, argv + argc);
64+
auto it = arguments.begin();
65+
66+
while (it != arguments.end()) {
67+
if (IsMatch(*it, tools::kHelpOption)) {
68+
std::cout << usage << std::endl;
69+
return 0;
70+
}
71+
72+
if (IsMatch(*it, tools::kKeyIdOption)) {
73+
// Here access key ID.
74+
if (++it == arguments.end()) {
75+
return RequiredArgumentError(tools::kKeyIdOption);
76+
}
77+
access_key.id = *it;
78+
} else if (IsMatch(*it, tools::kKeySecretOption)) {
79+
// Here access key secret.
80+
if (++it == arguments.end()) {
81+
return RequiredArgumentError(tools::kKeySecretOption);
82+
}
83+
access_key.secret = *it;
84+
} else if (IsMatch(*it, tools::kExampleOption)) {
85+
if (++it == arguments.end()) {
86+
return RequiredArgumentError(tools::kExampleOption);
87+
}
88+
89+
if (*it == "read") {
90+
examples_to_run = Examples::read_example;
91+
} else if (*it == "write") {
92+
examples_to_run = Examples::write_example;
93+
} else if (*it == "cache") {
94+
examples_to_run = Examples::cache_example;
95+
} else {
96+
std::cout
97+
<< "Example was not found. Please use values:read, write, cache"
98+
<< std::endl;
99+
return 0;
100+
}
101+
102+
} else if (IsMatch(*it, tools::kCatalogOption)) {
103+
if (++it == arguments.end()) {
104+
return RequiredArgumentError(tools::kCatalogOption);
105+
}
106+
catalog = *it;
107+
} else if (IsMatch(*it, tools::kLayerIdOption)) {
108+
if (++it == arguments.end()) {
109+
return RequiredArgumentError(tools::kLayerIdOption);
110+
}
111+
layer_id = *it;
112+
} else if (IsMatch(*it, tools::kAllOption)) {
113+
examples_to_run = Examples::all_examples;
114+
} else {
115+
fprintf(stderr, usage);
116+
}
117+
++it;
118+
}
119+
120+
if (examples_to_run == 0) {
121+
std::cout << "Please specify command line arguments." << std::endl;
122+
std::cout << usage << std::endl;
123+
}
124+
125+
return examples_to_run;
126+
}
127+
51128
int RunExamples(const AccessKey& access_key, int examples_to_run,
52129
const std::string& catalog, const std::string& layer_id) {
53130
if (examples_to_run & Examples::read_example) {
@@ -77,63 +154,17 @@ int RunExamples(const AccessKey& access_key, int examples_to_run,
77154
}
78155

79156
int main(int argc, char** argv) {
80-
static struct option long_options[] = {
81-
{"example", required_argument, 0, 'e'},
82-
{"key_id", required_argument, 0, 'i'},
83-
{"key_secret", required_argument, 0, 's'},
84-
{"catalog", required_argument, 0, 'c'},
85-
{"layer_id", required_argument, 0, 'l'},
86-
{"all", no_argument, 0, 'a'},
87-
{"help", no_argument, 0, 'h'},
88-
{0, 0, 0, 0}};
89-
90157
AccessKey access_key{}; // You can specify your here.access.key.id
91158
// and here.access.key.secret
92159
std::string catalog; // the HRN of the catalog to which you to publish data
93160
std::string layer_id; // the of the layer inside the catalog to which you
94161
// want to publish data
95162

96-
int examples_to_run = 0;
97-
int opt, option_index;
98-
while ((opt = getopt_long(argc, argv, "e:i:s:c:l:ah", long_options,
99-
&option_index)) != EOF)
100-
switch (opt) {
101-
case 'c':
102-
catalog = optarg;
103-
break;
104-
case 'l':
105-
layer_id = optarg;
106-
break;
107-
case 'i':
108-
access_key.id = optarg;
109-
break;
110-
case 's':
111-
access_key.secret = optarg;
112-
break;
113-
case 'e':
114-
std::cout << "Run example " << optarg << std::endl;
115-
if (strcmp(optarg, "read") == 0)
116-
examples_to_run = Examples::read_example;
117-
else if (strcmp(optarg, "write") == 0)
118-
examples_to_run = Examples::write_example;
119-
else if (strcmp(optarg, "cache") == 0)
120-
examples_to_run = Examples::cache_example;
121-
else
122-
std::cout
123-
<< "Example was not found. Please use values:read, write, cache"
124-
<< std::endl;
125-
break;
126-
case 'a':
127-
examples_to_run = Examples::all_examples;
128-
break;
129-
case 'h':
130-
std::cout << usage << std::endl;
131-
return 0;
132-
case '?':
133-
default:
134-
fprintf(stderr, usage);
135-
return 0;
136-
}
163+
int examples_to_run =
164+
ParseArguments(argc, argv, access_key, catalog, layer_id);
165+
if (examples_to_run == 0) {
166+
return 0;
167+
}
137168

138169
if (access_key.id.empty() || access_key.secret.empty()) {
139170
std::cout << "Please specify your access key ID and access key secret. For "

0 commit comments

Comments
 (0)