summaryrefslogtreecommitdiff
path: root/src/strutils.cpp
blob: 3cbf740d733ce4ac87ec2ffb577dfbce3ed5d90d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 /********************************************************************************
 *  File: strutils.cpp            								*
 *  Date: 20080508                    							*
 *  Author: Gaspar Fernández (helyo@totaki.com) 				*
 *										*
 *  Copyright (C) 2008   Gaspar Fernández					*
 *										*
 *  This program is free software: you can redistribute it and/or modify	*
 *  it under the terms of the GNU General Public License as published by	*
 *  the Free Software Foundation, either version 3 of the License, or 		*
 *  (at your option) any later version.  	      	  	   		*
 *										*
 *  This program is distributed in the hope that it will be useful,		*
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of		*
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		*
 *  GNU General Public License for more details.				*
 *										*
 *  You should have received a copy of the GNU General Public License		*
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.	*
 *									        *
 ********************************************************************************
 *   Description:
 *     Some functions with strings.
 *   
 *   Change History:
 *    Date       Author     Modification
 *    
 ********************************************************************************/ 
#include <cstring>
#include <string>

const std::string whiteSpaces( " \f\n\r\t\v" );

/*************************************************************
 *  Functions: trimRight, trimLeft, trim                      *
 *************************************************************
 *  Description:                                             *
 *     Erases whitespaces (or any other characters) at the   *
 *  beginning, at the end, and at both of one string.        *
 *  Everything considered whitespace is in shiteSpaces       *
 *  constant (at the top of this file)                       *
 *                                                           *
 * Input:                                                    *
 *   string& str - The strim we'd like to trim               *
 *   string& trimChars - Characters we want to erase.        * 
 *                                                           * 
 * Output:                                                   *
 *   string& str (trimRight, trimLeft) - Our original string * 
 *                                       trimmed.            * 
 *   string (return value in trim()) - Trimmed string.       * 
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void trimRight(string& str,
      const string& trimChars = whiteSpaces )
{
  string::size_type pos = str.find_last_not_of( trimChars );
  str.erase( pos + 1 );

}

void trimLeft(string& str,
      const string& trimChars = whiteSpaces )
{
   string::size_type pos = str.find_first_not_of( trimChars );
   str.erase( 0, pos );

}

string trim( string str, const string& trimChars = whiteSpaces )
{
   trimRight( str, trimChars );
   trimLeft( str, trimChars );
   return str;
}

/*************************************************************
 *     Function: load_str_chr                                *
 *************************************************************
 *  Description:                                             *
 *     Reserves memory for a char* that will contain the     *
 *  data within a string or another *char                    *
 *                                                           *
 *                                                           *
 * Input:                                                    *
 *   char **ptr - Where we will store the string             * 
 *   string str (or char* str) - Where the string is         * 
 *                                                           *
 * Output:                                                   *
 *   The string will be in char **ptr                        * 
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void load_str_chr(char **ptr, const string str)
{
  *ptr=new char[str.size()+1];
  std::strcpy(*ptr,str.data());
}

void load_str_chr(char **ptr, const char* str)
{
  *ptr=new char[std::strlen(str)+1];
  std::strcpy(*ptr,str);
}

/*************************************************************
 *     Function: find_str_in_array                           *
 *************************************************************
 *  Description:                                             *
 *     Finds a str as an element in an array of char*        *
 *                                                           *
 * Input:                                                    *
 *   const char *str - The string we want to find            *
 *   const char *array - The array where the string may be   *
 *   int elements - The number of strings the array has      *
 *                                                           * 
 * Output:                                                   *
 *   int - The index of the array where the string is.       *
 *      If we couldn't find it, it returns -1                * 
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
int find_str_in_array(const char *str, const char *array[], int elements)
{
  int i=0, pos=-1;
  while ((i<elements) && (pos==-1))
    {
      if (std::strcmp(array[i], str)==0)
	pos=i;
      i++;
    }
  return pos;
}