gc_file.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_genicam_api package.
3  *
4  * Copyright (c) 2020 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Heiko Hirschmueller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <rc_genicam_api/system.h>
38 #include <rc_genicam_api/device.h>
39 #include <rc_genicam_api/config.h>
40 
41 #include <GenApi/Filestream.h>
42 
43 #include <iostream>
44 #include <fstream>
45 #include <limits>
46 
47 #ifdef _WIN32
48 #undef min
49 #undef max
50 #endif
51 
52 int main(int argc, char *argv[])
53 {
54  int ret=0;
55 
56  try
57  {
58  if (argc >= 2 && std::string(argv[1]) != "-h")
59  {
60  // get device id
61 
62  int k=1;
63  if (k < argc)
64  {
65  std::string devid=argv[k++];
66 
67  // find specific device accross all systems and interfaces and show some
68  // information
69 
70  std::shared_ptr<rcg::Device> dev=rcg::getDevice(devid.c_str());
71 
72  if (dev)
73  {
74  // open device and optionally change some settings
75 
76  if (k < argc)
77  {
78  dev->open(rcg::Device::CONTROL);
79  }
80 
81  std::shared_ptr<GenApi::CNodeMapRef> nodemap=dev->getRemoteNodeMap();
82 
83  // process command line parameters
84 
85  if (k < argc)
86  {
87  std::string devfile=argv[k++];
88 
89  if (devfile == "-f")
90  {
91  std::vector<std::string> list;
92  rcg::getEnum(nodemap, "FileSelector", list, true);
93 
94  for (size_t i=0; i<list.size(); i++)
95  {
96  std::cout << list[i] << std::endl;
97  }
98  }
99  else
100  {
101  std::string op;
102  std::string file;
103 
104  if (k+1 < argc)
105  {
106  op=argv[k++];
107  file=argv[k++];
108  }
109 
110  if (op == "-w")
111  {
112  std::ifstream in(file);
113 
114  if (!in)
115  {
116  throw std::invalid_argument("Cannot open file: "+file);
117  }
118 
119  std::ostringstream buffer;
120  buffer << in.rdbuf();
121  std::string data=buffer.str();
122 
123  std::cout << "Input file length: " << data.size() << std::endl;
124 
125  rcg::saveFile(nodemap, devfile.c_str(), data, true);
126  }
127  else if (op == "-r" || op == "")
128  {
129  std::string data=rcg::loadFile(nodemap, devfile.c_str(), true);
130 
131  if (op == "-r")
132  {
133  std::ofstream out(file);
134  out << data;
135  out.close();
136  }
137  else
138  {
139  std::cout << data;
140  }
141  }
142  else
143  {
144  std::cerr << "Error: Expected parameter '-r' or '-w'!" << std::endl;
145  ret=1;
146  }
147  }
148  }
149  else
150  {
151  std::cerr << "Error: More parameters expected!" << std::endl;
152  ret=1;
153  }
154 
155  dev->close();
156  }
157  else
158  {
159  std::cerr << "Error: Device '" << devid << "' not found!" << std::endl;
160  ret=1;
161  }
162  }
163  else
164  {
165  std::cerr << "Error: Device name not given!" << std::endl;
166  ret=1;
167  }
168  }
169  else
170  {
171  std::cout << argv[0] << " -h | [<interface-id>:]<device-id> -f | (<device-file> [-w|-r <file>])" << std::endl;
172  std::cout << std::endl;
173  std::cout << "Downloading or uploading a file via GenICam." << std::endl;
174  std::cout << std::endl;
175  std::cout << "-h Prints help information and exits" << std::endl;
176  std::cout << "-f Lists names of files on the device" << std::endl;
177  std::cout << "-w <file> Writes the given local file into the selected file on the device" << std::endl;
178  std::cout << "-r <file> Reads the selected file on the device and stores it as local file" << std::endl;
179  std::cout << std::endl;
180  std::cout << "The selected file is printed on std out if none of -f, -w and -r are given." << std::endl;
181  ret=1;
182  }
183  }
184  catch (const std::exception &ex)
185  {
186  std::cerr << ex.what() << std::endl;
187  ret=2;
188  }
189  catch (const GENICAM_NAMESPACE::GenericException &ex)
190  {
191  std::cerr << ex.what() << std::endl;
192  ret=2;
193  }
194 
196 
197  return ret;
198 }
rcg::getDevice
std::shared_ptr< Device > getDevice(const char *id, uint64_t timeout)
Searches across all transport layers and interfaces for a device.
Definition: device.cc:726
rcg::loadFile
std::string loadFile(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, bool exception)
Loads the contents of a file via the GenICam FileAccessControl interface.
Definition: config.cc:1159
device.h
rcg::System::clearSystems
static void clearSystems()
Clears the internal list of systems.
Definition: system.cc:282
rcg::getEnum
std::string getEnum(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, bool exception)
Get the value of an enumeration of the given nodemap.
Definition: config.cc:747
main
int main(int argc, char *argv[])
Definition: gc_file.cc:52
rcg::saveFile
bool saveFile(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, const std::string &data, bool exception)
Loads the contents of the given string as a file via the GenICam FileAccessControl interface.
Definition: config.cc:1221
interface.h
GENICAM_NAMESPACE::GenericException::what
virtual const char * what() const
Get error description (overwrite from std:exception)
rcg::Device::CONTROL
@ CONTROL
Definition: device.h:60
Filestream.h
Definition of ODevFileStream and IDevFileStream.
GENICAM_NAMESPACE::GenericException
GenICam's exception class.
Definition: GCException.h:63
system.h
config.h
std::ostringstream::str
std::string str()
std::ostringstream
Definition: Portability.hh:42


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Wed Dec 4 2024 03:10:11