ovr_sdk

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