Swarm-NG  1.1
util.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Mario Juric *
3  * mjuric@astro.Princeton.EDU *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
26 #include "util.hpp"
27 #include <cstring>
28 #include <cstdio>
29 #include <cctype>
30 #include <cstdlib>
31 
32 #include <string>
33 
34 using namespace peyton;
35 
36 char *util::trim(char *dest, const char *src)
37 {
38  int cnt = 0;
39  while(isspace(*src) && *src) src++;
40  while(*src) dest[cnt++] = *(src++);
41  cnt--;
42  while(cnt > -1 && isspace(dest[cnt])) cnt--;
43  dest[cnt+1] = 0;
44  return dest;
45 }
46 
47 char *util::trim(char *txt)
48 {
49  char *s = strdup(txt);
50  trim(txt, s);
51  free(s);
52  return txt;
53 }
54 
56 
57 std::string util::ltrim( const std::string &str, const std::string &whitespace)
58 {
59 
60  int idx = str.find_first_not_of(whitespace);
61  if( idx != std::string::npos )
62  return str.substr(idx);
63 
64  return "";
65 }
66 
67 std::string util::rtrim( const std::string &str, const std::string &whitespace)
68 {
69 
70  int idx = str.find_last_not_of(whitespace);
71  if( idx != std::string::npos )
72  return str.substr(0,idx+1);
73 
74  return str;
75 }
76 
77 std::string util::trim( const std::string &str, const std::string &whitespace)
78 {
79  return rtrim(ltrim(str));
80 }