1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <Standard_Dump.hxx>
18 // =======================================================================
19 // function : Constructor
21 // =======================================================================
22 Standard_DumpSentry::Standard_DumpSentry (Standard_OStream& theOStream, const char* theClassName)
23 : myOStream (&theOStream)
25 (*myOStream) << "\"" << theClassName << "\": {";
28 // =======================================================================
29 // function : Destructor
31 // =======================================================================
32 Standard_DumpSentry::~Standard_DumpSentry()
37 // =======================================================================
38 // function : EndsWith
40 // =======================================================================
41 Standard_Boolean Standard_Dump::EndsWith (const Standard_OStream& theOStream,
42 const TCollection_AsciiString& theEndString)
44 Standard_SStream aStream;
45 aStream << theOStream.rdbuf();
46 TCollection_AsciiString aStreamStr = Standard_Dump::Text (aStream);
47 return aStreamStr.EndsWith (theEndString);
50 //=======================================================================
51 //function : DumpKeyToClass
53 //=======================================================================
54 void Standard_Dump::DumpKeyToClass (Standard_OStream& theOStream,
56 const TCollection_AsciiString& theField)
58 if (!Standard_Dump::EndsWith (theOStream, "{"))
60 theOStream << "\"" << theKey << "\": {" << theField << "}";
63 //=======================================================================
64 //function : DumpCharacterValues
66 //=======================================================================
67 void Standard_Dump::DumpCharacterValues (Standard_OStream& theOStream, int theCount, ...)
70 va_start(vl, theCount);
71 for(int i = 0; i < theCount; ++i)
75 theOStream << "\"" << va_arg(vl, char*) << "\"";
80 //=======================================================================
81 //function : DumpRealValues
83 //=======================================================================
84 void Standard_Dump::DumpRealValues (Standard_OStream& theOStream, int theCount, ...)
87 va_start(vl, theCount);
88 for(int i = 0; i < theCount; ++i)
92 theOStream << va_arg(vl, Standard_Real);
97 // =======================================================================
98 // function : GetPointerInfo
100 // =======================================================================
101 TCollection_AsciiString Standard_Dump::GetPointerInfo (const Handle(Standard_Transient)& thePointer,
102 const bool isShortInfo)
104 if (thePointer.IsNull())
105 return TCollection_AsciiString();
107 return GetPointerInfo (thePointer.get(), isShortInfo);
110 // =======================================================================
111 // function : GetPointerInfo
113 // =======================================================================
114 TCollection_AsciiString Standard_Dump::GetPointerInfo (const void* thePointer, const bool isShortInfo)
116 std::ostringstream aPtrStr;
117 aPtrStr << thePointer;
119 return aPtrStr.str().c_str();
121 TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
122 for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
124 if (anInfoPtr.Value(aSymbolId) != '0')
126 anInfoPtr = anInfoPtr.SubString (aSymbolId, anInfoPtr.Length());
127 anInfoPtr.Prepend (GetPointerPrefix());
131 return aPtrStr.str().c_str();
134 // =======================================================================
136 // =======================================================================
137 void Standard_Dump::DumpFieldToName (const char* theField, const char*& theName)
141 if (theName[0] == '&')
143 theName = theName + 1;
145 if (::LowerCase (theName[0]) == 'm' && theName[1] == 'y')
147 theName = theName + 2;
151 // =======================================================================
153 // =======================================================================
154 TCollection_AsciiString Standard_Dump::Text (const Standard_SStream& theStream)
156 return TCollection_AsciiString (theStream.str().c_str());
159 // =======================================================================
161 // =======================================================================
162 TCollection_AsciiString Standard_Dump::FormatJson (const Standard_SStream& theStream,
163 const Standard_Integer theIndent)
165 TCollection_AsciiString aStreamStr = Text (theStream);
166 TCollection_AsciiString anIndentStr;
167 for (Standard_Integer anIndentId = 0; anIndentId < theIndent; anIndentId++)
168 anIndentStr.AssignCat (' ');
170 TCollection_AsciiString aText;
172 Standard_Integer anIndentCount = 0;
173 Standard_Boolean isMassiveValues = Standard_False;
174 for (Standard_Integer anIndex = 1; anIndex < aStreamStr.Length(); anIndex++)
176 Standard_Character aSymbol = aStreamStr.Value (anIndex);
184 for (int anIndent = 0; anIndent < anIndentCount; anIndent++)
185 aText += anIndentStr;
187 else if (aSymbol == '}')
192 for (int anIndent = 0; anIndent < anIndentCount; anIndent++)
193 aText += anIndentStr;
196 else if (aSymbol == '[')
198 isMassiveValues = Standard_True;
201 else if (aSymbol == ']')
203 isMassiveValues = Standard_False;
206 else if (aSymbol == ',')
208 if (!isMassiveValues)
212 for (int anIndent = 0; anIndent < anIndentCount; anIndent++)
213 aText += anIndentStr;
214 if (anIndex + 1 < aStreamStr.Length() && aStreamStr.Value (anIndex + 1) == ' ')
215 anIndex++; // skip empty value after comma