1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42#ifndef AI_OGREPARSINGUTILS_H_INC
43#define AI_OGREPARSINGUTILS_H_INC
44
45#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
46
47#include "ParsingUtils.h"
48#include <functional>
49#include <algorithm>
50#include <stdint.h>
51#include <sstream>
52#include <cctype>
53
54namespace Assimp
55{
56namespace Ogre
57{
58
59/// Returns a lower cased copy of @s.
60static inline std::string ToLower(std::string s)
61{
62 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
63 return s;
64}
65
66/// Returns if @c s ends with @c suffix. If @c caseSensitive is false, both strings will be lower cased before matching.
67static inline bool EndsWith(const std::string &s, const std::string &suffix, bool caseSensitive = true)
68{
69 if (s.empty() || suffix.empty())
70 {
71 return false;
72 }
73 else if (s.length() < suffix.length())
74 {
75 return false;
76 }
77
78 if (!caseSensitive) {
79 return EndsWith(ToLower(s), ToLower(suffix), true);
80 }
81
82 size_t len = suffix.length();
83 std::string sSuffix = s.substr(s.length()-len, len);
84 return (ASSIMP_stricmp(sSuffix, suffix) == 0);
85}
86
87// Below trim functions adapted from http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
88
89/// Trim from start
90static inline std::string &TrimLeft(std::string &s, bool newlines = true)
91{
92 if (!newlines)
93 {
94 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char c) { return !Assimp::IsSpace<char>(c); }));
95 }
96 else
97 {
98 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char c) { return !Assimp::IsSpaceOrNewLine<char>(c); }));
99 }
100 return s;
101}
102
103/// Trim from end
104static inline std::string &TrimRight(std::string &s, bool newlines = true)
105{
106 if (!newlines)
107 {
108 s.erase(std::find_if(s.rbegin(), s.rend(), [](char c) { return !Assimp::IsSpace<char>(c); }).base(),s.end());
109 }
110 else
111 {
112 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char c) { return !Assimp::IsSpaceOrNewLine<char>(c); }));
113 }
114 return s;
115}
116
117/// Trim from both ends
118static inline std::string &Trim(std::string &s, bool newlines = true)
119{
120 return TrimLeft(TrimRight(s, newlines), newlines);
121}
122
123/// Skips a line from current @ss position until a newline. Returns the skipped part.
124static inline std::string SkipLine(std::stringstream &ss)
125{
126 std::string skipped;
127 getline(ss, skipped);
128 return skipped;
129}
130
131/// Skips a line and reads next element from @c ss to @c nextElement.
132/** @return Skipped line content until newline. */
133static inline std::string NextAfterNewLine(std::stringstream &ss, std::string &nextElement)
134{
135 std::string skipped = SkipLine(ss);
136 ss >> nextElement;
137 return skipped;
138}
139
140} // Ogre
141} // Assimp
142
143#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
144#endif // AI_OGREPARSINGUTILS_H_INC
145