vsnprintf.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #if HAVE_VSNPRINTF
00016 
00017 #if !defined(ARCH_VSNPRINTF)
00018 #   define ARCH_VSNPRINTF vsnprintf
00019 #endif
00020 
00021 int
00022 ARCH_STRING::vsnprintf(char* str, int size, const char* fmt, va_list ap)
00023 {
00024     int n = ::ARCH_VSNPRINTF(str, size, fmt, ap);
00025     if (n > size) {
00026         n = -1;
00027     }
00028     return n;
00029 }
00030 
00031 #elif SYSAPI_UNIX // !HAVE_VSNPRINTF
00032 
00033 #include <stdio.h>
00034 
00035 int
00036 ARCH_STRING::vsnprintf(char* str, int size, const char* fmt, va_list ap)
00037 {
00038     static FILE* bitbucket = fopen("/dev/null", "w");
00039     if (bitbucket == NULL) {
00040         // uh oh
00041         if (size > 0) {
00042             str[0] = '\0';
00043         }
00044         return 0;
00045     }
00046     else {
00047         // count the characters using the bitbucket
00048         int n = vfprintf(bitbucket, fmt, ap);
00049         if (n + 1 <= size) {
00050             // it'll fit so print it into str
00051             vsprintf(str, fmt, ap);
00052         }
00053         return n;
00054     }
00055 }
00056 
00057 #else // !HAVE_VSNPRINTF && !SYSAPI_UNIX
00058 
00059 #error vsnprintf not implemented
00060 
00061 #endif // !HAVE_VSNPRINTF

Generated on Fri Nov 6 00:18:46 2009 for synergy-plus by  doxygen 1.4.7