Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / common / samples / args_helper.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief a header file with common samples functionality
7  * @file args_helper.hpp
8  */
9
10 #pragma once
11
12 #include <string>
13 #include <vector>
14 #include <gflags/gflags.h>
15 #include <iostream>
16 #include <sys/stat.h>
17
18 #ifdef _WIN32
19 #include <os/windows/w_dirent.h>
20 #else
21 #include <dirent.h>
22 #endif
23
24 /**
25 * @brief This function checks input args and existence of specified files in a given folder
26 * @param arg path to a file to be checked for existence
27 * @return files updated vector of verified input files
28 */
29 void readInputFilesArguments(std::vector<std::string> &files, const std::string& arg) {
30     struct stat sb;
31     if (stat(arg.c_str(), &sb) != 0) {
32         slog::warn << "File " << arg << " cannot be opened!" << slog::endl;
33         return;
34     }
35     if (S_ISDIR(sb.st_mode)) {
36         DIR *dp;
37         dp = opendir(arg.c_str());
38         if (dp == nullptr) {
39             slog::warn << "Directory " << arg << " cannot be opened!" << slog::endl;
40             return;
41         }
42
43         struct dirent *ep;
44         while (nullptr != (ep = readdir(dp))) {
45             std::string fileName = ep->d_name;
46             if (fileName == "." || fileName == "..") continue;
47             files.push_back(arg + "/" + ep->d_name);
48         }
49         closedir(dp);
50     } else {
51         files.push_back(arg);
52     }
53
54     if (files.size() < 20) {
55         slog::info << "Files were added: " << files.size() << slog::endl;
56         for (std::string filePath : files) {
57             slog::info << "    " << filePath << slog::endl;
58         }
59     } else {
60         slog::info << "Files were added: " << files.size() << ". Too many to display each of them." << slog::endl;
61     }
62 }
63
64 /**
65 * @brief This function find -i/--images key in input args
66 *        It's necessary to process multiple values for single key
67 * @return files updated vector of verified input files
68 */
69 void parseInputFilesArguments(std::vector<std::string> &files) {
70     std::vector<std::string> args = gflags::GetArgvs();
71     bool readArguments = false;
72     for (size_t i = 0; i < args.size(); i++) {
73         if (args.at(i) == "-i" || args.at(i) == "--images") {
74             readArguments = true;
75             continue;
76         }
77         if (!readArguments) {
78             continue;
79         }
80         if (args.at(i).c_str()[0] == '-') {
81             break;
82         }
83         readInputFilesArguments(files, args.at(i));
84     }
85 }