/src/assimp/code/AssetLib/VRML/VrmlConverter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2025, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | /// \file VrmlImporter.cpp |
42 | | /// \brief Convert VRML-formatted (.wrl, .x3dv) files to X3D .xml format |
43 | | /// \date 2024 |
44 | | /// \author tellypresence |
45 | | |
46 | | #ifndef ASSIMP_BUILD_NO_X3D_IMPORTER |
47 | | |
48 | | #include <memory> // std::unique_ptr |
49 | | #include "VrmlConverter.hpp" |
50 | | |
51 | | namespace Assimp { |
52 | | |
53 | 0 | bool isFileWrlVrml97Ext(const std::string &pFile) { |
54 | 0 | size_t pos = pFile.find_last_of('.'); |
55 | 0 | if (pos == std::string::npos) { |
56 | 0 | return false; |
57 | 0 | } |
58 | 0 | std::string ext = pFile.substr(pos + 1); |
59 | 0 | if (ext.size() != 3) { |
60 | 0 | return false; |
61 | 0 | } |
62 | 0 | return (ext[0] == 'w' || ext[0] == 'W') && (ext[1] == 'r' || ext[1] == 'R') && (ext[2] == 'l' || ext[2] == 'L'); |
63 | 0 | } |
64 | | |
65 | 0 | bool isFileX3dvClassicVrmlExt(const std::string &pFile) { |
66 | 0 | size_t pos = pFile.find_last_of('.'); |
67 | 0 | if (pos == std::string::npos) { |
68 | 0 | return false; |
69 | 0 | } |
70 | 0 | std::string ext = pFile.substr(pos + 1); |
71 | 0 | if (ext.size() != 4) { |
72 | 0 | return false; |
73 | 0 | } |
74 | 0 | return (ext[0] == 'x' || ext[0] == 'X') && (ext[1] == '3') && (ext[2] == 'd' || ext[2] == 'D') && (ext[3] == 'v' || ext[3] == 'V'); |
75 | 0 | } |
76 | | |
77 | | #if !defined(ASSIMP_BUILD_NO_VRML_IMPORTER) |
78 | | static VrmlTranslator::Scanner createScanner(const std::string &pFile) { |
79 | | std::unique_ptr<wchar_t[]> wide_stringPtr{ new wchar_t[ pFile.length() + 1 ] }; |
80 | | std::copy(pFile.begin(), pFile.end(), wide_stringPtr.get()); |
81 | | wide_stringPtr[ pFile.length() ] = 0; |
82 | | |
83 | | return VrmlTranslator::Scanner(wide_stringPtr.get()); |
84 | | } // wide_stringPtr auto-deleted when leaving scope |
85 | | #endif // #if !defined(ASSIMP_BUILD_NO_VRML_IMPORTER) |
86 | | |
87 | 0 | std::stringstream ConvertVrmlFileToX3dXmlFile(const std::string &pFile) { |
88 | 0 | std::stringstream ss; |
89 | 0 | if (isFileWrlVrml97Ext(pFile) || isFileX3dvClassicVrmlExt(pFile)) { |
90 | | #if !defined(ASSIMP_BUILD_NO_VRML_IMPORTER) |
91 | | VrmlTranslator::Scanner scanner = createScanner(pFile); |
92 | | VrmlTranslator::Parser parser(&scanner); |
93 | | parser.Parse(); |
94 | | ss.str(""); |
95 | | parser.doc_.save(ss); |
96 | | #endif // #if !defined(ASSIMP_BUILD_NO_VRML_IMPORTER) |
97 | 0 | } |
98 | 0 | return ss; |
99 | 0 | } |
100 | | |
101 | | } // namespace Assimp |
102 | | |
103 | | #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER |