1 /*---------------------------------------------------------------------------*
2 * platform_utils.c *
3 * *
4 * Copyright 2007, 2008 Nuance Communciations, Inc. *
5 * *
6 * Licensed under the Apache License, Version 2.0 (the 'License'); *
7 * you may not use this file except in compliance with the License. *
8 * *
9 * You may obtain a copy of the License at *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, software *
13 * distributed under the License is distributed on an 'AS IS' BASIS, *
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 * See the License for the specific language governing permissions and *
16 * limitations under the License. *
17 * *
18 *---------------------------------------------------------------------------*/
19
20
21
22 #ifndef NO_STDERR
23 #include <stdio.h>
24 #endif
25
26 #include <string.h>
27 #include "pmemory.h"
28 #include "platform_utils.h"
29
30
31 /*
32 safe_strtok()
33 Use this in place of regular strtok. which is dangerous because it modifies
34 a global variable. This function does not.
35
36 Returns the position in NULL terminated input_str where seps are found,
37 and the length of the token
38 Seps contains a NULL terminated string of separators
39 Returns NULL if error
40
41 If no more tokens left, token_len is 0
42 */
43
safe_strtok(char * input_str,char * seps,int * token_len)44 char * safe_strtok(char *input_str, char *seps, int * token_len)
45 {
46 int i, j, k, n, m;
47 int sep_found;
48 char *pos;
49
50 m = strlen(seps);
51
52 if (!m || !input_str)
53 return NULL;
54
55 n = strlen(input_str);
56 pos = input_str;
57
58 for (i=0, sep_found = 0; i<n; i++) {
59 for (j=0; j<m; j++) {
60 if (*pos == seps[j]) {
61 /* found seperator */
62 sep_found++;
63 break;
64 }
65 }
66 if (sep_found == i) {
67 /* found first non separator */
68 break;
69 }
70 pos++;
71 }
72
73 *token_len = 0;
74
75 /* now find ending position of token */
76 for (k=i; k<n; k++) {
77 for (j=0; j<m; j++) {
78 if (pos[k-i] == seps[j]) {
79 /* first occurance equals separator*/
80 return pos;
81 }
82 }
83 (*token_len)++;
84 }
85
86 /* no more tokens */
87 return pos;
88 }
89
90
91 /* C54 and WinCE does not have strdup or stricmp */
92 #if defined TI_DSP || defined UNDER_CE
stricmp(const char * str1,const char * str2)93 int stricmp(const char *str1, const char *str2)
94 {
95 if(str1 == NULL || str2 == NULL){
96 #ifndef NO_STDERR
97 PLogError(L("stricmp: str1 or str2 is NULL\n"));
98 #endif
99 exit (1);
100 }
101
102 for (; *str1 != '\0' && *str2 != '\0' && tolower(*str1) == tolower(*str2);
103 str1++, str2++)
104 ;
105 if (*str1 == '\0')
106 return *str2 == '\0'? 0 : -1;
107 else if (*str2 == '\0')
108 return 1;
109 else
110 return tolower(*str1) < tolower(*str2)? -1 : 1;
111 }
112
strdup(char * in_string)113 char * strdup(char *in_string)
114 {
115 char * new_string = NULL;
116
117 if(in_string == NULL){
118 #ifndef NO_STDERR
119 PLogError(L("strdup: input string is NULL\n"));
120 #endif
121 return NULL;
122 }
123
124 new_string = (char *)MALLOC(sizeof(char)*(strlen(in_string)+1), MTAG);
125 if(!new_string)
126 return NULL;
127
128 strcpy(new_string, in_string);
129 return new_string;
130 }
131 #endif
132
133