| 19 | ImgImageWriter(const ImageWriterProto& config): ImageWriter(config) {} |
| 20 | |
| 21 | void writeImage(const Image& image) override |
| 22 | { |
| 23 | const Geometry geometry = image.getGeometry(); |
| 24 | |
| 25 | auto& layout = globalConfig()->layout(); |
| 26 | int tracks = |
| 27 | layout.has_tracks() ? layout.tracks() : geometry.numCylinders; |
| 28 | int sides = layout.has_sides() ? layout.sides() : geometry.numHeads; |
| 29 | |
| 30 | std::ofstream outputFile( |
| 31 | _config.filename(), std::ios::out | std::ios::binary); |
| 32 | if (!outputFile.is_open()) |
| 33 | error("cannot open output file"); |
| 34 | |
| 35 | const auto diskLayout = createDiskLayout(); |
| 36 | bool in_filesystem_order = _config.img().filesystem_sector_order(); |
| 37 | |
| 38 | for (auto& logicalLocation : |
| 39 | in_filesystem_order ? diskLayout->logicalLocationsInFilesystemOrder |
| 40 | : diskLayout->logicalLocations) |
| 41 | { |
| 42 | auto& ltl = diskLayout->layoutByLogicalLocation.at(logicalLocation); |
| 43 | |
| 44 | for (unsigned sectorId : in_filesystem_order |
| 45 | ? ltl->filesystemSectorOrder |
| 46 | : ltl->naturalSectorOrder) |
| 47 | { |
| 48 | const auto& sector = image.get( |
| 49 | logicalLocation.cylinder, logicalLocation.head, sectorId); |
| 50 | if (sector) |
| 51 | sector->data.slice(0, ltl->sectorSize).writeTo(outputFile); |
| 52 | else |
| 53 | outputFile.seekp(ltl->sectorSize, std::ios::cur); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | log("IMG: wrote {} tracks, {} sides, {} kB total to {}", |
| 58 | tracks, |
| 59 | sides, |
| 60 | outputFile.tellp() / 1024, |
| 61 | _config.filename()); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | std::unique_ptr<ImageWriter> ImageWriter::createImgImageWriter( |