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
14copyright notice, this list of conditions and the
15following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18copyright notice, this list of conditions and the
19following disclaimer in the documentation and/or other
20materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23contributors may be used to endorse or promote products
24derived from this software without specific prior
25written 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#ifndef INCLUDED_AI_STRINGUTILS_H
42#define INCLUDED_AI_STRINGUTILS_H
43
44#include <sstream>
45#include <stdarg.h>
46#include <cstdlib>
47
48/// @fn ai_snprintf
49/// @brief The portable version of the function snprintf ( C99 standard ), which works on visual studio compilers 2013 and earlier.
50/// @param outBuf The buffer to write in
51/// @param size The buffer size
52/// @param format The format string
53/// @param ap The additional arguments.
54/// @return The number of written characters if the buffer size was big enough. If an encoding error occurs, a negative number is returned.
55#if defined(_MSC_VER) && _MSC_VER < 1900
56
57 inline int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
58 int count(-1);
59 if (0 != size) {
60 count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
61 }
62 if (count == -1) {
63 count = _vscprintf(format, ap);
64 }
65
66 return count;
67 }
68
69 inline int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
70 int count;
71 va_list ap;
72
73 va_start(ap, format);
74 count = c99_ai_vsnprintf(outBuf, size, format, ap);
75 va_end(ap);
76
77 return count;
78 }
79
80#else
81# define ai_snprintf snprintf
82#endif
83
84template <typename T>
85inline
86std::string to_string( T value ) {
87 std::ostringstream os;
88 os << value;
89 return os.str();
90}
91
92inline
93float ai_strtof( const char *begin, const char *end ) {
94 if ( nullptr == begin ) {
95 return 0.0f;
96 }
97 float val( 0.0f );
98 if ( nullptr == end ) {
99 val = static_cast< float >( ::atof( begin ) );
100 } else {
101 std::string::size_type len( end - begin );
102 std::string token( begin, len );
103 val = static_cast< float >( ::atof( token.c_str() ) );
104 }
105
106 return val;
107}
108
109#endif // INCLUDED_AI_STRINGUTILS_H
110
111