16 #include "IException.h"
28 Blob::Blob(
const QString &name,
const QString &type) {
35 p_blobPvl.setName(p_type);
49 Blob::Blob(
const QString &name,
const QString &type,
50 const QString &file) {
65 Blob::Blob(
const Blob &other) {
77 p_buffer =
new char[p_nbytes];
79 for (
int i = 0; i < p_nbytes; i++) {
104 p_buffer =
new char[p_nbytes];
106 for (
int i = 0; i < p_nbytes; i++) {
116 if (p_buffer != NULL)
delete [] p_buffer;
124 QString Blob::Type()
const {
133 QString Blob::Name()
const {
142 int Blob::Size()
const {
167 void Blob::Find(
const Pvl &pvl,
const std::vector<PvlKeyword> keywords) {
171 QString blobName = p_blobName.toUpper();
172 for (
int o = 0; o < pvl.
objects(); o++) {
175 QString curName = obj[
"Name"];
176 curName = curName.toUpper();
177 if (blobName == curName) {
179 if (!keywords.empty()){
180 bool keywordFound =
true;
184 if (blobKeyword == keyword && !blobKeyword.
isEquivalent(keyword[0])) {
185 keywordFound =
false;
190 keywordFound =
false;
207 if (p_type ==
"OriginalLabel" && curName ==
"ORIGINALLABEL") {
217 QString msg =
"Invalid " + p_type +
" label format";
218 throw IException(e, IException::Unknown, msg, _FILEINFO_);
223 QString msg =
"Unable to find " + p_type +
" [" + p_blobName +
"]";
224 throw IException(IException::Programmer, msg, _FILEINFO_);
229 p_startByte = p_blobPvl[
"StartByte"];
230 p_nbytes = p_blobPvl[
"Bytes"];
232 if (p_blobPvl.hasKeyword(
"^" + p_type)) {
234 if (p_labelFile !=
"") {
237 p_detached = path + (QString) p_blobPvl[
"^"+p_type];
238 p_blobPvl.deleteKeyword(
"^" + p_type);
242 QString msg =
"Invalid " + p_type +
" label format";
243 throw IException(e, IException::Unknown, msg, _FILEINFO_);
255 void Blob::Read(
const QString &file,
const std::vector<PvlKeyword> keywords) {
257 QString temp(
FileName(file).expanded());
265 QString msg =
"Invalid " + p_type +
" label format";
266 throw IException(e, IException::Unknown, msg, _FILEINFO_);
268 Read(file, pvl, keywords);
279 void Blob::Read(
const QString &file,
const Pvl &pvlLabels,
const std::vector<PvlKeyword> keywords) {
281 QString temp(
FileName(file).expanded());
285 istm.open(temp.toLatin1().data(), std::ios::in);
287 QString message = Message::FileOpen(temp);
288 throw IException(IException::Io, message, _FILEINFO_);
293 Read(pvlLabels, istm, keywords);
297 QString msg =
"Unable to open " + p_type +
" [" + p_blobName +
298 "] in file [" + temp +
"]";
299 throw IException(e, IException::Io, msg, _FILEINFO_);
313 void Blob::Read(
const Pvl &pvl, std::istream &istm,
const std::vector<PvlKeyword> keywords){
317 if (p_detached !=
"") {
319 dstm.open(p_detached.toLatin1().data(), std::ios::in);
321 QString message = Message::FileOpen(p_detached);
322 throw IException(IException::Io, message, _FILEINFO_);
331 QString msg =
"Unable to read " + p_type +
" [" + p_blobName +
"]";
332 throw IException(e, IException::Io, msg, _FILEINFO_);
341 void Blob::ReadInit(){
351 void Blob::ReadData(std::istream &stream) {
353 if (p_buffer != NULL)
delete [] p_buffer;
354 p_buffer =
new char[p_nbytes];
356 streampos sbyte = p_startByte - 1;
357 stream.seekg(sbyte, std::ios::beg);
358 if (!stream.good()) {
359 QString msg =
"Error preparing to read data from " + p_type +
360 " [" + p_blobName +
"]";
361 throw IException(IException::Io, msg, _FILEINFO_);
364 stream.read(p_buffer, p_nbytes);
365 if (!stream.good()) {
366 QString msg =
"Error reading data from " + p_type +
" [" + p_blobName +
"]";
367 throw IException(IException::Io, msg, _FILEINFO_);
382 void Blob::setData(
const char *buffer,
int nbytes) {
383 char *buf =
new char[nbytes];
384 memcpy(buf, buffer, nbytes);
385 takeData(buf, nbytes);
398 void Blob::takeData(
char *buffer,
int nbytes) {
401 if (p_buffer != NULL) {
417 void Blob::Write(
const QString &file) {
425 os.seekp(0, std::ios::end);
427 p_startByte = nbytes + 1 + 1;
434 ios::openmode flags = std::ios::in | std::ios::binary | std::ios::out;
435 stream.open(file.toLatin1().data(), flags);
437 QString message =
"Unable to open [" + file +
"]";
438 throw IException(IException::Io, message, _FILEINFO_);
441 streampos sbyte = p_startByte - 1;
442 stream.seekp(sbyte, std::ios::beg);
443 if (!stream.good()) {
445 QString msg =
"Error preparing to write data to " +
446 p_type +
" [" + p_blobName +
"]";
447 throw IException(IException::Io, msg, _FILEINFO_);
454 QString msg =
"Unable to create " + p_type +
" file [" + file +
"]";
455 throw IException(e, IException::Io, msg, _FILEINFO_);
466 void Blob::Write(
Pvl &pvl, std::fstream &stm,
467 const QString &detachedFileName,
bool overwrite) {
472 streampos sbyte = stm.tellp();
476 stm.seekp(0, std::ios::end);
477 streampos eofbyte = stm.tellp();
481 if (detachedFileName !=
"") {
482 p_blobPvl +=
PvlKeyword(
"^" + p_type, detachedFileName);
487 p_blobPvl[
"Bytes"] =
toString(p_nbytes);
494 for (
int i = 0; i < pvl.
objects(); i++) {
495 if (pvl.
object(i).
name() == p_blobPvl.name()) {
497 if ((QString)obj[
"Name"] == (QString)p_blobPvl[
"Name"]) {
500 BigInt oldSbyte = obj[
"StartByte"];
501 int oldNbytes = (int) obj[
"Bytes"];
504 if (p_nbytes <= oldNbytes) {
505 p_blobPvl[
"StartByte"] = obj[
"StartByte"];
510 else if (((oldSbyte + oldNbytes) == eofbyte) &&
511 (eofbyte >= sbyte)) {
512 p_blobPvl[
"StartByte"] = obj[
"StartByte"];
528 if (!found || !overwrite) {
536 if (detachedFileName !=
"") {
537 p_blobPvl.deleteKeyword(
"^" + p_type);
546 char *Blob::getBuffer() {
554 void Blob::WriteInit(){
564 void Blob::WriteData(std::fstream &stream) {
565 stream.write(p_buffer, p_nbytes);
566 if (!stream.good()) {
567 QString msg =
"Error writing data to " + p_type +
" [" + p_blobName +
"]";
568 throw IException(IException::Io, msg, _FILEINFO_);
581 if (obj.
isNamed(
"TABLE"))
return true;