nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: Filename : OVR_String_FormatUtil.cpp nuclear@0: Content : String format functions. nuclear@0: Created : February 27, 2013 nuclear@0: Notes : nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #include "OVR_String.h" nuclear@0: #include "OVR_Log.h" nuclear@0: nuclear@0: namespace OVR { nuclear@0: nuclear@0: void StringBuffer::AppendFormat(const char* format, ...) nuclear@0: { nuclear@0: va_list argList; nuclear@0: char buffer[512]; nuclear@0: char* bufferUsed = buffer; nuclear@0: char* bufferAllocated = NULL; nuclear@0: nuclear@0: va_start(argList, format); nuclear@0: nuclear@0: #if !defined(OVR_CC_MSVC) // Non-Microsoft compilers require you to save a copy of the va_list. nuclear@0: va_list argListSaved; nuclear@0: va_copy(argListSaved, argList); nuclear@0: #endif nuclear@0: nuclear@0: int requiredStrlen = OVR_vsnprintf(bufferUsed, OVR_ARRAY_COUNT(buffer), format, argList); // The large majority of the time this will succeed. nuclear@0: nuclear@0: if(requiredStrlen >= (int)sizeof(buffer)) // If the initial capacity wasn't enough... nuclear@0: { nuclear@0: bufferAllocated = (char*)OVR_ALLOC(sizeof(char) * (requiredStrlen + 1)); nuclear@0: bufferUsed = bufferAllocated; nuclear@0: if(bufferAllocated) nuclear@0: { nuclear@0: #if !defined(OVR_CC_MSVC) nuclear@0: va_end(argList); nuclear@0: va_copy(argList, argListSaved); nuclear@0: #endif nuclear@0: requiredStrlen = OVR_vsnprintf(bufferAllocated, (requiredStrlen + 1), format, argList); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(requiredStrlen < 0) // If there was a printf format error... nuclear@0: { nuclear@0: bufferUsed = NULL; nuclear@0: } nuclear@0: nuclear@0: va_end(argList); nuclear@0: nuclear@0: if(bufferUsed) nuclear@0: AppendString(bufferUsed); nuclear@0: nuclear@0: if(bufferAllocated) nuclear@0: OVR_FREE(bufferAllocated); nuclear@0: } nuclear@0: nuclear@0: } // OVR