DeSR Dependency Parser |
00001 /* 00002 ** IXE C++ Library 00003 ** ixe/io/Format.h 00004 ** ---------------------------------------------------------------------- 00005 ** Copyright (c) 2000 Ideare SpA. 00006 ** Copyright (c) 2000 Giuseppe Attardi (attardi@di.unipi.it). 00007 ** ---------------------------------------------------------------------- 00008 ** 00009 ** This file is part of DeSR. 00010 ** 00011 ** DeSR is free software; you can redistribute it and/or modify it 00012 ** under the terms of the GNU General Public License, version 3, 00013 ** as published by the Free Software Foundation. 00014 ** 00015 ** DeSR is distributed in the hope that it will be useful, 00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 ** GNU General Public License for more details. 00019 ** 00020 ** You should have received a copy of the GNU General Public License 00021 ** along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 ** ---------------------------------------------------------------------- 00023 */ 00024 00025 // Settings 00026 #include "Format.h" 00027 00028 // standard 00029 #include <stdio.h> 00030 #include <cstdarg> 00031 #ifdef _WIN32 00032 #include <malloc.h> 00033 #define alloca _alloca 00034 #define vsnprintf _vsnprintf 00035 #else 00036 #include <alloca.h> 00037 #endif 00038 00039 namespace IXE { 00040 namespace io { 00041 00042 Format::Format(const char* fmt ...) 00043 { 00044 va_list argList; 00045 va_start(argList, fmt); 00046 00047 // If the string is less than 512 characters, use fixed buffer 00048 char buffer[512]; 00049 00050 int numChars = vsnprintf(buffer, sizeof(buffer), fmt, argList); 00051 00052 if (numChars < sizeof(buffer)) { 00053 // Got it on the first try. 00054 assign(buffer, numChars); 00055 } else { 00056 // Now use alloca. 00057 char* stackBuffer = (char*)alloca(numChars+1); // 1 for null 00058 00059 numChars = vsnprintf(stackBuffer, numChars+1, fmt, argList); 00060 assign(stackBuffer, numChars); 00061 } 00062 va_end(argList); 00063 } 00064 00065 } 00066 }