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 #define LOG_NDEBUG 0
30 #define LOG_TAG "LocSvc_misc_utils"
31 #include <stdio.h>
32 #include <string.h>
33 #include <dlfcn.h>
34 #include <log_util.h>
35 #include <loc_misc_utils.h>
36 #include <ctype.h>
37 
38 
loc_util_split_string(char * raw_string,char ** split_strings_ptr,int max_num_substrings,char delimiter)39 int loc_util_split_string(char *raw_string, char **split_strings_ptr,
40                           int max_num_substrings, char delimiter)
41 {
42     int raw_string_index=0;
43     int num_split_strings=0;
44     unsigned char end_string=0;
45     int raw_string_length=0;
46 
47     if(!raw_string || !split_strings_ptr) {
48         LOC_LOGE("%s:%d]: NULL parameters", __func__, __LINE__);
49         num_split_strings = -1;
50         goto err;
51     }
52     LOC_LOGD("%s:%d]: raw string: %s\n", __func__, __LINE__, raw_string);
53     raw_string_length = strlen(raw_string) + 1;
54     split_strings_ptr[num_split_strings] = &raw_string[raw_string_index];
55     for(raw_string_index=0; raw_string_index < raw_string_length; raw_string_index++) {
56         if(raw_string[raw_string_index] == '\0')
57             end_string=1;
58         if((raw_string[raw_string_index] == delimiter) || end_string) {
59             raw_string[raw_string_index] = '\0';
60             LOC_LOGD("%s:%d]: split string: %s\n",
61                      __func__, __LINE__, split_strings_ptr[num_split_strings]);
62             num_split_strings++;
63             if(((raw_string_index + 1) < raw_string_length) &&
64                (num_split_strings < max_num_substrings)) {
65                 split_strings_ptr[num_split_strings] = &raw_string[raw_string_index+1];
66             }
67             else {
68                 break;
69             }
70         }
71         if(end_string)
72             break;
73     }
74 err:
75     LOC_LOGD("%s:%d]: num_split_strings: %d\n", __func__, __LINE__, num_split_strings);
76     return num_split_strings;
77 }
78 
loc_util_trim_space(char * org_string)79 void loc_util_trim_space(char *org_string)
80 {
81     char *scan_ptr, *write_ptr;
82     char *first_nonspace = NULL, *last_nonspace = NULL;
83 
84     if(org_string == NULL) {
85         LOC_LOGE("%s:%d]: NULL parameter", __func__, __LINE__);
86         goto err;
87     }
88 
89     scan_ptr = write_ptr = org_string;
90 
91     while (*scan_ptr) {
92         //Find the first non-space character
93         if ( !isspace(*scan_ptr) && first_nonspace == NULL) {
94             first_nonspace = scan_ptr;
95         }
96         //Once the first non-space character is found in the
97         //above check, keep shifting the characters to the left
98         //to replace the spaces
99         if (first_nonspace != NULL) {
100             *(write_ptr++) = *scan_ptr;
101             //Keep track of which was the last non-space character
102             //encountered
103             //last_nonspace will not be updated in the case where
104             //the string ends with spaces
105             if ( !isspace(*scan_ptr)) {
106                 last_nonspace = write_ptr;
107             }
108         }
109         scan_ptr++;
110     }
111     //Add NULL terminator after the last non-space character
112     if (last_nonspace) { *last_nonspace = '\0'; }
113 err:
114     return;
115 }
116 
logDlError(const char * failedCall)117 inline void logDlError(const char* failedCall) {
118     const char * err = dlerror();
119     LOC_LOGe("%s error: %s", failedCall, (nullptr == err) ? "unknown" : err);
120 }
121 
dlGetSymFromLib(void * & libHandle,const char * libName,const char * symName)122 void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName)
123 {
124     void* sym = nullptr;
125     if ((nullptr != libHandle || nullptr != libName) && nullptr != symName) {
126         if (nullptr == libHandle) {
127             libHandle = dlopen(libName, RTLD_NOW);
128             if (nullptr == libHandle) {
129                 logDlError("dlopen");
130             }
131         }
132         // NOT else, as libHandle gets assigned 5 line above
133         if (nullptr != libHandle) {
134             sym = dlsym(libHandle, symName);
135             if (nullptr == sym) {
136                 logDlError("dlsym");
137             }
138         }
139     } else {
140         LOC_LOGe("Either libHandle (%p) or libName (%p) must not be null; "
141                  "symName (%p) can not be null.", libHandle, libName, symName);
142     }
143 
144     return sym;
145 }
146