• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

trim.h

00001 /* 
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of ser, a free SIP server.
00007  *
00008  * ser is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * ser is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00028 
00029 #ifndef TRIM_H
00030 #define TRIM_H
00031 
00032 #include "str.h"
00033 
00034 
00035 /*
00036  * This switch-case statement is used in
00037  * trim_leading and trim_trailing. You can
00038  * define characters that should be skipped 
00039  * here.
00040  */
00041 #define TRIM_SWITCH(c) switch(c) {     \
00042                        case ' ':       \
00043                        case '\t':      \
00044                        case '\r':      \
00045                        case '\n':      \
00046                                break;  \
00047                                        \
00048                        default:        \
00049                                return; \
00050                        }
00051 
00052 
00053 /*
00054  * Remove any leading whitechars, like spaces,
00055  * horizontal tabs, carriage returns and line
00056  * feeds
00057  *
00058  * WARNING: String descriptor structure will be
00059  *          modified ! Make a copy otherwise you
00060  *          might be unable to free _s->s for
00061  *          example !
00062  *
00063  */
00064 static inline void trim_leading(str* _s)
00065 {
00066         for(; _s->len > 0; _s->len--, _s->s++) {
00067                 TRIM_SWITCH(*(_s->s));
00068         }
00069 }
00070 
00071 
00072 /*
00073  * Remove any trailing white char, like spaces,
00074  * horizontal tabs, carriage returns and line feeds
00075  *
00076  * WARNING: String descriptor structure will be
00077  *          modified ! Make a copy otherwise you
00078  *          might be unable to free _s->s for
00079  *          example !
00080  */
00081 static inline void trim_trailing(str* _s)
00082 {
00083         for(; _s->len > 0; _s->len--) {
00084                 TRIM_SWITCH(_s->s[_s->len - 1]);
00085         }
00086 }
00087 
00088 
00089 /*
00090  * Do trim_leading and trim_trailing
00091  *
00092  * WARNING: String structure will be modified !
00093  *          Make a copy otherwise you might be
00094  *          unable to free _s->s for example !
00095  */
00096 static inline void trim(str* _s)
00097 {
00098         trim_leading(_s);
00099         trim_trailing(_s);
00100 }
00101 
00102 
00103 /*
00104  * right and left space trimming
00105  *
00106  * WARNING: String structure will be modified !
00107  *          Make a copy otherwise you might be
00108  *          unable to free _s_->s for example !
00109  */
00110 #define trim_spaces_lr(_s_)                                                                                             \
00111         do{                                                                                                                                     \
00112                 for(;(_s_).s[(_s_).len-1]==' ';(_s_).s[--(_s_).len]=0);                 \
00113                 for(;(_s_).s[0]==' ';(_s_).s=(_s_).s+1,(_s_).len--);                    \
00114                                                                                                                                                 \
00115         } while(0);
00116         
00117 #endif /* TRIM_H */

Generated on Tue May 22 2012 13:10:17 for SIP Router by  doxygen 1.7.1