Skip to content

Commit 392b4c6

Browse files
committed
libusb_ll/scan: improved display by align column according to max size (#549)
1 parent 4756fc1 commit 392b4c6

File tree

1 file changed

+78
-16
lines changed

1 file changed

+78
-16
lines changed

src/libusb_ll.cpp

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
#include <cstdio>
1010
#include <cstdlib>
1111
#include <cstring>
12+
#include <iomanip>
13+
#include <list>
1214
#include <vector>
1315
#include <string>
16+
#include <sstream> // For std::stringstream
1417
#include <stdexcept>
1518

1619
#include "cable.hpp"
@@ -101,21 +104,46 @@ int libusb_ll::get_devices_list(const cable_t *cable)
101104
return static_cast<int>(_usb_dev_list.size());
102105
}
103106

107+
struct cable_details_t {
108+
uint8_t bus;
109+
uint8_t device;
110+
uint16_t vid;
111+
uint16_t pid;
112+
std::string probe;
113+
std::string manufacturer;
114+
std::string serial;
115+
std::string product;
116+
cable_details_t(uint8_t& b, uint8_t& d,
117+
uint16_t& v, uint16_t& p,
118+
std::string prb, std::string m,
119+
std::string s, std::string prd):
120+
bus(b), device(d), vid(v), pid(p),
121+
probe(prb), manufacturer(m),
122+
serial(s), product(prd) {}
123+
};
124+
std::string formatHex(uint16_t c, int len) {
125+
std::stringstream ss;
126+
ss << "0x";
127+
ss << std::hex << std::setfill('0') << std::setw(len)
128+
<< (static_cast<unsigned int>(static_cast<unsigned short>(c)) & 0xFFFF);
129+
return ss.str();
130+
}
131+
132+
std::string formatDec(char c, int len) {
133+
std::stringstream ss;
134+
ss << std::setfill('0') << std::setw(len) << std::to_string(c);
135+
return ss.str();
136+
}
137+
104138
bool libusb_ll::scan()
105139
{
106-
char *mess = reinterpret_cast<char *>(malloc(1024));
107-
if (!mess) {
108-
printError("Error: failed to allocate buffer");
109-
return false;
110-
}
140+
std::list<cable_details_t> list_cables;
141+
size_t manufacturer_len = 12;
142+
size_t probe_len = 10;
143+
size_t serial_len = 6;
111144

112145
get_devices_list(nullptr);
113146

114-
snprintf(mess, 1024, "%3s %3s %-13s %-15s %-12s %-20s %s",
115-
"Bus", "device", "vid:pid", "probe type", "manufacturer",
116-
"serial", "product");
117-
printSuccess(mess);
118-
119147
for (libusb_device *usb_dev : _usb_dev_list) {
120148
bool found = false;
121149
struct libusb_device_descriptor desc;
@@ -171,6 +199,7 @@ bool libusb_ll::scan()
171199
libusb_device_handle *handle;
172200
int ret = libusb_open(usb_dev, &handle);
173201
if (ret != 0) {
202+
char mess[1024];
174203
snprintf(mess, 1024,
175204
"Error: can't open device with vid:vid = 0x%04x:0x%04x. "
176205
"Error code %d %s",
@@ -198,17 +227,50 @@ bool libusb_ll::scan()
198227
uint8_t bus_addr = libusb_get_bus_number(usb_dev);
199228
uint8_t dev_addr = libusb_get_device_address(usb_dev);
200229

201-
snprintf(mess, 1024, "%03d %03d 0x%04x:0x%04x %-15s %-12s %-20s %s",
202-
bus_addr, dev_addr,
203-
desc.idVendor, desc.idProduct,
204-
probe_type, imanufacturer, iserial, iproduct);
230+
list_cables.emplace_back(cable_details_t(
231+
bus_addr, dev_addr, desc.idVendor, desc.idProduct,
232+
std::string(probe_type), std::string((const char *)imanufacturer),
233+
std::string((const char *)iserial), std::string((const char *)iproduct)));
205234

206-
printInfo(mess);
235+
if (strlen((const char *)imanufacturer) > manufacturer_len)
236+
manufacturer_len = strlen((const char *)imanufacturer);
237+
if (strlen((const char *)probe_type) > probe_len)
238+
probe_len = strlen((const char *)probe_type);
239+
if (strlen((const char *)iserial) > serial_len)
240+
serial_len = strlen((const char *)iserial);
207241

208242
libusb_close(handle);
209243
}
210244

211-
free(mess);
245+
manufacturer_len++;
246+
serial_len++;
247+
probe_len++;
248+
249+
std::stringstream buffer;
250+
buffer << std::left
251+
<< std::setw(4) << "Bus"
252+
<< std::setw(7) << "device"
253+
<< std::setw(14) << "vid:pid"
254+
<< std::setw(probe_len) << "probe type"
255+
<< std::setw(manufacturer_len) << "manufacturer"
256+
<< std::setw(serial_len) << "serial"
257+
<< "product";
258+
printSuccess(buffer.str());
259+
260+
for (const auto& cable : list_cables) {
261+
std::stringstream buffer;
262+
buffer << std::left // Left-align all fields
263+
<< std::setw(4) << formatDec(cable.bus, 3)
264+
<< std::setw(7) << formatDec(cable.device, 3)
265+
<< std::setw(14)
266+
<< (formatHex(cable.vid, 4) + ":" + formatHex(cable.pid, 4))
267+
<< std::setw(probe_len) << cable.probe
268+
<< std::setw(manufacturer_len) << cable.manufacturer
269+
<< std::setw(serial_len) << cable.serial
270+
<< cable.product;
271+
272+
printInfo(buffer.str());
273+
}
212274

213275
return true;
214276
}

0 commit comments

Comments
 (0)