"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/be13_api/utils.cpp" (19 Feb 2021, 2990 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils.cpp" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * A collection of utility functions that are useful.
    3  */
    4 
    5 // Just for this module
    6 #define _FILE_OFFSET_BITS 64
    7 
    8 /* required per C++ standard */
    9 #ifndef __STDC_FORMAT_MACROS
   10 #define __STDC_FORMAT_MACROS
   11 #endif
   12 
   13 #include "config.h"
   14 #include "cppmutex.h"
   15 #include "utils.h"
   16 
   17 #ifdef HAVE_UNISTD_H
   18 #include <unistd.h>
   19 #endif
   20 
   21 #include <stdio.h>
   22 #include <errno.h>
   23 #include <fcntl.h>
   24 #include <ctype.h>
   25 #include <assert.h>
   26 #include <inttypes.h>
   27 #include <stdlib.h>
   28 #include <sys/stat.h>
   29 #include <string.h>
   30 
   31 #ifndef HAVE_ERR
   32 #include <stdarg.h>
   33 void err(int eval,const char *fmt,...)
   34 {
   35   va_list ap;
   36   va_start(ap,fmt);
   37   vfprintf(stderr,fmt,ap);
   38   va_end(ap);
   39   fprintf(stderr,": %s\n",strerror(errno));
   40   exit(eval);
   41 }
   42 #endif
   43 
   44 #ifndef HAVE_ERRX
   45 #include <stdarg.h>
   46 void errx(int eval,const char *fmt,...)
   47 {
   48   va_list ap;
   49   va_start(ap,fmt);
   50   vfprintf(stderr,fmt,ap);
   51   fprintf(stderr,"%s\n",strerror(errno));
   52   va_end(ap);
   53   exit(eval);
   54 }
   55 #endif
   56 
   57 #ifndef HAVE_WARN
   58 #include <stdarg.h>
   59 void    warn(const char *fmt, ...)
   60 {
   61     va_list args;
   62     va_start(args,fmt);
   63     vfprintf(stderr,fmt, args);
   64     fprintf(stderr,": %s\n",strerror(errno));
   65 }
   66 #endif
   67 
   68 #ifndef HAVE_WARNX
   69 #include <stdarg.h>
   70 void warnx(const char *fmt,...)
   71 {
   72   va_list ap;
   73   va_start(ap,fmt);
   74   vfprintf(stderr,fmt,ap);
   75   va_end(ap);
   76 }
   77 #endif
   78 
   79 /** Extract a buffer...
   80  * @param buf - the buffer to extract;
   81  * @param buflen - the size of the page to extract
   82  * @param pos0 - the byte position of buf[0]
   83  */
   84 
   85 #ifndef HAVE_LOCALTIME_R
   86 /* locking localtime_r implementation */
   87 cppmutex localtime_mutex;
   88 void localtime_r(time_t *t,struct tm *tm)
   89 {
   90     cppmutex::lock lock(localtime_mutex);
   91     *tm = *localtime(t);
   92 }
   93 #endif
   94 
   95 #ifndef HAVE_GMTIME_R
   96 /* locking gmtime_r implementation */
   97 cppmutex gmtime_mutex;
   98 void gmtime_r(time_t *t,struct tm *tm)
   99 {
  100     if(t && tm){
  101     cppmutex::lock lock(gmtime_mutex);
  102     struct tm *tmret = gmtime(t);
  103     if(tmret){
  104         *tm = *tmret;
  105     } else {
  106         memset(tm,0,sizeof(*tm));
  107     }
  108     }
  109 }
  110 #endif
  111 
  112 
  113 
  114 bool ends_with(const std::string &buf,const std::string &with)
  115 {
  116     size_t buflen = buf.size();
  117     size_t withlen = with.size();
  118     return buflen>withlen && buf.substr(buflen-withlen,withlen)==with;
  119 }
  120 
  121 bool ends_with(const std::wstring &buf,const std::wstring &with)
  122 {
  123     size_t buflen = buf.size();
  124     size_t withlen = with.size();
  125     return buflen>withlen && buf.substr(buflen-withlen,withlen)==with;
  126 }
  127 
  128 
  129 #include <sstream>
  130 
  131 /****************************************************************/
  132 /* C++ string splitting code from http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c */
  133 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
  134 {
  135     std::stringstream ss(s);
  136     std::string item;
  137     while(std::getline(ss, item, delim)) {
  138         elems.push_back(item);
  139     }
  140     return elems;
  141 }
  142 
  143 std::vector<std::string> split(const std::string &s, char delim)
  144 {
  145     std::vector<std::string> elems;
  146     return split(s, delim, elems);
  147 }
  148 
  149 
  150 
  151