mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Use std::string_view and fmtlib across all libraries (#3402)
- Twine, StringRef, Format, and NativeFormatting have been removed - Logging now uses fmtlib style formatting - Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or std::puts()/std::fputs() (for unformatted strings). - A wpi/fmt/raw_ostream.h header has been added to enable fmt::print() with wpi::raw_ostream
This commit is contained in:
@@ -8,8 +8,10 @@
|
||||
#include <libgen.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <wpi/Format.h>
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fs.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
@@ -18,21 +20,18 @@
|
||||
|
||||
namespace cs {
|
||||
|
||||
static wpi::StringRef GetUsbNameFromFile(int vendor, int product,
|
||||
wpi::SmallVectorImpl<char>& buf) {
|
||||
static std::string GetUsbNameFromFile(int vendor, int product) {
|
||||
int fd = open("/var/lib/usbutils/usb.ids", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
wpi::raw_svector_ostream os{buf};
|
||||
wpi::SmallString<128> buf;
|
||||
wpi::raw_fd_istream is{fd, true};
|
||||
|
||||
// build vendor and product 4-char hex strings
|
||||
wpi::SmallString<16> vendorStr, productStr;
|
||||
wpi::raw_svector_ostream vendorOs{vendorStr}, productOs{productStr};
|
||||
vendorOs << wpi::format_hex_no_prefix(vendor, 4);
|
||||
productOs << wpi::format_hex_no_prefix(product, 4);
|
||||
auto vendorStr = fmt::format("{:04x}", vendor);
|
||||
auto productStr = fmt::format("{:04x}", product);
|
||||
|
||||
// scan file
|
||||
wpi::SmallString<128> lineBuf;
|
||||
@@ -48,23 +47,24 @@ static wpi::StringRef GetUsbNameFromFile(int vendor, int product,
|
||||
}
|
||||
|
||||
// look for vendor at start of line
|
||||
if (line.startswith(vendorStr)) {
|
||||
if (wpi::starts_with(line, vendorStr)) {
|
||||
foundVendor = true;
|
||||
os << line.substr(5).trim() << ' ';
|
||||
buf += wpi::trim(line.substr(5));
|
||||
buf += ' ';
|
||||
continue;
|
||||
}
|
||||
|
||||
if (foundVendor) {
|
||||
// next vendor, but didn't match product?
|
||||
if (line[0] != '\t') {
|
||||
os << "Unknown";
|
||||
return os.str();
|
||||
buf += "Unknown";
|
||||
return buf;
|
||||
}
|
||||
|
||||
// look for product
|
||||
if (line.substr(1).startswith(productStr)) {
|
||||
os << line.substr(6).trim();
|
||||
return os.str();
|
||||
if (wpi::starts_with(line.substr(1), productStr)) {
|
||||
buf += wpi::trim(line.substr(6));
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,93 +72,90 @@ static wpi::StringRef GetUsbNameFromFile(int vendor, int product,
|
||||
return {};
|
||||
}
|
||||
|
||||
wpi::StringRef GetUsbNameFromId(int vendor, int product,
|
||||
wpi::SmallVectorImpl<char>& buf) {
|
||||
std::string GetUsbNameFromId(int vendor, int product) {
|
||||
// try reading usb.ids
|
||||
wpi::StringRef rv = GetUsbNameFromFile(vendor, product, buf);
|
||||
std::string rv = GetUsbNameFromFile(vendor, product);
|
||||
if (!rv.empty()) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// Fall back to internal database
|
||||
wpi::raw_svector_ostream os{buf};
|
||||
switch (vendor) {
|
||||
case 0x046d:
|
||||
os << "Logitech, Inc. ";
|
||||
case 0x046d: {
|
||||
std::string_view productStr;
|
||||
switch (product) {
|
||||
case 0x0802:
|
||||
os << "Webcam C200";
|
||||
productStr = "Webcam C200";
|
||||
break;
|
||||
case 0x0804:
|
||||
os << "Webcam C250";
|
||||
productStr = "Webcam C250";
|
||||
break;
|
||||
case 0x0805:
|
||||
os << "Webcam C300";
|
||||
productStr = "Webcam C300";
|
||||
break;
|
||||
case 0x0807:
|
||||
os << "Webcam B500";
|
||||
productStr = "Webcam B500";
|
||||
break;
|
||||
case 0x0808:
|
||||
os << "Webcam C600";
|
||||
productStr = "Webcam C600";
|
||||
break;
|
||||
case 0x0809:
|
||||
os << "Webcam Pro 9000";
|
||||
productStr = "Webcam Pro 9000";
|
||||
break;
|
||||
case 0x080a:
|
||||
os << "Portable Webcam C905";
|
||||
productStr = "Portable Webcam C905";
|
||||
break;
|
||||
case 0x080f:
|
||||
os << "Webcam C120";
|
||||
productStr = "Webcam C120";
|
||||
break;
|
||||
case 0x0819:
|
||||
os << "Webcam C210";
|
||||
productStr = "Webcam C210";
|
||||
break;
|
||||
case 0x081b:
|
||||
os << "Webcam C310";
|
||||
productStr = "Webcam C310";
|
||||
break;
|
||||
case 0x081d:
|
||||
os << "HD Webcam C510";
|
||||
productStr = "HD Webcam C510";
|
||||
break;
|
||||
case 0x0821:
|
||||
os << "HD Webcam C910";
|
||||
productStr = "HD Webcam C910";
|
||||
break;
|
||||
case 0x0825:
|
||||
os << "Webcam C270";
|
||||
productStr = "Webcam C270";
|
||||
break;
|
||||
case 0x0826:
|
||||
os << "HD Webcam C525";
|
||||
productStr = "HD Webcam C525";
|
||||
break;
|
||||
case 0x0828:
|
||||
os << "HD Webcam B990";
|
||||
productStr = "HD Webcam B990";
|
||||
break;
|
||||
case 0x082b:
|
||||
os << "Webcam C170";
|
||||
productStr = "Webcam C170";
|
||||
break;
|
||||
case 0x082d:
|
||||
os << "HD Pro Webcam C920";
|
||||
productStr = "HD Pro Webcam C920";
|
||||
break;
|
||||
case 0x0836:
|
||||
os << "B525 HD Webcam";
|
||||
productStr = "B525 HD Webcam";
|
||||
break;
|
||||
case 0x0843:
|
||||
os << "Webcam C930e";
|
||||
productStr = "Webcam C930e";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
return fmt::format("Logitech, Inc. {}", productStr);
|
||||
}
|
||||
}
|
||||
|
||||
return os.str();
|
||||
return {};
|
||||
}
|
||||
|
||||
int CheckedIoctl(int fd, unsigned long req, void* data, // NOLINT(runtime/int)
|
||||
const char* name, const char* file, int line, bool quiet) {
|
||||
int retval = ioctl(fd, req, data);
|
||||
if (!quiet && retval < 0) {
|
||||
wpi::SmallString<64> localfile{file};
|
||||
localfile.push_back('\0');
|
||||
WPI_ERROR(Instance::GetInstance().logger,
|
||||
"ioctl " << name << " failed at " << basename(localfile.data())
|
||||
<< ":" << line << ": " << std::strerror(errno));
|
||||
WPI_ERROR(Instance::GetInstance().logger, "ioctl {} failed at {}:{}: {}",
|
||||
name, fs::path{file}.filename().string(), line,
|
||||
std::strerror(errno));
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user