1/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation, nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29#include <stdio.h>
30#include <string.h>
31#include <log_util.h>
32#include <loc_misc_utils.h>
33#include <ctype.h>
34
35#define LOG_NDDEBUG 0
36#define LOG_TAG "LocSvc_misc_utils"
37
38int loc_util_split_string(char *raw_string, char **split_strings_ptr,
39                          int max_num_substrings, char delimiter)
40{
41    int raw_string_index=0;
42    int num_split_strings=0;
43    unsigned char end_string=0;
44    int raw_string_length=0;
45
46    if(!raw_string || !split_strings_ptr) {
47        LOC_LOGE("%s:%d]: NULL parameters", __func__, __LINE__);
48        num_split_strings = -1;
49        goto err;
50    }
51    LOC_LOGD("%s:%d]: raw string: %s\n", __func__, __LINE__, raw_string);
52    raw_string_length = strlen(raw_string) + 1;
53    split_strings_ptr[num_split_strings] = &raw_string[raw_string_index];
54    for(raw_string_index=0; raw_string_index < raw_string_length; raw_string_index++) {
55        if(raw_string[raw_string_index] == '\0')
56            end_string=1;
57        if((raw_string[raw_string_index] == delimiter) || end_string) {
58            raw_string[raw_string_index] = '\0';
59            LOC_LOGD("%s:%d]: split string: %s\n",
60                     __func__, __LINE__, split_strings_ptr[num_split_strings]);
61            num_split_strings++;
62            if(((raw_string_index + 1) < raw_string_length) &&
63               (num_split_strings < max_num_substrings)) {
64                split_strings_ptr[num_split_strings] = &raw_string[raw_string_index+1];
65            }
66            else {
67                break;
68            }
69        }
70        if(end_string)
71            break;
72    }
73err:
74    LOC_LOGD("%s:%d]: num_split_strings: %d\n", __func__, __LINE__, num_split_strings);
75    return num_split_strings;
76}
77
78void loc_util_trim_space(char *org_string)
79{
80    char *scan_ptr, *write_ptr;
81    char *first_nonspace = NULL, *last_nonspace = NULL;
82
83    if(org_string == NULL) {
84        LOC_LOGE("%s:%d]: NULL parameter", __func__, __LINE__);
85        goto err;
86    }
87
88    scan_ptr = write_ptr = org_string;
89
90    while (*scan_ptr) {
91        //Find the first non-space character
92        if ( !isspace(*scan_ptr) && first_nonspace == NULL) {
93            first_nonspace = scan_ptr;
94        }
95        //Once the first non-space character is found in the
96        //above check, keep shifting the characters to the left
97        //to replace the spaces
98        if (first_nonspace != NULL) {
99            *(write_ptr++) = *scan_ptr;
100            //Keep track of which was the last non-space character
101            //encountered
102            //last_nonspace will not be updated in the case where
103            //the string ends with spaces
104            if ( !isspace(*scan_ptr)) {
105                last_nonspace = write_ptr;
106            }
107        }
108        scan_ptr++;
109    }
110    //Add NULL terminator after the last non-space character
111    if (last_nonspace) { *last_nonspace = '\0'; }
112err:
113    return;
114}
115