tizen 2.4 release
[framework/convergence/service/service-plugin-client.git] / src / trimming.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18  * @file trimming.c
19  * @author Dawid Kozinski (d.kozinski@samsung.com)
20  *
21  * @brief Trimming functions taken from
22  * http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way
23  */
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <stdlib.h>
29
30 /**
31 * @brief trim from start, i.e. left side
32 * Trimming leading whitespace from string
33 *
34 * @param s ...
35 * @return :char*
36 **/
37 char *plugin_internal_ltrim(char *s);
38
39 /**
40 * @brief trim from end, i.e. right side
41 * Trimming trailing whitespace from string
42 *
43 * @param s ...
44 * @return :char*
45 **/
46 char *plugin_internal_rtrim(char *s);
47
48 /**
49 * @brief trim from both ends
50 * Trimming leading and trailing whitespace from string
51 * @param s ...
52 * @return :string&
53 **/
54 char *plugin_internal_trim(char *s);
55
56 /* ////////////////////////////////////////////////////////////////////////////////
57    // Implementation
58    //////////////////////////////////////////////////////////////////////////////// */
59 char *plugin_internal_ltrim(char *s)
60 {
61         char *newstart = s;
62
63         while (isspace(*newstart)) {
64                 ++newstart;
65         }
66
67         /* newstart points to first non-whitespace char (which might be '\0') */
68         memmove(s, newstart, strlen(newstart) + 1);   /* don't forget to move the '\0' terminator */
69
70         return s;
71 }
72
73 char *plugin_internal_rtrim(char *s)
74 {
75         char *end = s + strlen(s);
76
77         /* find the last non-whitespace character */
78         while ((end != s) && isspace(*(end - 1))) {
79                 --end;
80         }
81
82         /* at this point either (end == s) and s is either empty or all whitespace
83                 so it needs to be made empty, or
84                 end points just past the last non-whitespace character (it might point
85                 at the '\0' terminator, in which case there's no problem writing
86                 another there). */
87         *end = '\0';
88
89         return s;
90 }
91
92 char *plugin_internal_trim(char *s)
93 {
94         return plugin_internal_rtrim(plugin_internal_ltrim(s));
95 }