4 #include "Alignment/Geners/interface/AbsArchive.hh"
5 #include "Alignment/Geners/interface/IOException.hh"
7 #define GS_STREAM_COPY_BUFFER_SIZE 65536
17 bool in_fail = in.fail();
18 bool out_fail = out.fail();
19 while (count > bufsize && !in_fail && !out_fail)
21 in.read(buffer, bufsize);
25 out.write(buffer, bufsize);
26 out_fail = out.fail();
30 if (!in_fail && !out_fail)
32 in.read(buffer, count);
34 out.write(buffer, count);
40 class NotWritableRecord :
public gs::AbsRecord
43 inline NotWritableRecord(
const gs::ClassId& classId,
44 const char* ioPrototype,
46 :
gs::AbsRecord(classId, ioPrototype, name, category) {}
49 inline bool writeData(std::ostream&)
const {
return false;}
54 AbsArchive::AbsArchive(
const char*
name)
55 : name_(name ? name :
""),
61 void AbsArchive::addItemToReference(AbsReference&
r,
62 const unsigned long long id)
const
67 unsigned long long AbsArchive::copyItem(
const unsigned long long id,
68 AbsArchive* destination,
70 const char* newCategory)
73 throw gs::IOInvalidArgument(
"In gs::AbsArchive::copyItem: "
74 "origin archive is not readable");
76 if (
this == destination)
77 throw gs::IOInvalidArgument(
"In gs::AbsArchive::copyItem: "
78 "origin and destination archives are the same");
79 AbsArchive& ar(*destination);
81 throw gs::IOInvalidArgument(
"In gs::AbsArchive::copyItem: "
82 "destination archive is not writable");
83 CPP11_shared_ptr<const CatalogEntry> entry(catalogEntry(
id));
85 throw gs::IOInvalidArgument(
"In gs::AbsArchive::copyItem: no item "
86 "in the archive with the given id");
90 std::istream& is = inputStream(
id, &sz);
94 std::ostream& os = ar.outputStream();
95 std::streampos
base = os.tellp();
96 std::ostream& compressed = ar.compressedStream(os);
99 unsigned long long len;
103 len = entry->itemLength();
106 throw IOReadFailure(
"In gs::AbsArchive::copyItem: "
107 "input stream failure");
108 if (compressed.fail())
109 throw IOWriteFailure(
"In gs::AbsArchive::copyItem: "
110 "output stream failure");
111 const unsigned compressCode = ar.flushCompressedRecord(compressed);
113 throw IOWriteFailure(
"In gs::AbsArchive::copyItem: "
114 "failed to transfer compressed data");
117 std::streamoff off = os.tellp() -
base;
118 const long long delta = off;
119 assert(delta >= 0LL);
122 const char* name = newName;
124 name = entry->name().c_str();
127 category = entry->category().c_str();
128 NotWritableRecord
record(entry->type(), entry->ioPrototype().c_str(),
132 const unsigned long long id2 = ar.addToCatalog(
133 record, compressCode, delta);
135 throw IOWriteFailure(
"In gs::AbsArchive::copyItem: "
136 "failed to add catalog entry");
137 ar.lastItemId_ = id2;
138 ar.lastItemLength_ =
delta;
144 const gs::AbsRecord&
record,
145 const char* failedAction)
147 std::ostringstream err;
148 err <<
"In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
149 <<
"failed to " << failedAction <<
" to the archive \""
150 << ar.name() <<
"\" for item with type \""
151 << record.type().name() <<
"\", name \""
152 << record.name() <<
"\", and category \""
153 << record.category() <<
'"';
160 if (record.id())
throw gs::IOInvalidArgument(
161 "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
162 "records can not be reused");
169 std::ostream& os = ar.outputStream();
170 std::streampos
base = os.tellp();
171 std::ostream& compressed = ar.compressedStream(os);
174 if (!record.writeData(compressed))
177 const unsigned compressCode = ar.flushCompressedRecord(compressed);
180 ar, record,
"transfer compressed data"));
183 std::streamoff off = os.tellp() -
base;
184 const long long delta = off;
185 assert(delta >= 0LL);
188 const unsigned long long id = ar.addToCatalog(record, compressCode, delta);
191 ar, record,
"add catalog entry"));
195 record.itemLength_ =
delta;
199 ar.lastItemLength_ =
delta;
static void archive_stream_copy(std::istream &in, std::size_t count, std::ostream &out)
#define GS_STREAM_COPY_BUFFER_SIZE
std::ostream & operator<<(std::ostream &out, const ALILine &li)
static std::string local_error_message(gs::AbsArchive &ar, const gs::AbsRecord &record, const char *failedAction)