C++ wrapper: Fix use-after-free issue in List::find_all()
[libftdi] / examples / find_all_pp.cpp
CommitLineData
b0c551aa 1/* final_all_pp.cpp
818b1196
MV
2
3 Simple libftdi-cpp usage
4
5 This program is distributed under the GPL, version 2
6*/
7
8#include "ftdi.hpp"
9#include <iostream>
10#include <iomanip>
11#include <cstdlib>
12#include <cstring>
13using namespace Ftdi;
14
15int main(int argc, char **argv)
16{
22d12cda
TJ
17 // Show help
18 if (argc > 1)
19 {
20 if (strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0)
21 {
22 std::cout << "Usage: " << argv[0] << " [-v VENDOR_ID] [-p PRODUCT_ID]" << std::endl;
23 return EXIT_SUCCESS;
24 }
25 }
26
27 // Parse args
28 int vid = 0x0403, pid = 0x6010, tmp = 0;
29 for (int i = 0; i < (argc - 1); i++)
30 {
31 if (strcmp(argv[i], "-v") == 0)
32 if ((tmp = strtol(argv[++i], 0, 16)) >= 0)
33 vid = tmp;
34
35 if (strcmp(argv[i], "-p") == 0)
36 if ((tmp = strtol(argv[++i], 0, 16)) >= 0)
37 pid = tmp;
38 }
39
40 // Print header
41 std::cout << std::hex << std::showbase
42 << "Found devices ( VID: " << vid << ", PID: " << pid << " )"
43 << std::endl
44 << "------------------------------------------------"
45 << std::endl << std::dec;
46
47 // Print whole list
7c21beca
TJ
48 Context context;
49 List* list = List::find_all(context, vid, pid);
22d12cda
TJ
50 for (List::iterator it = list->begin(); it != list->end(); it++)
51 {
52 std::cout << "FTDI (" << &*it << "): "
53 << it->vendor() << ", "
54 << it->description() << ", "
2f6b4bb6
MV
55 << it->serial();
56
57 // Open test
784b2935 58 if(it->open() == 0)
2f6b4bb6
MV
59 std::cout << " (Open OK)";
60 else
61 std::cout << " (Open FAILED)";
62
63 it->close();
64
65 std::cout << std::endl;
22d12cda
TJ
66
67 }
68
69 delete list;
70
71 return EXIT_SUCCESS;
818b1196 72}