sprint 1-alpha
sprint/io/utils.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::stream::utils
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SPRINT_STREAM_UTILS_H
00023 #define _SPRINT_STREAM_UTILS_H
00024 
00025 #include <sprint/log.h>
00026 
00031 namespace sprint {
00032 
00040 template<class _Stream, class _Storage>
00041 int read_until(_Stream & b, _Storage & s, char c, int max_read_size = 1048576)
00042 {
00043      int r;
00044      int n  = 0;
00045      do {
00046          r = b.get();
00047          if(r == -1) 
00048               return n;
00049               
00050          s.push_back(r);
00051          n++;
00052      } while ((r != c) && (n<max_read_size));
00053      return n;
00054 }
00055 
00062 template<class _S>
00063 int readsome(_S & s, char * buffer, int len)
00064 {
00065 int ret;
00066 int size = 0;
00067 
00068 if(buffer)
00069 {
00070 while(len>0)
00071         {
00072         ret = s.read(buffer + size, len);       /* len0 bug free */
00073         if(ret==0)
00074                 {
00075                 // ?
00076                 logPutsf(LOG_LEVEL_WARNING,"read return 0 at %u", size);
00077                 return size;    
00078                 }
00079         else
00080         if(ret==-1) 
00081                 {
00082                 logPuts(LOG_LEVEL_ERROR,"read return error");
00083                 return -1;
00084                 }
00085         size+=ret;
00086         len-=ret;
00087         }
00088 }
00089 else
00090         logPuts(LOG_LEVEL_ERROR,"internal error: readsome called with NULL buffer");
00091 return size;   
00092 }
00093           
00094 } // sprint
00095 
00096 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines