forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileIO_Common.cpp
908 lines (824 loc) · 27.3 KB
/
fileIO_Common.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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
//
// FileIO_Common.cpp
#include "pxr/pxr.h"
#include "pxr/usd/sdf/fileIO.h"
#include "pxr/usd/sdf/fileIO_Common.h"
#include "pxr/base/tf/stringUtils.h"
#include <cctype>
using std::map;
using std::ostream;
using std::string;
using std::vector;
PXR_NAMESPACE_OPEN_SCOPE
static const char *_IndentString = " ";
// Check if 'cp' points to a valid UTF-8 multibyte sequence. If so, return its
// length (either 2, 3, or 4). If not return 0.
static inline int
_IsUTF8MultiByte(char const *cp) {
// Return a byte with the high `n` bits set, rest clear.
auto highBits = [](int n) {
return static_cast<unsigned char>(((1 << n) - 1) << (8 - n));
};
// Return true if `ch` is a continuation byte.
auto isContinuation = [&highBits](unsigned char ch) {
return (ch & highBits(2)) == highBits(1);
};
// Check for 2, 3, or 4-byte code.
for (int i = 2; i <= 4; ++i) {
// This is an N-byte code if the high-order N+1 bytes are N 1s
// followed by a single 0.
if ((*cp & highBits(i + 1)) == highBits(i)) {
// If that's the case then the following N-1 bytes must be
// "continuation bytes".
for (int j = 1; j != i; ++j) {
if (!isContinuation(cp[j])) {
return 0;
}
}
return i;
}
}
return 0;
}
// Return true if 'ch' is a printable ASCII character, independent of the
// current locale.
static inline bool
_IsASCIIPrintable(unsigned char ch)
{
// Locale-independent ascii printable is 32-126.
return 32 <= ch && ch <= 126;
}
// Append 'ch' to 'out' as an escaped 2-digit hex code (e.g. \x3f).
static inline void
_WriteHexEscape(unsigned char ch, string *out)
{
const char* hexdigit = "0123456789abcdef";
char buf[] = "\\x__";
buf[2] = hexdigit[(ch >> 4) & 15];
buf[3] = hexdigit[ch & 15];
out->append(buf);
}
// Helper for creating string representation of an asset path. Caller is
// assumed to have validated \p assetPath (e.g. by having obtained it from an
// SdfAssetPath, SdfReference, or SdfPayload).
static string
_StringFromAssetPath(const string& assetPath)
{
// See Sdf_EvalAssetPath for the code that reads asset paths at parse time.
// We want to avoid writing asset paths with escape sequences in them so
// that it's easy for users to copy and paste these paths into other apps
// without having to clean up those escape sequences, and so that asset
// resolvers are as free as possible to determine their own syntax.
//
// We use "@"s as delimiters so that asset paths are easily identifiable.
// but use "@@@" if the path already has an "@" in it rather than escaping
// it. If the path has a "@@@", then we'll escape that, but hopefully that's
// a rarer case.
const char delim = '@';
bool useTripleDelim = assetPath.find(delim) != std::string::npos;
string s;
s.reserve(assetPath.size() + (useTripleDelim ? 6 : 2));
s.append(useTripleDelim ? 3 : 1, delim);
for (char const *cp = assetPath.c_str(); *cp; ++cp) {
// If we're using triple delimiters and we encounter a triple delimiter
// in the asset path, we must escape it.
if (useTripleDelim &&
cp[0] == delim && cp[1] == delim && cp[2] == delim) {
s.push_back('\\');
s.append(3, delim);
cp += 2; // account for next loop increment.
continue;
}
// Otherwise we can just emit the bytes since callers are required to
// have validated the asset path content.
s.push_back(*cp);
}
// Tack on the final delimiter.
s.append(useTripleDelim ? 3 : 1, delim);
return s;
}
static string
_StringFromValue(const string& s)
{
return Sdf_FileIOUtility::Quote(s);
}
static string
_StringFromValue(const TfToken& s)
{
return Sdf_FileIOUtility::Quote(s);
}
static string
_StringFromValue(const SdfAssetPath& assetPath)
{
return _StringFromAssetPath(assetPath.GetAssetPath());
}
template <class T>
static void
_StringFromVtArray(
string *valueStr,
const VtArray<T> &valArray)
{
valueStr->append("[");
if (typename VtArray<T>::const_pointer d = valArray.cdata()) {
if (const size_t n = valArray.size()) {
valueStr->append(_StringFromValue(d[0]));
for (size_t i = 1; i != n; ++i) {
valueStr->append(", ");
valueStr->append(_StringFromValue(d[i]));
}
}
}
valueStr->append("]");
}
// Helper for creating strings for VtValues holding certain types
// that can't use TfStringify, and arrays of those types.
template <class T>
static bool
_StringFromVtValueHelper(string* valueStr, const VtValue& value)
{
if (value.IsHolding<T>()) {
*valueStr = _StringFromValue(value.UncheckedGet<T>());
return true;
}
else if (value.IsHolding<VtArray<T> >()) {
const VtArray<T>& valArray = value.UncheckedGet<VtArray<T> >();
_StringFromVtArray(valueStr,valArray);
return true;
}
return false;
}
// ------------------------------------------------------------
// Helpers functions for writing SdfListOp<T>. Consumers can
// specialize the _ListOpWriter struct for custom behavior based
// on the element type of the list op.
namespace
{
template <class T>
struct _ListOpWriter
{
static constexpr bool ItemPerLine = false;
static constexpr bool SingleItemRequiresBrackets(const T& item)
{
return true;
}
static void Write(Sdf_TextOutput& out, size_t indent, const T& item)
{
Sdf_FileIOUtility::Write(out, indent, "%s", TfStringify(item).c_str());
}
};
template <>
struct _ListOpWriter<string>
{
static constexpr bool ItemPerLine = false;
static constexpr bool SingleItemRequiresBrackets(const string& s)
{
return true;
}
static void Write(Sdf_TextOutput& out, size_t indent, const string& s)
{
Sdf_FileIOUtility::WriteQuotedString(out, indent, s);
}
};
template <>
struct _ListOpWriter<TfToken>
{
static constexpr bool ItemPerLine = false;
static constexpr bool SingleItemRequiresBrackets(const TfToken& s)
{
return true;
}
static void Write(Sdf_TextOutput& out, size_t indent, const TfToken& s)
{
Sdf_FileIOUtility::WriteQuotedString(out, indent, s.GetString());
}
};
template <>
struct _ListOpWriter<SdfPath>
{
static constexpr bool ItemPerLine = true;
static constexpr bool SingleItemRequiresBrackets(const SdfPath& path)
{
return false;
}
static void Write(Sdf_TextOutput& out, size_t indent, const SdfPath& path)
{
Sdf_FileIOUtility::WriteSdfPath(out, indent, path);
}
};
template <>
struct _ListOpWriter<SdfReference>
{
static constexpr bool ItemPerLine = true;
static bool SingleItemRequiresBrackets(const SdfReference& ref)
{
return !ref.GetCustomData().empty();
}
static void Write(
Sdf_TextOutput& out, size_t indent, const SdfReference& ref)
{
bool multiLineRefMetaData = !ref.GetCustomData().empty();
Sdf_FileIOUtility::Write(out, indent, "");
if (!ref.GetAssetPath().empty()) {
Sdf_FileIOUtility::WriteAssetPath(out, 0, ref.GetAssetPath());
if (!ref.GetPrimPath().IsEmpty())
Sdf_FileIOUtility::WriteSdfPath(out, 0, ref.GetPrimPath());
}
else {
// If this is an internal reference, we always have to write
// out a path, even if it's empty since that encodes a reference
// to the default prim.
Sdf_FileIOUtility::WriteSdfPath(out, 0, ref.GetPrimPath());
}
if (multiLineRefMetaData) {
Sdf_FileIOUtility::Puts(out, 0, " (\n");
}
Sdf_FileIOUtility::WriteLayerOffset(
out, indent+1, multiLineRefMetaData, ref.GetLayerOffset());
if (!ref.GetCustomData().empty()) {
Sdf_FileIOUtility::Puts(out, indent+1, "customData = ");
Sdf_FileIOUtility::WriteDictionary(
out, indent+1, /* multiline = */ true, ref.GetCustomData());
}
if (multiLineRefMetaData) {
Sdf_FileIOUtility::Puts(out, indent, ")");
}
}
};
template <>
struct _ListOpWriter<SdfPayload>
{
static constexpr bool ItemPerLine = true;
static bool SingleItemRequiresBrackets(const SdfPayload& payload)
{
return false;
}
static void Write(
Sdf_TextOutput& out, size_t indent, const SdfPayload& payload)
{
Sdf_FileIOUtility::Write(out, indent, "");
if (!payload.GetAssetPath().empty()) {
Sdf_FileIOUtility::WriteAssetPath(out, 0, payload.GetAssetPath());
if (!payload.GetPrimPath().IsEmpty())
Sdf_FileIOUtility::WriteSdfPath(out, 0, payload.GetPrimPath());
}
else {
// If this is an internal payload, we always have to write
// out a path, even if it's empty since that encodes a payload
// to the default prim.
Sdf_FileIOUtility::WriteSdfPath(out, 0, payload.GetPrimPath());
}
Sdf_FileIOUtility::WriteLayerOffset(
out, indent+1, false /*multiLineMetaData*/, payload.GetLayerOffset());
}
};
template <class ListOpList>
void
_WriteListOpList(
Sdf_TextOutput& out, size_t indent,
const string& name, const ListOpList& listOpList,
const string& op = string())
{
typedef _ListOpWriter<typename ListOpList::value_type> _Writer;
Sdf_FileIOUtility::Write(out, indent, "%s%s%s = ",
op.c_str(), op.empty() ? "" : " ", name.c_str());
if (listOpList.empty()) {
Sdf_FileIOUtility::Puts(out, 0, "None\n");
}
else if (listOpList.size() == 1 &&
!_Writer::SingleItemRequiresBrackets(listOpList.front())) {
_Writer::Write(out, 0, listOpList.front());
Sdf_FileIOUtility::Puts(out, 0, "\n");
}
else {
const bool itemPerLine = _Writer::ItemPerLine;
Sdf_FileIOUtility::Puts(out, 0, itemPerLine ? "[\n" : "[");
TF_FOR_ALL(it, listOpList) {
_Writer::Write(out, itemPerLine ? indent + 1 : 0, *it);
if (it.GetNext()) {
Sdf_FileIOUtility::Puts(out, 0, itemPerLine ? ",\n" : ", ");
}
else {
Sdf_FileIOUtility::Puts(out, 0, itemPerLine ? "\n" : "");
}
}
Sdf_FileIOUtility::Puts(out, itemPerLine ? indent : 0, "]\n");
}
}
template <class ListOp>
void
_WriteListOp(
Sdf_TextOutput &out, size_t indent,
const std::string& name, const ListOp& listOp)
{
if (listOp.IsExplicit()) {
_WriteListOpList(out, indent, name, listOp.GetExplicitItems());
}
else {
if (!listOp.GetDeletedItems().empty()) {
_WriteListOpList(out, indent, name,
listOp.GetDeletedItems(), "delete");
}
if (!listOp.GetAddedItems().empty()) {
_WriteListOpList(out, indent, name,
listOp.GetAddedItems(), "add");
}
if (!listOp.GetPrependedItems().empty()) {
_WriteListOpList(out, indent, name,
listOp.GetPrependedItems(), "prepend");
}
if (!listOp.GetAppendedItems().empty()) {
_WriteListOpList(out, indent, name,
listOp.GetAppendedItems(), "append");
}
if (!listOp.GetOrderedItems().empty()) {
_WriteListOpList(out, indent, name,
listOp.GetOrderedItems(), "reorder");
}
}
}
} // end anonymous namespace
// ------------------------------------------------------------
void
Sdf_FileIOUtility::Puts(
Sdf_TextOutput &out, size_t indent, const std::string &str)
{
for (size_t i = 0; i < indent; ++i) {
out.Write(_IndentString);
}
out.Write(str);
}
void
Sdf_FileIOUtility::Write(
Sdf_TextOutput& out, size_t indent, const char *fmt, ...)
{
for (size_t i = 0; i < indent; ++i) {
out.Write(_IndentString);
}
va_list ap;
va_start(ap, fmt);
out.Write(TfVStringPrintf(fmt, ap));
va_end(ap);
}
bool
Sdf_FileIOUtility::OpenParensIfNeeded(
Sdf_TextOutput &out, bool didParens, bool multiLine)
{
if (!didParens) {
Puts(out, 0, multiLine ? " (\n" : " (");
} else if (!multiLine) {
Puts(out, 0, "; ");
}
return true;
}
void
Sdf_FileIOUtility::CloseParensIfNeeded(
Sdf_TextOutput &out, size_t indent, bool didParens, bool multiLine)
{
if (didParens) {
Puts(out, multiLine ? indent : 0, ")");
}
}
void
Sdf_FileIOUtility::WriteQuotedString(
Sdf_TextOutput &out, size_t indent, const string &str)
{
Puts(out, indent, Quote(str));
}
void
Sdf_FileIOUtility::WriteAssetPath(
Sdf_TextOutput &out, size_t indent, const string &assetPath)
{
Puts(out, indent, _StringFromAssetPath(assetPath));
}
void
Sdf_FileIOUtility::WriteDefaultValue(
Sdf_TextOutput &out, size_t indent, VtValue value)
{
// Special case for SdfPath value types
if (value.IsHolding<SdfPath>()) {
WriteSdfPath(out, indent, value.Get<SdfPath>() );
return;
}
// We never write opaque values to layers; SetDefault and other high-level
// APIs should prevent us from ever having an opaque value set on an
// attribute, but low-level methods like SetField can still be used to sneak
// one in, so we guard against authoring them here as well.
if (value.IsHolding<SdfOpaqueValue>()) {
TF_CODING_ERROR("Tried to write opaque value to layer");
return;
}
// General case value to string conversion and write-out.
std::string valueString = Sdf_FileIOUtility::StringFromVtValue(value);
Sdf_FileIOUtility::Write(out, 0, " = %s", valueString.c_str());
}
void
Sdf_FileIOUtility::WriteSdfPath(
Sdf_TextOutput &out, size_t indent, const SdfPath &path)
{
Write(out, indent, "<%s>", path.GetString().c_str());
}
template <class StrType>
static bool
_WriteNameVector(Sdf_TextOutput &out, size_t indent, const vector<StrType> &vec)
{
size_t i, c = vec.size();
if (c>1) {
Sdf_FileIOUtility::Puts(out, 0, "[");
}
for (i=0; i<c; i++) {
if (i > 0) {
Sdf_FileIOUtility::Puts(out, 0, ", ");
}
Sdf_FileIOUtility::WriteQuotedString(out, 0, vec[i]);
}
if (c>1) {
Sdf_FileIOUtility::Puts(out, 0, "]");
}
return true;
}
bool
Sdf_FileIOUtility::WriteNameVector(
Sdf_TextOutput &out, size_t indent, const vector<string> &vec)
{
return _WriteNameVector(out, indent, vec);
}
bool
Sdf_FileIOUtility::WriteNameVector(
Sdf_TextOutput &out, size_t indent, const vector<TfToken> &vec)
{
return _WriteNameVector(out, indent, vec);
}
bool
Sdf_FileIOUtility::WriteTimeSamples(
Sdf_TextOutput &out, size_t indent, const SdfPropertySpec &prop)
{
VtValue timeSamplesVal = prop.GetField(SdfFieldKeys->TimeSamples);
if (timeSamplesVal.IsHolding<SdfTimeSampleMap>()) {
SdfTimeSampleMap samples =
timeSamplesVal.UncheckedGet<SdfTimeSampleMap>();
TF_FOR_ALL(i, samples) {
Write(out, indent+1, "%s: ", TfStringify(i->first).c_str());
if (i->second.IsHolding<SdfPath>()) {
WriteSdfPath(out, 0, i->second.Get<SdfPath>() );
} else {
Puts(out, 0, StringFromVtValue( i->second ));
}
Puts(out, 0, ",\n");
}
}
else if (timeSamplesVal.IsHolding<SdfHumanReadableValue>()) {
Write(out, indent+1, "%s\n",
TfStringify(timeSamplesVal.
UncheckedGet<SdfHumanReadableValue>()).c_str());
}
return true;
}
bool
Sdf_FileIOUtility::WriteRelocates(
Sdf_TextOutput &out, size_t indent, bool multiLine,
const SdfRelocatesMap &reloMap)
{
Write(out, indent, "relocates = %s", multiLine ? "{\n" : "{ ");
size_t itemCount = reloMap.size();
TF_FOR_ALL(it, reloMap) {
WriteSdfPath(out, indent+1, it->first);
Puts(out, 0, ": ");
WriteSdfPath(out, 0, it->second);
if (--itemCount > 0) {
Puts(out, 0, ", ");
}
if (multiLine) {
Puts(out, 0, "\n");
}
}
if (multiLine) {
Puts(out, indent, "}\n");
}
else {
Puts(out, 0, " }");
}
return true;
}
void
Sdf_FileIOUtility::_WriteDictionary(
Sdf_TextOutput &out, size_t indent, bool multiLine,
Sdf_FileIOUtility::_OrderedDictionary &dictionary,
bool stringValuesOnly)
{
Puts(out, 0, multiLine ? "{\n" : "{ ");
size_t counter = dictionary.size();
TF_FOR_ALL(i, dictionary) {
counter--;
const VtValue &value = *i->second;
if (stringValuesOnly) {
if (value.IsHolding<std::string>()) {
WriteQuotedString(out, multiLine ? indent+1 : 0, *(i->first));
Write(out, 0, ": ");
WriteQuotedString(out, 0, value.Get<string>());
if (counter > 0) {
Puts(out, 0, ", ");
}
if (multiLine) {
Puts(out, 0, "\n");
}
} else {
// CODE_COVERAGE_OFF
// This is not possible to hit with the current public API.
TF_RUNTIME_ERROR("Dictionary has a non-string value under key "
"\"%s\"; skipping", i->first->c_str());
// CODE_COVERAGE_ON
}
} else {
// Put quotes around the keyName if it is not a valid identifier
string keyName = *(i->first);
if (!TfIsValidIdentifier(keyName)) {
keyName = "\"" + keyName + "\"";
}
if (value.IsHolding<VtDictionary>()) {
Write(out, multiLine ? indent+1 : 0, "dictionary %s = ",
keyName.c_str());
const VtDictionary &nestedDictionary =
value.Get<VtDictionary>();
Sdf_FileIOUtility::_OrderedDictionary newDictionary;
TF_FOR_ALL(it, nestedDictionary) {
newDictionary[&it->first] = &it->second;
}
_WriteDictionary(out, indent+1, multiLine, newDictionary,
/* stringValuesOnly = */ false );
} else {
const TfToken& typeName =
SdfValueTypeNames->GetSerializationName(value);
Write(out, multiLine ? indent+1 : 0, "%s %s = ",
typeName.GetText(), keyName.c_str());
// XXX: The logic here is very similar to that in
// WriteDefaultValue. WBN to refactor.
string str;
if (_StringFromVtValueHelper<string>(&str, value) ||
_StringFromVtValueHelper<TfToken>(&str, value) ||
_StringFromVtValueHelper<SdfAssetPath>(&str, value)) {
Puts(out, 0, str);
} else {
Puts(out, 0, TfStringify(value));
}
if (multiLine) {
Puts(out, 0, "\n");
}
}
}
if (!multiLine && counter > 0) {
// CODE_COVERAGE_OFF
// See multiLine comment below.
Puts(out, 0, "; ");
// CODE_COVERAGE_ON
}
}
if (multiLine) {
Puts(out, indent, "}\n");
} else {
// CODE_COVERAGE_OFF
// Not currently hittable from public API.
Puts(out, 0, " }");
// CODE_COVERAGE_ON
}
}
void
Sdf_FileIOUtility::WriteDictionary(
Sdf_TextOutput &out, size_t indent, bool multiLine,
const VtDictionary &dictionary, bool stringValuesOnly)
{
// Make sure the dictionary keys are written out in order.
_OrderedDictionary newDictionary;
TF_FOR_ALL(it, dictionary) {
newDictionary[&it->first] = &it->second;
}
_WriteDictionary(out, indent, multiLine, newDictionary, stringValuesOnly);
}
template <class T>
void
Sdf_FileIOUtility::WriteListOp(
Sdf_TextOutput &out, size_t indent, const TfToken& fieldName,
const SdfListOp<T>& listOp)
{
_WriteListOp(out, indent, fieldName, listOp);
}
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfPathListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfPayloadListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfReferenceListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfIntListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfInt64ListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfUIntListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfUInt64ListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfStringListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfTokenListOp&);
template void
Sdf_FileIOUtility::WriteListOp(Sdf_TextOutput&, size_t, const TfToken&,
const SdfUnregisteredValueListOp&);
void
Sdf_FileIOUtility::WriteLayerOffset(
Sdf_TextOutput &out, size_t indent, bool multiLine,
const SdfLayerOffset& layerOffset)
{
// If there's anything interesting to write, write it.
if (layerOffset != SdfLayerOffset()) {
if (!multiLine) {
Write(out, 0, " (");
}
double offset = layerOffset.GetOffset();
double scale = layerOffset.GetScale();
if (offset != 0.0) {
Write(out, multiLine ? indent : 0, "offset = %s%s",
TfStringify(offset).c_str(), multiLine ? "\n" : "");
}
if (scale != 1.0) {
if (!multiLine && offset != 0) {
Write(out, 0, "; ");
}
Write(out, multiLine ? indent : 0, "scale = %s%s",
TfStringify(scale).c_str(), multiLine ? "\n" : "");
}
if (!multiLine) {
Write(out, 0, ")");
}
}
}
string
Sdf_FileIOUtility::Quote(const string &str)
{
const bool allowTripleQuotes = true;
string result;
// Choose quotes, double quote preferred.
char quote = '"';
if (str.find('"') != string::npos && str.find('\'') == string::npos) {
quote = '\'';
}
// Open quote. Choose single or triple quotes.
bool tripleQuotes = false;
if (allowTripleQuotes) {
if (str.find('\n') != string::npos) {
tripleQuotes = true;
result += quote;
result += quote;
}
}
result += quote;
// Write `ch` as a regular ascii character, an escaped control character
// (like \n, \t, etc.) or a hex byte code (\xa8).
auto writeASCIIorHex = [&result, quote, tripleQuotes](char ch) {
switch (ch) {
case '\n':
// Pass newline as-is if using triple quotes, otherwise escape.
if (tripleQuotes) {
result += ch;
}
else {
result += "\\n";
}
break;
case '\r':
result += "\\r";
break;
case '\t':
result += "\\t";
break;
case '\\':
result += "\\\\";
break;
default:
if (ch == quote) {
// Always escape the character we're using for quoting.
result += '\\';
result += quote;
}
else if (!_IsASCIIPrintable(ch)) {
// Non-printable; use two digit hex form.
_WriteHexEscape(ch, &result);
}
else {
// Printable, non-special.
result += ch;
}
break;
}
};
// Escape string.
for (char const *i = str.c_str(); *i; ++i) {
// Check UTF-8 sequence.
int nBytes = _IsUTF8MultiByte(i);
if (nBytes) {
result.append(i, i + nBytes);
i += nBytes - 1; // account for next loop increment.
}
else {
writeASCIIorHex(*i);
}
}
// End quote.
result.append(tripleQuotes ? 3 : 1, quote);
return result;
}
string
Sdf_FileIOUtility::Quote(const TfToken &token)
{
return Quote(token.GetString());
}
string
Sdf_FileIOUtility::StringFromVtValue(const VtValue &value)
{
string s;
if (_StringFromVtValueHelper<string>(&s, value) ||
_StringFromVtValueHelper<TfToken>(&s, value) ||
_StringFromVtValueHelper<SdfAssetPath>(&s, value)) {
return s;
}
if (value.IsHolding<char>()) {
return TfStringify(static_cast<int>(value.UncheckedGet<char>()));
} else if (value.IsHolding<unsigned char>()) {
return TfStringify(
static_cast<unsigned int>(value.UncheckedGet<unsigned char>()));
} else if (value.IsHolding<signed char>()) {
return TfStringify(
static_cast<int>(value.UncheckedGet<signed char>()));
}
return TfStringify(value);
}
const char* Sdf_FileIOUtility::Stringify( SdfPermission val )
{
switch(val) {
case SdfPermissionPublic:
return "public";
case SdfPermissionPrivate:
return "private";
default:
TF_CODING_ERROR("unknown value");
return "";
}
}
const char* Sdf_FileIOUtility::Stringify( SdfSpecifier val )
{
switch(val) {
case SdfSpecifierDef:
return "def";
case SdfSpecifierOver:
return "over";
case SdfSpecifierClass:
return "class";
default:
TF_CODING_ERROR("unknown value");
return "";
}
}
const char* Sdf_FileIOUtility::Stringify( SdfVariability val )
{
switch(val) {
case SdfVariabilityVarying:
// Empty string implies SdfVariabilityVarying
return "";
case SdfVariabilityUniform:
return "uniform";
default:
TF_CODING_ERROR("unknown value");
return "";
}
}
PXR_NAMESPACE_CLOSE_SCOPE