inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_sprintf 00005 #define inlib_sprintf 00006 00007 #include <string> 00008 #include <cstdarg> 00009 #include <cstdio> 00010 00011 namespace inlib { 00012 00013 inline bool sprintf(std::string& a_string,int a_length,const char* a_format,...){ 00014 a_string.clear(); 00015 if(a_length<0) return false; 00016 if(!a_format) return false; 00017 char* s = new char[a_length+1]; 00018 if(!s) return false; 00019 s[a_length] = '\0'; 00020 va_list args; 00021 va_start(args,a_format); 00022 #ifdef WIN32 00023 int n = _vsnprintf(s,a_length+1,a_format,args); 00024 #else 00025 int n = ::vsnprintf(s,a_length+1,a_format,args); 00026 #endif 00027 va_end(args); 00028 if(n>a_length) { 00029 delete [] s; 00030 return false; 00031 } 00032 if(s[a_length]!='\0') { 00033 delete [] s; 00034 return false; 00035 } 00036 a_string = s; 00037 delete [] s; 00038 return true; 00039 } 00040 00041 00042 } 00043 00044 #endif