ovr_sdk

view LibOVR/Src/Kernel/OVR_String_FormatUtil.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line source
1 /************************************************************************************
3 Filename : OVR_String_FormatUtil.cpp
4 Content : String format functions.
5 Created : February 27, 2013
6 Notes :
8 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
10 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
15 You may obtain a copy of the License at
17 http://www.oculusvr.com/licenses/LICENSE-3.2
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
25 ************************************************************************************/
27 #include "OVR_String.h"
28 #include "OVR_Log.h"
30 namespace OVR {
32 void StringBuffer::AppendFormat(const char* format, ...)
33 {
34 va_list argList;
35 char buffer[512];
36 char* bufferUsed = buffer;
37 char* bufferAllocated = NULL;
39 va_start(argList, format);
41 #if !defined(OVR_CC_MSVC) // Non-Microsoft compilers require you to save a copy of the va_list.
42 va_list argListSaved;
43 va_copy(argListSaved, argList);
44 #endif
46 int requiredStrlen = OVR_vsnprintf(bufferUsed, OVR_ARRAY_COUNT(buffer), format, argList); // The large majority of the time this will succeed.
48 if(requiredStrlen >= (int)sizeof(buffer)) // If the initial capacity wasn't enough...
49 {
50 bufferAllocated = (char*)OVR_ALLOC(sizeof(char) * (requiredStrlen + 1));
51 bufferUsed = bufferAllocated;
52 if(bufferAllocated)
53 {
54 #if !defined(OVR_CC_MSVC)
55 va_end(argList);
56 va_copy(argList, argListSaved);
57 #endif
58 requiredStrlen = OVR_vsnprintf(bufferAllocated, (requiredStrlen + 1), format, argList);
59 }
60 }
62 if(requiredStrlen < 0) // If there was a printf format error...
63 {
64 bufferUsed = NULL;
65 }
67 va_end(argList);
69 if(bufferUsed)
70 AppendString(bufferUsed);
72 if(bufferAllocated)
73 OVR_FREE(bufferAllocated);
74 }
76 } // OVR