4 #include "Alignment/Geners/interface/AbsArchive.hh"
5 #include "Alignment/Geners/interface/IOException.hh"
7 #define GS_STREAM_COPY_BUFFER_SIZE 65536
14 bool in_fail = in.fail();
15 bool out_fail = out.fail();
16 while (count > bufsize && !in_fail && !out_fail) {
17 in.read(buffer, bufsize);
20 out.write(buffer, bufsize);
21 out_fail = out.fail();
25 if (!in_fail && !out_fail) {
26 in.read(buffer, count);
28 out.write(buffer, count);
34 class NotWritableRecord :
public gs::AbsRecord {
36 inline NotWritableRecord(
const gs::ClassId &classId,
const char *ioPrototype,
const char *
name,
const char *
category)
37 : gs::AbsRecord(classId, ioPrototype, name, category) {}
39 NotWritableRecord() =
delete;
42 inline bool writeData(std::ostream &)
const override {
return false; }
47 AbsArchive::AbsArchive(
const char *
name) : name_(name ? name :
""), lastItemId_(0), lastItemLength_(0) {}
49 void AbsArchive::addItemToReference(AbsReference &
r,
const unsigned long long id)
const { r.addItemId(
id); }
51 unsigned long long AbsArchive::copyItem(
const unsigned long long id,
54 const char *newCategory) {
56 throw gs::IOInvalidArgument(
57 "In gs::AbsArchive::copyItem: "
58 "origin archive is not readable");
60 if (
this == destination)
61 throw gs::IOInvalidArgument(
62 "In gs::AbsArchive::copyItem: "
63 "origin and destination archives are the same");
64 AbsArchive &ar(*destination);
66 throw gs::IOInvalidArgument(
67 "In gs::AbsArchive::copyItem: "
68 "destination archive is not writable");
69 std::shared_ptr<const CatalogEntry>
entry(catalogEntry(
id));
71 throw gs::IOInvalidArgument(
72 "In gs::AbsArchive::copyItem: no item "
73 "in the archive with the given id");
77 std::istream &is = inputStream(
id, &sz);
81 std::ostream &os = ar.outputStream();
82 std::streampos
base = os.tellp();
83 std::ostream &compressed = ar.compressedStream(os);
86 unsigned long long len;
90 len = entry->itemLength();
94 "In gs::AbsArchive::copyItem: "
95 "input stream failure");
96 if (compressed.fail())
98 "In gs::AbsArchive::copyItem: "
99 "output stream failure");
100 const unsigned compressCode = ar.flushCompressedRecord(compressed);
102 throw IOWriteFailure(
103 "In gs::AbsArchive::copyItem: "
104 "failed to transfer compressed data");
107 std::streamoff off = os.tellp() -
base;
108 const long long delta = off;
112 const char *name = newName;
114 name = entry->name().c_str();
117 category = entry->category().c_str();
118 NotWritableRecord
record(entry->type(), entry->ioPrototype().c_str(),
name,
category);
121 const unsigned long long id2 = ar.addToCatalog(
record, compressCode, delta);
123 throw IOWriteFailure(
124 "In gs::AbsArchive::copyItem: "
125 "failed to add catalog entry");
126 ar.lastItemId_ = id2;
127 ar.lastItemLength_ =
delta;
133 std::ostringstream
err;
134 err <<
"In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
135 <<
"failed to " << failedAction <<
" to the archive \"" << ar.name() <<
"\" for item with type \""
136 << record.type().name() <<
"\", name \"" << record.name() <<
"\", and category \"" << record.category() <<
'"';
143 throw gs::IOInvalidArgument(
144 "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
145 "records can not be reused");
152 std::ostream &os = ar.outputStream();
153 std::streampos
base = os.tellp();
154 std::ostream &compressed = ar.compressedStream(os);
157 if (!record.writeData(compressed))
160 const unsigned compressCode = ar.flushCompressedRecord(compressed);
165 std::streamoff off = os.tellp() -
base;
166 const long long delta = off;
170 const unsigned long long id = ar.addToCatalog(record, compressCode, delta);
176 record.itemLength_ =
delta;
180 ar.lastItemLength_ =
delta;
static void archive_stream_copy(std::istream &in, std::size_t count, std::ostream &out)
uint16_t *__restrict__ id
static const char category[]
#define GS_STREAM_COPY_BUFFER_SIZE
std::ostream & operator<<(std::ostream &out, const ALILine &li)
constexpr Matriplex::idx_t LL
static std::string local_error_message(gs::AbsArchive &ar, const gs::AbsRecord &record, const char *failedAction)