Updated Library Documentation (markdown)
@@ -772,45 +772,132 @@ Interface to build and query partition maps.
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
try {
|
||||
PartitionMap::Map builder;
|
||||
// -------------------------------
|
||||
// Builder usage
|
||||
// -------------------------------
|
||||
PartitionMap::BuildMap builder; // Read default dirs
|
||||
|
||||
// Read default directories
|
||||
// Build map from default directories
|
||||
if (!builder.readDefaultDirectories()) {
|
||||
std::cerr << "Failed to read default directories" << std::endl;
|
||||
return 1;
|
||||
std::cerr << "Failed to read default directories.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Query partitions
|
||||
auto allPartitions = builder.getPartitionList();
|
||||
if (allPartitions) {
|
||||
for (const auto &p : *allPartitions) {
|
||||
std::cout << p << std::endl;
|
||||
}
|
||||
// Build map from a specific directory
|
||||
if (!builder.readDirectory("/dev/block/by-name")) {
|
||||
std::cerr << "Failed to read /dev/block/by-name\n";
|
||||
}
|
||||
|
||||
// Use doForAllPartitions lambda
|
||||
builder.doForAllPartitions([](const std::string &name, PartitionMap::BasicInf props) {
|
||||
std::cout << name << " -> size: " << props.size << ", logical: " << props.isLogical << std::endl;
|
||||
return true; // Continue iteration
|
||||
// -------------------------------
|
||||
// Accessing the map
|
||||
// -------------------------------
|
||||
PartitionMap::Map_t map = *builder;
|
||||
|
||||
// Check if a partition exists
|
||||
if (builder.hasPartition("boot")) {
|
||||
std::cout << "Partition boot exists\n";
|
||||
}
|
||||
|
||||
// Check if a partition is logical
|
||||
if (builder.isLogical("system")) {
|
||||
std::cout << "Partition system is logical\n";
|
||||
}
|
||||
|
||||
// Get partition size
|
||||
uint64_t size = builder.sizeOf("boot");
|
||||
std::cout << "Size of boot: " << size << " bytes\n";
|
||||
|
||||
// Get partition info (size + logical)
|
||||
auto partInfo = builder.get("boot");
|
||||
if (partInfo) {
|
||||
std::cout << "boot - size: " << partInfo->first
|
||||
<< ", logical: " << (partInfo->second ? "yes" : "no") << "\n";
|
||||
}
|
||||
|
||||
// Get lists
|
||||
auto allParts = builder.getPartitionList();
|
||||
auto logicalParts = builder.getLogicalPartitionList();
|
||||
auto physicalParts = builder.getPhysicalPartitionList();
|
||||
|
||||
if (allParts) {
|
||||
std::cout << "All partitions:\n";
|
||||
for (const auto &p : *allParts)
|
||||
std::cout << " " << p << "\n";
|
||||
}
|
||||
|
||||
if (logicalParts) {
|
||||
std::cout << "Logical partitions:\n";
|
||||
for (const auto &p : *logicalParts)
|
||||
std::cout << " " << p << "\n";
|
||||
}
|
||||
|
||||
if (physicalParts) {
|
||||
std::cout << "Physical partitions:\n";
|
||||
for (const auto &p : *physicalParts)
|
||||
std::cout << " " << p << "\n";
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// Lambda examples
|
||||
// -------------------------------
|
||||
builder.doForAllPartitions([](const std::string &name, PartitionMap::BasicInf info) {
|
||||
std::cout << "Partition: " << name
|
||||
<< " size: " << info.size
|
||||
<< " logical: " << (info.isLogical ? "yes" : "no") << "\n";
|
||||
return true;
|
||||
});
|
||||
|
||||
// Check specific partition
|
||||
if (builder.hasPartition("boot")) {
|
||||
std::cout << "sda1 size: " << builder.sizeOf("boot") << std::endl;
|
||||
}
|
||||
|
||||
// Other library functions...
|
||||
builder.doForPhysicalPartitions([](const std::string &name, PartitionMap::BasicInf info) {
|
||||
std::cout << "[Physical] " << name << "\n";
|
||||
return true;
|
||||
});
|
||||
|
||||
} catch (PartitionMap::Error &e) {
|
||||
std::cerr << "PartitionMap Error: " << e.what() << std::endl;
|
||||
builder.doForLogicalPartitions([](const std::string &name, PartitionMap::BasicInf info) {
|
||||
std::cout << "[Logical] " << name << "\n";
|
||||
return true;
|
||||
});
|
||||
|
||||
std::vector<std::string> partitionVec;
|
||||
builder.copyPartitionsToVector(partitionVec);
|
||||
|
||||
std::cout << "Copied partitions to vector:\n";
|
||||
for (auto &p : partitionVec)
|
||||
std::cout << " " << p << "\n";
|
||||
|
||||
// -------------------------------
|
||||
// Map operators
|
||||
// -------------------------------
|
||||
int count = map;
|
||||
std::cout << "Total partitions: " << count << "\n";
|
||||
|
||||
std::vector<PartitionMap::Info> infoVec = map;
|
||||
for (auto &info : infoVec) {
|
||||
std::cout << "Partition: " << info.name
|
||||
<< " size: " << info.props.size
|
||||
<< " logical: " << (info.props.isLogical ? "yes" : "no") << "\n";
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// For-each support (iterator)
|
||||
// -------------------------------
|
||||
for (auto &[name, props] : map) {
|
||||
std::cout << "[Iterator] " << name
|
||||
<< " size: " << props.size
|
||||
<< " logical: " << (props.isLogical ? "yes" : "no") << "\n";
|
||||
}
|
||||
|
||||
} catch (const PartitionMap::Error &e) {
|
||||
std::cerr << "PartitionMap error: " << e.message() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
> Note: `PartitionMap::Error` can be thrown, so always wrap operations in try-catch blocks.
|
||||
|
||||
Reference in New Issue
Block a user