~noskcaj/ubuntu/trusty/libextractor/merge

« back to all changes in this revision

Viewing changes to src/plugins/exiv2/value.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-11-17 20:27:32 UTC
  • mfrom: (1.10.4 upstream) (5.2.5 sid)
  • Revision ID: james.westby@ubuntu.com-20091117202732-ipm2h3gks5bdw2vx
Tags: 0.5.23+dfsg-3
* Building against libltdl7.
* Updating to standards version 3.8.3.
* Adding maintainer homepage field to control.
* Marking maintainer homepage field to be also included in binary
  packages and changelog.
* Adding README.source.
* Simplifying autotools handling in rules.
* Updating README.source.
* Moving maintainer homepage field from control to copyright.
* Dropping la files.
* Simplyfing debhelper install files.
* Bumping versioned build-depends on debhelper.
* Adding depends to dpkg install info.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ***************************************************************** -*- C++ -*-
2
 
/*
3
 
 * Copyright (C) 2004, 2005 Andreas Huggel <ahuggel@gmx.net>
4
 
 *
5
 
 * This program is part of the Exiv2 distribution.
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU General Public License
9
 
 * as published by the Free Software Foundation; either version 2
10
 
 * of the License, or (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 
 */
21
 
/*
22
 
  File:      value.cpp
23
 
  Version:   $Rev: 560 $
24
 
  Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
25
 
  History:   26-Jan-04, ahu: created
26
 
             11-Feb-04, ahu: isolated as a component
27
 
             31-Jul-04, brad: added Time, Date and String values
28
 
 */
29
 
// *****************************************************************************
30
 
#include "rcsid.hpp"
31
 
EXIV2_RCSID("@(#) $Id: value.cpp 560 2005-04-17 11:51:32Z ahuggel $");
32
 
 
33
 
// *****************************************************************************
34
 
// included header files
35
 
#include "value.hpp"
36
 
#include "types.hpp"
37
 
#include "error.hpp"
38
 
 
39
 
// + standard includes
40
 
#include <cstdlib>
41
 
#include <iostream>
42
 
#include <iomanip>
43
 
#include <sstream>
44
 
#include <cassert>
45
 
#include <ctime>
46
 
#include <stdlib.h>
47
 
 
48
 
// *****************************************************************************
49
 
// class member definitions
50
 
namespace Exiv2 {
51
 
 
52
 
    Value& Value::operator=(const Value& rhs)
53
 
    {
54
 
        if (this == &rhs) return *this;
55
 
        type_ = rhs.type_;
56
 
        return *this;
57
 
    }
58
 
 
59
 
    Value::AutoPtr Value::create(TypeId typeId)
60
 
    {
61
 
        AutoPtr value;
62
 
        switch (typeId) {
63
 
        case invalidTypeId:
64
 
            value = AutoPtr(new DataValue(invalidTypeId));
65
 
            break;
66
 
        case unsignedByte:
67
 
            value = AutoPtr(new DataValue(unsignedByte));
68
 
            break;
69
 
        case asciiString:
70
 
            value = AutoPtr(new AsciiValue);
71
 
            break;
72
 
        case unsignedShort:
73
 
            value = AutoPtr(new ValueType<uint16_t>);
74
 
            break;
75
 
        case unsignedLong:
76
 
            value = AutoPtr(new ValueType<uint32_t>);
77
 
            break;
78
 
        case unsignedRational:
79
 
            value = AutoPtr(new ValueType<URational>);
80
 
            break;
81
 
        case invalid6:
82
 
            value = AutoPtr(new DataValue(invalid6));
83
 
            break;
84
 
        case undefined:
85
 
            value = AutoPtr(new DataValue);
86
 
            break;
87
 
        case signedShort:
88
 
            value = AutoPtr(new ValueType<int16_t>);
89
 
            break;
90
 
        case signedLong:
91
 
            value = AutoPtr(new ValueType<int32_t>);
92
 
            break;
93
 
        case signedRational:
94
 
            value = AutoPtr(new ValueType<Rational>);
95
 
            break;
96
 
        case string:
97
 
            value = AutoPtr(new StringValue);
98
 
            break;
99
 
        case date:
100
 
            value = AutoPtr(new DateValue);
101
 
            break;
102
 
        case time:
103
 
            value = AutoPtr(new TimeValue);
104
 
            break;
105
 
        case comment:
106
 
            value = AutoPtr(new CommentValue);
107
 
            break;
108
 
        default:
109
 
            value = AutoPtr(new DataValue(typeId));
110
 
            break;
111
 
        }
112
 
        return value;
113
 
    } // Value::create
114
 
 
115
 
    std::string Value::toString() const
116
 
    {
117
 
        std::ostringstream os;
118
 
        write(os);
119
 
        return os.str();
120
 
    }
121
 
 
122
 
    DataValue& DataValue::operator=(const DataValue& rhs)
123
 
    {
124
 
        if (this == &rhs) return *this;
125
 
        Value::operator=(rhs);
126
 
        value_ = rhs.value_;
127
 
        return *this;
128
 
    }
129
 
 
130
 
    void DataValue::read(const byte* buf, long len, ByteOrder byteOrder)
131
 
    {
132
 
        // byteOrder not needed
133
 
        value_.assign(buf, buf + len);
134
 
    }
135
 
 
136
 
    void DataValue::read(const std::string& buf)
137
 
    {
138
 
        std::istringstream is(buf);
139
 
        int tmp;
140
 
        value_.clear();
141
 
        while (is >> tmp) {
142
 
            value_.push_back(static_cast<byte>(tmp));
143
 
        }
144
 
    }
145
 
 
146
 
    long DataValue::copy(byte* buf, ByteOrder byteOrder) const
147
 
    {
148
 
        // byteOrder not needed
149
 
        return static_cast<long>(
150
 
            std::copy(value_.begin(), value_.end(), buf) - buf
151
 
            );
152
 
    }
153
 
 
154
 
    long DataValue::size() const
155
 
    {
156
 
        return static_cast<long>(value_.size());
157
 
    }
158
 
 
159
 
    DataValue* DataValue::clone_() const
160
 
    {
161
 
        return new DataValue(*this);
162
 
    }
163
 
 
164
 
    std::ostream& DataValue::write(std::ostream& os) const
165
 
    {
166
 
        std::vector<byte>::size_type end = value_.size();
167
 
        for (std::vector<byte>::size_type i = 0; i != end; ++i) {
168
 
            os << static_cast<int>(value_[i]) << " ";
169
 
        }
170
 
        return os;
171
 
    }
172
 
 
173
 
    StringValueBase& StringValueBase::operator=(const StringValueBase& rhs)
174
 
    {
175
 
        if (this == &rhs) return *this;
176
 
        Value::operator=(rhs);
177
 
        value_ = rhs.value_;
178
 
        return *this;
179
 
    }
180
 
 
181
 
    void StringValueBase::read(const std::string& buf)
182
 
    {
183
 
        value_ = buf;
184
 
    }
185
 
 
186
 
    void StringValueBase::read(const byte* buf, long len, ByteOrder byteOrder)
187
 
    {
188
 
        // byteOrder not needed
189
 
        value_ = std::string(reinterpret_cast<const char*>(buf), len);
190
 
    }
191
 
 
192
 
    long StringValueBase::copy(byte* buf, ByteOrder byteOrder) const
193
 
    {
194
 
        // byteOrder not needed
195
 
        return static_cast<long>(
196
 
            value_.copy(reinterpret_cast<char*>(buf), value_.size())
197
 
            );
198
 
    }
199
 
 
200
 
    long StringValueBase::size() const
201
 
    {
202
 
        return static_cast<long>(value_.size());
203
 
    }
204
 
 
205
 
    std::ostream& StringValueBase::write(std::ostream& os) const
206
 
    {
207
 
        return os << value_;
208
 
    }
209
 
 
210
 
    StringValue& StringValue::operator=(const StringValue& rhs)
211
 
    {
212
 
        if (this == &rhs) return *this;
213
 
        StringValueBase::operator=(rhs);
214
 
        return *this;
215
 
    }
216
 
 
217
 
    StringValue* StringValue::clone_() const
218
 
    {
219
 
        return new StringValue(*this);
220
 
    }
221
 
 
222
 
    AsciiValue& AsciiValue::operator=(const AsciiValue& rhs)
223
 
    {
224
 
        if (this == &rhs) return *this;
225
 
        StringValueBase::operator=(rhs);
226
 
        return *this;
227
 
    }
228
 
 
229
 
    void AsciiValue::read(const std::string& buf)
230
 
    {
231
 
        value_ = buf;
232
 
        if (value_[value_.size()-1] != '\0') value_ += '\0';
233
 
    }
234
 
 
235
 
    AsciiValue* AsciiValue::clone_() const
236
 
    {
237
 
        return new AsciiValue(*this);
238
 
    }
239
 
 
240
 
    std::ostream& AsciiValue::write(std::ostream& os) const
241
 
    {
242
 
        // Strip all trailing '\0's (if any)
243
 
        std::string::size_type pos = value_.find_last_not_of('\0');
244
 
        return os << value_.substr(0, pos + 1);
245
 
    }
246
 
 
247
 
    CommentValue::CharsetTable::CharsetTable(CharsetId charsetId,
248
 
                                             const char* name,
249
 
                                             const char* code)
250
 
        : charsetId_(charsetId), name_(name), code_(code)
251
 
    {
252
 
    }
253
 
 
254
 
    //! Lookup list of supported IFD type information
255
 
    const CommentValue::CharsetTable CommentValue::CharsetInfo::charsetTable_[] = {
256
 
        CharsetTable(ascii,            "Ascii",            "ASCII\0\0\0"),
257
 
        CharsetTable(jis,              "Jis",              "JIS\0\0\0\0\0"),
258
 
        CharsetTable(unicode,          "Unicode",          "UNICODE\0"),
259
 
        CharsetTable(undefined,        "Undefined",        "\0\0\0\0\0\0\0\0"),
260
 
        CharsetTable(invalidCharsetId, "InvalidCharsetId", "\0\0\0\0\0\0\0\0"),
261
 
        CharsetTable(lastCharsetId,    "InvalidCharsetId", "\0\0\0\0\0\0\0\0")
262
 
    };
263
 
 
264
 
    const char* CommentValue::CharsetInfo::name(CharsetId charsetId)
265
 
    {
266
 
        return charsetTable_[ charsetId < lastCharsetId ? charsetId : undefined ].name_;
267
 
    }
268
 
 
269
 
    const char* CommentValue::CharsetInfo::code(CharsetId charsetId)
270
 
    {
271
 
        return charsetTable_[ charsetId < lastCharsetId ? charsetId : undefined ].code_;
272
 
    }
273
 
 
274
 
    CommentValue::CharsetId CommentValue::CharsetInfo::charsetIdByName(
275
 
        const std::string& name)
276
 
    {
277
 
        int i = 0;
278
 
        for (;    charsetTable_[i].charsetId_ != lastCharsetId
279
 
               && charsetTable_[i].name_ != name; ++i) {}
280
 
        return charsetTable_[i].charsetId_ == lastCharsetId ?
281
 
               invalidCharsetId : charsetTable_[i].charsetId_;
282
 
    }
283
 
 
284
 
    CommentValue::CharsetId CommentValue::CharsetInfo::charsetIdByCode(
285
 
        const std::string& code)
286
 
    {
287
 
        int i = 0;
288
 
        for (;    charsetTable_[i].charsetId_ != lastCharsetId
289
 
               && std::string(charsetTable_[i].code_, 8) != code; ++i) {}
290
 
        return charsetTable_[i].charsetId_ == lastCharsetId ?
291
 
               invalidCharsetId : charsetTable_[i].charsetId_;
292
 
    }
293
 
 
294
 
    CommentValue::CommentValue(const std::string& comment)
295
 
        : StringValueBase(Exiv2::undefined)
296
 
    {
297
 
        read(comment);
298
 
    }
299
 
 
300
 
    CommentValue& CommentValue::operator=(const CommentValue& rhs)
301
 
    {
302
 
        if (this == &rhs) return *this;
303
 
        StringValueBase::operator=(rhs);
304
 
        return *this;
305
 
    }
306
 
 
307
 
    void CommentValue::read(const std::string& comment)
308
 
    {
309
 
        std::string c = comment;
310
 
        CharsetId charsetId = undefined;
311
 
        if (comment.length() > 8 && comment.substr(0, 8) == "charset=") {
312
 
            std::string::size_type pos = comment.find_first_of(' ');
313
 
            std::string name = comment.substr(8, pos-8);
314
 
            // Strip quotes (so you can also to specify the charset without quotes)
315
 
            if (name[0] == '"') name = name.substr(1);
316
 
            if (name[name.length()-1] == '"') name = name.substr(0, name.length()-1);
317
 
            charsetId = CharsetInfo::charsetIdByName(name);
318
 
            if (charsetId == invalidCharsetId) throw Error(28, name);
319
 
            c.clear();
320
 
            if (pos != std::string::npos) c = comment.substr(pos+1);
321
 
        }
322
 
        const std::string code(CharsetInfo::code(charsetId), 8);
323
 
        StringValueBase::read(code + c);
324
 
    }
325
 
 
326
 
    std::ostream& CommentValue::write(std::ostream& os) const
327
 
    {
328
 
        CharsetId charsetId = this->charsetId();
329
 
        if (charsetId != undefined) {
330
 
            os << "charset=\"" << CharsetInfo::name(charsetId) << "\" ";
331
 
        }
332
 
        return os << comment();
333
 
    }
334
 
 
335
 
    std::string CommentValue::comment() const
336
 
    {
337
 
        if (value_.length() >= 8) return value_.substr(8);
338
 
        return "";
339
 
    }
340
 
 
341
 
    CommentValue::CharsetId CommentValue::charsetId() const
342
 
    {
343
 
        CharsetId charsetId = undefined;
344
 
        if (value_.length() >= 8) {
345
 
            const std::string code = value_.substr(0, 8);
346
 
            charsetId = CharsetInfo::charsetIdByCode(code);
347
 
        }
348
 
        return charsetId;
349
 
    }
350
 
 
351
 
    CommentValue* CommentValue::clone_() const
352
 
    {
353
 
        return new CommentValue(*this);
354
 
    }
355
 
 
356
 
    DateValue::DateValue(int year, int month, int day)
357
 
        : Value(date)
358
 
    {
359
 
        date_.year = year;
360
 
        date_.month = month;
361
 
        date_.day = day;
362
 
    }
363
 
 
364
 
    DateValue& DateValue::operator=(const DateValue& rhs)
365
 
    {
366
 
        if (this == &rhs) return *this;
367
 
        Value::operator=(rhs);
368
 
        date_.year = rhs.date_.year;
369
 
        date_.month = rhs.date_.month;
370
 
        date_.day = rhs.date_.day;
371
 
        return *this;
372
 
    }
373
 
 
374
 
    void DateValue::read(const byte* buf, long len, ByteOrder byteOrder)
375
 
    {
376
 
        // byteOrder not needed
377
 
        // Hard coded to read Iptc style dates
378
 
        if (len != 8) throw Error(29);
379
 
        int scanned = sscanf(reinterpret_cast<const char*>(buf),
380
 
                   "%4d%2d%2d",
381
 
                   &date_.year, &date_.month, &date_.day);
382
 
        if (scanned != 3) throw Error(29);
383
 
    }
384
 
 
385
 
    void DateValue::read(const std::string& buf)
386
 
    {
387
 
        // byteOrder not needed
388
 
        // Hard coded to read Iptc style dates
389
 
        if (buf.length() < 8) throw Error(29);
390
 
        int scanned = sscanf(buf.data(),
391
 
                   "%4d-%d-%d",
392
 
                   &date_.year, &date_.month, &date_.day);
393
 
        if (scanned != 3) throw Error(29);
394
 
    }
395
 
 
396
 
    void DateValue::setDate( const Date& src )
397
 
    {
398
 
        date_.year = src.year;
399
 
        date_.month = src.month;
400
 
        date_.day = src.day;
401
 
    }
402
 
 
403
 
    long DateValue::copy(byte* buf, ByteOrder byteOrder) const
404
 
    {
405
 
        // byteOrder not needed
406
 
        // sprintf wants to add the null terminator, so use oversized buffer
407
 
        char temp[9];
408
 
 
409
 
        int wrote = sprintf( temp, "%04d%02d%02d",
410
 
                           date_.year, date_.month, date_.day);
411
 
        assert(wrote == 8);
412
 
        memcpy(buf, temp, 8);
413
 
        return 8;
414
 
    }
415
 
 
416
 
    long DateValue::size() const
417
 
    {
418
 
        return 8;
419
 
    }
420
 
 
421
 
    DateValue* DateValue::clone_() const
422
 
    {
423
 
        return new DateValue(*this);
424
 
    }
425
 
 
426
 
    std::ostream& DateValue::write(std::ostream& os) const
427
 
    {
428
 
        return os << date_.year << '-' << std::right
429
 
               << std::setw(2) << std::setfill('0') << date_.month << '-'
430
 
               << std::setw(2) << std::setfill('0') << date_.day;
431
 
    }
432
 
 
433
 
    long DateValue::toLong(long n) const
434
 
    {
435
 
        // Range of tm struct is limited to about 1970 to 2038
436
 
        // This will return -1 if outside that range
437
 
        std::tm tms;
438
 
        memset(&tms, 0, sizeof(tms));
439
 
        tms.tm_mday = date_.day;
440
 
        tms.tm_mon = date_.month - 1;
441
 
        tms.tm_year = date_.year - 1900;
442
 
        return static_cast<long>(std::mktime(&tms));
443
 
    }
444
 
 
445
 
    TimeValue::TimeValue(int hour, int minute,
446
 
                         int second, int tzHour,
447
 
                         int tzMinute)
448
 
        : Value(date)
449
 
    {
450
 
        time_.hour=hour;
451
 
        time_.minute=minute;
452
 
        time_.second=second;
453
 
        time_.tzHour=tzHour;
454
 
        time_.tzMinute=tzMinute;
455
 
    }
456
 
 
457
 
    TimeValue& TimeValue::operator=(const TimeValue& rhs)
458
 
    {
459
 
        if (this == &rhs) return *this;
460
 
        Value::operator=(rhs);
461
 
        memcpy(&time_, &rhs.time_, sizeof(time_));
462
 
        return *this;
463
 
    }
464
 
 
465
 
    void TimeValue::read(const byte* buf, long len, ByteOrder byteOrder)
466
 
    {
467
 
        // byteOrder not needed
468
 
        // Hard coded to read Iptc style times
469
 
        if (len != 11) throw Error(30);
470
 
        char plusMinus;
471
 
        int scanned = sscanf(reinterpret_cast<const char*>(buf),
472
 
                   "%2d%2d%2d%1c%2d%2d",
473
 
                   &time_.hour, &time_.minute, &time_.second,
474
 
                   &plusMinus, &time_.tzHour, &time_.tzMinute );
475
 
 
476
 
        if (scanned != 6) throw Error(30);
477
 
        if (plusMinus == '-') {
478
 
            time_.tzHour *= -1;
479
 
            time_.tzMinute *= -1;
480
 
        }
481
 
    }
482
 
 
483
 
    void TimeValue::read(const std::string& buf)
484
 
    {
485
 
        // byteOrder not needed
486
 
        // Hard coded to read Iptc style times
487
 
        if (buf.length() < 9) throw Error(30);
488
 
        char plusMinus;
489
 
        int scanned = sscanf(buf.data(),
490
 
                   "%d:%d:%d%1c%d:%d",
491
 
                   &time_.hour, &time_.minute, &time_.second,
492
 
                   &plusMinus, &time_.tzHour, &time_.tzMinute );
493
 
 
494
 
        if (scanned != 6) throw Error(30);
495
 
        if (plusMinus == '-') {
496
 
            time_.tzHour *= -1;
497
 
            time_.tzMinute *= -1;
498
 
        }
499
 
    }
500
 
 
501
 
    void TimeValue::setTime( const Time& src )
502
 
    {
503
 
        memcpy(&time_, &src, sizeof(time_));
504
 
    }
505
 
 
506
 
    long TimeValue::copy(byte* buf, ByteOrder byteOrder) const
507
 
    {
508
 
        // byteOrder not needed
509
 
        // sprintf wants to add the null terminator, so use oversized buffer
510
 
        char temp[12];
511
 
        char plusMinus = '+';
512
 
        if (time_.tzHour < 0 || time_.tzMinute < 0) plusMinus = '-';
513
 
 
514
 
        int wrote = sprintf(temp,
515
 
                   "%02d%02d%02d%1c%02d%02d",
516
 
                   time_.hour, time_.minute, time_.second,
517
 
                   plusMinus, abs(time_.tzHour), abs(time_.tzMinute));
518
 
 
519
 
        assert(wrote == 11);
520
 
        memcpy(buf, temp, 11);
521
 
        return 11;
522
 
    }
523
 
 
524
 
    long TimeValue::size() const
525
 
    {
526
 
        return 11;
527
 
    }
528
 
 
529
 
    TimeValue* TimeValue::clone_() const
530
 
    {
531
 
        return new TimeValue(*this);
532
 
    }
533
 
 
534
 
    std::ostream& TimeValue::write(std::ostream& os) const
535
 
    {
536
 
        char plusMinus = '+';
537
 
        if (time_.tzHour < 0 || time_.tzMinute < 0) plusMinus = '-';
538
 
 
539
 
        return os << std::right
540
 
           << std::setw(2) << std::setfill('0') << time_.hour << ':'
541
 
           << std::setw(2) << std::setfill('0') << time_.minute << ':'
542
 
           << std::setw(2) << std::setfill('0') << time_.second << plusMinus
543
 
           << std::setw(2) << std::setfill('0') << abs(time_.tzHour) << ':'
544
 
           << std::setw(2) << std::setfill('0') << abs(time_.tzMinute);
545
 
    }
546
 
 
547
 
    long TimeValue::toLong(long n) const
548
 
    {
549
 
        // Returns number of seconds in the day in UTC.
550
 
        long result = (time_.hour - time_.tzHour) * 60 * 60;
551
 
        result += (time_.minute - time_.tzMinute) * 60;
552
 
        result += time_.second;
553
 
        if (result < 0) {
554
 
            result += 86400;
555
 
        }
556
 
        return result;
557
 
    }
558
 
 
559
 
}                                       // namespace Exiv2