C++ wrapper: Fix use-after-free issue in List::find_all()
[libftdi] / examples / find_all_pp.cpp
1 /* final_all_pp.cpp
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>
13 using namespace Ftdi;
14
15 int main(int argc, char **argv)
16 {
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
48     Context context;
49     List* list = List::find_all(context, vid, pid);
50     for (List::iterator it = list->begin(); it != list->end(); it++)
51     {
52         std::cout << "FTDI (" << &*it << "): "
53         << it->vendor() << ", "
54         << it->description() << ", "
55         << it->serial();
56
57         // Open test
58         if(it->open() == 0)
59            std::cout << " (Open OK)";
60         else
61            std::cout << " (Open FAILED)";
62
63         it->close();
64
65         std::cout << std::endl;
66
67     }
68
69     delete list;
70
71     return EXIT_SUCCESS;
72 }