sformat.C

Go to the documentation of this file.
00001 /*!@file Util/sformat.C convert varargs to a std::string as if by snprintf() */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/sformat.C $
00035 // $Id: sformat.C 6100 2006-01-17 01:59:41Z rjpeters $
00036 //
00037 
00038 #ifndef SFORMAT_C_DEFINED
00039 #define SFORMAT_C_DEFINED
00040 
00041 #include "Util/sformat.H"
00042 
00043 #include "Util/Assert.H"
00044 #include "Util/log.H"
00045 #include "rutz/compat_snprintf.h"
00046 
00047 std::string vsformat(const char* fmt, va_list ap)
00048 {
00049   // if we have a null pointer or an empty string, then just return an
00050   // empty std::string
00051   if (fmt == 0 || fmt[0] == '\0')
00052     {
00053       return std::string();
00054     }
00055 
00056   int bufsize = 512;
00057   while (1)
00058     {
00059       // relying on a gcc extension to create stack arrays whose size
00060       // is not a compile-time constant -- could also use alloca or
00061       // std::vector here
00062       char buf[bufsize];
00063 
00064       const int nchars = vsnprintf(buf, bufsize,
00065                                    fmt, ap);
00066 
00067       if (nchars < 0)
00068         {
00069           // Better leave this as LFATAL() rather than LERROR(),
00070           // otherwise we have to return a bogus std::string (e.g. an
00071           // empty string, or "none", or...), which might be dangerous
00072           // if it is later used as a filename, for example.
00073           LFATAL("vsnprintf failed for format '%s' with bufsize = %d",
00074                  fmt, bufsize);
00075         }
00076       else if (nchars >= bufsize)
00077         {
00078           // buffer was too small, so let's double the bufsize and try
00079           // again:
00080           bufsize *= 2;
00081           continue;
00082         }
00083       else
00084         {
00085           // OK, the vsnprintf() succeeded:
00086           return std::string(&buf[0], nchars);
00087         }
00088     }
00089   // should never get here:
00090   ASSERT(0);
00091   return std::string(); // can't happen, but placate the compiler
00092 }
00093 
00094 std::string sformat(const char* fmt, ...)
00095 {
00096   va_list a;
00097   va_start(a, fmt);
00098   std::string result = vsformat(fmt, a);
00099   va_end(a);
00100   return result;
00101 }
00102 
00103 // ######################################################################
00104 /* So things look consistent in everyone's emacs... */
00105 /* Local Variables: */
00106 /* indent-tabs-mode: nil */
00107 /* End: */
00108 
00109 #endif // SFORMAT_C_DEFINED
Generated on Sun May 8 08:42:28 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3