-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathtypes.cpp
814 lines (717 loc) · 26.3 KB
/
types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2018 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
/*
File: types.cpp
Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
History: 26-Jan-04, ahu: created
11-Feb-04, ahu: isolated as a component
*/
// *****************************************************************************
// included header files
#include "types.hpp"
#include "enforce.hpp"
#include "futils.hpp"
#include "i18n.h" // for _exvGettext
#include "safe_op.hpp"
#include "unused.h"
// + standard includes
#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW__)
# include <windows.h> // for MultiByteToWideChar etc
#endif // Windows
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <utility>
// *****************************************************************************
namespace {
//! Information pertaining to the defined %Exiv2 value type identifiers.
struct TypeInfoTable {
Exiv2::TypeId typeId_; //!< Type id
const char* name_; //!< Name of the type
long size_; //!< Bytes per data entry
//! Comparison operator for \em typeId
bool operator==(Exiv2::TypeId typeId) const
{
return typeId_ == typeId;
}
//! Comparison operator for \em name
bool operator==(const std::string& name) const
{
return 0 == strcmp(name_, name.c_str());
}
}; // struct TypeInfoTable
//! Lookup list with information of Exiv2 types
const TypeInfoTable typeInfoTable[] = {
{ Exiv2::invalidTypeId, "Invalid", 0 },
{ Exiv2::unsignedByte, "Byte", 1 },
{ Exiv2::asciiString, "Ascii", 1 },
{ Exiv2::unsignedShort, "Short", 2 },
{ Exiv2::unsignedLong, "Long", 4 },
{ Exiv2::unsignedRational, "Rational", 8 },
{ Exiv2::signedByte, "SByte", 1 },
{ Exiv2::undefined, "Undefined", 1 },
{ Exiv2::signedShort, "SShort", 2 },
{ Exiv2::signedLong, "SLong", 4 },
{ Exiv2::signedRational, "SRational", 8 },
{ Exiv2::tiffFloat, "Float", 4 },
{ Exiv2::tiffDouble, "Double", 8 },
{ Exiv2::tiffIfd, "Ifd", 4 },
{ Exiv2::string, "String", 1 },
{ Exiv2::date, "Date", 8 },
{ Exiv2::time, "Time", 11 },
{ Exiv2::comment, "Comment", 1 },
{ Exiv2::directory, "Directory", 1 },
{ Exiv2::xmpText, "XmpText", 1 },
{ Exiv2::xmpAlt, "XmpAlt", 1 },
{ Exiv2::xmpBag, "XmpBag", 1 },
{ Exiv2::xmpSeq, "XmpSeq", 1 },
{ Exiv2::langAlt, "LangAlt", 1 }
};
} // namespace
// *****************************************************************************
// class member definitions
namespace Exiv2 {
const char* TypeInfo::typeName(TypeId typeId)
{
const TypeInfoTable* tit = find(typeInfoTable, typeId);
if (!tit) return nullptr;
return tit->name_;
}
TypeId TypeInfo::typeId(const std::string& typeName)
{
const TypeInfoTable* tit = find(typeInfoTable, typeName);
if (!tit) return invalidTypeId;
return tit->typeId_;
}
size_t TypeInfo::typeSize(TypeId typeId)
{
const TypeInfoTable* tit = find(typeInfoTable, typeId);
if (!tit) return 0;
return tit->size_;
}
DataBuf::DataBuf(DataBuf& rhs)
: pData_(rhs.pData_), size_(rhs.size_)
{
auto ret = rhs.release();
UNUSED(ret);
}
DataBuf::~DataBuf()
{
delete[] pData_;
}
DataBuf::DataBuf() = default;
DataBuf::DataBuf(size_t size) : pData_(new byte[size]()), size_(size)
{}
DataBuf::DataBuf(const byte* pData, size_t size)
{
if (size > 0) {
pData_ = new byte[size];
std::memcpy(pData_, pData, size);
size_ = size;
}
}
DataBuf& DataBuf::operator=(DataBuf rhs)
{
if (this == &rhs) return *this;
reset(rhs.release());
return *this;
}
void DataBuf::alloc(size_t size)
{
if (size > size_) {
delete[] pData_;
pData_ = nullptr;
size_ = 0;
pData_ = new byte[size];
size_ = size;
}
}
EXV_WARN_UNUSED_RESULT std::pair<byte *, size_t> DataBuf::release()
{
std::pair<byte*, size_t> p = std::make_pair(pData_, size_);
pData_ = nullptr;
size_ = 0;
return p;
}
void DataBuf::free()
{
delete[] pData_;
pData_ = nullptr;
size_ = 0;
}
void DataBuf::reset(std::pair<byte *, size_t> p)
{
if (pData_ != p.first) {
delete[] pData_;
pData_ = p.first;
}
size_ = p.second;
}
DataBuf::DataBuf(const DataBufRef &rhs) : pData_(rhs.p.first), size_(rhs.p.second) {}
DataBuf &DataBuf::operator=(DataBufRef rhs) { reset(rhs.p); return *this; }
byte* DataBuf::begin() noexcept
{
return pData_;
}
const byte *DataBuf::cbegin() const noexcept
{
return pData_;
}
byte* DataBuf::end() noexcept
{
return pData_ + size_;
}
const byte *DataBuf::cend() const noexcept
{
return pData_ + size_;
}
Exiv2::DataBuf::operator DataBufRef() { return DataBufRef(release()); }
// *************************************************************************
// free functions
static void checkDataBufBounds(const DataBuf& buf, size_t end) {
enforce<std::invalid_argument>(end <= static_cast<size_t>(std::numeric_limits<long>::max()),
"end of slice too large to be compared with DataBuf bounds.");
enforce<std::out_of_range>(end <= buf.size_, "Invalid slice bounds specified");
}
Slice<byte*> makeSlice(DataBuf& buf, size_t begin, size_t end)
{
checkDataBufBounds(buf, end);
return {buf.pData_, begin, end};
}
Slice<const byte*> makeSlice(const DataBuf& buf, size_t begin, size_t end)
{
checkDataBufBounds(buf, end);
return {buf.pData_, begin, end};
}
std::ostream& operator<<(std::ostream& os, const Rational& r)
{
return os << r.first << "/" << r.second;
}
std::istream& operator>>(std::istream& is, Rational& r)
{
// http://dev.exiv2.org/boards/3/topics/1912?r=1915
if ( std::tolower(is.peek()) == 'f' ) {
char F = 0;
float f = 0.F;
is >> F >> f ;
f = 2.0F * std::log(f) / std::log(2.0F);
r = Exiv2::floatToRationalCast(f);
} else {
int32_t nominator = 0;
int32_t denominator = 0;
char c('\0');
is >> nominator >> c >> denominator;
if (c != '/')
is.setstate(std::ios::failbit);
if (is)
r = std::make_pair(nominator, denominator);
}
return is;
}
std::ostream& operator<<(std::ostream& os, const URational& r)
{
return os << r.first << "/" << r.second;
}
std::istream& operator>>(std::istream& is, URational& r)
{
// http://dev.exiv2.org/boards/3/topics/1912?r=1915
/// \todo This implementation seems to be duplicated for the Rational type. Try to remove duplication
if ( std::tolower(is.peek()) == 'f' ) {
char F = 0;
float f = 0.F;
is >> F >> f ;
f = 2.0F * std::log(f) / std::log(2.0F);
r = Exiv2::floatToRationalCast(f);
} else {
uint32_t nominator = 0;
uint32_t denominator = 0;
char c('\0');
is >> nominator >> c >> denominator;
if (c != '/')
is.setstate(std::ios::failbit);
if (is)
r = std::make_pair(nominator, denominator);
}
return is;
}
uint16_t getUShort(const byte* buf, ByteOrder byteOrder)
{
return getUShort(makeSliceUntil(buf, 2), byteOrder);
}
uint32_t getULong(const byte* buf, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
return buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
}
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}
uint64_t getULongLong(const byte* buf, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
return static_cast<uint64_t>(buf[7]) << 56 | static_cast<uint64_t>(buf[6]) << 48 |
static_cast<uint64_t>(buf[5]) << 40 | static_cast<uint64_t>(buf[4]) << 32 |
static_cast<uint64_t>(buf[3]) << 24 | static_cast<uint64_t>(buf[2]) << 16 |
static_cast<uint64_t>(buf[1]) << 8 | static_cast<uint64_t>(buf[0]);
}
return static_cast<uint64_t>(buf[0]) << 56 | static_cast<uint64_t>(buf[1]) << 48 |
static_cast<uint64_t>(buf[2]) << 40 | static_cast<uint64_t>(buf[3]) << 32 |
static_cast<uint64_t>(buf[4]) << 24 | static_cast<uint64_t>(buf[5]) << 16 |
static_cast<uint64_t>(buf[6]) << 8 | static_cast<uint64_t>(buf[7]);
}
URational getURational(const byte* buf, ByteOrder byteOrder)
{
uint32_t nominator = getULong(buf, byteOrder);
uint32_t denominator = getULong(buf + 4, byteOrder);
return std::make_pair(nominator, denominator);
}
int16_t getShort(const byte* buf, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
return buf[1] << 8 | buf[0];
}
return buf[0] << 8 | buf[1];
}
int32_t getLong(const byte* buf, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
return buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
}
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}
Rational getRational(const byte* buf, ByteOrder byteOrder)
{
int32_t nominator = getLong(buf, byteOrder);
int32_t denominator = getLong(buf + 4, byteOrder);
return std::make_pair(nominator, denominator);
}
float getFloat(const byte* buf, ByteOrder byteOrder)
{
// This algorithm assumes that the internal representation of the float
// type is the 4-byte IEEE 754 binary32 format, which is common but not
// required by the C++ standard.
static_assert(sizeof(float) == 4, "float type requires 4-byte IEEE 754 binary32 format");
union {
uint32_t ul_;
float f_;
} u;
u.ul_ = getULong(buf, byteOrder);
return u.f_;
}
double getDouble(const byte* buf, ByteOrder byteOrder)
{
// This algorithm assumes that the internal representation of the double
// type is the 8-byte IEEE 754 binary64 format, which is common but not
// required by the C++ standard.
static_assert(sizeof(double) == 8, "double type requires 8-byte IEEE 754 binary64 format");
union {
uint64_t ull_;
double d_;
} u;
u.ull_ = 0;
if (byteOrder == littleEndian) {
u.ull_ = static_cast<uint64_t>(buf[7]) << 56
| static_cast<uint64_t>(buf[6]) << 48
| static_cast<uint64_t>(buf[5]) << 40
| static_cast<uint64_t>(buf[4]) << 32
| static_cast<uint64_t>(buf[3]) << 24
| static_cast<uint64_t>(buf[2]) << 16
| static_cast<uint64_t>(buf[1]) << 8
| static_cast<uint64_t>(buf[0]);
}
else {
u.ull_ = static_cast<uint64_t>(buf[0]) << 56
| static_cast<uint64_t>(buf[1]) << 48
| static_cast<uint64_t>(buf[2]) << 40
| static_cast<uint64_t>(buf[3]) << 32
| static_cast<uint64_t>(buf[4]) << 24
| static_cast<uint64_t>(buf[5]) << 16
| static_cast<uint64_t>(buf[6]) << 8
| static_cast<uint64_t>(buf[7]);
}
return u.d_;
}
long us2Data(byte* buf, uint16_t s, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
buf[0] = static_cast<byte>(s & 0x00ff);
buf[1] = static_cast<byte>((s & 0xff00) >> 8);
}
else {
buf[0] = static_cast<byte>((s & 0xff00) >> 8);
buf[1] = static_cast<byte>(s & 0x00ff);
}
return 2;
}
long ul2Data(byte* buf, uint32_t l, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
buf[0] = static_cast<byte>(l & 0x000000ff);
buf[1] = static_cast<byte>((l & 0x0000ff00) >> 8);
buf[2] = static_cast<byte>((l & 0x00ff0000) >> 16);
buf[3] = static_cast<byte>((l & 0xff000000) >> 24);
}
else {
buf[0] = static_cast<byte>((l & 0xff000000) >> 24);
buf[1] = static_cast<byte>((l & 0x00ff0000) >> 16);
buf[2] = static_cast<byte>((l & 0x0000ff00) >> 8);
buf[3] = static_cast<byte>(l & 0x000000ff);
}
return 4;
}
long ur2Data(byte* buf, URational l, ByteOrder byteOrder)
{
long o = ul2Data(buf, l.first, byteOrder);
o += ul2Data(buf+o, l.second, byteOrder);
return o;
}
long s2Data(byte* buf, int16_t s, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
buf[0] = static_cast<byte>(s & 0x00ff);
buf[1] = static_cast<byte>((s & 0xff00) >> 8);
}
else {
buf[0] = static_cast<byte>((s & 0xff00) >> 8);
buf[1] = static_cast<byte>(s & 0x00ff);
}
return 2;
}
long l2Data(byte* buf, int32_t l, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
buf[0] = static_cast<byte>(l & 0x000000ff);
buf[1] = static_cast<byte>((l & 0x0000ff00) >> 8);
buf[2] = static_cast<byte>((l & 0x00ff0000) >> 16);
buf[3] = static_cast<byte>((l & 0xff000000) >> 24);
}
else {
buf[0] = static_cast<byte>((l & 0xff000000) >> 24);
buf[1] = static_cast<byte>((l & 0x00ff0000) >> 16);
buf[2] = static_cast<byte>((l & 0x0000ff00) >> 8);
buf[3] = static_cast<byte>(l & 0x000000ff);
}
return 4;
}
long r2Data(byte* buf, Rational l, ByteOrder byteOrder)
{
long o = l2Data(buf, l.first, byteOrder);
o += l2Data(buf+o, l.second, byteOrder);
return o;
}
long f2Data(byte* buf, float f, ByteOrder byteOrder)
{
// This algorithm assumes that the internal representation of the float
// type is the 4-byte IEEE 754 binary32 format, which is common but not
// required by the C++ standard.
static_assert(sizeof(float) == 4, "float type requires 4-byte IEEE 754 binary32 format");
union {
uint32_t ul_;
float f_;
} u;
u.f_ = f;
return ul2Data(buf, u.ul_, byteOrder);
}
long d2Data(byte* buf, double d, ByteOrder byteOrder)
{
// This algorithm assumes that the internal representation of the double
// type is the 8-byte IEEE 754 binary64 format, which is common but not
// required by the C++ standard.
static_assert(sizeof(double) == 8, "double type requires 8-byte IEEE 754 binary64 format");
union {
uint64_t ull_;
double d_;
} u;
u.d_ = d;
uint64_t m = 0xff;
if (byteOrder == littleEndian) {
buf[0] = static_cast<byte>(u.ull_ & m);
buf[1] = static_cast<byte>((u.ull_ & (m << 8)) >> 8);
buf[2] = static_cast<byte>((u.ull_ & (m << 16)) >> 16);
buf[3] = static_cast<byte>((u.ull_ & (m << 24)) >> 24);
buf[4] = static_cast<byte>((u.ull_ & (m << 32)) >> 32);
buf[5] = static_cast<byte>((u.ull_ & (m << 40)) >> 40);
buf[6] = static_cast<byte>((u.ull_ & (m << 48)) >> 48);
buf[7] = static_cast<byte>((u.ull_ & (m << 56)) >> 56);
}
else {
buf[0] = static_cast<byte>((u.ull_ & (m << 56)) >> 56);
buf[1] = static_cast<byte>((u.ull_ & (m << 48)) >> 48);
buf[2] = static_cast<byte>((u.ull_ & (m << 40)) >> 40);
buf[3] = static_cast<byte>((u.ull_ & (m << 32)) >> 32);
buf[4] = static_cast<byte>((u.ull_ & (m << 24)) >> 24);
buf[5] = static_cast<byte>((u.ull_ & (m << 16)) >> 16);
buf[6] = static_cast<byte>((u.ull_ & (m << 8)) >> 8);
buf[7] = static_cast<byte>(u.ull_ & m);
}
return 8;
}
void hexdump(std::ostream& os, const byte* buf, long len, long offset)
{
const std::string::size_type pos = 8 + 16 * 3 + 2;
const std::string align(pos, ' ');
std::ios::fmtflags f( os.flags() );
long i = 0;
while (i < len) {
os << " "
<< std::setw(4) << std::setfill('0') << std::hex
<< i + offset << " ";
std::ostringstream ss;
do {
byte c = buf[i];
os << std::setw(2) << std::setfill('0') << std::right << std::hex << static_cast<int>(c) << " ";
ss << (static_cast<int>(c) >= 31 && static_cast<int>(c) < 127 ? char(buf[i]) : '.');
} while (++i < len && i%16 != 0);
std::string::size_type width = 9 + ((i-1)%16 + 1) * 3;
os << (width > pos ? "" : align.substr(width)) << ss.str() << "\n";
}
os << std::dec << std::setfill(' ');
os.flags(f);
} // hexdump
bool isHex(const std::string& str, size_t size, const std::string& prefix)
{
if ( str.size() <= prefix.size()
|| str.substr(0, prefix.size()) != prefix) return false;
if ( size > 0
&& str.size() != size + prefix.size()) return false;
for (size_t i = prefix.size(); i < str.size(); ++i) {
if (!isxdigit(str[i])) return false;
}
return true;
} // isHex
int exifTime(const char* buf, struct tm* tm)
{
assert(buf != nullptr);
assert(tm != nullptr);
int rc = 1;
int year, mon, mday, hour, min, sec;
int scanned = std::sscanf(buf, "%4d:%2d:%2d %2d:%2d:%2d",
&year, &mon, &mday, &hour, &min, &sec);
if (scanned == 6) {
tm->tm_year = year - 1900;
tm->tm_mon = mon - 1;
tm->tm_mday = mday;
tm->tm_hour = hour;
tm->tm_min = min;
tm->tm_sec = sec;
rc = 0;
}
return rc;
} // exifTime
const char* exvGettext(const char* str)
{
#ifdef EXV_ENABLE_NLS
return _exvGettext(str);
#else
return str;
#endif
}
std::string ws2s(const std::wstring& s)
{
#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW__)
const int slength = (int)s.length() + 1;
const int len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), slength, 0, 0, 0, 0);
// conversion failed => return an empty string
if (len == -1) {
return std::string("");
}
char* buf = new char[len];
WideCharToMultiByte(CP_UTF8, 0, s.c_str(), slength, buf, len, 0, 0);
std::string r(buf);
delete[] buf;
return r;
#else // Windows
std::mbstate_t state = std::mbstate_t();
const wchar_t* wstr = s.c_str();
const std::size_t converted_length = std::wcsrtombs(nullptr, &wstr, 0, &state);
// conversion failed => return an empty string
if (converted_length == static_cast<std::size_t>(-1)) {
return std::string("");
}
// create a string large enough to store the result + \0
std::string result(converted_length + 1, 0);
const std::size_t actual_conversion_result = std::wcsrtombs(&result[0], &wstr, result.size(), &state);
#ifdef NDEBUG
UNUSED(actual_conversion_result);
#else
assert(actual_conversion_result == converted_length);
#endif
return result;
#endif // Windows
}
std::wstring s2ws(const std::string& s)
{
#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW__)
const int slength = (int)s.length() + 1;
const int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, 0, 0);
// conversion failed => return an empty string
if (len == 0) {
return std::wstring(L"");
}
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
#else // Windows
std::mbstate_t state = std::mbstate_t();
const char* str = s.c_str();
const std::size_t converted_length = std::mbsrtowcs(nullptr, &str, 0, &state);
// conversion failed => return an empty string
if (converted_length == static_cast<std::size_t>(-1)) {
return std::wstring(L"");
}
// create a string large enough to store the result + L'\0'
std::wstring result(converted_length + 1, 0);
const std::size_t actual_conversion_result = std::mbsrtowcs(&result[0], &str, result.size(), &state);
#ifdef NDEBUG
UNUSED(actual_conversion_result);
#else
assert(actual_conversion_result == converted_length);
#endif
return result;
#endif // Windows
}
template<>
bool stringTo<bool>(const std::string& s, bool& ok)
{
std::string lcs(s); /* lowercase string */
for(unsigned i = 0; i < lcs.length(); i++) {
lcs[i] = std::tolower(s[i]);
}
/* handle the same values as xmp sdk */
if (lcs == "false" || lcs == "f" || lcs == "0") {
ok = true;
return false;
}
if (lcs == "true" || lcs == "t" || lcs == "1") {
ok = true;
return true;
}
ok = false;
return false;
}
long parseLong(const std::string& s, bool& ok)
{
long ret = stringTo<long>(s, ok);
if (ok) return ret;
auto f = stringTo<float>(s, ok);
if (ok) return static_cast<long>(f);
auto r = stringTo<Rational>(s, ok);
if (ok) {
if (r.second == 0) {
ok = false;
return 0;
}
return static_cast<long>(static_cast<float>(r.first) / r.second);
}
bool b = stringTo<bool>(s, ok);
if (ok) return b ? 1 : 0;
// everything failed, return from stringTo<long> is probably the best fit
return ret;
}
float parseFloat(const std::string& s, bool& ok)
{
auto ret = stringTo<float>(s, ok);
if (ok) return ret;
auto r = stringTo<Rational>(s, ok);
if (ok) {
if (r.second == 0) {
ok = false;
return 0.0;
}
return static_cast<float>(r.first) / r.second;
}
bool b = stringTo<bool>(s, ok);
if (ok)
return b ? 1.0F : 0.0F;
// everything failed, return from stringTo<float> is probably the best fit
return ret;
}
Rational parseRational(const std::string& s, bool& ok)
{
auto ret = stringTo<Rational>(s, ok);
if (ok) return ret;
long l = stringTo<long>(s, ok);
if (ok)
return {l, 1};
auto f = stringTo<float>(s, ok);
if (ok) return floatToRationalCast(f);
bool b = stringTo<bool>(s, ok);
if (ok) return {b ? 1 : 0, 1};
// everything failed, return from stringTo<Rational> is probably the best fit
return ret;
}
Rational floatToRationalCast(float f)
{
if (!std::isfinite(f)) {
return {f > 0 ? 1 : -1, 0};
}
// Beware: primitive conversion algorithm
int32_t den = 1000000;
const long f_as_long = static_cast<long>(f);
if (Safe::abs(f_as_long) > 2147) {
den = 10000;
}
if (Safe::abs(f_as_long) > 214748) {
den = 100;
}
if (Safe::abs(f_as_long) > 21474836) {
den = 1;
}
const float rnd = f >= 0 ? 0.5F : -0.5F;
const auto nom = static_cast<int32_t>(f * den + rnd);
const int32_t g = gcd(nom, den);
return {nom / g, den / g};
}
} // namespace Exiv2
#ifdef EXV_ENABLE_NLS
namespace
{
bool exvGettextInitialized = false;
std::mutex exvGettextInitializedMutex;
} // namespace
// Declaration is in i18n.h
const char* _exvGettext(const char* str)
{
// hold the mutex only as long as necessary
{
std::lock_guard<std::mutex> lock(exvGettextInitializedMutex);
if (!exvGettextInitialized) {
// bindtextdomain(EXV_PACKAGE_NAME, EXV_LOCALEDIR);
const std::string localeDir = EXV_LOCALEDIR[0] == '/' ? EXV_LOCALEDIR : (Exiv2::getProcessPath() + EXV_SEPARATOR_STR + EXV_LOCALEDIR);
bindtextdomain(EXV_PACKAGE_NAME, localeDir.c_str());
#ifdef EXV_HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset(EXV_PACKAGE_NAME, "UTF-8");
#endif
exvGettextInitialized = true;
}
}
return dgettext(EXV_PACKAGE_NAME, str);
}
#endif // EXV_ENABLE_NLS